Enterprise agent platform for building, deploying, and operating AI teammates.
"""Crypto migration must retain issued tokens, not authentication weaknesses."""
from datetime import datetime, timedelta, timezone
from unittest.mock import Mock
import pytest
from xagent.web import jwt_codec as jwt
SECRET = "compatibility-secret-with-at-least-32-bytes"
def test_pre_migration_python_jose_token_remains_valid():
# Fixed fixture generated by python-jose 3.5.0, not the replacement library.
token = (
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJzdWIiOiJsZWdhY3ktdXNlciIsInVzZXJfaWQiOjcsInR5cGUiOiJhY2Nlc3MiLCJleHAiOjQxMDI0NDQ4MDB9."
"zEfNdvSRd5wTCTUUBXctar_-5nW0JjsRmv5tlLnpwuo"
)
assert jwt.decode(
token, "legacy-compatibility-secret-at-least-32-bytes", algorithms=["HS256"]
) == {"sub": "legacy-user", "user_id": 7, "type": "access", "exp": 4102444800}
@pytest.mark.parametrize("claim", ["exp", "nbf", "iat"])
@pytest.mark.parametrize("value", [None, [], {}, "not-a-number"])
def test_malformed_temporal_claim_is_a_jwt_error(claim, value):
token = jwt.encode({claim: value}, SECRET, algorithm="HS256")
with pytest.raises(jwt.JWTError):
jwt.decode(token, SECRET, algorithms=["HS256"])
def test_expired_and_wrong_algorithm_tokens_are_rejected():
token = jwt.encode(
{"exp": datetime.now(timezone.utc) - timedelta(minutes=1)},
SECRET,
algorithm="HS256",
)
with pytest.raises(jwt.ExpiredSignatureError):
jwt.decode(token, SECRET, algorithms=["HS256"])
with pytest.raises(jwt.JWTError):
jwt.decode(token, SECRET, algorithms=["HS512"])
@pytest.mark.parametrize("claim", ["exp", "nbf", "iat"])
def test_verified_non_convertible_dates_have_shared_error_type(claim):
token = jwt.encode({claim: None}, SECRET, algorithm="HS256")
with pytest.raises(jwt.InvalidTemporalClaims):
jwt.decode(token, SECRET, algorithms=["HS256"])
assert jwt.decode(
token, SECRET, algorithms=["HS256"], options={f"verify_{claim}": False}
) == {claim: None}
with pytest.raises(jwt.JWTError) as error:
jwt.decode(
token, "different-secret-with-at-least-32-bytes", algorithms=["HS256"]
)
assert not isinstance(error.value, jwt.InvalidTemporalClaims)
def test_invalid_signature_does_not_retry_decode(monkeypatch):
token = jwt.encode({"exp": None}, SECRET, algorithm="HS256")
decode = Mock(wraps=jwt._jwt.decode)
monkeypatch.setattr(jwt._jwt, "decode", decode)
with pytest.raises(jwt._jwt.InvalidSignatureError):
jwt.decode(token, "wrong-key-with-at-least-32-characters", algorithms=["HS256"])
assert decode.call_count == 1
@pytest.mark.parametrize("claim", ["nbf", "iat"])
@pytest.mark.parametrize("value", [None, [], float("inf")])
def test_disabling_expiry_still_rejects_other_malformed_dates(claim, value):
token = jwt.encode({"exp": [], claim: value}, SECRET, algorithm="HS256")
with pytest.raises(jwt.InvalidTemporalClaims):
jwt.decode(token, SECRET, algorithms=["HS256"], options={"verify_exp": False})
@pytest.mark.parametrize(
"claims",
[
{"iat": "not-a-number", "exp": []},
{"nbf": "not-a-number", "exp": []},
{"iat": "not-a-number", "nbf": []},
],
)
def test_first_value_error_keeps_library_error_classification(claims):
token = jwt.encode(claims, SECRET, algorithm="HS256")
with pytest.raises(jwt.JWTError) as raised:
jwt.decode(token, SECRET, algorithms=["HS256"])
assert not isinstance(raised.value, jwt.InvalidTemporalClaims)