Skip to content

Commit

Permalink
Merge pull request AdaCore#413 from camilo1729/fix_is_valid
Browse files Browse the repository at this point in the history
Fix JWT token validation
  • Loading branch information
Nikokrock committed Jul 27, 2020
2 parents 54de2cc + 1389c20 commit 833b7d7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/e3/net/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def is_valid(token: str) -> bool:
# do not consider a token valid if it will be valid less than 5 min
deadline = utc_timestamp() + 5 * 60

return payload.get("typ") == "Bearer" and payload.get("exp", 0) > deadline
return payload.get("exp", 0) > deadline
13 changes: 4 additions & 9 deletions tests/tests_e3/net/token/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,21 @@ def test_get_payload():


def test_valid_token():
valid_token = create_token({"typ": "Bearer", "exp": FUTURE_TIMESTAMP})
valid_token = create_token({"exp": FUTURE_TIMESTAMP})
assert is_valid(valid_token)

near_future = utc_timestamp() + 7 * 60
near_future_token = create_token({"typ": "Bearer", "exp": near_future})
near_future_token = create_token({"exp": near_future})
assert is_valid(near_future_token)


def test_wrong_token_type():
badtype_token = create_token({"typ": "badtype", "exp": FUTURE_TIMESTAMP})
assert not is_valid(badtype_token)


def test_old_token():
old_token = create_token({"typ": "Bearer", "exp": 1419064452})
old_token = create_token({"exp": 1419064452})
assert not is_valid(old_token)

# Verify that a token valid for less than 5 min will be considered invalid
expire_soon_date = utc_timestamp() + 4 * 60
expire_soon_token = create_token({"typ": "Bearer", "exp": expire_soon_date})
expire_soon_token = create_token({"exp": expire_soon_date})
assert not is_valid(expire_soon_token)


Expand Down

0 comments on commit 833b7d7

Please sign in to comment.