-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJWTDecoder.php
executable file
·40 lines (35 loc) · 1.02 KB
/
JWTDecoder.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
<?php
namespace Evaneos\JWT\Util;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use stdClass;
use UnexpectedValueException;
class JWTDecoder
{
/**
* @var Key|array<string,Key>
*/
private $keyOrKeyArray;
public function __construct(string $secretKey, array $allowedAlgorithms = [])
{
if (count($allowedAlgorithms) === 1) {
$this->keyOrKeyArray = new Key($secretKey, array_pop($allowedAlgorithms));
} else {
$this->keyOrKeyArray = [];
foreach ($allowedAlgorithms as $kid => $algorithm) {
$this->keyOrKeyArray[$kid] = new Key($secretKey, $algorithm);
}
}
}
/**
* @throws JWTDecodeUnexpectedValueException
*/
public function decode(string $encodedToken): stdClass
{
try {
return JWT::decode($encodedToken, $this->keyOrKeyArray);
} catch (UnexpectedValueException $e) {
throw new JWTDecodeUnexpectedValueException('JWT can not be decoded.', 0, $e);
}
}
}