1 | <?php |
||
16 | class JwtToken |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $claims = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $header = []; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $payload; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $signature; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $tokenString; |
||
42 | |||
43 | /** |
||
44 | * @param $tokenData |
||
45 | */ |
||
46 | public function __construct($tokenData) |
||
54 | |||
55 | /** |
||
56 | * @param string $tokenString |
||
57 | */ |
||
58 | public function setTokenFromString($tokenString) |
||
59 | { |
||
60 | $this->tokenString = $tokenString; |
||
61 | $segments = explode('.', $tokenString); |
||
62 | |||
63 | if (count($segments) !== 3) { |
||
64 | throw new \InvalidArgumentException("Not a JWT token string"); |
||
65 | } |
||
66 | |||
67 | list($headerBase64, $claimsBase64, $signatureBase64) = $segments; |
||
68 | |||
69 | $this->payload = "{$headerBase64}.{$claimsBase64}"; |
||
70 | |||
71 | $decoder = new Decoder(); |
||
72 | $this->header = $decoder->decode($headerBase64); |
||
73 | $this->claims = $decoder->decode($claimsBase64); |
||
74 | $this->signature = $decoder->base64Decode($signatureBase64); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param array $header |
||
79 | * @param array $claims |
||
80 | * @param $secret |
||
81 | */ |
||
82 | public function setTokenFromParams($header, $claims, $secret) |
||
83 | { |
||
84 | $this->header = $header; |
||
85 | $this->claims = $claims; |
||
86 | |||
87 | $encoder = new Encoder(); |
||
88 | $headerBase64 = $encoder->encode($header); |
||
89 | $claimsBase64 = $encoder->encode($claims); |
||
90 | |||
91 | $this->payload = "{$headerBase64}.{$claimsBase64}"; |
||
92 | |||
93 | $this->signature = hash_hmac( |
||
94 | 'sha256', |
||
95 | $this->payload, |
||
96 | $secret, |
||
97 | true |
||
98 | ); |
||
99 | $signatureBase64 = $encoder->base64Encode($this->signature); |
||
100 | |||
101 | $segments = compact('headerBase64', 'claimsBase64', 'signatureBase64'); |
||
102 | $this->tokenString = implode('.', $segments); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string|null |
||
107 | */ |
||
108 | public function getKeyId() |
||
112 | |||
113 | /** |
||
114 | * @param string $secret |
||
115 | * @param SignatureValidator $validator |
||
116 | * |
||
117 | * @throws \InvalidArgumentException |
||
118 | */ |
||
119 | public function validateSignature($secret, SignatureValidator $validator) |
||
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getSubject() |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getClaims() |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getHeader() |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getTokenString() |
||
161 | |||
162 | |||
163 | } |
||
164 |