feat(credentials): add stage 3 authenticated OAuth and access-token issuance

Add explicit legacy/broker configuration. Broker mode requires a protected
keyring, product-approved permission profiles and enabled platform OAuth;
production requires PostgreSQL. Default legacy behavior stays unchanged.

Wire the broker to v2 signed device status, authorization start/status and
access-token issuance routes. Bind proofs to protocol, environment, app,
method/path and complete payload; verify nonce, certificate, owner and
binding generation. Add an additive generation migration and return the
current generation in binding receipts or authenticated bootstrap status.

Persist encrypted OAuth state and atomically claim callbacks. Verify the
actual consenting user, supersede older pending authorization intents and
recheck bindings/transactions before credential writes and issuance.
Preserve rotated credentials if unbind occurs mid-refresh, but deny issuance.
Return an explicit access-only response and record metadata-only audit.

Extract shared legacy device authentication so notifications do not depend
on the business proxy module. Broker mode omits legacy refresh/claim/start
and /work/api routes; authorization cards reference server-owned v2
transactions. Document signatures, configuration and migration boundaries.

Validation: 185 passed, 3 skipped (real PostgreSQL not configured), with
15 existing dependency deprecation warnings. New API coverage: 38 passed,
1 real PostgreSQL case skipped. AST, docs and staged diff checks pass.

No production deployment, key generation, client upgrade or real Work API
calls were performed. Compose/mount configuration, full unbind/connection
compensation and durable message-task lifecycle remain later stages.
This commit is contained in:
ex_tianzy
2026-11-04 10:52:16 +08:00
parent 02d433250f
commit 93bfbca83a
13 changed files with 1066 additions and 47 deletions
+21
View File
@@ -1,4 +1,6 @@
from pathlib import Path
import re
from urllib.parse import urlsplit
from pydantic import model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -39,9 +41,28 @@ class Settings(BaseSettings):
max_result: int = 16000
allow_insecure_local: bool = False
enrollment_signer_url: str = ""
credential_mode: str = "legacy"
credential_keyring_file: Path | None = None
credential_permission_profiles: dict[str, list[str]] = {}
@model_validator(mode="after")
def validate_runtime(self):
if self.credential_mode not in {"legacy", "broker"}:
raise ValueError("invalid credential mode")
for profile, scopes in self.credential_permission_profiles.items():
if (not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", profile) or not scopes or len(scopes) > 128
or any(not re.fullmatch(r"[A-Za-z0-9_:.-]{1,256}", scope) for scope in scopes)):
raise ValueError("invalid credential permission profile")
if self.credential_mode == "broker" and self.role == "connector":
if self.environment == "production" and not self.database_url:
raise ValueError("production broker mode requires PostgreSQL")
if (not self.credential_keyring_file or not self.credential_permission_profiles
or not self.feishu_enabled or not self.oauth_enabled):
raise ValueError("broker mode requires keyring, product permissions and enabled platform OAuth")
authorize = urlsplit(self.feishu_authorize_url)
if (authorize.scheme != "https" or not authorize.hostname or authorize.username or authorize.password
or authorize.query or authorize.fragment):
raise ValueError("broker mode requires a fixed HTTPS authorization endpoint")
if self.environment not in {"production", "test", "development"}:
raise ValueError("invalid environment")
if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: