summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRob DiMarco <rdimarco@google.com>2016-06-16 12:53:44 -0700
committerGitHub <noreply@github.com>2016-06-16 12:53:44 -0700
commitb1816ba9d3c3c6637c081844d7abaf015f99f87a (patch)
treebc1cbf9057fc174c448d7164bb51ea442ab0407f /src
parent8087bbe7d8355d34e6ea8abfdcff82abc00c4503 (diff)
parentd6e222c1406367e879668d24968e087de1241c0f (diff)
downloadphp-jwt-b1816ba9d3c3c6637c081844d7abaf015f99f87a.zip
php-jwt-b1816ba9d3c3c6637c081844d7abaf015f99f87a.tar.gz
php-jwt-b1816ba9d3c3c6637c081844d7abaf015f99f87a.tar.bz2
Merge pull request #93 from josephmcdermott/specify-current-timestamp
Use static $timestamp instead of time()
Diffstat (limited to 'src')
-rw-r--r--src/JWT.php16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/JWT.php b/src/JWT.php
index 955351b..66b2788 100644
--- a/src/JWT.php
+++ b/src/JWT.php
@@ -29,6 +29,14 @@ class JWT
*/
public static $leeway = 0;
+ /**
+ * Allow the current timestamp to be specified.
+ * Useful for fixing a value within unit testing.
+ *
+ * Will default to PHP time() value if null.
+ */
+ public static $timestamp = null;
+
public static $supported_algs = array(
'HS256' => array('hash_hmac', 'SHA256'),
'HS512' => array('hash_hmac', 'SHA512'),
@@ -59,6 +67,8 @@ class JWT
*/
public static function decode($jwt, $key, $allowed_algs = array())
{
+ $timestamp = is_null(self::$timestamp) ? time() : self::$timestamp;
+
if (empty($key)) {
throw new InvalidArgumentException('Key may not be empty');
}
@@ -99,7 +109,7 @@ class JWT
// Check if the nbf if it is defined. This is the time that the
// token can actually be used. If it's not yet that time, abort.
- if (isset($payload->nbf) && $payload->nbf > (time() + self::$leeway)) {
+ if (isset($payload->nbf) && $payload->nbf > ($timestamp + self::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
);
@@ -108,14 +118,14 @@ class JWT
// Check that this token has been created before 'now'. This prevents
// using tokens that have been created for later use (and haven't
// correctly used the nbf claim).
- if (isset($payload->iat) && $payload->iat > (time() + self::$leeway)) {
+ if (isset($payload->iat) && $payload->iat > ($timestamp + self::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
);
}
// Check if this token has expired.
- if (isset($payload->exp) && (time() - self::$leeway) >= $payload->exp) {
+ if (isset($payload->exp) && ($timestamp - self::$leeway) >= $payload->exp) {
throw new ExpiredException('Expired token');
}