|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Gandung\JWT\Accessor\JoseHeaderAccessor; |
|
6
|
|
|
use Gandung\JWT\Accessor\JoseHeaderAccessorInterface; |
|
7
|
|
|
use Gandung\JWT\Accessor\PayloadAccessor; |
|
8
|
|
|
use Gandung\JWT\Accessor\PayloadAccessorInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Parser implements ParserInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $portion; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function parse($token) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->checkToken($token); |
|
26
|
|
|
|
|
27
|
|
|
$key = ['jose', 'payload', 'signature']; |
|
28
|
|
|
|
|
29
|
|
|
foreach ($this->portion as $k => $v) { |
|
30
|
|
|
if ($k === 0 || $k === 1) { |
|
31
|
|
|
$this->portion[$k] = json_decode( |
|
32
|
|
|
\Gandung\JWT\__jwt_base64UrlDecode($v), |
|
33
|
|
|
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
|
34
|
|
|
); |
|
35
|
|
|
} else { |
|
36
|
|
|
$this->portion[$k] = $v; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->portion = array_combine($key, $this->portion); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getSignature($raw = true) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!isset($this->portion['signature'])) { |
|
49
|
|
|
throw new \RuntimeException( |
|
50
|
|
|
"JWT portion doesn't have 'signature' index." |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $raw === true |
|
55
|
|
|
? \Gandung\JWT\__jwt_base64UrlDecode($this->portion['signature']) |
|
56
|
|
|
: $this->portion['signature']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getJoseHeader(): JoseHeaderAccessorInterface |
|
63
|
|
|
{ |
|
64
|
|
|
if (!isset($this->portion['jose'])) { |
|
65
|
|
|
throw new \RuntimeException( |
|
66
|
|
|
"JWT portion doesn't have 'jose' index." |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new JoseHeaderAccessor($this->portion['jose']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getPayload(): PayloadAccessorInterface |
|
77
|
|
|
{ |
|
78
|
|
|
if (!isset($this->portion['payload'])) { |
|
79
|
|
|
throw new \RuntimeException( |
|
80
|
|
|
"JWT portion doesn't have 'payload' index." |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new PayloadAccessor($this->portion['payload']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Check if given JWT token is valid. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $token |
|
91
|
|
|
*/ |
|
92
|
|
|
private function checkToken($token) |
|
93
|
|
|
{ |
|
94
|
|
|
if (empty($token)) { |
|
95
|
|
|
throw new \InvalidArgumentException( |
|
96
|
|
|
"JWT token must not be empty." |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->portion = explode('.', $token); |
|
101
|
|
|
|
|
102
|
|
|
if (sizeof($this->portion) !== 3) { |
|
103
|
|
|
throw new \RuntimeException( |
|
104
|
|
|
"Invalid JWT token." |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|