15
15
*/
16
16
class JWT
17
17
{
18
+
19
+ /**
20
+ * When cheking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
21
+ */
22
+ const LEEWAYTIME = 60 ;
23
+
18
24
public static $ supported_algs = array (
19
25
'HS256 ' => array ('hash_hmac ' , 'SHA256 ' ),
20
26
'HS512 ' => array ('hash_hmac ' , 'SHA512 ' ),
@@ -80,7 +86,7 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
80
86
81
87
// Check if the nbf if it is defined. This is the time that the
82
88
// token can actually be used. If it's not yet that time, abort.
83
- if (isset ($ payload ->nbf ) && $ payload ->nbf > time ()) {
89
+ if (isset ($ payload ->nbf ) && $ payload ->nbf > ( time () + self :: LEEWAYTIME )) {
84
90
throw new BeforeValidException (
85
91
'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->nbf )
86
92
);
@@ -89,14 +95,14 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
89
95
// Check that this token has been created before 'now'. This prevents
90
96
// using tokens that have been created for later use (and haven't
91
97
// correctly used the nbf claim).
92
- if (isset ($ payload ->iat ) && $ payload ->iat > time ()) {
98
+ if (isset ($ payload ->iat ) && $ payload ->iat > ( time () + self :: LEEWAYTIME )) {
93
99
throw new BeforeValidException (
94
100
'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->iat )
95
101
);
96
102
}
97
103
98
104
// Check if this token has expired.
99
- if (isset ($ payload ->exp ) && time () >= $ payload ->exp ) {
105
+ if (isset ($ payload ->exp ) && ( time () - self :: LEEWAYTIME ) >= $ payload ->exp ) {
100
106
throw new ExpiredException ('Expired token ' );
101
107
}
102
108
}
0 commit comments