1313
1414namespace Ahc \Jwt ;
1515
16+ use stdClass ;
17+ use function array_merge ;
18+ use function base64_decode ;
19+ use function base64_encode ;
20+ use function explode ;
21+ use function hash_equals ;
22+ use function hash_hmac ;
23+ use function is_array ;
24+ use function json_decode ;
25+ use function json_encode ;
26+ use function openssl_pkey_get_details ;
27+ use function openssl_sign ;
28+ use function openssl_verify ;
29+ use function reset ;
30+ use function rtrim ;
31+ use function strtr ;
32+ use function substr_count ;
33+ use function time ;
34+ use const JSON_UNESCAPED_SLASHES ;
35+ use const OPENSSL_ALGO_SHA256 ;
36+ use const OPENSSL_ALGO_SHA384 ;
37+ use const OPENSSL_ALGO_SHA512 ;
38+
1639/**
1740 * JSON Web Token (JWT) implementation in PHP5.5+.
1841 *
@@ -43,9 +66,9 @@ class JWT
4366 'HS256 ' => 'sha256 ' ,
4467 'HS384 ' => 'sha384 ' ,
4568 'HS512 ' => 'sha512 ' ,
46- 'RS256 ' => \ OPENSSL_ALGO_SHA256 ,
47- 'RS384 ' => \ OPENSSL_ALGO_SHA384 ,
48- 'RS512 ' => \ OPENSSL_ALGO_SHA512 ,
69+ 'RS256 ' => OPENSSL_ALGO_SHA256 ,
70+ 'RS384 ' => OPENSSL_ALGO_SHA384 ,
71+ 'RS512 ' => OPENSSL_ALGO_SHA512 ,
4972 ];
5073
5174 /** @var string|resource The signature key. */
@@ -55,7 +78,7 @@ class JWT
5578 protected $ keys = [];
5679
5780 /** @var int|null Use setTestTimestamp() to set custom value for time(). Useful for testability. */
58- protected $ timestamp = null ;
81+ protected $ timestamp ;
5982
6083 /** @var string The JWT signing algorithm. Defaults to HS256. */
6184 protected $ algo = 'HS256 ' ;
@@ -88,9 +111,9 @@ public function __construct(
88111 ) {
89112 $ this ->validateConfig ($ key , $ algo , $ maxAge , $ leeway );
90113
91- if (\ is_array ($ key )) {
114+ if (is_array ($ key )) {
92115 $ this ->registerKeys ($ key );
93- $ key = \ reset ($ key ); // use first one!
116+ $ key = reset ($ key ); // use first one!
94117 }
95118
96119 $ this ->key = $ key ;
@@ -109,7 +132,7 @@ public function __construct(
109132 */
110133 public function registerKeys (array $ keys ): self
111134 {
112- $ this ->keys = \ array_merge ($ this ->keys , $ keys );
135+ $ this ->keys = array_merge ($ this ->keys , $ keys );
113136
114137 return $ this ;
115138 }
@@ -129,7 +152,7 @@ public function encode(array $payload, array $header = []): string
129152 $ this ->validateKid ($ header );
130153
131154 if (!isset ($ payload ['iat ' ]) && !isset ($ payload ['exp ' ])) {
132- $ payload ['exp ' ] = ($ this ->timestamp ?: \ time ()) + $ this ->maxAge ;
155+ $ payload ['exp ' ] = ($ this ->timestamp ?: time ()) + $ this ->maxAge ;
133156 }
134157
135158 $ header = $ this ->urlSafeEncode ($ header );
@@ -151,11 +174,11 @@ public function encode(array $payload, array $header = []): string
151174 */
152175 public function decode (string $ token , bool $ verify = true ): array
153176 {
154- if (\ substr_count ($ token , '. ' ) < 2 ) {
177+ if (substr_count ($ token , '. ' ) < 2 ) {
155178 throw new JWTException ('Invalid token: Incomplete segments ' , static ::ERROR_TOKEN_INVALID );
156179 }
157180
158- $ token = \ explode ('. ' , $ token , 3 );
181+ $ token = explode ('. ' , $ token , 3 );
159182 if (!$ verify ) {
160183 return (array ) $ this ->urlSafeDecode ($ token [1 ]);
161184 }
@@ -196,13 +219,13 @@ public function setTestTimestamp(int $timestamp = null): self
196219 protected function sign (string $ input ): string
197220 {
198221 // HMAC SHA.
199- if (\substr ($ this ->algo , 0 , 2 ) === ' HS ' ) {
200- return \ hash_hmac ($ this ->algos [$ this ->algo ], $ input , $ this ->key , true );
222+ if (strpos ($ this ->algo , ' HS ' ) === 0 ) {
223+ return hash_hmac ($ this ->algos [$ this ->algo ], $ input , $ this ->key , true );
201224 }
202225
203226 $ this ->validateKey ();
204227
205- \ openssl_sign ($ input , $ signature , $ this ->key , $ this ->algos [$ this ->algo ]);
228+ openssl_sign ($ input , $ signature , $ this ->key , $ this ->algos [$ this ->algo ]);
206229
207230 return $ signature ;
208231 }
@@ -222,15 +245,15 @@ protected function verify(string $input, string $signature): bool
222245 $ algo = $ this ->algos [$ this ->algo ];
223246
224247 // HMAC SHA.
225- if (\substr ($ this ->algo , 0 , 2 ) === ' HS ' ) {
226- return \ hash_equals ($ this ->urlSafeEncode (\ hash_hmac ($ algo , $ input , $ this ->key , true )), $ signature );
248+ if (strpos ($ this ->algo , ' HS ' ) === 0 ) {
249+ return hash_equals ($ this ->urlSafeEncode (hash_hmac ($ algo , $ input , $ this ->key , true )), $ signature );
227250 }
228251
229252 $ this ->validateKey ();
230253
231- $ pubKey = \ openssl_pkey_get_details ($ this ->key )['key ' ];
254+ $ pubKey = openssl_pkey_get_details ($ this ->key )['key ' ];
232255
233- return \ openssl_verify ($ input , $ this ->urlSafeDecode ($ signature , false ), $ pubKey , $ algo ) === 1 ;
256+ return openssl_verify ($ input , $ this ->urlSafeDecode ($ signature , false ), $ pubKey , $ algo ) === 1 ;
234257 }
235258
236259 /**
@@ -246,12 +269,12 @@ protected function verify(string $input, string $signature): bool
246269 */
247270 protected function urlSafeEncode ($ data ): string
248271 {
249- if (\ is_array ($ data )) {
250- $ data = \ json_encode ($ data , \ JSON_UNESCAPED_SLASHES );
272+ if (is_array ($ data )) {
273+ $ data = json_encode ($ data , JSON_UNESCAPED_SLASHES );
251274 $ this ->validateLastJson ();
252275 }
253276
254- return \ rtrim (\ strtr (\ base64_encode ($ data ), '+/ ' , '-_ ' ), '= ' );
277+ return rtrim (strtr (base64_encode ($ data ), '+/ ' , '-_ ' ), '= ' );
255278 }
256279
257280 /**
@@ -260,17 +283,17 @@ protected function urlSafeEncode($data): string
260283 * @param array|string $data
261284 * @param bool $asJson Whether to parse as JSON (defaults to true).
262285 *
263- * @throws JWTException When JSON encode fails.
286+ * @return array|stdClass|string
287+ *@throws JWTException When JSON encode fails.
264288 *
265- * @return array|\stdClass|string
266289 */
267290 protected function urlSafeDecode ($ data , bool $ asJson = true )
268291 {
269292 if (!$ asJson ) {
270- return \ base64_decode (\ strtr ($ data , '-_ ' , '+/ ' ));
293+ return base64_decode (strtr ($ data , '-_ ' , '+/ ' ));
271294 }
272295
273- $ data = \ json_decode (\ base64_decode (\ strtr ($ data , '-_ ' , '+/ ' )));
296+ $ data = json_decode (base64_decode (strtr ($ data , '-_ ' , '+/ ' )), false );
274297 $ this ->validateLastJson ();
275298
276299 return $ data ;
0 commit comments