@davoodmehraban89 / Aram-2 Mirror · read-only / commits / afe380f
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 20:45 UTC

test(auth): reject unsigned, wrong issuer/audience, expired and forged reviewer tokens

davoodmehraban89 committed Oct 30, 2026 at 22:11 UTC afe380fc0a2236f9eb2cdfce21168a1dbc507bfd
1 file changed +181
backend/tests/test_authenticated_identity.py new
+181
@@ -0,0 +1,181 @@
1 +"""Synthetic RSA token checks only; NOT a connected real enterprise IdP."""
2 +from datetime import UTC, datetime
3 +from time import time
4 +
5 +import jwt
6 +import pytest
7 +from cryptography.hazmat.primitives import serialization
8 +from cryptography.hazmat.primitives.asymmetric import rsa
9 +from fastapi.testclient import TestClient
10 +
11 +from aram_backend.config import Settings, get_settings
12 +from aram_backend.main import app
13 +
14 +ISSUER = "https://synthetic-identity.example.invalid/"
15 +AUDIENCE = "aram-mapping-api"
16 +
17 +
18 +@pytest.fixture(scope="module")
19 +def keys():
20 + private = rsa.generate_private_key(public_exponent=65537, key_size=2048)
21 + public = private.public_key().public_bytes(
22 + encoding=serialization.Encoding.PEM,
23 + format=serialization.PublicFormat.SubjectPublicKeyInfo,
24 + ).decode("ascii")
25 + other = rsa.generate_private_key(public_exponent=65537, key_size=2048)
26 + return private, public, other
27 +
28 +
29 +@pytest.fixture
30 +def auth_client(keys):
31 + private, public, other = keys
32 + app.dependency_overrides[get_settings] = lambda: Settings(
33 + auth_issuer=ISSUER, auth_audience=AUDIENCE,
34 + auth_public_key_pem=public,
35 + )
36 + try:
37 + with TestClient(app) as client:
38 + yield client
39 + finally:
40 + app.dependency_overrides.pop(get_settings, None)
41 +
42 +
43 +def claims(**extra):
44 + now = int(time())
45 + data = {
46 + "iss": ISSUER, "aud": AUDIENCE, "sub": "employee-108",
47 + "exp": now + 300, "iat": now - 10, "nbf": now - 10,
48 + "token_use": "access", "aram_roles": ["mapping_proposer"],
49 + }
50 + data.update(extra)
51 + return data
52 +
53 +
54 +def token(private, **extra):
55 + return jwt.encode(claims(**extra), private, algorithm="RS256")
56 +
57 +
58 +def request(client, bearer, **headers):
59 + return client.get(
60 + "/api/v1/auth/me",
61 + headers={"Authorization": f"Bearer {bearer}", **headers},
62 + )
63 +
64 +
65 +def test_valid_rs256_access_token_returns_verified_subject_and_role(auth_client, keys):
66 + response = request(auth_client, token(keys[0]))
67 + assert response.status_code == 200
68 + body = response.json()
69 + assert body["issuer"] == ISSUER
70 + assert body["subject"] == "employee-108"
71 + assert body["roles"] == ["mapping_proposer"]
72 + assert body["expires_at"] > int(time())
73 + assert "token" not in body
74 + assert "private_key" not in body
75 +
76 +
77 +def test_missing_bearer_or_forged_identity_headers_are_not_authentication(auth_client):
78 + response = auth_client.get(
79 + "/api/v1/auth/me",
80 + headers={"X-User": "admin", "X-ARAM-Roles": "mapping_reviewer"},
81 + )
82 + assert response.status_code == 401
83 + assert response.headers["www-authenticate"] == "Bearer"
84 +
85 +
86 +def test_wrong_key_signed_token_cannot_impersonate_user(auth_client, keys):
87 + response = request(
88 + auth_client,
89 + token(keys[2], sub="finance-director", aram_roles=["mapping_reviewer"]),
90 + )
91 + assert response.status_code == 401
92 + assert response.json()["detail"] == "Invalid bearer token"
93 +
94 +
95 +@pytest.mark.parametrize("overrides", [
96 + {"iss": "https://attacker.invalid/"},
97 + {"aud": "not-aram"},
98 + {"exp": 1},
99 + {"nbf": int(time()) + 10000},
100 + {"iat": int(time()) + 10000},
101 + {"exp": int(time()) + 20000},
102 + {"sub": ""},
103 + {"sub": " invalid "},
104 + {"sub": 123},
105 + {"token_use": "id"},
106 + {"aram_roles": "mapping_reviewer"},
107 + {"aram_roles": ["system_administrator"]},
108 + {"aram_roles": ["mapping_reviewer", "mapping_reviewer"]},
109 + {"aram_roles": ["mapping_proposer", "mapping_reviewer"]},
110 + {"aram_roles": []},
111 + {"aram_roles": [123]},
112 +])
113 +def test_invalid_or_overprivileged_claims_are_rejected(auth_client, keys, overrides):
114 + assert request(auth_client, token(keys[0], **overrides)).status_code == 401
115 +
116 +
117 +@pytest.mark.parametrize("missing", [
118 + "iss", "aud", "sub", "exp", "iat", "nbf", "token_use", "aram_roles",
119 +])
120 +def test_required_claims_cannot_be_omitted(auth_client, keys, missing):
121 + data = claims()
122 + del data[missing]
123 + assert request(
124 + auth_client, jwt.encode(data, keys[0], algorithm="RS256")
125 + ).status_code == 401
126 +
127 +
128 +def test_unsigned_and_hmac_algorithms_are_never_accepted(auth_client, keys):
129 + unsigned = jwt.encode(claims(), key="", algorithm="none")
130 + hmac_signed = jwt.encode(claims(), key="untrusted", algorithm="HS256")
131 + assert request(auth_client, unsigned).status_code == 401
132 + assert request(auth_client, hmac_signed).status_code == 401
133 +
134 +
135 +def test_roles_must_come_from_signed_claims_not_http_headers(auth_client, keys):
136 + response = request(
137 + auth_client, token(keys[0]),
138 + **{"X-ARAM-Roles": "mapping_reviewer", "X-Approver-ID": "boss"},
139 + )
140 + assert response.status_code == 200
141 + assert response.json()["roles"] == ["mapping_proposer"]
142 +
143 +
144 +def test_unconfigured_identity_is_closed_and_never_bypasses(auth_client, keys):
145 + app.dependency_overrides[get_settings] = lambda: Settings()
146 + response = request(auth_client, token(keys[0]))
147 + assert response.status_code == 503
148 + assert "not configured" in response.json()["detail"].lower()
149 +
150 +
151 +def test_private_key_misconfiguration_is_rejected(auth_client, keys):
152 + secret_pem = keys[0].private_bytes(
153 + encoding=serialization.Encoding.PEM,
154 + format=serialization.PrivateFormat.PKCS8,
155 + encryption_algorithm=serialization.NoEncryption(),
156 + ).decode()
157 + app.dependency_overrides[get_settings] = lambda: Settings(
158 + auth_issuer=ISSUER, auth_audience=AUDIENCE,
159 + auth_public_key_pem=secret_pem,
160 + )
161 + assert request(auth_client, token(keys[0])).status_code == 503
162 +
163 +
164 +def test_only_bearer_scheme_allowed(auth_client, keys):
165 + response = auth_client.get(
166 + "/api/v1/auth/me",
167 + headers={"Authorization": "Basic abc"},
168 + )
169 + assert response.status_code == 401
170 +
171 +
172 +def test_long_bearer_token_is_rejected(auth_client):
173 + response = request(auth_client, "x" * 9000)
174 + assert response.status_code == 401
175 +
176 +
177 +def test_only_a_read_only_identity_endpoint_is_exposed(auth_client, keys):
178 + assert auth_client.post(
179 + "/api/v1/auth/me", headers={"Authorization": f"Bearer {token(keys[0])}"}
180 + ).status_code == 405
181 + assert auth_client.get("/api/v1/health").status_code == 200