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