@@ -60,13 +60,11 @@ class JWT
60
60
* Decodes a JWT string into a PHP object.
61
61
*
62
62
* @param string $jwt The JWT
63
- * @param Key|array<Key>|mixed $keyOrKeyArray The Key or array of Key objects.
63
+ * @param Key|array<Key> $keyOrKeyArray The Key or array of Key objects.
64
64
* If the algorithm used is asymmetric, this is the public key
65
65
* Each Key object contains an algorithm and matching key.
66
66
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
67
67
* 'HS512', 'RS256', 'RS384', and 'RS512'
68
- * @param array $allowed_algs [DEPRECATED] List of supported verification algorithms. Only
69
- * should be used for backwards compatibility.
70
68
*
71
69
* @return object The JWT's payload as a PHP object
72
70
*
@@ -80,8 +78,9 @@ class JWT
80
78
* @uses jsonDecode
81
79
* @uses urlsafeB64Decode
82
80
*/
83
- public static function decode ($ jwt , $ keyOrKeyArray, array $ allowed_algs = array () )
81
+ public static function decode ($ jwt , $ keyOrKeyArray )
84
82
{
83
+ // Validate JWT
85
84
$ timestamp = \is_null (static ::$ timestamp ) ? \time () : static ::$ timestamp ;
86
85
87
86
if (empty ($ keyOrKeyArray )) {
@@ -108,31 +107,18 @@ public static function decode($jwt, $keyOrKeyArray, array $allowed_algs = array(
108
107
throw new UnexpectedValueException ('Algorithm not supported ' );
109
108
}
110
109
111
- list ($ keyMaterial , $ algorithm ) = self ::getKeyMaterialAndAlgorithm (
112
- $ keyOrKeyArray ,
113
- empty ($ header ->kid ) ? null : $ header ->kid
114
- );
110
+ $ key = self ::getKey ($ keyOrKeyArray , empty ($ header ->kid ) ? null : $ header ->kid );
115
111
116
- if (empty ($ algorithm )) {
117
- // Use deprecated "allowed_algs" to determine if the algorithm is supported.
118
- // This opens up the possibility of an attack in some implementations.
119
- // @see https://github.com/firebase/php-jwt/issues/351
120
- if (!\in_array ($ header ->alg , $ allowed_algs )) {
121
- throw new UnexpectedValueException ('Algorithm not allowed ' );
122
- }
123
- } else {
124
- // Check the algorithm
125
- if (!self ::constantTimeEquals ($ algorithm , $ header ->alg )) {
126
- // See issue #351
127
- throw new UnexpectedValueException ('Incorrect key for this algorithm ' );
128
- }
112
+ // Check the algorithm
113
+ if (!self ::constantTimeEquals ($ key ->getAlgorithm (), $ header ->alg )) {
114
+ // See issue #351
115
+ throw new UnexpectedValueException ('Incorrect key for this algorithm ' );
129
116
}
130
117
if ($ header ->alg === 'ES256 ' || $ header ->alg === 'ES384 ' ) {
131
118
// OpenSSL expects an ASN.1 DER sequence for ES256/ES384 signatures
132
119
$ sig = self ::signatureToDER ($ sig );
133
120
}
134
-
135
- if (!static ::verify ("$ headb64. $ bodyb64 " , $ sig , $ keyMaterial , $ header ->alg )) {
121
+ if (!static ::verify ("$ headb64. $ bodyb64 " , $ sig , $ key ->getKeyMaterial (), $ header ->alg )) {
136
122
throw new SignatureInvalidException ('Signature verification failed ' );
137
123
}
138
124
@@ -393,40 +379,34 @@ public static function urlsafeB64Encode($input)
393
379
*
394
380
* @return array containing the keyMaterial and algorithm
395
381
*/
396
- private static function getKeyMaterialAndAlgorithm ($ keyOrKeyArray , $ kid = null )
382
+ private static function getKey ($ keyOrKeyArray , $ kid = null )
397
383
{
398
- if (
399
- is_string ($ keyOrKeyArray )
400
- || is_resource ($ keyOrKeyArray )
401
- || $ keyOrKeyArray instanceof OpenSSLAsymmetricKey
402
- ) {
403
- return array ($ keyOrKeyArray , null );
404
- }
405
-
406
384
if ($ keyOrKeyArray instanceof Key) {
407
- return array ( $ keyOrKeyArray-> getKeyMaterial (), $ keyOrKeyArray -> getAlgorithm ()) ;
385
+ return $ keyOrKeyArray ;
408
386
}
409
387
410
388
if (is_array ($ keyOrKeyArray ) || $ keyOrKeyArray instanceof ArrayAccess) {
389
+ foreach ($ keyOrKeyArray as $ keyId => $ key ) {
390
+ if (!$ key instanceof Key) {
391
+ throw new UnexpectedValueException (
392
+ '$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an '
393
+ . 'array of Firebase\JWT\Key keys '
394
+ );
395
+ }
396
+ }
411
397
if (!isset ($ kid )) {
412
398
throw new UnexpectedValueException ('"kid" empty, unable to lookup correct key ' );
413
399
}
414
400
if (!isset ($ keyOrKeyArray [$ kid ])) {
415
401
throw new UnexpectedValueException ('"kid" invalid, unable to lookup correct key ' );
416
402
}
417
403
418
- $ key = $ keyOrKeyArray [$ kid ];
419
-
420
- if ($ key instanceof Key) {
421
- return array ($ key ->getKeyMaterial (), $ key ->getAlgorithm ());
422
- }
423
-
424
- return array ($ key , null );
404
+ return $ keyOrKeyArray [$ kid ];
425
405
}
426
406
427
407
throw new UnexpectedValueException (
428
- '$keyOrKeyArray must be a string|resource key, an array of string|resource keys, '
429
- . 'an instance of Firebase\JWT\Key key or an array of Firebase\JWT\Key keys '
408
+ '$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an '
409
+ . 'array of Firebase\JWT\Key keys '
430
410
);
431
411
}
432
412
0 commit comments