Skip to content

FIx #18 - expiration time bug . #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions jwt.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,27 @@ int jwt_verify_body(char *body, zval *return_value)
err_msg = msg; \
} while(0);

/* Expiration */
/* set expiration and not before */
JWT_G(expiration) = jwt_hash_str_find_long(return_value, "exp");
JWT_G(not_before) = jwt_hash_str_find_long(return_value, "nbf");
JWT_G(iat) = jwt_hash_str_find_long(return_value, "iat");

/* expiration */
if (JWT_G(expiration) && (curr_time - JWT_G(leeway)) >= JWT_G(expiration))
FORMAT_CEX_MSG("Expired token", jwt_expired_signature_cex);

/* not before */
if (JWT_G(not_before) && JWT_G(not_before) > (curr_time + JWT_G(leeway)))
FORMAT_CEX_TIME(JWT_G(not_before), jwt_before_valid_cex);

/* iat */
if (JWT_G(iat) && JWT_G(iat) > (curr_time + JWT_G(leeway)))
FORMAT_CEX_TIME(JWT_G(iat), jwt_invalid_iat_cex);

/* iss */
if (jwt_verify_claims_str(return_value, "iss", JWT_G(iss)))
FORMAT_CEX_MSG("Invalid Issuer", jwt_invalid_issuer_cex);

/* iat */
if (JWT_G(iat) && JWT_G(iat) > (curr_time + JWT_G(leeway))) {
FORMAT_CEX_TIME(JWT_G(iat), jwt_invalid_iat_cex);
}

/* jti */
if (jwt_verify_claims_str(return_value, "jti", JWT_G(jti)))
FORMAT_CEX_MSG("Invalid Jti", jwt_invalid_jti_cex);
Expand Down Expand Up @@ -462,11 +466,6 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
goto encode_done;
}

/* set expiration and not before */
JWT_G(expiration) = jwt_hash_str_find_long(payload, "exp");
JWT_G(not_before) = jwt_hash_str_find_long(payload, "nbf");
JWT_G(iat) = jwt_hash_str_find_long(payload, "iat");

/* init */
array_init(&header);

Expand Down
17 changes: 17 additions & 0 deletions tests/014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ISSUE #18 expiration time bug
--SKIPIF--
<?php if (!extension_loaded("jwt")) print "skip"; ?>
--FILE--
<?php
$hmackey = "example-hmac-key";

try {
$decoded_token = jwt_decode('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoiZGF0YSIsImV4cCI6MTU0MTMzNTUxNH0.CsQXJI3d2b9LOZSO3rD2xrr9ar7bWBcbrrm-mXJto3g', $hmackey, ['algorithm' => 'HS256']);
} catch (ExpiredSignatureException $e) {
// Handle expired token
echo "FAIL\n";
}
?>
--EXPECT--
FAIL