This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
JwtVerifier.php
192 lines (156 loc) · 6.22 KB
/
JwtVerifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/******************************************************************************
* Copyright 2017 Okta, Inc. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
namespace Okta\JwtVerifier;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Okta\JwtVerifier\Adaptors\Adaptor;
use Okta\JwtVerifier\Adaptors\AutoDiscover;
use Okta\JwtVerifier\Discovery\DiscoveryMethod;
use Okta\JwtVerifier\Discovery\Oauth;
class JwtVerifier
{
/**
* @var string
*/
protected $issuer;
/**
* @var DiscoveryMethod
*/
protected $discovery;
/**
* @var array
*/
protected $claimsToValidate;
/**
* @var string
*/
protected $wellknown;
/**
* @var mixed
*/
protected $metaData;
/**
* @var Adaptor
*/
protected $adaptor;
public function __construct(
string $issuer,
DiscoveryMethod $discovery = null,
Adaptor $adaptor = null,
Request $request = null,
int $leeway = 120,
array $claimsToValidate = []
) {
$this->issuer = $issuer;
$this->discovery = $discovery ?: new Oauth;
$this->adaptor = $adaptor ?: AutoDiscover::getAdaptor();
$request = $request ?: new Request;
$this->wellknown = $this->issuer.$this->discovery->getWellKnown();
$this->metaData = json_decode($request->setUrl($this->wellknown)->get()
->getBody());
$this->claimsToValidate = $claimsToValidate;
}
public function getIssuer()
{
return $this->issuer;
}
public function getDiscovery()
{
return $this->discovery;
}
public function getMetaData()
{
return $this->metaData;
}
public function verify($jwt)
{
if($this->metaData->jwks_uri == null) {
throw new \DomainException("Could not access a valid JWKS_URI from the metadata. We made a call to {$this->wellknown} endpoint, but jwks_uri was null. Please make sure you are using a custom authorization server for the jwt verifier.");
}
$keys = $this->adaptor->getKeys($this->metaData->jwks_uri);
$decoded = $this->adaptor->decode($jwt, $keys);
$this->validateClaims($decoded->getClaims());
return $decoded;
}
public function verifyIdToken($jwt)
{
if($this->metaData->jwks_uri == null) {
throw new \DomainException("Could not access a valid JWKS_URI from the metadata. We made a call to {$this->wellknown} endpoint, but jwks_uri was null. Please make sure you are using a custom authorization server for the jwt verifier.");
}
$keys = $this->adaptor->getKeys($this->metaData->jwks_uri);
$decoded = $this->adaptor->decode($jwt, $keys);
$this->validateClaims($decoded->getClaims(), "id");
return $decoded;
}
public function verifyAccessToken($jwt)
{
if($this->metaData->jwks_uri == null) {
throw new \DomainException("Could not access a valid JWKS_URI from the metadata. We made a call to {$this->wellknown} endpoint, but jwks_uri was null. Please make sure you are using a custom authorization server for the jwt verifier.");
}
$keys = $this->adaptor->getKeys($this->metaData->jwks_uri);
$decoded = $this->adaptor->decode($jwt, $keys);
$this->validateClaims($decoded->getClaims(), "access");
return $decoded;
}
private function validateClaims(array $claims, string $type)
{
switch($type) {
case 'id':
$this->validateAudience($claims);
$this->validateNonce($claims);
break;
case 'access':
$this->validateAudience($claims);
$this->validateClientId($claims);
$this->validateNonce($claims);
break;
}
}
private function validateNonce($claims)
{
if(!isset($claims['nonce']) && $this->claimsToValidate['nonce'] == null) {
return false;
}
if($claims['nonce'] != $this->claimsToValidate['nonce']) {
throw new \Exception('Nonce does not match what is expected. Make sure to provide the nonce with
`setNonce()` from the JwtVerifierBuilder.');
}
}
private function validateAudience($claims)
{
if(!isset($claims['aud']) && $this->claimsToValidate['audience'] == null) {
return false;
}
if($claims['aud'] != $this->claimsToValidate['audience']) {
throw new \Exception('Audience does not match what is expected. Make sure to provide the audience with
`setAudience()` from the JwtVerifierBuilder.');
}
}
private function validateClientId($claims)
{
if(!isset($claims['cid']) && $this->claimsToValidate['clientId'] == null) {
return false;
}
if($claims['cid'] != $this->claimsToValidate['clientId']) {
throw new \Exception('ClientId does not match what is expected. Make sure to provide the client id with
`setClientId()` from the JwtVerifierBuilder.');
}
}
}