docs(code): add concise docstrings

This commit is contained in:
2026-07-14 11:05:07 +03:30
parent c54f6edc1e
commit 5f88e964f6
20 changed files with 299 additions and 26 deletions

View File

@@ -7,10 +7,15 @@ from gapido_auth.domain.ports import OtpStore
class RedisOtpStore(OtpStore):
"""Redis adapter for OTP hashes, verification attempts, and request throttling."""
def __init__(self, redis: Redis) -> None:
"""Bind the store to an async Redis client."""
self._redis = redis
async def allow_request(self, key: str, limit: int, window_seconds: int) -> bool:
"""Increment a rate-limit counter and report whether it remains within limit."""
count = await self._redis.incr(key)
if count == 1:
await self._redis.expire(key, window_seconds)
@@ -24,6 +29,8 @@ class RedisOtpStore(OtpStore):
ttl_seconds: int,
max_attempts: int,
) -> None:
"""Store a hashed OTP and reset its attempt counter with the same TTL window."""
key = self._otp_key(mobile, purpose)
attempts_key = self._attempts_key(mobile, purpose)
async with self._redis.pipeline(transaction=True) as pipe:
@@ -34,6 +41,8 @@ class RedisOtpStore(OtpStore):
await pipe.execute()
async def verify_otp(self, mobile: str, purpose: str, candidate_hash: str) -> bool:
"""Compare a submitted OTP hash and delete state on success or exhausted attempts."""
key = self._otp_key(mobile, purpose)
attempts_key = self._attempts_key(mobile, purpose)
@@ -60,8 +69,10 @@ class RedisOtpStore(OtpStore):
@staticmethod
def _otp_key(mobile: str, purpose: str) -> str:
"""Return the Redis hash key for an OTP challenge."""
return f"otp:{mobile}:{purpose}"
@staticmethod
def _attempts_key(mobile: str, purpose: str) -> str:
"""Return the Redis counter key for OTP verification attempts."""
return f"otp-attempts:{mobile}:{purpose}"