-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tolerance window in token lifetime validation (#359)
* feat: clock skew in payload verification library * feat: clock skew in verification helper * chore: docs, typo
- Loading branch information
Showing
6 changed files
with
187 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from pyeudiw.jwt.helper import validate_jwt_timestamps_claims | ||
from pyeudiw.tools.utils import iat_now | ||
|
||
|
||
def test_validate_jwt_timestamps_claims_ok(): | ||
now = iat_now() | ||
payload = { | ||
"iat": now - 10, | ||
"nbf": now - 10, | ||
"exp": now + 9999 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload) | ||
except Exception as e: | ||
assert True, f"encountered unexpeted error when validating the lifetime of a good token payload: {e}" | ||
|
||
|
||
def test_validate_jwt_timestamps_claims_bad_iat(): | ||
now = iat_now() | ||
payload = { | ||
"iat": now + 100, | ||
"exp": now + 9999 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload) | ||
assert False, "failed to raise exception when validating a token payload with bad iat" | ||
except Exception: | ||
pass | ||
|
||
|
||
def test_validate_jwt_timestamps_claims_bad_nbf(): | ||
now = iat_now() | ||
payload = { | ||
"nbf": now + 100, | ||
"exp": now + 9999 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload) | ||
assert False, "failed to raise exception when validating a token payload with bad nbf" | ||
except Exception: | ||
pass | ||
|
||
|
||
def test_validate_jwt_timestamps_claims_bad_exp(): | ||
now = iat_now() | ||
payload = { | ||
"iat": now - 100, | ||
"exp": now - 10 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload) | ||
assert False, "failed to raise exception when validating a token payload with bad exp" | ||
except Exception: | ||
pass | ||
|
||
|
||
def test_test_validate_jwt_timestamps_claims_tolerance_window(): | ||
tolerance_window = 30 # in seconds | ||
|
||
# case 0: tolerance window covers a token issuer "slightly" in the future | ||
now = iat_now() | ||
payload = { | ||
"iat": now + 15, | ||
"nbf": now + 15, | ||
"exp": now + 9999 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload, tolerance_window) | ||
except Exception as e: | ||
assert False, f"encountered unexpeted error when validating the lifetime of a token payload with a tolerance window (for iat, nbf): {e}" | ||
|
||
# case 1: tolerance window covers a token "slightly" expired | ||
now = iat_now() | ||
payload = { | ||
"iat": now - 100, | ||
"nbf": now - 100, | ||
"exp": now - 15 | ||
} | ||
try: | ||
validate_jwt_timestamps_claims(payload, tolerance_window) | ||
except Exception as e: | ||
assert False, f"encountered unexpeted error when validating the lifetime of a token payload with a tolerance window (for exp): {e}" |
Oops, something went wrong.