PostgreSQL как источник истины, непрозрачные сессии в Redis с TTL и AOF, миграции под advisory lock, пул и таймауты, проверки гонки/отката/отказов и отчёт с evidence.
59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Wrong password, Basic-on-business-endpoint, logout and reuse checks."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
|
|
from common import HttpClient, assert_status, error_code
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--base-url", default="http://localhost:8080")
|
|
args = parser.parse_args()
|
|
client = HttpClient(args.base_url)
|
|
user, registration = client.register()
|
|
|
|
wrong = base64.b64encode(
|
|
(registration["username"] + ":wrong-password-value").encode("utf-8")
|
|
).decode("ascii")
|
|
status, headers, payload, _ = client.call(
|
|
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + wrong}
|
|
)
|
|
assert_status(status, 401, "wrong password")
|
|
assert error_code(payload) == "unauthorized"
|
|
assert headers.get("WWW-Authenticate", "").lower().startswith("basic")
|
|
|
|
good = base64.b64encode(
|
|
(registration["username"] + ":" + registration["password"]).encode("utf-8")
|
|
).decode("ascii")
|
|
status, headers, _, _ = client.call(
|
|
"GET", "/api/v1/users/me", headers={"Authorization": "Basic " + good}
|
|
)
|
|
assert_status(status, 401, "Basic on business endpoint")
|
|
assert headers.get("WWW-Authenticate", "").lower().startswith("bearer")
|
|
|
|
session = client.login(registration)
|
|
token = session["token"]
|
|
status, _, me, _ = client.call("GET", "/api/v1/users/me", token=token)
|
|
assert_status(status, 200, "login")
|
|
assert me["id"] == user["id"]
|
|
|
|
status, _, _, raw = client.call("DELETE", "/api/v1/auth/sessions/current", token=token)
|
|
assert_status(status, 204, "logout")
|
|
assert raw == b"", "204 must have an empty body"
|
|
|
|
status, headers, payload, _ = client.call("GET", "/api/v1/users/me", token=token)
|
|
assert_status(status, 401, "token after logout")
|
|
assert error_code(payload) == "unauthorized"
|
|
assert headers.get("WWW-Authenticate", "").lower().startswith("bearer")
|
|
|
|
status, _, _, _ = client.call("DELETE", "/api/v1/auth/sessions/current", token=token)
|
|
assert_status(status, 401, "second logout")
|
|
print("PASS: wrong password, Basic rejection, logout and token reuse checks")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|