Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 09:15 UTC
feat/gate3-package9-isolated-20261103
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 | """Opt-in local password verification and bounded opaque sessions. |
| 2 | |
| 3 | This remains a single-owner local adapter. It is not a production IdP and must |
| 4 | not be enabled until credential provisioning, DPAPI key custody, API wiring, |
| 5 | and deployment binding have been reviewed. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import hashlib |
| 10 | import hmac |
| 11 | import secrets |
| 12 | import threading |
| 13 | import time |
| 14 | from dataclasses import dataclass |
| 15 | |
| 16 | from aram_backend.mapping_access import AuthenticatedPrincipal |
| 17 | |
| 18 | _PBKDF2_ITERATIONS = 600_000 |
| 19 | _MAX_PASSWORD_LENGTH = 1024 |
| 20 | _ALLOWED_ROLES = frozenset({"maker", "reviewer", "approver", "owner"}) |
| 21 | |
| 22 | |
| 23 | @dataclass(frozen=True) |
| 24 | class LocalCredential: |
| 25 | subject: str |
| 26 | company_scopes: frozenset[str] |
| 27 | roles: frozenset[str] |
| 28 | salt: bytes |
| 29 | password_hash: bytes |
| 30 | |
| 31 | |
| 32 | def create_credential( |
| 33 | subject: str, |
| 34 | password: str, |
| 35 | companies: frozenset[str], |
| 36 | roles: frozenset[str], |
| 37 | ) -> LocalCredential: |
| 38 | if ( |
| 39 | not isinstance(subject, str) |
| 40 | or not subject.strip() |
| 41 | or len(subject) > 200 |
| 42 | or not isinstance(password, str) |
| 43 | or not 14 <= len(password) <= _MAX_PASSWORD_LENGTH |
| 44 | or not companies |
| 45 | or any(not isinstance(code, str) or not code.strip() or len(code) > 64 for code in companies) |
| 46 | or not roles |
| 47 | or not roles.issubset(_ALLOWED_ROLES) |
| 48 | ): |
| 49 | raise ValueError("invalid local credential configuration") |
| 50 | salt = secrets.token_bytes(32) |
| 51 | digest = hashlib.pbkdf2_hmac( |
| 52 | "sha256", password.encode("utf-8"), salt, _PBKDF2_ITERATIONS |
| 53 | ) |
| 54 | return LocalCredential(subject.strip(), frozenset(companies), frozenset(roles), salt, digest) |
| 55 | |
| 56 | |
| 57 | class LocalSessionAuthority: |
| 58 | """Volatile, single-credential session authority with bounded lockout. |
| 59 | |
| 60 | Restart revokes all sessions and clears lockout state. Persisted identity, |
| 61 | multi-user account management, and cross-process rate limiting are out of |
| 62 | scope; do not expose this adapter on a public network. |
| 63 | """ |
| 64 | |
| 65 | def __init__( |
| 66 | self, |
| 67 | credential: LocalCredential, |
| 68 | *, |
| 69 | lifetime_seconds: int = 900, |
| 70 | max_failed_attempts: int = 5, |
| 71 | lockout_seconds: int = 60, |
| 72 | max_sessions: int = 8, |
| 73 | ): |
| 74 | if not 0 < lifetime_seconds <= 3600: |
| 75 | raise ValueError("invalid session lifetime") |
| 76 | if not 1 <= max_failed_attempts <= 20: |
| 77 | raise ValueError("invalid failed-attempt limit") |
| 78 | if not 1 <= lockout_seconds <= 3600: |
| 79 | raise ValueError("invalid lockout duration") |
| 80 | if not 1 <= max_sessions <= 64: |
| 81 | raise ValueError("invalid session limit") |
| 82 | self._credential = credential |
| 83 | self._lifetime = lifetime_seconds |
| 84 | self._max_failed_attempts = max_failed_attempts |
| 85 | self._lockout_seconds = lockout_seconds |
| 86 | self._max_sessions = max_sessions |
| 87 | self._failed_attempts = 0 |
| 88 | self._locked_until = 0.0 |
| 89 | self._sessions: dict[bytes, float] = {} |
| 90 | self._lock = threading.RLock() |
| 91 | |
| 92 | def login(self, password: str) -> str | None: |
| 93 | if not isinstance(password, str) or len(password) > _MAX_PASSWORD_LENGTH: |
| 94 | return None |
| 95 | with self._lock: |
| 96 | now = time.monotonic() |
| 97 | if now < self._locked_until: |
| 98 | return None |
| 99 | candidate = hashlib.pbkdf2_hmac( |
| 100 | "sha256", |
| 101 | password.encode("utf-8"), |
| 102 | self._credential.salt, |
| 103 | _PBKDF2_ITERATIONS, |
| 104 | ) |
| 105 | if not hmac.compare_digest(candidate, self._credential.password_hash): |
| 106 | self._failed_attempts += 1 |
| 107 | if self._failed_attempts >= self._max_failed_attempts: |
| 108 | self._locked_until = now + self._lockout_seconds |
| 109 | self._failed_attempts = 0 |
| 110 | return None |
| 111 | |
| 112 | self._failed_attempts = 0 |
| 113 | self._locked_until = 0.0 |
| 114 | self._prune_expired(now) |
| 115 | if len(self._sessions) >= self._max_sessions: |
| 116 | return None |
| 117 | token = secrets.token_urlsafe(32) |
| 118 | self._sessions[hashlib.sha256(token.encode("utf-8")).digest()] = ( |
| 119 | now + self._lifetime |
| 120 | ) |
| 121 | return token |
| 122 | |
| 123 | def resolve(self, token: str) -> AuthenticatedPrincipal | None: |
| 124 | if not isinstance(token, str) or not token or len(token) > 256: |
| 125 | return None |
| 126 | key = hashlib.sha256(token.encode("utf-8")).digest() |
| 127 | with self._lock: |
| 128 | expires = self._sessions.get(key) |
| 129 | if expires is None: |
| 130 | return None |
| 131 | if time.monotonic() >= expires: |
| 132 | self._sessions.pop(key, None) |
| 133 | return None |
| 134 | return AuthenticatedPrincipal( |
| 135 | self._credential.subject, |
| 136 | self._credential.company_scopes, |
| 137 | self._credential.roles, |
| 138 | ) |
| 139 | |
| 140 | def revoke(self, token: str) -> None: |
| 141 | if isinstance(token, str) and len(token) <= 256: |
| 142 | key = hashlib.sha256(token.encode("utf-8")).digest() |
| 143 | with self._lock: |
| 144 | self._sessions.pop(key, None) |
| 145 | |
| 146 | def revoke_all(self) -> None: |
| 147 | with self._lock: |
| 148 | self._sessions.clear() |
| 149 | |
| 150 | def _prune_expired(self, now: float) -> None: |
| 151 | expired = [key for key, expires in self._sessions.items() if now >= expires] |
| 152 | for key in expired: |
| 153 | self._sessions.pop(key, None) |