#!/usr/bin/env python3 import base64 import uuid from client import Client, arguments, check, run def main(): args = arguments("Verify the shared login rate limit") client = Client(args.base_url, args.ca_file) username = "limit_" + uuid.uuid4().hex[:16] client.call( "POST", "/api/v1/users", 201, body={"username": username, "display_name": "Rate limit test", "password": "Correct-Password-123"}, schema="User", ) invalid = base64.b64encode((username + ":Wrong-Password-123").encode()).decode("ascii") for _ in range(5): client.call( "POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic " + invalid}, ) result = client.call( "POST", "/api/v1/auth/sessions", 429, headers={"Authorization": "Basic " + invalid}, ) check(result["json"]["error"]["code"] == "rate_limited", "expected rate_limited response") check(int(result["headers"].get("Retry-After", "0")) > 0, "missing positive Retry-After") print("PASS: sixth failed login in the window is rejected with 429 and Retry-After.") if __name__ == "__main__": run(main)