File size: 3,262 Bytes
1d697a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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"