forked from mtusi2026/mtusi2026lab2
71 lines
3.7 KiB
Python
Executable File
71 lines
3.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Sessions: wrong password, Basic/Bearer separation, X-User-Id ignored, logout, expiry.
|
|
|
|
Expiry runs against the api-short-ttl service (SESSION_TTL_SECONDS=2):
|
|
docker compose --profile ttl-test up -d --wait api-short-ttl
|
|
"""
|
|
import base64
|
|
import time
|
|
import uuid
|
|
|
|
from stack import BASE_URL, SHORT_TTL_URL, Client, check, main_wrapper, ok
|
|
|
|
|
|
def basic(username, password):
|
|
return {"Authorization": "Basic " + base64.b64encode((username + ":" + password).encode("utf-8")).decode()}
|
|
|
|
|
|
def main():
|
|
c = Client(BASE_URL)
|
|
alice = c.user("auth")
|
|
reg = alice["registration"]
|
|
|
|
# Wrong password and unknown user: the same 401 with a Basic challenge.
|
|
r1 = c.call("POST", "/api/v1/auth/sessions", 401, headers=basic(reg["username"], "wrong-password-123"))
|
|
r2 = c.call("POST", "/api/v1/auth/sessions", 401, headers=basic("nobody_" + uuid.uuid4().hex[:8], "wrong-password-123"))
|
|
check(r1["json"]["error"]["message"] == r2["json"]["error"]["message"], "unknown user must look like a wrong password")
|
|
ok("wrong password / unknown user -> identical 401 Basic")
|
|
|
|
c.call("POST", "/api/v1/auth/sessions", 401)
|
|
c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic !!!not-base64"})
|
|
c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Bearer " + alice["token"]})
|
|
ok("missing / malformed / non-Basic login -> 401")
|
|
|
|
# A password with ':' and non-ASCII characters survives Basic encoding.
|
|
tricky = "пароль:с:двоеточием-" + uuid.uuid4().hex[:6]
|
|
user = c.call("POST", "/api/v1/users", 201, body={"username": "u_colon_" + uuid.uuid4().hex[:8], "display_name": "Двоеточие", "password": tricky}, schema="User")["json"]
|
|
c.call("POST", "/api/v1/auth/sessions", 201, headers=basic(user["username"], tricky), schema="Session")
|
|
ok("UTF-8 password containing ':' -> 201")
|
|
|
|
c.call("POST", "/api/v1/users", 400, body={"username": "u_short_" + uuid.uuid4().hex[:8], "display_name": "x", "password": "short-11ch"})
|
|
c.call("POST", "/api/v1/users", 400, body={"username": "u_nopass_" + uuid.uuid4().hex[:8], "display_name": "x"})
|
|
ok("password shorter than 12 / missing -> 400")
|
|
|
|
me = c.call("GET", "/api/v1/users/me", 401, headers=basic(reg["username"], reg["password"]))
|
|
check(me["headers"].get("WWW-Authenticate", "").startswith("Bearer"), "Bearer challenge expected")
|
|
c.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
|
|
c.call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer " + uuid.uuid4().hex * 2})
|
|
ok("Basic / X-User-Id / unknown token on business endpoint -> 401")
|
|
|
|
session = c.call("POST", "/api/v1/auth/sessions", 201, headers=basic(reg["username"], reg["password"]), schema="Session")["json"]
|
|
check(session["token"] != alice["token"], "every login must issue a new token")
|
|
second = dict(alice, token=session["token"])
|
|
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
|
|
c.call("GET", "/api/v1/users/me", 401, actor=alice)
|
|
c.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=alice)
|
|
c.call("GET", "/api/v1/users/me", 200, actor=second, schema="User")
|
|
ok("logout revokes only the current token; repeat logout -> 401")
|
|
|
|
# Expiry: separate instance of the same image with SESSION_TTL_SECONDS=2.
|
|
s = Client(SHORT_TTL_URL)
|
|
bob = s.user("ttl")
|
|
s.call("GET", "/api/v1/users/me", 200, actor=bob, schema="User")
|
|
time.sleep(3)
|
|
s.call("GET", "/api/v1/users/me", 401, actor=bob)
|
|
s.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=bob)
|
|
ok("session with TTL=2s is rejected after 3s (cache entry included)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main_wrapper(main)
|