5
5
use ArrayAccess ;
6
6
use DomainException ;
7
7
use Exception ;
8
- use Firebase \JWT \Keys \JWTKey ;
9
- use Firebase \JWT \Keys \Keyring ;
10
8
use InvalidArgumentException ;
11
9
use UnexpectedValueException ;
12
10
use DateTime ;
@@ -81,8 +79,9 @@ class JWT
81
79
* @uses jsonDecode
82
80
* @uses urlsafeB64Decode
83
81
*/
84
- public static function decode ($ jwt , $ keyOrKeyArray, array $ allowed_algs = array () )
82
+ public static function decode ($ jwt , $ keyOrKeyArray )
85
83
{
84
+ // Validate JWT
86
85
$ timestamp = \is_null (static ::$ timestamp ) ? \time () : static ::$ timestamp ;
87
86
88
87
if (empty ($ keyOrKeyArray )) {
@@ -109,31 +108,18 @@ public static function decode($jwt, $keyOrKeyArray, array $allowed_algs = array(
109
108
throw new UnexpectedValueException ('Algorithm not supported ' );
110
109
}
111
110
112
- list ($ keyMaterial , $ algorithm ) = self ::getKeyMaterialAndAlgorithm (
113
- $ keyOrKeyArray ,
114
- empty ($ header ->kid ) ? null : $ header ->kid
115
- );
111
+ $ key = self ::getKey ($ keyOrKeyArray , empty ($ header ->kid ) ? null : $ header ->kid );
116
112
117
- if (empty ($ algorithm )) {
118
- // Use deprecated "allowed_algs" to determine if the algorithm is supported.
119
- // This opens up the possibility of an attack in some implementations.
120
- // @see https://github.com/firebase/php-jwt/issues/351
121
- if (!\in_array ($ header ->alg , $ allowed_algs )) {
122
- throw new UnexpectedValueException ('Algorithm not allowed ' );
123
- }
124
- } else {
125
- // Check the algorithm
126
- if (!self ::constantTimeEquals ($ algorithm , $ header ->alg )) {
127
- // See issue #351
128
- throw new UnexpectedValueException ('Incorrect key for this algorithm ' );
129
- }
113
+ // Check the algorithm
114
+ if (!self ::constantTimeEquals ($ key ->getAlgorithm (), $ header ->alg )) {
115
+ // See issue #351
116
+ throw new UnexpectedValueException ('Incorrect key for this algorithm ' );
130
117
}
131
118
if ($ header ->alg === 'ES256 ' || $ header ->alg === 'ES384 ' ) {
132
119
// OpenSSL expects an ASN.1 DER sequence for ES256/ES384 signatures
133
120
$ sig = self ::signatureToDER ($ sig );
134
121
}
135
-
136
- if (!static ::verify ("$ headb64. $ bodyb64 " , $ sig , $ keyMaterial , $ header ->alg )) {
122
+ if (!static ::verify ("$ headb64. $ bodyb64 " , $ sig , $ key ->getKeyMaterial (), $ header ->alg )) {
137
123
throw new SignatureInvalidException ('Signature verification failed ' );
138
124
}
139
125
@@ -391,36 +377,34 @@ public static function urlsafeB64Encode($input)
391
377
*
392
378
* @return an array containing the keyMaterial and algorithm
393
379
*/
394
- private static function getKeyMaterialAndAlgorithm ($ keyOrKeyArray , $ kid = null )
380
+ private static function getKey ($ keyOrKeyArray , $ kid = null )
395
381
{
396
- if (is_string ($ keyOrKeyArray )) {
397
- return array ($ keyOrKeyArray , null );
398
- }
399
-
400
382
if ($ keyOrKeyArray instanceof Key) {
401
- return array ( $ keyOrKeyArray-> getKeyMaterial (), $ keyOrKeyArray -> getAlgorithm ()) ;
383
+ return $ keyOrKeyArray ;
402
384
}
403
385
404
386
if (is_array ($ keyOrKeyArray ) || $ keyOrKeyArray instanceof ArrayAccess) {
387
+ foreach ($ keyOrKeyArray as $ keyId => $ key ) {
388
+ if (!$ key instanceof Key) {
389
+ throw new UnexpectedValueException (
390
+ '$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an '
391
+ . 'array of Firebase\JWT\Key keys '
392
+ );
393
+ }
394
+ }
405
395
if (!isset ($ kid )) {
406
396
throw new UnexpectedValueException ('"kid" empty, unable to lookup correct key ' );
407
397
}
408
398
if (!isset ($ keyOrKeyArray [$ kid ])) {
409
399
throw new UnexpectedValueException ('"kid" invalid, unable to lookup correct key ' );
410
400
}
411
401
412
- $ key = $ keyOrKeyArray [$ kid ];
413
-
414
- if ($ key instanceof Key) {
415
- return array ($ key ->getKeyMaterial (), $ key ->getAlgorithm ());
416
- }
417
-
418
- return array ($ key , null );
402
+ return $ keyOrKeyArray [$ kid ];
419
403
}
420
404
421
405
throw new UnexpectedValueException (
422
- '$keyOrKeyArray must be a string key, an array of string keys, '
423
- . 'an instance of Firebase\JWT\Key key or an array of Firebase\JWT\Key keys '
406
+ '$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an '
407
+ . 'array of Firebase\JWT\Key keys '
424
408
);
425
409
}
426
410
@@ -475,7 +459,7 @@ private static function handleJsonError($errno)
475
459
*
476
460
* @return int
477
461
*/
478
- public static function safeStrlen ($ str )
462
+ private static function safeStrlen ($ str )
479
463
{
480
464
if (\function_exists ('mb_strlen ' )) {
481
465
return \mb_strlen ($ str , '8bit ' );
0 commit comments