1 | <?php |
||
12 | class JWT |
||
13 | { |
||
14 | /** |
||
15 | * When checking nbf, iat or exp |
||
16 | * we provide additional time screw/leeway |
||
17 | * |
||
18 | * @link https://github.com/SocialConnect/auth/issues/26 |
||
19 | */ |
||
20 | public static $screw = 0; |
||
21 | |||
22 | /** |
||
23 | * Map of supported algorithms |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | public static $algorithms = array( |
||
28 | // HS |
||
29 | 'HS256' => ['hash_hmac', 'SHA256'], |
||
30 | 'HS384' => ['hash_hmac', 'SHA384'], |
||
31 | 'HS512' => ['hash_hmac', 'SHA512'], |
||
32 | // RS |
||
33 | 'RS256' => ['openssl', 'SHA256'], |
||
34 | 'RS384' => ['openssl', 'SHA384'], |
||
35 | 'RS512' => ['openssl', 'SHA512'], |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $header; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $payload; |
||
47 | |||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | protected $signature; |
||
52 | |||
53 | /** |
||
54 | * @param string $input |
||
55 | * @return string |
||
56 | */ |
||
57 | public static function urlsafeB64Decode($input) |
||
68 | |||
69 | /** |
||
70 | * @param array $payload |
||
71 | * @param array $header |
||
72 | * @param string|null $signature |
||
73 | */ |
||
74 | 4 | public function __construct(array $payload, array $header, $signature = null) |
|
75 | { |
||
76 | 4 | $this->payload = $payload; |
|
77 | 4 | $this->header = $header; |
|
78 | 4 | $this->signature = $signature; |
|
79 | 4 | } |
|
80 | |||
81 | /** |
||
82 | * @param string $token |
||
83 | * @param array $keys |
||
84 | * @return JWT |
||
85 | * @throws InvalidJWT |
||
86 | */ |
||
87 | public static function decode($token, array $keys) |
||
121 | |||
122 | protected function validateHeader() |
||
132 | |||
133 | 4 | protected function validateClaims() |
|
134 | { |
||
135 | 4 | $now = time(); |
|
136 | |||
137 | /** |
||
138 | * @link https://tools.ietf.org/html/rfc7519#section-4.1.5 |
||
139 | * "nbf" (Not Before) Claim check |
||
140 | */ |
||
141 | 4 | if (isset($this->payload['nbf']) && $this->payload['nbf'] > ($now + self::$screw)) { |
|
142 | 1 | throw new InvalidJWT( |
|
143 | 1 | 'nbf (Not Fefore) claim is not valid ' . date(DateTime::RFC3339, $this->payload['nbf']) |
|
144 | 1 | ); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @link https://tools.ietf.org/html/rfc7519#section-4.1.6 |
||
149 | * "iat" (Issued At) Claim |
||
150 | */ |
||
151 | 3 | if (isset($this->payload['iat']) && $this->payload['iat'] > ($now + self::$screw)) { |
|
152 | throw new InvalidJWT( |
||
153 | 'iat (Issued At) claim is not valid ' . date(DateTime::RFC3339, $this->payload['iat']) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @link https://tools.ietf.org/html/rfc7519#section-4.1.4 |
||
159 | * "exp" (Expiration Time) Claim |
||
160 | */ |
||
161 | 3 | if (isset($this->payload['exp']) && ($now - self::$screw) >= $this->payload['exp']) { |
|
162 | 1 | throw new InvalidJWT( |
|
163 | 1 | 'exp (Expiration Time) claim is not valid ' . date(DateTime::RFC3339, $this->payload['exp']) |
|
164 | 1 | ); |
|
165 | } |
||
166 | 2 | } |
|
167 | |||
168 | /** |
||
169 | * @param string $data |
||
170 | * @param array $keys |
||
171 | * @throws InvalidJWT |
||
172 | */ |
||
173 | protected function validate($data, array $keys) |
||
183 | |||
184 | /** |
||
185 | * @param array $keys |
||
186 | * @param string $kid |
||
187 | * @return JWK |
||
188 | * @throws \RuntimeException |
||
189 | */ |
||
190 | protected function findKeyByKind(array $keys, $kid) |
||
200 | |||
201 | /** |
||
202 | * @param string $data |
||
203 | * @param array $keys |
||
204 | * @return bool |
||
205 | * @throws UnsupportedSignatureAlgoritm |
||
206 | */ |
||
207 | protected function verifySignature($data, array $keys) |
||
261 | } |
||
262 |