feat(credentials): add stage 2 centralized broker and refresh fencing
Add an internal CredentialBroker that serializes refresh and authorization by environment/app/user using renewable Redis leases. Re-read credentials under the lease and persist rotated pairs with expected-version CAS before returning an explicit access-only grant. Verify the actual platform user, remaining lifetime and scopes; never return refresh tokens from the broker. Persist source-version refresh attempts before platform calls. Timeouts, cancellation, lease loss, malformed responses and ambiguous database errors fence reuse of the old rotating refresh token, including after restart. A new authorization can establish a new version without stale overwrite. Add a strict, separate private Work v1 OIDC adapter while preserving legacy OAuth method behavior. Enable fakeredis Lua test support for Redis lease renewal tests. Document stage boundaries, recovery limitations and testing. Validation: 147 passed, 2 skipped (real PostgreSQL not configured), with 15 existing uvicorn/websockets deprecation warnings. New broker/adapter coverage: 45 tests. AST, runtime opt-in boundary and diff checks pass. An earlier Windows temp-file replace failure in a legacy client test passed both isolated rerun and the final full suite; no client code was changed. No public credential/OAuth routes are wired, no production database/key is initialized, and no real Work authorization, refresh or ECS deployment was performed. Authenticated access-token issuance, state transactions, binding checks and unbind integration remain subsequent stages.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Server-only encrypted user credentials. Not wired to public routes in stage 1."""
|
||||
"""Server-only encrypted credentials and durable refresh fencing, opt-in only."""
|
||||
import base64
|
||||
import json
|
||||
import math
|
||||
@@ -106,6 +106,38 @@ class CredentialStore:
|
||||
credential_version INTEGER NOT NULL, key_id TEXT NOT NULL,
|
||||
ciphertext TEXT NOT NULL, updated_at DOUBLE PRECISION NOT NULL,
|
||||
PRIMARY KEY(environment, app_id, principal_id))""")
|
||||
self.db.execute("""CREATE TABLE IF NOT EXISTS work_refresh_attempts_v2(
|
||||
environment TEXT NOT NULL, app_id TEXT NOT NULL, principal_id TEXT NOT NULL,
|
||||
source_version INTEGER NOT NULL, attempt_id TEXT NOT NULL,
|
||||
status TEXT NOT NULL, updated_at DOUBLE PRECISION NOT NULL,
|
||||
PRIMARY KEY(environment, app_id, principal_id, source_version))""")
|
||||
|
||||
def refresh_status(self, environment, app_id, principal_id, version):
|
||||
identity = tuple(_identifier(v) for v in (environment, app_id, principal_id))
|
||||
row = self.db.execute("""SELECT status FROM work_refresh_attempts_v2
|
||||
WHERE environment=? AND app_id=? AND principal_id=? AND source_version=?""",
|
||||
(*identity, version)).fetchone()
|
||||
return row["status"] if row else None
|
||||
|
||||
def begin_refresh(self, environment, app_id, principal_id, version, attempt_id):
|
||||
identity = tuple(_identifier(v) for v in (environment, app_id, principal_id))
|
||||
_identifier(attempt_id)
|
||||
with self.db:
|
||||
count = self.db.execute("""INSERT INTO work_refresh_attempts_v2
|
||||
VALUES(?,?,?,?,?,?,?) ON CONFLICT DO NOTHING""",
|
||||
(*identity, version, attempt_id, "started", time.time())).rowcount
|
||||
return count == 1
|
||||
|
||||
def finish_refresh(self, environment, app_id, principal_id, version, attempt_id, status):
|
||||
identity = tuple(_identifier(v) for v in (environment, app_id, principal_id))
|
||||
if status not in {"completed", "unknown"}:
|
||||
raise CredentialError("invalid refresh status")
|
||||
with self.db:
|
||||
count = self.db.execute("""UPDATE work_refresh_attempts_v2 SET status=?,updated_at=?
|
||||
WHERE environment=? AND app_id=? AND principal_id=? AND source_version=?
|
||||
AND attempt_id=? AND status='started'""",
|
||||
(status, time.time(), *identity, version, attempt_id)).rowcount
|
||||
return count == 1
|
||||
|
||||
@staticmethod
|
||||
def _aad(identity, version, key_id):
|
||||
|
||||
Reference in New Issue
Block a user