1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the PHP-JWT package. |
7
|
|
|
* |
8
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
9
|
|
|
* <https://github.com/adhocore> |
10
|
|
|
* |
11
|
|
|
* Licensed under MIT license. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ahc\Jwt; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* JSON Web Token (JWT) implementation in PHP7. |
18
|
|
|
* |
19
|
|
|
* @author Jitendra Adhikari <[email protected]> |
20
|
|
|
* @license MIT |
21
|
|
|
* |
22
|
|
|
* @link https://github.com/adhocore/jwt |
23
|
|
|
*/ |
24
|
|
|
trait ValidatesJWT |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Throw up if input parameters invalid. |
28
|
|
|
* |
29
|
|
|
* @codeCoverageIgnore |
30
|
|
|
*/ |
31
|
|
|
protected function validateConfig($key, string $algo, int $maxAge, int $leeway) |
32
|
|
|
{ |
33
|
|
|
if (empty($key)) { |
34
|
|
|
throw new JWTException('Signing key cannot be empty', static::ERROR_KEY_EMPTY); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!isset($this->algos[$algo])) { |
38
|
|
|
throw new JWTException('Unsupported algo ' . $algo, static::ERROR_ALGO_UNSUPPORTED); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($maxAge < 1) { |
42
|
|
|
throw new JWTException('Invalid maxAge: Should be greater than 0', static::ERROR_INVALID_MAXAGE); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($leeway < 0 || $leeway > 120) { |
46
|
|
|
throw new JWTException('Invalid leeway: Should be between 0-120', static::ERROR_INVALID_LEEWAY); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Throw up if header invalid. |
52
|
|
|
*/ |
53
|
|
|
protected function validateHeader(array $header) |
54
|
|
|
{ |
55
|
|
|
if (empty($header['alg'])) { |
56
|
|
|
throw new JWTException('Invalid token: Missing header algo', static::ERROR_ALGO_MISSING); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
if (empty($this->algos[$header['alg']])) { |
59
|
|
|
throw new JWTException('Invalid token: Unsupported header algo', static::ERROR_ALGO_UNSUPPORTED); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->validateKid($header); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Throw up if kid exists and invalid. |
67
|
|
|
*/ |
68
|
|
|
protected function validateKid(array $header) |
69
|
|
|
{ |
70
|
|
|
if (!isset($header['kid'])) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
if (empty($this->keys[$header['kid']])) { |
|
|
|
|
74
|
|
|
throw new JWTException('Invalid token: Unknown key ID', static::ERROR_KID_UNKNOWN); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->key = $this->keys[$header['kid']]; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Throw up if timestamp claims like iat, exp, nbf are invalid. |
82
|
|
|
*/ |
83
|
|
|
protected function validateTimestamps(array $payload) |
84
|
|
|
{ |
85
|
|
|
$timestamp = $this->timestamp ?: \time(); |
86
|
|
|
$checks = [ |
87
|
|
|
['exp', $this->leeway /* */ , static::ERROR_TOKEN_EXPIRED, 'Expired'], |
|
|
|
|
88
|
|
|
['iat', $this->maxAge - $this->leeway, static::ERROR_TOKEN_EXPIRED, 'Expired'], |
89
|
|
|
['nbf', $this->maxAge - $this->leeway, static::ERROR_TOKEN_NOT_NOW, 'Not now'], |
|
|
|
|
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
foreach ($checks as list($key, $offset, $code, $error)) { |
93
|
|
|
if (isset($payload[$key])) { |
94
|
|
|
$offset += $payload[$key]; |
95
|
|
|
$fail = $key === 'nbf' ? $timestamp <= $offset : $timestamp >= $offset; |
96
|
|
|
|
97
|
|
|
if ($fail) { |
98
|
|
|
throw new JWTException('Invalid token: ' . $error, $code); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Throw up if key is not resource or file path to private key. |
106
|
|
|
*/ |
107
|
|
|
protected function validateKey() |
108
|
|
|
{ |
109
|
|
|
if (\is_string($key = $this->key)) { |
110
|
|
|
if (\substr($key, 0, 7) !== 'file://') { |
111
|
|
|
$key = 'file://' . $key; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->key = \openssl_get_privatekey($key, $this->passphrase ?: ''); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!\is_resource($this->key)) { |
118
|
|
|
throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Throw up if last json_encode/decode was a failure. |
124
|
|
|
*/ |
125
|
|
|
protected function validateLastJson() |
126
|
|
|
{ |
127
|
|
|
if (\JSON_ERROR_NONE === \json_last_error()) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
throw new JWTException('JSON failed: ' . \json_last_error_msg(), static::ERROR_JSON_FAILED); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|