@@ -30,8 +30,13 @@ class JWT
30
30
* @param bool $verify Don't skip verification process
31
31
*
32
32
* @return object The JWT's payload as a PHP object
33
- * @throws UnexpectedValueException Provided JWT was invalid
34
- * @throws DomainException Algorithm was not provided
33
+ *
34
+ * @throws DomainException Algorithm was not provided
35
+ * @throws UnexpectedValueException Provided JWT was invalid
36
+ * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
37
+ * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
38
+ * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
39
+ * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
35
40
*
36
41
* @uses jsonDecode
37
42
* @uses urlsafeB64Decode
@@ -67,17 +72,27 @@ public static function decode($jwt, $key = null, $verify = true)
67
72
throw new SignatureInvalidException ('Signature verification failed ' );
68
73
}
69
74
70
- // Check token expiry time if defined.
71
- if (isset ($ payload ->exp ) && time () >= $ payload ->exp ) {
72
- throw new ExpiredException ('Expired token ' );
73
- }
74
-
75
- // Check if the nbf if it is defined.
75
+ // Check if the nbf if it is defined. This is the time that the
76
+ // token can actually be used. If it's not yet that time, abort.
76
77
if (isset ($ payload ->nbf ) && $ payload ->nbf > time ()) {
77
78
throw new BeforeValidException (
78
79
'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->nbf )
79
80
);
80
81
}
82
+
83
+ // Check that this token has been created before 'now'. This prevents
84
+ // using tokens that have been created for later use (and haven't
85
+ // correctly used the nbf claim).
86
+ if (isset ($ payload ->iat ) && $ payload ->iat > time ()) {
87
+ throw new BeforeValidException (
88
+ 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->iat )
89
+ );
90
+ }
91
+
92
+ // Check if this token has expired.
93
+ if (isset ($ payload ->exp ) && time () >= $ payload ->exp ) {
94
+ throw new ExpiredException ('Expired token ' );
95
+ }
81
96
}
82
97
83
98
return $ payload ;
0 commit comments