import base64 import hashlib import hmac import json import time import uuid import pytest import auth def _b64(b: bytes) -> str: return base64.urlsafe_b64encode(b).decode().rstrip("=") def make_token(secret="test-secret", **over): now = int(time.time()) payload = {"u": 7, "e": now + 600, "j": uuid.uuid4().hex, "s": "chat", "k": "free", "iat": now} payload.update(over) msg = _b64(json.dumps(payload).encode()) sig = _b64(hmac.new(secret.encode(), msg.encode(), hashlib.sha256).digest()) return msg + "." + sig @pytest.fixture(autouse=True) def _fresh_jti(monkeypatch, tmp_path): monkeypatch.setattr(auth, "_used_jti", {}) monkeypatch.setattr(auth, "_JTI_FILE", str(tmp_path / "jti.json")) monkeypatch.setattr(auth, "SIGNING_SECRET", "test-secret") monkeypatch.setattr(auth, "REQUIRE_SCOPE", False) def test_good_token_ok(): assert auth.check_usage_token(make_token(), "chat") == "ok" def test_replay_refused(): t = make_token() assert auth.check_usage_token(t, "chat") == "ok" assert auth.check_usage_token(t, "chat") == "replayed" def test_replay_survives_restart(tmp_path): t = make_token() assert auth.check_usage_token(t, "chat") == "ok" auth._used_jti.clear() # simulate a process restart auth._used_jti.update(auth._load_jti()) assert auth.check_usage_token(t, "chat") == "replayed" def test_wrong_secret_is_bad_signature(): assert auth.check_usage_token(make_token(secret="other"), "chat") == "bad-signature" def test_tampered_payload_is_bad_signature(): t = make_token() msg, sig = t.rsplit(".", 1) forged = _b64(json.dumps({"u": 7, "e": int(time.time()) + 600, "j": "x", "s": "chat"}).encode()) assert auth.check_usage_token(forged + "." + sig, "chat") == "bad-signature" def test_expired(): assert auth.check_usage_token(make_token(e=int(time.time()) - 1), "chat") == "expired" def test_future_iat(): now = int(time.time()) assert auth.check_usage_token(make_token(iat=now + 3600, e=now + 3900), "chat") == "future" def test_lifetime_too_long(): now = int(time.time()) assert auth.check_usage_token(make_token(iat=now - 100, e=now + 3000), "chat") == "expired" def test_wrong_scope(): assert auth.check_usage_token(make_token(s="run"), "chat") == "wrong-scope" def test_missing_scope_tolerated_until_switch(monkeypatch): t = make_token() t_noscope = make_token(**{"s": None}) assert auth.check_usage_token(t_noscope, "chat") == "ok" monkeypatch.setattr(auth, "REQUIRE_SCOPE", True) assert auth.check_usage_token(make_token(**{"s": None}), "chat") == "missing-scope" assert auth.check_usage_token(t, "chat") == "ok" def test_no_secret_refuses_everything(monkeypatch): monkeypatch.setattr(auth, "SIGNING_SECRET", "") assert auth.check_usage_token(make_token(), "chat") == "unconfigured" assert auth.verify_usage_token(make_token(), "chat") is False @pytest.mark.parametrize("bad", ["", None, "abc", "a.b", "x" * 3000, "not-base64.!!!"]) def test_garbage(bad): assert auth.check_usage_token(bad, "chat") in ("missing", "bad-signature") def test_missing_jti(): assert auth.check_usage_token(make_token(j=""), "chat") == "missing"