forked from mtusi2026/mtusi2026lab7
fix(auth): reject excess login concurrency before Argon2 work
This commit is contained in:
+20
-3
@@ -15,8 +15,20 @@ class Passwords:
|
||||
self.slots = threading.BoundedSemaphore(2)
|
||||
self.dummy = self.hasher.hash("Synthetic-dummy-password-not-for-login")
|
||||
|
||||
def _run(self, function, *args):
|
||||
if not self.slots.acquire(timeout=5):
|
||||
def _run(self, function, *args, wait=True):
|
||||
acquired = (
|
||||
self.slots.acquire(timeout=5)
|
||||
if wait
|
||||
else self.slots.acquire(blocking=False)
|
||||
)
|
||||
if not acquired:
|
||||
if not wait:
|
||||
raise DomainError(
|
||||
429,
|
||||
"rate_limited",
|
||||
"Authentication concurrency limit exceeded",
|
||||
{"Retry-After": "1"},
|
||||
)
|
||||
raise DomainError(503, "unavailable", "Authentication busy")
|
||||
try:
|
||||
return function(*args)
|
||||
@@ -29,7 +41,12 @@ class Passwords:
|
||||
def verify(self, *, password: str, encoded: str | None) -> bool:
|
||||
try:
|
||||
return (
|
||||
self._run(self.hasher.verify, encoded or self.dummy, password)
|
||||
self._run(
|
||||
self.hasher.verify,
|
||||
encoded or self.dummy,
|
||||
password,
|
||||
wait=False,
|
||||
)
|
||||
and encoded is not None
|
||||
)
|
||||
except VerificationError, InvalidHashError:
|
||||
|
||||
Reference in New Issue
Block a user