forked from mtusi2026/mtusi2026lab3
Реализовать лабораторную работу 3
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Additional black-box checks for the Lab2 authentication boundary."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, check, run # noqa: E402
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--check-expiry", action="store_true")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
|
||||
username = "student_" + uuid.uuid4().hex[:12]
|
||||
password = "Длинный пароль с двоеточием: " + uuid.uuid4().hex
|
||||
registration = {"username": username, "display_name": "Проверка Lab2", "password": password}
|
||||
user = client.call("POST", "/api/v1/users", 201, body=registration, schema="User")["json"]
|
||||
check("password" not in user and "password_hash" not in user, "password leaked in User")
|
||||
client.call("POST", "/api/v1/users", 409, body=registration)
|
||||
client.call("POST", "/api/v1/users", 400, body={**registration, "unexpected": True})
|
||||
client.call("POST", "/api/v1/users", 400, body={**registration, "username": username + "\n"})
|
||||
client.call("POST", "/api/v1/users", 400, body={**registration, "password": "short"})
|
||||
client.call("POST", "/api/v1/users", 400, raw=b'{"username":',
|
||||
headers={"Content-Type": "application/json"})
|
||||
client.call("POST", "/api/v1/users", 400, raw=b'{}',
|
||||
headers={"Content-Type": "text/plain"})
|
||||
|
||||
def basic(secret):
|
||||
encoded = base64.b64encode((username + ":" + secret).encode("utf-8")).decode("ascii")
|
||||
return {"Authorization": "Basic " + encoded}
|
||||
|
||||
client.call("POST", "/api/v1/auth/sessions", 401, headers=basic("wrong password"))
|
||||
client.call("POST", "/api/v1/auth/sessions", 400, headers=basic(password), body={})
|
||||
session = client.call("POST", "/api/v1/auth/sessions", 201,
|
||||
headers=basic(password), schema="Session")["json"]
|
||||
token = session["token"]
|
||||
check(token != password and username not in token, "token contains credentials")
|
||||
client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": user["id"]})
|
||||
client.call("GET", "/api/v1/users/me", 401, headers=basic(password))
|
||||
|
||||
actor = {"id": user["id"], "token": token}
|
||||
me = client.call("GET", "/api/v1/users/me", actor=actor, schema="User")["json"]
|
||||
check(me == user, "Bearer session resolved to a different user")
|
||||
client.call("POST", "/api/v1/chats", 400, actor=actor,
|
||||
body={"title": "Bad members", "member_ids": [user["id"], str(uuid.uuid4())]})
|
||||
|
||||
if args.check_expiry:
|
||||
from compose_helpers import redis
|
||||
key = "messenger:session:" + hashlib.sha256(token.encode("ascii")).hexdigest()
|
||||
check(0 < int(redis("PTTL", key)) <= 2000, "short-TTL session missing cache expiry")
|
||||
time.sleep(3)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
check(redis("EXISTS", key) == "0", "expired session remained in Redis")
|
||||
print("PASS: TTL=2 expires in PostgreSQL authentication and Redis cache")
|
||||
else:
|
||||
client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
|
||||
print("PASS: Lab2 password, Basic, Bearer, input validation and session lifecycle")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user