|
| 1 | +"""Local access-attempt limiter for recovery flows.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +import time |
| 7 | + |
| 8 | +from .config import access_lockout_seconds, access_max_failures |
| 9 | +from .state_store import LocalStateStore |
| 10 | + |
| 11 | +ATTEMPT_STATE_NAME = "access_attempts.json" |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class AttemptDecision: |
| 16 | + allowed: bool |
| 17 | + wait_seconds: int = 0 |
| 18 | + |
| 19 | + |
| 20 | +class AttemptLimiter: |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + max_failures: int | None = None, |
| 24 | + lockout_seconds: int | None = None, |
| 25 | + clock=None, |
| 26 | + ): |
| 27 | + self.max_failures = max_failures or access_max_failures() |
| 28 | + self.lockout_seconds = lockout_seconds or access_lockout_seconds() |
| 29 | + self.clock = clock or time.time |
| 30 | + self._state = {} |
| 31 | + |
| 32 | + def check(self, scope: str): |
| 33 | + state = self._state.get(scope, {"failures": 0, "locked_until": 0}) |
| 34 | + now = int(self.clock()) |
| 35 | + locked_until = int(state.get("locked_until", 0)) |
| 36 | + if locked_until > now: |
| 37 | + return AttemptDecision(False, locked_until - now) |
| 38 | + return AttemptDecision(True, 0) |
| 39 | + |
| 40 | + def record_failure(self, scope: str): |
| 41 | + state = self._state.get(scope, {"failures": 0, "locked_until": 0}) |
| 42 | + failures = int(state.get("failures", 0)) + 1 |
| 43 | + locked_until = 0 |
| 44 | + if failures >= self.max_failures: |
| 45 | + locked_until = int(self.clock()) + self.lockout_seconds |
| 46 | + self._state[scope] = { |
| 47 | + "failures": failures, |
| 48 | + "locked_until": locked_until, |
| 49 | + } |
| 50 | + |
| 51 | + def record_success(self, scope: str): |
| 52 | + self._state.pop(scope, None) |
| 53 | + |
| 54 | + |
| 55 | +class FileAttemptLimiter(AttemptLimiter): |
| 56 | + def __init__(self, store: LocalStateStore | None = None, **kwargs): |
| 57 | + super().__init__(**kwargs) |
| 58 | + self.store = store or LocalStateStore() |
| 59 | + self._state = self._load() |
| 60 | + |
| 61 | + def record_failure(self, scope: str): |
| 62 | + super().record_failure(scope) |
| 63 | + self._save() |
| 64 | + |
| 65 | + def record_success(self, scope: str): |
| 66 | + super().record_success(scope) |
| 67 | + self._save() |
| 68 | + |
| 69 | + def _load(self): |
| 70 | + try: |
| 71 | + data = self.store.read_json(ATTEMPT_STATE_NAME) |
| 72 | + except Exception: |
| 73 | + return {} |
| 74 | + if not isinstance(data, dict): |
| 75 | + return {} |
| 76 | + return { |
| 77 | + str(scope): { |
| 78 | + "failures": int(value.get("failures", 0)), |
| 79 | + "locked_until": int(value.get("locked_until", 0)), |
| 80 | + } |
| 81 | + for scope, value in data.items() |
| 82 | + if isinstance(value, dict) |
| 83 | + } |
| 84 | + |
| 85 | + def _save(self): |
| 86 | + self.store.write_json_atomic(ATTEMPT_STATE_NAME, self._state) |
0 commit comments