|
| 1 | +"""Unit tests for arcp.auth.jwt.JWTValidator.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import jwt |
| 6 | +import pytest |
| 7 | + |
| 8 | +from arcp.auth.jwt import JWTValidator |
| 9 | +from arcp.errors import ARCPError, ErrorCode |
| 10 | + |
| 11 | +# PyJWT 2.10+ warns on HS256 secrets shorter than the algorithm's key size; pad to 32 bytes. |
| 12 | +_SECRET = "supers3cret-padding-to-32-bytes!" |
| 13 | +_AUDIENCE = "arcp-test" |
| 14 | + |
| 15 | + |
| 16 | +def _token(claims: dict[str, object], *, secret: str = _SECRET, alg: str = "HS256") -> str: |
| 17 | + return jwt.encode(claims, secret, algorithm=alg) |
| 18 | + |
| 19 | + |
| 20 | +def test_validates_and_returns_sub() -> None: |
| 21 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 22 | + token = _token({"sub": "alice", "aud": _AUDIENCE}) |
| 23 | + assert v.validate(token) == "alice" |
| 24 | + |
| 25 | + |
| 26 | +def test_falls_back_to_principal_claim() -> None: |
| 27 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 28 | + token = _token({"principal": "bob", "aud": _AUDIENCE}) |
| 29 | + assert v.validate(token) == "bob" |
| 30 | + |
| 31 | + |
| 32 | +def test_rejects_wrong_audience() -> None: |
| 33 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 34 | + token = _token({"sub": "alice", "aud": "different"}) |
| 35 | + with pytest.raises(ARCPError) as excinfo: |
| 36 | + v.validate(token) |
| 37 | + assert excinfo.value.code == ErrorCode.UNAUTHENTICATED |
| 38 | + |
| 39 | + |
| 40 | +def test_rejects_bad_signature() -> None: |
| 41 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 42 | + token = _token( |
| 43 | + {"sub": "alice", "aud": _AUDIENCE}, |
| 44 | + secret="wrong-secret-also-32-bytes-pad!!", |
| 45 | + ) |
| 46 | + with pytest.raises(ARCPError) as excinfo: |
| 47 | + v.validate(token) |
| 48 | + assert excinfo.value.code == ErrorCode.UNAUTHENTICATED |
| 49 | + |
| 50 | + |
| 51 | +def test_rejects_missing_sub_and_principal() -> None: |
| 52 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 53 | + token = _token({"aud": _AUDIENCE}) |
| 54 | + with pytest.raises(ARCPError, match="missing 'sub' claim") as excinfo: |
| 55 | + v.validate(token) |
| 56 | + assert excinfo.value.code == ErrorCode.UNAUTHENTICATED |
| 57 | + |
| 58 | + |
| 59 | +def test_rejects_non_string_sub() -> None: |
| 60 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 61 | + token = _token({"sub": 12345, "aud": _AUDIENCE}) |
| 62 | + with pytest.raises(ARCPError) as excinfo: |
| 63 | + v.validate(token) |
| 64 | + # PyJWT itself rejects non-string sub before our own check runs. |
| 65 | + assert excinfo.value.code == ErrorCode.UNAUTHENTICATED |
| 66 | + |
| 67 | + |
| 68 | +def test_rejects_empty_string_sub() -> None: |
| 69 | + v = JWTValidator(secret=_SECRET, audience=_AUDIENCE) |
| 70 | + token = _token({"sub": "", "aud": _AUDIENCE}) |
| 71 | + with pytest.raises(ARCPError, match="missing 'sub' claim"): |
| 72 | + v.validate(token) |
0 commit comments