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
+19 -7
View File
@@ -142,8 +142,16 @@ def create_app(settings=None, redis=None, platform=None):
store = Store(s.data_dir / "metadata.sqlite", s.database_url)
f = platform or Feishu(s)
broker = Broker(s, r, store)
oauth = OAuth(s, r, f)
oauth.store = store
if s.credential_mode == "broker":
from .credential_api import CredentialAPI
from .credential_broker import CredentialBroker
from .credential_store import CredentialStore, CredentialKeyring
credential_store = CredentialStore(store, CredentialKeyring.from_file(s.credential_keyring_file))
credential_broker = CredentialBroker(credential_store, r, f, environment=s.environment, app_id=s.feishu_app_id)
oauth = CredentialAPI(s, r, store, credential_broker)
else:
oauth = OAuth(s, r, f)
oauth.store = store
from .notifications import router as notification_router
background = set()
@@ -168,8 +176,9 @@ def create_app(settings=None, redis=None, platform=None):
async def sweep():
while True:
await asyncio.sleep(30)
async with oauth.lock:
oauth.prune()
if s.credential_mode == "legacy":
async with oauth.lock:
oauth.prune()
sweep_task = asyncio.create_task(sweep())
try:
yield
@@ -207,8 +216,9 @@ def create_app(settings=None, redis=None, platform=None):
from .enrollment import enrollment_router
app.include_router(enrollment_router(s, r, store, broker.signer, admin))
from .work_api import router as work_router
app.include_router(work_router(s, r, store, f))
if s.credential_mode == "legacy":
from .work_api import router as work_router
app.include_router(work_router(s, r, store, f))
@app.get("/health")
async def health():
@@ -329,7 +339,9 @@ def create_app(settings=None, redis=None, platform=None):
raise HTTPException(409, "owner/device already bound; request new code") from None
await r.incr("hn:web-generation:" + body.host_id)
store.audit(owner, "bind", body.host_id, "success")
return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer)
return sign({"host_id": body.host_id, "owner": owner,
"binding_generation": store.device(body.host_id)["binding_generation"],
"ts": int(time.time())}, broker.signer)
@app.post("/admin/test/binding-code")
async def test_code(body: Instruction, request: Request):