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:
ex_tianzy
2026-11-04 10:30:27 +08:00
parent 71b9678d22
commit 02d433250f
8 changed files with 788 additions and 5 deletions
+31
View File
@@ -84,6 +84,37 @@ class Feishu:
headers={"Authorization": f"Bearer {token}"})
return data["data"]
async def _credential_request(self, grant):
"""Strict stage-2 adapter; legacy OAuth normalization remains unchanged.
Missing rotation fields are not silently defaulted; the Broker validates
them and treats an ambiguous refresh as unknown rather than replaying it.
"""
try:
token = await self.app_token()
path = ("/open-apis/authen/v1/oidc/access_token" if grant["grant_type"] == "authorization_code"
else "/open-apis/authen/v1/oidc/refresh_access_token")
response = await self.http.post(path, headers={"Authorization": f"Bearer {token}"}, json=grant)
response.raise_for_status()
body = response.json()
if not isinstance(body, dict) or type(body.get("code")) is not int or body["code"] != 0:
raise ValueError()
data = body["data"]
result = {"user_access_token": data["access_token"], "refresh_token": data["refresh_token"],
"expires_in": data.get("expires_in", data.get("expire")),
"refresh_expires_in": data.get("refresh_expires_in")}
if "scope" in data:
result["scope"] = data["scope"]
return result
except Exception:
raise PlatformError("credential platform request failed") from None
async def credential_exchange(self, code):
return await self._credential_request({"grant_type": "authorization_code", "code": code})
async def credential_refresh(self, refresh_token):
return await self._credential_request({"grant_type": "refresh_token", "refresh_token": refresh_token})
async def reply(self, message_id, text):
# Reply to the original message only: no caller-controlled recipient.
token = await self.tenant_token()