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
+18 -12
View File
@@ -12,7 +12,7 @@ from pydantic import BaseModel, ConfigDict, Field
from .common import no_cache, rate_limit
from .crypto import canonical
from .store import INTEGRITY_ERRORS
from .work_api import device_owner
from .device_auth import device_owner
class Notification(BaseModel):
@@ -56,17 +56,20 @@ def router(settings, redis, store, platform, oauth):
if body.kind == 'authorization':
if body.text or body.title or not body.oauth_state:
raise HTTPException(422, 'authorization requires only a pending OAuth state')
oauth.prune()
item = oauth.pending.get(body.oauth_state)
if (not settings.oauth_enabled or not item or item.status != 'waiting'
or item.owner != owner or item.host_id != body.host_id):
raise HTTPException(403, 'active device-owned authorization required')
params = {'app_id': settings.feishu_app_id,
'redirect_uri': settings.public_url.rstrip('/') + '/oauth/cb',
'state': body.oauth_state}
if settings.feishu_oauth_scopes:
params['scope'] = settings.feishu_oauth_scopes
url = settings.feishu_authorize_url + '?' + urlencode(params)
if hasattr(oauth, 'authorization_url_for_notification'):
url = oauth.authorization_url_for_notification(owner, body.host_id, body.oauth_state)
else:
oauth.prune()
item = oauth.pending.get(body.oauth_state)
if (not settings.oauth_enabled or not item or item.status != 'waiting'
or item.owner != owner or item.host_id != body.host_id):
raise HTTPException(403, 'active device-owned authorization required')
params = {'app_id': settings.feishu_app_id,
'redirect_uri': settings.public_url.rstrip('/') + '/oauth/cb',
'state': body.oauth_state}
if settings.feishu_oauth_scopes:
params['scope'] = settings.feishu_oauth_scopes
url = settings.feishu_authorize_url + '?' + urlencode(params)
if not url.startswith('https://'):
raise HTTPException(503, 'HTTPS authorization URL required')
content = {'config': {'wide_screen_mode': True},
@@ -99,6 +102,9 @@ def router(settings, redis, store, platform, oauth):
try:
# Complete credential acquisition before the last synchronous binding check.
token = await platform.tenant_token()
if body.kind == 'authorization' and hasattr(oauth, 'authorization_url_for_notification'):
# Recheck the transaction and generation after waiting on app credentials.
oauth.authorization_url_for_notification(owner, body.host_id, body.oauth_state)
device = store.device(body.host_id)
if not device or device['revoked'] or device['owner'] != owner:
status = 'binding_changed'