1515 */
1616class JWT
1717{
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+
1824 public static $ supported_algs = array (
1925 'HS256 ' => array ('hash_hmac ' , 'SHA256 ' ),
2026 'HS512 ' => array ('hash_hmac ' , 'SHA512 ' ),
@@ -80,7 +86,7 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
8086
8187 // Check if the nbf if it is defined. This is the time that the
8288 // 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 )) {
8490 throw new BeforeValidException (
8591 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->nbf )
8692 );
@@ -89,14 +95,14 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
8995 // Check that this token has been created before 'now'. This prevents
9096 // using tokens that have been created for later use (and haven't
9197 // correctly used the nbf claim).
92- if (isset ($ payload ->iat ) && $ payload ->iat > time ()) {
98+ if (isset ($ payload ->iat ) && $ payload ->iat > ( time () + self :: LEEWAYTIME )) {
9399 throw new BeforeValidException (
94100 'Cannot handle token prior to ' . date (DateTime::ISO8601 , $ payload ->iat )
95101 );
96102 }
97103
98104 // Check if this token has expired.
99- if (isset ($ payload ->exp ) && time () >= $ payload ->exp ) {
105+ if (isset ($ payload ->exp ) && ( time () - self :: LEEWAYTIME ) >= $ payload ->exp ) {
100106 throw new ExpiredException ('Expired token ' );
101107 }
102108 }
0 commit comments