-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathJwt.php
177 lines (154 loc) · 4.6 KB
/
Jwt.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
<?php
namespace sizeg\jwt;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Parsing\Decoder;
use Lcobucci\JWT\Parsing\Encoder;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\ValidationData;
use Yii;
use yii\base\Component;
use yii\base\InvalidArgumentException;
/**
* JSON Web Token implementation, based on this library:
* https://github.com/lcobucci/jwt
*
* @author Dmitriy Demin <sizemail@gmail.com>
* @since 1.0.0-a
*/
class Jwt extends Component
{
/**
* @var array Supported algorithms
*/
public $supportedAlgs = [
'HS256' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,
'HS384' => \Lcobucci\JWT\Signer\Hmac\Sha384::class,
'HS512' => \Lcobucci\JWT\Signer\Hmac\Sha512::class,
'ES256' => \Lcobucci\JWT\Signer\Ecdsa\Sha256::class,
'ES384' => \Lcobucci\JWT\Signer\Ecdsa\Sha384::class,
'ES512' => \Lcobucci\JWT\Signer\Ecdsa\Sha512::class,
'RS256' => \Lcobucci\JWT\Signer\Rsa\Sha256::class,
'RS384' => \Lcobucci\JWT\Signer\Rsa\Sha384::class,
'RS512' => \Lcobucci\JWT\Signer\Rsa\Sha512::class,
];
/**
* @var Key|string $key The key
*/
public $key;
/**
* @var string|array|callable \sizeg\jwtJwtValidationData
* @see [[Yii::createObject()]]
*/
public $jwtValidationData = JwtValidationData::class;
/**
* @see [[Lcobucci\JWT\Builder::__construct()]]
* @param Encoder|null $encoder
* @param ClaimFactory|null $claimFactory
* @return Builder
*/
public function getBuilder(Encoder $encoder = null, ClaimFactory $claimFactory = null)
{
return new Builder($encoder, $claimFactory);
}
/**
* @see [[Lcobucci\JWT\Parser::__construct()]]
* @param Decoder|null $decoder
* @param ClaimFactory|null $claimFactory
* @return Parser
*/
public function getParser(Decoder $decoder = null, ClaimFactory $claimFactory = null)
{
return new Parser($decoder, $claimFactory);
}
/**
* @see [[Lcobucci\JWT\ValidationData::__construct()]]
* @return ValidationData
*/
public function getValidationData()
{
return Yii::createObject($this->jwtValidationData)->getValidationData();
}
/**
* @param string $alg
* @return Signer
*/
public function getSigner($alg)
{
$class = $this->supportedAlgs[$alg];
return new $class();
}
/**
* @param strng $content
* @param string|null $passphrase
* @return Key
*/
public function getKey($content = null, $passphrase = null)
{
$content = $content ?: $this->key;
if ($content instanceof Key) {
return $content;
}
return new Key($content, $passphrase);
}
/**
* Parses the JWT and returns a token class
* @param string $token JWT
* @param bool $validate
* @param bool $verify
* @return Token|null
* @throws \Throwable
*/
public function loadToken($token, $validate = true, $verify = true)
{
try {
$token = $this->getParser()->parse((string) $token);
} catch (\RuntimeException $e) {
Yii::warning('Invalid JWT provided: ' . $e->getMessage(), 'jwt');
return null;
} catch (\InvalidArgumentException $e) {
Yii::warning('Invalid JWT provided: ' . $e->getMessage(), 'jwt');
return null;
}
if ($validate && !$this->validateToken($token)) {
return null;
}
if ($verify && !$this->verifyToken($token)) {
return null;
}
return $token;
}
/**
* Validate token
* @param Token $token token object
* @param int|null $currentTime
* @return bool
*/
public function validateToken(Token $token, $currentTime = null)
{
$validationData = $this->getValidationData();
if ($currentTime !== null) {
$validationData->setCurrentTime($currentTime);
}
return $token->validate($validationData);
}
/**
* Validate token
* @param Token $token token object
* @return bool
* @throws \Throwable
*/
public function verifyToken(Token $token)
{
$alg = $token->getHeader('alg');
if (empty($this->supportedAlgs[$alg])) {
throw new InvalidArgumentException('Algorithm not supported');
}
/** @var Signer $signer */
$signer = Yii::createObject($this->supportedAlgs[$alg]);
return $token->verify($signer, $this->key);
}
}