2
2
3
3
namespace Firebase \JWT ;
4
4
use \DomainException ;
5
+ use \InvalidArgumentException ;
5
6
use \UnexpectedValueException ;
6
7
use \DateTime ;
7
8
@@ -56,8 +57,11 @@ class JWT
56
57
* @uses jsonDecode
57
58
* @uses urlsafeB64Decode
58
59
*/
59
- public static function decode ($ jwt , $ key = null , $ allowed_algs = array ())
60
+ public static function decode ($ jwt , $ key , $ allowed_algs = array ())
60
61
{
62
+ if (empty ($ key )) {
63
+ throw new InvalidArgumentException ('Key may not be empty ' );
64
+ }
61
65
$ tks = explode ('. ' , $ jwt );
62
66
if (count ($ tks ) != 3 ) {
63
67
throw new UnexpectedValueException ('Wrong number of segments ' );
@@ -70,50 +74,49 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
70
74
throw new UnexpectedValueException ('Invalid claims encoding ' );
71
75
}
72
76
$ sig = JWT ::urlsafeB64Decode ($ cryptob64 );
73
- if (isset ($ key )) {
74
- if (empty ($ header ->alg )) {
75
- throw new DomainException ('Empty algorithm ' );
76
- }
77
- if (empty (self ::$ supported_algs [$ header ->alg ])) {
78
- throw new DomainException ('Algorithm not supported ' );
79
- }
80
- if (!is_array ($ allowed_algs ) || !in_array ($ header ->alg , $ allowed_algs )) {
81
- throw new DomainException ('Algorithm not allowed ' );
82
- }
83
- if (is_array ($ key ) || $ key instanceof \ArrayAccess) {
84
- if (isset ($ header ->kid )) {
85
- $ key = $ key [$ header ->kid ];
86
- } else {
87
- throw new DomainException ('"kid" empty, unable to lookup correct key ' );
88
- }
77
+
78
+ if (empty ($ header ->alg )) {
79
+ throw new DomainException ('Empty algorithm ' );
80
+ }
81
+ if (empty (self ::$ supported_algs [$ header ->alg ])) {
82
+ throw new DomainException ('Algorithm not supported ' );
83
+ }
84
+ if (!is_array ($ allowed_algs ) || !in_array ($ header ->alg , $ allowed_algs )) {
85
+ throw new DomainException ('Algorithm not allowed ' );
86
+ }
87
+ if (is_array ($ key ) || $ key instanceof \ArrayAccess) {
88
+ if (isset ($ header ->kid )) {
89
+ $ key = $ key [$ header ->kid ];
90
+ } else {
91
+ throw new DomainException ('"kid" empty, unable to lookup correct key ' );
89
92
}
93
+ }
90
94
91
- // Check the signature
92
- if (!JWT ::verify ("$ headb64. $ bodyb64 " , $ sig , $ key , $ header ->alg )) {
93
- throw new SignatureInvalidException ('Signature verification failed ' );
94
- }
95
+ // Check the signature
96
+ if (!JWT ::verify ("$ headb64. $ bodyb64 " , $ sig , $ key , $ header ->alg )) {
97
+ throw new SignatureInvalidException ('Signature verification failed ' );
98
+ }
95
99
96
- // Check if the nbf if it is defined. This is the time that the
97
- // token can actually be used. If it's not yet that time, abort.
98
- if (isset ($ payload ->nbf ) && $ payload ->nbf > (time () + self ::$ leeway )) {
99
- throw new BeforeValidException (
100
- 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->nbf )
101
- );
102
- }
100
+ // Check if the nbf if it is defined. This is the time that the
101
+ // token can actually be used. If it's not yet that time, abort.
102
+ if (isset ($ payload ->nbf ) && $ payload ->nbf > (time () + self ::$ leeway )) {
103
+ throw new BeforeValidException (
104
+ 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->nbf )
105
+ );
106
+ }
103
107
104
- // Check that this token has been created before 'now'. This prevents
105
- // using tokens that have been created for later use (and haven't
106
- // correctly used the nbf claim).
107
- if (isset ($ payload ->iat ) && $ payload ->iat > (time () + self ::$ leeway )) {
108
- throw new BeforeValidException (
109
- 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->iat )
110
- );
111
- }
108
+ // Check that this token has been created before 'now'. This prevents
109
+ // using tokens that have been created for later use (and haven't
110
+ // correctly used the nbf claim).
111
+ if (isset ($ payload ->iat ) && $ payload ->iat > (time () + self ::$ leeway )) {
112
+ throw new BeforeValidException (
113
+ 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->iat )
114
+ );
115
+ }
112
116
113
- // Check if this token has expired.
114
- if (isset ($ payload ->exp ) && (time () - self ::$ leeway ) >= $ payload ->exp ) {
115
- throw new ExpiredException ('Expired token ' );
116
- }
117
+ // Check if this token has expired.
118
+ if (isset ($ payload ->exp ) && (time () - self ::$ leeway ) >= $ payload ->exp ) {
119
+ throw new ExpiredException ('Expired token ' );
117
120
}
118
121
119
122
return $ payload ;
0 commit comments