fix/password-unicode-validation-20261111
add-index-html-10623938925177053894
arena/094f314c-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | """Single-owner local session adapter; not a substitute for an enterprise IdP. |
| 2 | |
| 3 | Credentials use Argon2id. Imported PBKDF2 credentials are upgraded after a |
| 4 | successful login. Sessions and lockout state are process-local and fail closed |
| 5 | on restart; do not expose this adapter publicly without persistent controls. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import hashlib |
| 10 | import secrets |
| 11 | import threading |
| 12 | import time |
| 13 | from dataclasses import dataclass, replace |
| 14 | |
| 15 | from aram_backend.mapping_access import AuthenticatedPrincipal |
| 16 | from aram_backend.password_security import ( |
| 17 | encode_legacy_credential, |
| 18 | hash_password, |
| 19 | verify_password, |
| 20 | ) |
| 21 | |
| 22 | _MAX_PASSWORD_LENGTH = 1024 |
| 23 | _ALLOWED_ROLES = frozenset({"maker", "reviewer", "approver", "owner"}) |
| 24 | |
| 25 | |
| 26 | @dataclass(frozen=True) |
| 27 | class LocalCredential: |
| 28 | subject: str |
| 29 | company_scopes: frozenset[str] |
| 30 | roles: frozenset[str] |
| 31 | password_hash: str |
| 32 | |
| 33 | |
| 34 | def create_credential( |
| 35 | subject: str, |
| 36 | password: str, |
| 37 | companies: frozenset[str], |
| 38 | roles: frozenset[str], |
| 39 | ) -> LocalCredential: |
| 40 | if ( |
| 41 | not isinstance(subject, str) |
| 42 | or not subject.strip() |
| 43 | or len(subject) > 200 |
| 44 | or not isinstance(password, str) |
| 45 | or not 14 <= len(password) <= _MAX_PASSWORD_LENGTH |
| 46 | or not companies |
| 47 | or any(not isinstance(code, str) or not code.strip() or len(code) > 64 for code in companies) |
| 48 | or not roles |
| 49 | or not roles.issubset(_ALLOWED_ROLES) |
| 50 | ): |
| 51 | raise ValueError("invalid local credential configuration") |
| 52 | return LocalCredential( |
| 53 | subject.strip(), frozenset(companies), frozenset(roles), hash_password(password) |
| 54 | ) |
| 55 | |
| 56 | |
| 57 | def import_legacy_credential( |
| 58 | subject: str, |
| 59 | companies: frozenset[str], |
| 60 | roles: frozenset[str], |
| 61 | salt: bytes, |
| 62 | password_hash: bytes, |
| 63 | ) -> LocalCredential: |
| 64 | """Import old PBKDF2 material without receiving or storing plaintext.""" |
| 65 | if ( |
| 66 | not isinstance(subject, str) or not subject.strip() or len(subject) > 200 |
| 67 | or not companies or any(not isinstance(c, str) or not c.strip() or len(c) > 64 for c in companies) |
| 68 | or not roles or not roles.issubset(_ALLOWED_ROLES) |
| 69 | ): |
| 70 | raise ValueError("invalid local credential configuration") |
| 71 | encoded = encode_legacy_credential(salt, password_hash) |
| 72 | return LocalCredential(subject.strip(), frozenset(companies), frozenset(roles), encoded) |
| 73 | |
| 74 | |
| 75 | class LocalSessionAuthority: |
| 76 | """Volatile single-credential sessions with bounded lockout. |
| 77 | |
| 78 | Restart revokes all sessions and clears lockout. Lockout is process-local; |
| 79 | persistent cross-process rate limiting and durable identity are not provided. |
| 80 | """ |
| 81 | |
| 82 | def __init__( |
| 83 | self, |
| 84 | credential: LocalCredential, |
| 85 | *, |
| 86 | lifetime_seconds: int = 900, |
| 87 | max_failed_attempts: int = 5, |
| 88 | lockout_seconds: int = 60, |
| 89 | max_sessions: int = 8, |
| 90 | ): |
| 91 | if not 0 < lifetime_seconds <= 3600: |
| 92 | raise ValueError("invalid session lifetime") |
| 93 | if not 1 <= max_failed_attempts <= 20: |
| 94 | raise ValueError("invalid failed-attempt limit") |
| 95 | if not 1 <= lockout_seconds <= 3600: |
| 96 | raise ValueError("invalid lockout duration") |
| 97 | if not 1 <= max_sessions <= 64: |
| 98 | raise ValueError("invalid session limit") |
| 99 | self._credential = credential |
| 100 | self._lifetime = lifetime_seconds |
| 101 | self._max_failed_attempts = max_failed_attempts |
| 102 | self._lockout_seconds = lockout_seconds |
| 103 | self._max_sessions = max_sessions |
| 104 | self._failed_attempts = 0 |
| 105 | self._locked_until = 0.0 |
| 106 | self._sessions: dict[bytes, float] = {} |
| 107 | self._lock = threading.RLock() |
| 108 | |
| 109 | @property |
| 110 | def lifetime_seconds(self) -> int: |
| 111 | return self._lifetime |
| 112 | |
| 113 | def login(self, password: str) -> str | None: |
| 114 | if not isinstance(password, str) or len(password) > _MAX_PASSWORD_LENGTH: |
| 115 | return None |
| 116 | with self._lock: |
| 117 | now = time.monotonic() |
| 118 | if now < self._locked_until: |
| 119 | return None |
| 120 | verification = verify_password(password, self._credential.password_hash) |
| 121 | if not verification.valid: |
| 122 | self._failed_attempts += 1 |
| 123 | if self._failed_attempts >= self._max_failed_attempts: |
| 124 | self._locked_until = now + self._lockout_seconds |
| 125 | self._failed_attempts = 0 |
| 126 | return None |
| 127 | |
| 128 | if verification.upgraded_hash is not None: |
| 129 | self._credential = replace( |
| 130 | self._credential, password_hash=verification.upgraded_hash |
| 131 | ) |
| 132 | self._failed_attempts = 0 |
| 133 | self._locked_until = 0.0 |
| 134 | self._prune_expired(now) |
| 135 | if len(self._sessions) >= self._max_sessions: |
| 136 | return None |
| 137 | token = secrets.token_urlsafe(32) |
| 138 | self._sessions[hashlib.sha256(token.encode("utf-8")).digest()] = ( |
| 139 | now + self._lifetime |
| 140 | ) |
| 141 | return token |
| 142 | |
| 143 | def resolve(self, token: str) -> AuthenticatedPrincipal | None: |
| 144 | if not isinstance(token, str) or not token or len(token) > 256: |
| 145 | return None |
| 146 | key = hashlib.sha256(token.encode("utf-8")).digest() |
| 147 | with self._lock: |
| 148 | expires = self._sessions.get(key) |
| 149 | if expires is None: |
| 150 | return None |
| 151 | if time.monotonic() >= expires: |
| 152 | self._sessions.pop(key, None) |
| 153 | return None |
| 154 | return AuthenticatedPrincipal( |
| 155 | self._credential.subject, |
| 156 | self._credential.company_scopes, |
| 157 | self._credential.roles, |
| 158 | ) |
| 159 | |
| 160 | def revoke(self, token: str) -> None: |
| 161 | if isinstance(token, str) and len(token) <= 256: |
| 162 | key = hashlib.sha256(token.encode("utf-8")).digest() |
| 163 | with self._lock: |
| 164 | self._sessions.pop(key, None) |
| 165 | |
| 166 | def revoke_all(self) -> None: |
| 167 | with self._lock: |
| 168 | self._sessions.clear() |
| 169 | |
| 170 | def _prune_expired(self, now: float) -> None: |
| 171 | expired = [key for key, expires in self._sessions.items() if now >= expires] |
| 172 | for key in expired: |
| 173 | self._sessions.pop(key, None) |