forked from mtusi2026/mtusi2026lab2
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Raw expiry check without OpenAPI schema validation overhead."""
|
|
import base64
|
|
import json
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
|
|
base = "http://127.0.0.1:8080"
|
|
username = "u_raw_" + uuid.uuid4().hex[:10]
|
|
password = "T3st-" + uuid.uuid4().hex
|
|
body = json.dumps(
|
|
{"username": username, "display_name": "Raw", "password": password}
|
|
).encode()
|
|
req = urllib.request.Request(
|
|
base + "/api/v1/users",
|
|
data=body,
|
|
headers={"Content-Type": "application/json", "Accept": "application/json"},
|
|
method="POST",
|
|
)
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
user = json.loads(r.read())
|
|
print("user", user["id"])
|
|
|
|
cred = base64.b64encode((username + ":" + password).encode()).decode()
|
|
req = urllib.request.Request(
|
|
base + "/api/v1/auth/sessions",
|
|
data=b"",
|
|
headers={"Authorization": "Basic " + cred, "Accept": "application/json"},
|
|
method="POST",
|
|
)
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
session = json.loads(r.read())
|
|
print("token_prefix", session["token"][:16], "exp", session["expires_at"])
|
|
|
|
req = urllib.request.Request(
|
|
base + "/api/v1/users/me",
|
|
headers={"Authorization": "Bearer " + session["token"], "Accept": "application/json"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=10) as r:
|
|
print("me", json.loads(r.read())["id"])
|
|
|
|
time.sleep(6)
|
|
req = urllib.request.Request(
|
|
base + "/api/v1/users/me",
|
|
headers={"Authorization": "Bearer " + session["token"], "Accept": "application/json"},
|
|
)
|
|
try:
|
|
urllib.request.urlopen(req, timeout=10)
|
|
raise SystemExit("FAIL still valid")
|
|
except urllib.error.HTTPError as exc:
|
|
print("after wait status", exc.code)
|
|
if exc.code != 401:
|
|
raise SystemExit("expected 401")
|
|
print("PASS: session expired after short TTL")
|