diff --git a/deployable/CREDENTIAL_MIGRATION.md b/deployable/CREDENTIAL_MIGRATION.md index 05a1e94..fc16dfb 100644 --- a/deployable/CREDENTIAL_MIGRATION.md +++ b/deployable/CREDENTIAL_MIGRATION.md @@ -10,7 +10,7 @@ | --- | --- | --- | --- | | 1 | 服务端加密凭据存储基础 | 密文持久化、应用/用户/环境隔离、版本冲突、密钥轮换与篡改检测测试;不接入旧路由 | 已完成本地开发测试,提交71b9678 | | 2 | 集中凭据Broker及刷新 | 应用/用户锁、持锁重读、版本CAS、响应校验、刷新unknown;新授权不受旧刷新覆盖 | 本次完成本地开发测试 | -| 3 | OAuth与认证领取 | 共享state事务、实际用户核验、绑定代次、只返回access的独立DTO及领取认证 | 待开发 | +| 3 | OAuth与认证领取 | 共享state事务、实际用户核验、绑定代次、只返回access的独立DTO及领取认证 | 本次完成本地开发测试,显式broker模式启用 | | 4 | ECS权威解绑与消息任务关联 | 停发Token、失效事务/任务、连接和旧码联动,多实例/异常恢复 | 待开发 | | 5 | 配置部署、迁移与旧接口退出 | 密钥挂载、产品权限配置、客户端对接、停用旧刷新/claim及代理、ARM64和真实平台验收 | 待开发 | @@ -75,7 +75,7 @@ python -m pytest deployable/tests deployment-source/tests -q ### 调用边界与后续集成要求 -Broker仍是内部模块,未由connector实例化,没有新HTTP接口或后台调度。`owner`参数暂按应用范围内已核验的open_id使用,只能来自后续可信设备/授权事务映射;不得把此内部方法直接暴露给模型、客户端自选用户或无认证HTTP路由。阶段3仍需完成state、绑定代次、领取认证、产品权限配置及公开响应白名单。 +截至阶段2,Broker仍是内部模块,未由connector实例化,没有新HTTP接口或后台调度。`owner`参数暂按应用范围内已核验的open_id使用,只能来自可信设备/授权事务映射;不得把此内部方法直接暴露给模型、客户端自选用户或无认证HTTP路由。阶段3的接线与边界见下节。 此阶段不改变旧OAuth向PC交付refresh的行为,也不读取旧PC凭据。部署阶段必须关闭旧刷新并避免双主,不能仅发布此模块就宣称refresh只在ECS。 @@ -94,3 +94,35 @@ Broker仍是内部模块,未由connector实例化,没有新HTTP接口或后 首轮完整回归有一条原有PC测试遇到Windows临时文件replace拒绝访问(WinError 5);该用例单独重跑和最后完整回归均通过,未修改该客户端实现,具体系统原因未确定。warnings仍为uvicorn/websockets旧API弃用提示。 未进行真实Redis多进程/多节点压测、真实PostgreSQL、Docker/ARM64、现机部署或真实授权/刷新。FakeRedis共享实例测试证明本地协议并发行为,不外推为生产集群验收。 + +## 阶段3:OAuth落库与认证领取(2026-11-04) + +接口细节见 [集中凭据协议v2](CREDENTIAL_PROTOCOL_V2.md)。 + +### 改造内容 + +- `Settings`新增legacy/broker显式模式、服务端密钥文件和产品权限版本集合。默认legacy,不自动改变现有部署;broker缺少密钥/权限或平台OAuth未启用时拒绝启动。生产broker要求PostgreSQL。 +- 新增`CredentialAPI`:认证状态、发起授权、授权状态查询、access-token领取和新OAuth回调。所有设备接口验证证书/签名、环境/应用/协议版本、method/path、完整请求摘要、时间nonce、当前owner及绑定代次。 +- 加密存储共享授权state及请求关联;一次性原子认领,固定600秒有效,重复回调不再次换码;其他connector可处理同一持久事务。实际授权用户必须匹配绑定本人,授权完成直接加密保存ECS,不将refresh下发PC。 +- 授权稳定request_id幂等且不延长有效期;新授权意图使同用户旧waiting/exchanging事务superseded,旧授权提交前重查,避免较早权限意图覆盖较新的请求。数据库设备行写锁用于跨worker串行化创建/取代。 +- 设备表增加binding_generation,保留原数据与身份;bind/unbind/revoke推进版本。新绑定回执返回版本,已有客户端可通过设备认证status安全同步,不要求重新配对。 +- Broker增加同步绑定/事务guard:等待锁后、换码提交前和领取返回前核验当前状态;刷新已轮换时先保存最新凭据,再拒绝向已解绑设备发放,避免丢失服务端refresh。 +- 领取响应是显式字段白名单,只含access token和必要元数据;成功授权与发放写脱敏审计,不含code、state、Token或完整授权URL。 +- 共用旧设备证明函数抽取到`device_auth.py`,OAuth/通知与旧代理复用,不再要求保留`work_api`路由才能认证。 +- broker模式不注册旧`/oauth/start`、`claim`、`pending`、`refresh`及`/work/api`,防止同模式双主。新授权卡以事务ID引用服务器链接;文本/通知卡沿用本人发送边界。 + +### 兼容与未完成 + +legacy模式继续旧凭据协议;新字段/表是增量迁移,不删除旧设备和绑定。旧客户端与broker模式不兼容,阶段5需协调客户端版本、一次新授权和旧本地凭据清理;不能未经准备切换生产配置。 + +本次没有更新实际Compose参数/密钥挂载、安装包或服务器;公开接口仅在显式broker配置下注册。数据库绑定版本让新接口拒绝旧设备上下文,但完整连接断开补偿、持久任务/回包关联、旧绑定码失效和后台刷新暂停等仍为阶段4;不能宣称整体解绑闭环已完成。 + +事务在exchanging中崩溃时不自动重放code,超期需重新授权;授权scope不足时显式返回permission_update_required,不把刷新当扩权。平台刷新未知、已发Bearer残余有效、客户端最终缓存清理和用户Scope审批边界保持不变。 + +### 验证结果 + +Windows / Python 3.12.10,平台桩、临时证书/密钥、SQLite及带Lua的FakeRedis。本轮新增v2接口测试38项通过,另增1项真实PostgreSQL事务/绑定迁移用例因未配置连接而跳过。覆盖跨用户/跨应用环境签名、路径/正文篡改、nonce重放、错人授权、state过期/取消/重放、双connector单次换码、数据库重开、授权取代、部分Scope、旧入口关闭、代次失效和刷新中解绑不发Token。 + +最后完整回归:**185 passed,3 skipped,15 warnings**(49.77秒)。3个跳过项均需要真实PostgreSQL测试连接;warnings仍为原有uvicorn/websockets弃用提示。文档表格/围栏、relay模块AST及`git diff --check`通过。 + +真实PostgreSQL、真实Redis多进程、ARM64镜像、私有化平台及现机业务验收尚未执行;本地ASGI/FakeRedis结果不能作为线上多节点或生产部署结论。 diff --git a/deployable/CREDENTIAL_PROTOCOL_V2.md b/deployable/CREDENTIAL_PROTOCOL_V2.md new file mode 100644 index 0000000..0de2be2 --- /dev/null +++ b/deployable/CREDENTIAL_PROTOCOL_V2.md @@ -0,0 +1,102 @@ +# 集中凭据协议 v2:阶段3接口指导 + +日期:2026-11-04。本文是已经完成本地开发测试的服务端接口契约,不表示现机已启用或新客户端已交付。产品经理决定权限集合;本文件不替产品选择具体业务或Scope。 + +## 1. 启用与模式边界 + +Settings新增以下配置,环境变量均以`HN_`开头: + +| 配置 | 默认值/要求 | +| --- | --- | +| `CREDENTIAL_MODE` | 默认`legacy`;显式`broker`才启用新模式 | +| `CREDENTIAL_KEYRING_FILE` | broker必须提供受控服务端密钥文件,格式见阶段1记录;无有效密钥启动失败,不回退 | +| `CREDENTIAL_PERMISSION_PROFILES` | JSON对象:产品权限版本名映射Scope字符串数组;broker不可为空,不允许客户端传任意Scope | +| `DATABASE_URL` | 生产broker必须使用PostgreSQL;SQLite仅用于本地开发和测试 | +| 平台与OAuth | broker要求`FEISHU_ENABLED`和`OAUTH_ENABLED`为true,应用凭据仍只在ECS | +| 授权入口与公开URL | 固定HTTPS授权端点,不能含预设query/fragment或用户名密码;公开HTTPS回调精确登记 | + +密钥不在本文或客户端配置中生成/分发。当前Compose及初始化工具尚未完成这些新参数、密钥挂载和权限集合的正式部署接线(阶段5)。不能仅在旧settings.env中增加变量就认定容器已经启用broker,也不要自行在生产切换。 + +私有化平台端点按历史实测和现场配置复核:`/open-apis/authen/v1/index`曾成功授权,v1 OIDC曾成功换码/刷新;不要默认沿用公网域名或自动切换agent-hub的v3协议。 + +| 模式 | OAuth与凭据接口 | 旧业务代理 | +| --- | --- | --- | +| legacy | 继续原OAuth start/claim/refresh行为,v2路由不存在 | 保留`/work/api` | +| broker | 注册本文v2路由和新`/oauth/cb`;旧`/oauth/start`、`claim`、`pending`、`refresh`不注册 | 不注册`/work/api` | + +现有消息接收、回复、通知和设备注册/绑定仍保留。旧客户端不能在broker模式继续使用原凭据协议,切换需要协调升级与新授权;不会按某次请求失败自动退回旧刷新。 + +## 2. 设备证明与绑定版本 + +所有`/work/v2/*`接口均为POST,JSON禁止额外字段。基础字段: + +| 字段 | 要求 | +| --- | --- | +| `host_id` | 当前注册设备,32位小写十六进制 | +| `binding_owner` | 本机绑定回执中的用户声明,必须与ECS当前绑定一致;不是可自选收件人或凭据用户 | +| `binding_generation` | 当前绑定版本,非负整数,禁止字符串、bool;只有status可显式为null引导同步 | +| `request_id` | 8–100字符,字母数字下划线短横线;授权发起可用稳定ID重取同一事务 | +| `proof` | 以下内容的设备私钥签名信封,签名编码沿用现有`crypto.sign`协议 | + +proof正文包含`version=2`(严格整数)、`environment`、`app_id`、`host_id`、`purpose="work-v2"`、`method="POST"`、实际`path`、`ts`、32位十六进制`nonce`、`payload_hash`。environment和app_id必须匹配目标ECS部署,后续客户端公开发布配置提供这两项,不能在多个环境盲目复用签名。payload_hash为去掉顶层proof之后,**完整模型字段**经过现有`crypto.canonical`规范化后的SHA256,包括owner、generation、request_id及接口附加字段。status的generation使用null时也必须纳入签名正文。 + +规范化沿用现有UTF-8 JSON:键排序、紧凑分隔符、不做ASCII转义。时间窗口90秒,nonce一次性记录180秒;重发HTTP使用新nonce,业务request_id可保持。path被签名,因此不能把一个接口的证明转用到另一个接口。 + +服务端依次核对当前绑定/吊销、设备证书/签名、时间和nonce、请求路径与摘要;Redis等待后再次核对绑定。IP和设备有独立限流。请求/校验错误不回显输入值,Token响应及普通错误响应均禁止缓存。 + +`Store`增加`binding_generation`,原数据迁移默认0,保留设备证书、私钥、公钥和owner;新bind、unbind使版本递增,首次revoke递增。绑定回执新增该字段;已有安装可通过认证status读取当前版本,不需为此重新生成身份或重绑。 + +## 3. 接口 + +### POST /work/v2/status + +仅基础字段;首次同步可令generation为null,但仍要用设备私钥证明正确binding_owner。返回当前绑定版本、协议/模式、产品权限版本名和凭据状态;不返回access/refresh。owner不符、未绑定或吊销仍拒绝。 + +### POST /work/v2/authorizations + +增加`permission_profile`,只能使用服务器配置的产品权限版本。返回`transaction_id`、status、expires_at;waiting时返回authorization_url。事务ID格式`oauth2_`加32位十六进制,期限600秒。同request_id同请求返回同事务且不延长期限;同ID改内容拒绝。 + +一个应用/用户只保留一个当前待授权意图,新请求会使之前waiting/exchanging事务变为superseded;重复同ID不作此操作。数据库设备行锁串行化跨worker新授权,回调提交前重查事务,防止较早授权覆盖较新的权限意图。已存在有效凭据不会因发起新授权直接删除。 + +服务端保存state摘要及加密state,不保存code;Scope快照和身份/设备/版本与事务绑定。Scope由产品权限配置决定,不接受请求中的scope/open_id/refresh_token等额外参数。 + +### GET /oauth/cb + +平台跳转带code/state或取消结果,不要求设备签名。服务端验证随机state、到期时间、密文绑定、当前设备代次和产品权限版本,并原子将waiting变为exchanging/cancelled。重复/过期回调不再次换码。 + +Broker换码后调用user_info核验实际授权人与原绑定一致,回写前再次验证事务/绑定;加密保存access/refresh,仅返回通用完成提示。错人、缺字段、绑定变化或事务被新请求取代时不提交旧授权。回调成功不等于产品业务已执行。 + +阶段3数据库共享事务允许其他connector接续未消费回调。若进程在平台换码中崩溃,exchanging不能自动重放一次性code,期限后需重新授权;不宣称平台调用exactly-once。事务容量有全局配置及用户频率限制,过期记录清理/归档在部署运维阶段完善。 + +### POST /work/v2/authorizations/status + +增加transaction_id,必须属于当前用户/设备/绑定版本。返回waiting/exchanging/authorized/failed/cancelled/superseded/expired;成功但实际Scope不足产品集合时返回permission_update_required。状态查询不返回Token。已完成授权的终态不因等待期限过去而改称expired;过期的waiting/exchanging按expired返回。 + +### POST /work/v2/credentials/access-token + +增加permission_profile。owner来自已验证绑定,Broker按用户加锁读取/刷新,权限不足不靠刷新补齐;绑定变化拒绝发放。 + +响应采用固定字段白名单:`access_token`、`token_type="Bearer"`、`expires_at`、`expires_in`、`server_time`、`credential_version`、`granted_scopes`、`app_id`。expires_in是返回时的剩余秒数,不保证每次都得到完整两小时。**任何响应均不包含refresh token、其密文或应用凭据。** + +客户端默认只在内存缓存access。未来本机CredentialProvider请求本接口,不能把它注册为模型可调用的get_token工具。业务SDK拿到access后直接请求固定的私有化飞书域名,refresh留在ECS。 + +如果刷新时解绑,平台已经轮换的凭据仍需保存,避免丢失最新refresh;发放前重新校验绑定,拒绝把access交给已解绑设备。ECS数据库状态提交与HTTP发出之间仍存在不可消除的竞争边界,已发Bearer的即时失效仍依赖平台撤权。 + +### POST /device/notifications(原接口的新授权卡兼容) + +文本/通知卡协议不变。broker模式的`kind=authorization`将原`oauth_state`字段用作**transaction_id引用**,不是原始state或任意URL;ECS验证事务归属、当前绑定版本和waiting状态后从服务器生成链接。发送前再校验,不能把其他设备事务发给本人或用旧代次链接继续授权。 + +本人通知收件人与路由继续由ECS确定,应用Token不下发。完整通知、路由和旧绑定码失效补偿在阶段4继续完善。 + +## 4. 错误与审计 + +- 401:authorization_required;403:设备/绑定/证明错误、产品权限未批准或permission_update_required。 +- 409:请求nonce重放、稳定ID改内容、refresh_unknown、权限版本变化。 +- 410:授权事务过期;429:限流;503:凭据/状态不可用、等待租约或容量超限。 +- 回调换码/核验失败返回固定502,不回显code、state、平台错误正文或Token。 + +成功授权和Token发放写原有审计表,仅记录用户、设备/事务引用、凭据版本和结果。不得记录Token正文、完整授权URL或请求头。 + +## 5. 本阶段尚未完成 + +无实际服务器部署、真实PostgreSQL/多节点Redis验收、真实Work OAuth复测或客户端发布。完整解绑连接和任务补偿、旧绑定码作废、凭据拒绝自动失效上报、集中刷新后台调度、运维清理和正式Compose参数挂载为后续阶段。不要绕过新领取认证临时开放旧refresh接口。 diff --git a/deployable/README.md b/deployable/README.md index be35489..adbb431 100644 --- a/deployable/README.md +++ b/deployable/README.md @@ -1,6 +1,6 @@ # 海诺中继服务部署项目 -集中凭据改造按阶段交付,见 [改造与阶段记录](CREDENTIAL_MIGRATION.md)。阶段1已提供加密存储基础,阶段2已提供内部Broker与集中刷新;尚未接入公开OAuth/领取路由或改变当前客户端协议,不能据此判断现网已切换。 +集中凭据改造按阶段交付,见 [改造与阶段记录](CREDENTIAL_MIGRATION.md)。阶段1/2已提供存储与刷新,阶段3新增显式broker模式的OAuth及认证领取接口,默认仍为legacy;[v2接口契约](CREDENTIAL_PROTOCOL_V2.md)包含签名、配置和兼容边界。尚未部署或发布新客户端,不能据此判断现网已切换。 2026-10-28 新增[客户端主动消息与授权卡片](NOTIFICATIONS.md),本地代码已实现,现网部署和卡片验收待完成。 diff --git a/deployable/relay/config.py b/deployable/relay/config.py index c4c32ac..bc2b7af 100644 --- a/deployable/relay/config.py +++ b/deployable/relay/config.py @@ -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: diff --git a/deployable/relay/connector.py b/deployable/relay/connector.py index c6218e8..81d4e00 100644 --- a/deployable/relay/connector.py +++ b/deployable/relay/connector.py @@ -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): diff --git a/deployable/relay/credential_api.py b/deployable/relay/credential_api.py new file mode 100644 index 0000000..1567bb3 --- /dev/null +++ b/deployable/relay/credential_api.py @@ -0,0 +1,317 @@ +"""Opt-in broker-mode OAuth and device-authenticated access-token issuance.""" +import hashlib +import hmac +import json +import re +import secrets +import time +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, ConfigDict, Field + +from .common import no_cache, peer, rate_limit +from .credential_broker import (AuthorizationRequired, BrokerUnavailable, PermissionRequired, RefreshUnknown) +from .credential_store import CredentialError +from .crypto import canonical, verified, verify_device + + +class DeviceRequest(BaseModel): + model_config = ConfigDict(extra="forbid") + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + binding_owner: str = Field(min_length=1, max_length=128) + binding_generation: int = Field(strict=True, ge=0) + request_id: str = Field(pattern=r"^[A-Za-z0-9_-]{8,100}$") + proof: dict = Field(repr=False) + + +class StatusRequest(DeviceRequest): + # Only status may bootstrap the generation, still proving the bound owner. + binding_generation: int | None = Field(default=None, strict=True, ge=0) + + +class PermissionRequest(DeviceRequest): + permission_profile: str = Field(pattern=r"^[A-Za-z0-9_-]{1,64}$") + + +class TransactionRequest(DeviceRequest): + transaction_id: str = Field(pattern=r"^oauth2_[a-f0-9]{32}$") + + +class CredentialAPI: + def __init__(self, settings, redis, store, broker, clock=time.time): + self.s, self.r, self.store, self.broker, self.clock = settings, redis, store, broker, clock + self.db = store.db + with self.db: + self.db.execute("""CREATE TABLE IF NOT EXISTS work_authorizations_v2( + transaction_id TEXT PRIMARY KEY, environment TEXT NOT NULL, app_id TEXT NOT NULL, + owner TEXT NOT NULL, host_id TEXT NOT NULL, binding_generation BIGINT NOT NULL, + permission_profile TEXT NOT NULL, scopes_json TEXT NOT NULL, + request_id TEXT NOT NULL, request_hash TEXT NOT NULL, state_hash TEXT NOT NULL UNIQUE, + state_key_id TEXT NOT NULL, state_ciphertext TEXT NOT NULL, + created_at DOUBLE PRECISION NOT NULL, expires_at DOUBLE PRECISION NOT NULL, + status TEXT NOT NULL, + UNIQUE(environment,app_id,host_id,binding_generation,request_id))""") + self.router = APIRouter() + self.router.add_api_route("/work/v2/status", self.status, methods=["POST"]) + self.router.add_api_route("/work/v2/authorizations", self.start, methods=["POST"]) + self.router.add_api_route("/work/v2/authorizations/status", self.transaction_status, methods=["POST"]) + self.router.add_api_route("/work/v2/credentials/access-token", self.access_token, methods=["POST"]) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + + def require_binding(self, host, owner, generation=None): + device = self.store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "device_unbound_or_revoked") + if (device["owner"] != owner + or (generation is not None and device["binding_generation"] != generation)): + raise HTTPException(403, "stale_binding") + return device + + async def authenticate(self, body, request): + await rate_limit(self.r, "credential-ip:" + peer(request), 300) + device = self.require_binding(body.host_id, body.binding_owner, body.binding_generation) + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (type(proof["version"]) is not int or proof["version"] != 2 + or proof["environment"] != self.s.environment or proof["app_id"] != self.s.feishu_app_id + or proof["host_id"] != body.host_id or proof["purpose"] != "work-v2" + or proof["method"] != "POST" or proof["path"] != request.url.path + or type(proof["ts"]) not in (int, float) + or not abs(self.clock() - proof["ts"]) <= 90 + or not isinstance(proof["nonce"], str) + or not re.fullmatch(r"[a-f0-9]{32}", proof["nonce"]) + or proof["payload_hash"] != hashlib.sha256(canonical(body.model_dump(exclude={"proof"}))).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid_device_proof") from None + if not await self.r.set("hn:credential-proof:" + body.host_id + ":" + proof["nonce"], + "used", nx=True, ex=180): + raise HTTPException(409, "device_request_replayed") + await rate_limit(self.r, "credential-device:" + body.host_id, 120) + # Binding may have changed during Redis awaits. + return self.require_binding(body.host_id, body.binding_owner, body.binding_generation) + + def scopes(self, profile): + scopes = self.s.credential_permission_profiles.get(profile) + if not scopes: + raise HTTPException(403, "permission_profile_not_allowed") + return tuple(sorted(set(scopes))) + + def _credential_status(self, owner): + value = self.broker.store.get(self.s.environment, self.s.feishu_app_id, owner) + if value is None: + return {"credential_status": "authorization_required", "granted_scopes": []} + if self.broker.store.refresh_status(self.s.environment, self.s.feishu_app_id, owner, value.version): + status = "refresh_unknown" + elif value.access_expires_at > self.clock() or value.refresh_expires_at > self.clock(): + status = "available" + else: + status = "authorization_required" + return {"credential_status": status, "granted_scopes": list(value.scopes), + "credential_version": value.version, "access_expires_at": value.access_expires_at} + + async def status(self, body: StatusRequest, request: Request): + device = await self.authenticate(body, request) + try: + result = self._credential_status(device["owner"]) + except CredentialError: + raise HTTPException(503, "credential_temporarily_unavailable") from None + self.require_binding(body.host_id, device["owner"], device["binding_generation"]) + return no_cache(JSONResponse({"credential_mode": "broker", "protocol_version": 2, + "binding_owner": device["owner"], + "binding_generation": device["binding_generation"], + "permission_profiles": sorted(self.s.credential_permission_profiles), **result})) + + def _row(self, transaction_id): + row = self.db.execute("""SELECT * FROM work_authorizations_v2 + WHERE transaction_id=? AND environment=? AND app_id=?""", + (transaction_id, self.s.environment, self.s.feishu_app_id)).fetchone() + if not row: + raise HTTPException(404, "authorization_not_found") + return dict(row) + + def _live_row(self, transaction_id): + row = self._row(transaction_id) + if row["expires_at"] <= self.clock(): + raise HTTPException(410, "authorization_expired") + self.require_binding(row["host_id"], row["owner"], row["binding_generation"]) + if list(self.scopes(row["permission_profile"])) != json.loads(row["scopes_json"]): + raise HTTPException(409, "permission_profile_changed") + return row + + @staticmethod + def _state_aad(row): + names = ("transaction_id", "environment", "app_id", "owner", "host_id", "binding_generation", + "permission_profile", "scopes_json", "request_hash", "expires_at", "state_hash", "state_key_id") + return canonical(["hainuo-oauth-state-v2", *[row[name] for name in names]]) + + def _state(self, row): + try: + state = self.broker.store.keyring.decrypt(row["state_key_id"], row["state_ciphertext"], + self._state_aad(row)).decode("ascii") + if not hmac.compare_digest(hashlib.sha256(state.encode()).hexdigest(), row["state_hash"]): + raise ValueError() + return state + except Exception: + raise HTTPException(503, "authorization_state_unavailable") from None + + def _authorization_url(self, row): + params = {"app_id": self.s.feishu_app_id, "redirect_uri": self.s.public_url.rstrip("/") + "/oauth/cb", + "state": self._state(row), "scope": " ".join(json.loads(row["scopes_json"]))} + return self.s.feishu_authorize_url + "?" + urlencode(params) + + def authorization_url_for_notification(self, owner, host, transaction_id): + row = self._live_row(transaction_id) + if row["owner"] != owner or row["host_id"] != host or row["status"] != "waiting": + raise HTTPException(403, "active_device_owned_authorization_required") + return self._authorization_url(row) + + async def start(self, body: PermissionRequest, request: Request): + device = await self.authenticate(body, request) + scopes = self.scopes(body.permission_profile) + await rate_limit(self.r, "credential-authorize:" + device["owner"], 5) + self.require_binding(body.host_id, device["owner"], device["binding_generation"]) + request_hash = hashlib.sha256(canonical(body.model_dump(exclude={"proof"}))).hexdigest() + row = self.db.execute("""SELECT * FROM work_authorizations_v2 + WHERE environment=? AND app_id=? AND host_id=? AND binding_generation=? AND request_id=?""", + (self.s.environment, self.s.feishu_app_id, body.host_id, body.binding_generation, body.request_id)).fetchone() + if row: + row = dict(row) + if row["request_hash"] != request_hash: + raise HTTPException(409, "request_id_reused") + else: + count = self.db.execute("""SELECT COUNT(*) AS total FROM work_authorizations_v2 + WHERE environment=? AND app_id=? AND status IN ('waiting','exchanging') AND expires_at>?""", + (self.s.environment, self.s.feishu_app_id, self.clock())).fetchone()["total"] + if count >= self.s.max_pending: + raise HTTPException(503, "authorization_capacity_reached") + state = "v2_" + secrets.token_urlsafe(32) + row = {"transaction_id": "oauth2_" + secrets.token_hex(16), "environment": self.s.environment, + "app_id": self.s.feishu_app_id, "owner": device["owner"], "host_id": body.host_id, + "binding_generation": device["binding_generation"], "permission_profile": body.permission_profile, + "scopes_json": json.dumps(scopes, separators=(",", ":")), "request_id": body.request_id, + "request_hash": request_hash, "state_hash": hashlib.sha256(state.encode()).hexdigest(), + "state_key_id": self.broker.store.keyring.active_key_id, + "created_at": float(self.clock()), "expires_at": float(self.clock() + 600), "status": "waiting"} + _, row["state_ciphertext"] = self.broker.store.keyring.encrypt(state.encode(), self._state_aad(row)) + with self.db: + # Take a database row lock without changing the binding. This + # serializes distinct authorization starts across PG workers; + # Redis lease ownership is not a substitute for this transaction. + bound = self.db.execute("""UPDATE devices SET binding_generation=binding_generation + WHERE host_id=? AND owner=? AND binding_generation=? AND revoked=0""", + (body.host_id, device["owner"], device["binding_generation"])).rowcount + if bound != 1: + raise HTTPException(403, "stale_binding") + inserted = self.db.execute("""INSERT INTO work_authorizations_v2 + (transaction_id,environment,app_id,owner,host_id,binding_generation,permission_profile, + scopes_json,request_id,request_hash,state_hash,state_key_id,state_ciphertext,created_at,expires_at,status) + VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING""", + tuple(row[key] for key in ("transaction_id", "environment", "app_id", "owner", "host_id", + "binding_generation", "permission_profile", "scopes_json", "request_id", "request_hash", + "state_hash", "state_key_id", "state_ciphertext", "created_at", "expires_at", "status"))).rowcount + if inserted == 1: + # One current authorization intent per application/user. An + # older narrow consent must not overwrite newer permissions. + self.db.execute("""UPDATE work_authorizations_v2 SET status='superseded' + WHERE environment=? AND app_id=? AND owner=? AND transaction_id<>? + AND status IN ('waiting','exchanging')""", + (self.s.environment, self.s.feishu_app_id, device["owner"], row["transaction_id"])) + actual = self.db.execute("""SELECT transaction_id,request_hash FROM work_authorizations_v2 + WHERE environment=? AND app_id=? AND host_id=? AND binding_generation=? AND request_id=?""", + (self.s.environment, self.s.feishu_app_id, body.host_id, body.binding_generation, body.request_id)).fetchone() + if not actual or actual["request_hash"] != request_hash: + raise HTTPException(409, "request_id_reused") + row = self._live_row(actual["transaction_id"]) + row = self._live_row(row["transaction_id"]) + result = {"transaction_id": row["transaction_id"], "status": row["status"], "expires_at": row["expires_at"]} + if row["status"] == "waiting": + result["authorization_url"] = self._authorization_url(row) + return no_cache(JSONResponse(result, status_code=201)) + + async def transaction_status(self, body: TransactionRequest, request: Request): + await self.authenticate(body, request) + row = self._row(body.transaction_id) + if (row["owner"], row["host_id"], row["binding_generation"]) != ( + body.binding_owner, body.host_id, body.binding_generation): + raise HTTPException(404, "authorization_not_found") + self.require_binding(body.host_id, body.binding_owner, body.binding_generation) + status = ("expired" if row["expires_at"] <= self.clock() and row["status"] in {"waiting", "exchanging"} + else row["status"]) + result = {"transaction_id": row["transaction_id"], "status": status} + if status == "authorized": + try: + result.update(self._credential_status(body.binding_owner)) + except CredentialError: + raise HTTPException(503, "credential_temporarily_unavailable") from None + if not set(json.loads(row["scopes_json"])).issubset(result["granted_scopes"]): + result["status"] = "permission_update_required" + return no_cache(JSONResponse(result)) + + async def access_token(self, body: PermissionRequest, request: Request): + await self.authenticate(body, request) + required = self.scopes(body.permission_profile) + guard = lambda: self.require_binding(body.host_id, body.binding_owner, body.binding_generation) + try: + grant = await self.broker.get_access(body.binding_owner, required_scopes=required, guard=guard) + except HTTPException: + raise + except AuthorizationRequired: + raise HTTPException(401, "authorization_required") from None + except PermissionRequired: + raise HTTPException(403, "permission_update_required") from None + except RefreshUnknown: + raise HTTPException(409, "refresh_unknown") from None + except (BrokerUnavailable, CredentialError): + raise HTTPException(503, "credential_temporarily_unavailable") from None + guard() + self.store.audit(body.binding_owner, "credential_issue", body.host_id + ":" + body.request_id, + "completed_v" + str(grant.credential_version)) + # Explicit whitelist: never asdict(UserCredential) or pass a platform response. + return no_cache(JSONResponse({"access_token": grant.access_token, "token_type": "Bearer", + "expires_at": grant.expires_at, "expires_in": max(0, int(grant.expires_at - self.clock())), + "server_time": self.clock(), "credential_version": grant.credential_version, + "granted_scopes": list(grant.scopes), "app_id": self.s.feishu_app_id})) + + async def callback(self, request: Request, code: str = "", state: str = "", error: str = ""): + await rate_limit(self.r, "credential-callback:" + peer(request), 60) + if not re.fullmatch(r"v2_[A-Za-z0-9_-]{43}", state) or len(code) > 4096: + raise HTTPException(400, "invalid_authorization_callback") + row = self.db.execute("""SELECT transaction_id FROM work_authorizations_v2 + WHERE state_hash=? AND environment=? AND app_id=?""", + (hashlib.sha256(state.encode()).hexdigest(), self.s.environment, self.s.feishu_app_id)).fetchone() + if not row: + raise HTTPException(400, "invalid_authorization_callback") + transaction = self._live_row(row["transaction_id"]) + if not hmac.compare_digest(self._state(transaction), state) or transaction["status"] != "waiting": + raise HTTPException(400, "authorization_already_consumed") + if not code and not error: + raise HTTPException(400, "invalid_authorization_callback") + with self.db: + count = self.db.execute("""UPDATE work_authorizations_v2 SET status=? + WHERE transaction_id=? AND status='waiting' AND expires_at>?""", + ("cancelled" if error else "exchanging", transaction["transaction_id"], self.clock())).rowcount + if count != 1: + raise HTTPException(400, "authorization_already_consumed") + if error: + return no_cache(HTMLResponse("
授权已取消,请返回客户端。
", status_code=400)) + def guard(): + current = self._live_row(transaction["transaction_id"]) + if current["status"] != "exchanging": + raise HTTPException(409, "authorization_not_active") + try: + await self.broker.authorize(transaction["owner"], code, guard=guard) + guard() + except Exception: + with self.db: + self.db.execute("UPDATE work_authorizations_v2 SET status='failed' WHERE transaction_id=? AND status='exchanging'", + (transaction["transaction_id"],)) + raise HTTPException(502, "authorization_did_not_complete") from None + with self.db: + self.db.execute("UPDATE work_authorizations_v2 SET status='authorized' WHERE transaction_id=? AND status='exchanging'", + (transaction["transaction_id"],)) + self.store.audit(transaction["owner"], "credential_authorized", transaction["transaction_id"], "completed") + return no_cache(HTMLResponse("授权完成,请返回客户端。
")) diff --git a/deployable/relay/credential_broker.py b/deployable/relay/credential_broker.py index b731d65..4554b26 100644 --- a/deployable/relay/credential_broker.py +++ b/deployable/relay/credential_broker.py @@ -148,12 +148,14 @@ class CredentialBroker: return AccessGrant(credential.access_token, credential.access_expires_at, remaining, credential.version, credential.scopes) - async def authorize(self, owner, code): + async def authorize(self, owner, code, *, guard=None): """Only callable after the future OAuth transaction authenticates owner/state.""" identity = self._identity(owner) if not isinstance(code, str) or not code or len(code) > 4096: raise CredentialError("invalid authorization input") async with self._lease(owner) as lease: + if guard: + guard() current = self.store.get(*identity) expected_version = current.version if current else 0 started = self.clock() @@ -164,6 +166,8 @@ class CredentialBroker: if not isinstance(info, dict) or info.get("open_id") != owner: raise CredentialError("authorization identity mismatch") await lease.check() + if guard: + guard() version = self.store.save(*identity, expected_version=expected_version, **payload) except CredentialConflict: raise BrokerUnavailable("credential version changed") from None @@ -176,11 +180,13 @@ class CredentialBroker: credential = UserCredential(version=version, **{**payload, "scopes": tuple(payload["scopes"])}) return self._grant(credential, ()) - async def get_access(self, owner, *, required_scopes=()): + async def get_access(self, owner, *, required_scopes=(), guard=None): identity = self._identity(owner) required = self._scopes(required_scopes) # Lock even cache hits: authorization replacement and refresh share one owner. async with self._lease(owner) as lease: + if guard: + guard() current = self.store.get(*identity) if current is None: raise AuthorizationRequired("authorization required") @@ -190,6 +196,8 @@ class CredentialBroker: raise PermissionRequired("permission update required") if current.access_expires_at - self.clock() > self.minimum_validity: await lease.check() + if guard: + guard() return self._grant(current, required) if current.refresh_expires_at <= self.clock(): raise AuthorizationRequired("authorization required") @@ -200,6 +208,8 @@ class CredentialBroker: started = self.clock() try: await lease.check() + if guard: + guard() result = await asyncio.wait_for(self.platform.credential_refresh(current.refresh_token), self.platform_timeout) payload = self._payload(result, started, previous=current) @@ -223,4 +233,6 @@ class CredentialBroker: self.store.finish_refresh(*identity, current.version, attempt, "completed") from .credential_store import UserCredential credential = UserCredential(version=version, **{**payload, "scopes": tuple(payload["scopes"])}) + if guard: + guard() return self._grant(credential, required) diff --git a/deployable/relay/device_auth.py b/deployable/relay/device_auth.py new file mode 100644 index 0000000..4b1b641 --- /dev/null +++ b/deployable/relay/device_auth.py @@ -0,0 +1,25 @@ +"""Shared legacy device proof verification; v2 adds binding/version checks.""" +import hashlib +import re +import time + +from fastapi import HTTPException +from .crypto import canonical, verified, verify_device + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] diff --git a/deployable/relay/notifications.py b/deployable/relay/notifications.py index 7cc2553..3b1821c 100644 --- a/deployable/relay/notifications.py +++ b/deployable/relay/notifications.py @@ -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' diff --git a/deployable/relay/oauth.py b/deployable/relay/oauth.py index 53905f6..4ffe44c 100644 --- a/deployable/relay/oauth.py +++ b/deployable/relay/oauth.py @@ -65,7 +65,7 @@ class OAuth: await rate_limit(self.redis, "oauth:start:" + peer(request), 20) owner = "" if body.host_id: - from .work_api import device_owner + from .device_auth import device_owner owner = await device_owner(self.settings, self.redis, self.store, body.host_id, body.proof, "work-oauth-v1", {}) async with self.lock: diff --git a/deployable/relay/store.py b/deployable/relay/store.py index 9d6f8ef..ce55fce 100644 --- a/deployable/relay/store.py +++ b/deployable/relay/store.py @@ -51,6 +51,12 @@ class Store: self.db = sqlite3.connect(path, check_same_thread=False) self.db.row_factory = sqlite3.Row self.db.executescript("PRAGMA journal_mode=WAL;" + schema) + # Additive migration; retain all existing device identities and bindings. + with self.db: + if database_url: + self.db.execute("ALTER TABLE devices ADD COLUMN IF NOT EXISTS binding_generation BIGINT NOT NULL DEFAULT 0") + elif "binding_generation" not in {row["name"] for row in self.db.execute("PRAGMA table_info(devices)").fetchall()}: + self.db.execute("ALTER TABLE devices ADD COLUMN binding_generation BIGINT NOT NULL DEFAULT 0") def device(self, host): row = self.db.execute("SELECT * FROM devices WHERE host_id=?", (host,)).fetchone() @@ -67,17 +73,17 @@ class Store: def bind(self, host, owner): with self.db: # Conditional update also protects against two connectors racing for the same device. - count = self.db.execute("UPDATE devices SET owner=? WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount + count = self.db.execute("UPDATE devices SET owner=?,binding_generation=binding_generation+1 WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount if count != 1: raise ValueError("device unavailable or already bound") def unbind(self, owner): with self.db: - self.db.execute("UPDATE devices SET owner=NULL WHERE owner=?", (owner,)) + self.db.execute("UPDATE devices SET owner=NULL,binding_generation=binding_generation+1 WHERE owner=?", (owner,)) def revoke(self, host): with self.db: - self.db.execute("UPDATE devices SET revoked=1 WHERE host_id=?", (host,)) + self.db.execute("UPDATE devices SET revoked=1,binding_generation=binding_generation+1 WHERE host_id=? AND revoked=0", (host,)) def begin_turn(self, turn, owner, host): with self.db: diff --git a/deployable/relay/work_api.py b/deployable/relay/work_api.py index 96a6e49..83a74fa 100644 --- a/deployable/relay/work_api.py +++ b/deployable/relay/work_api.py @@ -10,7 +10,8 @@ from fastapi import APIRouter, HTTPException, Request from fastapi.responses import JSONResponse from pydantic import BaseModel, Field from .common import no_cache, rate_limit -from .crypto import canonical, verified, verify_device +from .crypto import canonical +from .device_auth import device_owner from .store import INTEGRITY_ERRORS @@ -43,24 +44,6 @@ def business_allowed(method, path): return any(re.fullmatch(p, path) for p in rules.get(method, [])) -async def device_owner(settings, redis, store, host, proof, purpose, payload): - device = store.device(host) - if not device or device["revoked"] or not device["owner"]: - raise HTTPException(403, "active bound device required") - try: - key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) - data = verified(proof, key) - if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 - or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) - or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): - raise ValueError() - except Exception: - raise HTTPException(403, "invalid device request proof") from None - if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): - raise HTTPException(409, "device request replayed") - return device["owner"] - - def router(settings, redis, store, platform): routes = APIRouter() store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( diff --git a/deployable/tests/test_credential_api.py b/deployable/tests/test_credential_api.py new file mode 100644 index 0000000..49868ac --- /dev/null +++ b/deployable/tests/test_credential_api.py @@ -0,0 +1,503 @@ +"""Broker-mode public boundaries with synthetic devices, platform and secrets.""" +import asyncio +import base64 +import hashlib +import json +import os +import secrets +import sqlite3 +import sys +import time +from pathlib import Path +from urllib.parse import parse_qs, urlsplit + +import fakeredis +import fakeredis.aioredis +import httpx +import pytest +import pytest_asyncio +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from relay.config import Settings +from relay.connector import create_app +from relay.crypto import canonical, fingerprint, issue_ca, issue_device, pem_private, sign +from relay.credential_api import CredentialAPI +from relay.store import Store + + +class Platform: + def __init__(self): + self.owner = "ou_a" + self.exchanges = self.refreshes = 0 + self.exchanging = asyncio.Event() + self.resume = None + self.scopes = "read" + self.sent = [] + self.user_info_failure = False + + async def credential_exchange(self, code): + self.exchanges += 1 + self.exchanging.set() + if self.resume is not None: + await self.resume.wait() + return dict(user_access_token="fake-access-v2", refresh_token="fake-refresh-v2", + expires_in=3600, refresh_expires_in=86400, scope=self.scopes) + + async def credential_refresh(self, token): + self.refreshes += 1 + self.exchanging.set() + if self.resume is not None: + await self.resume.wait() + return dict(user_access_token="fake-access-refreshed", refresh_token="fake-refresh-rotated", + expires_in=3600, refresh_expires_in=86400, scope=self.scopes) + + async def user_info(self, token): + if self.user_info_failure: + raise RuntimeError("fake-refresh-private-detail") + return {"open_id": self.owner} + + async def tenant_token(self): + return "fake-tenant" + + async def send(self, owner, kind, content, delivery_id, *, token): + self.sent.append((owner, content)) + return {"message_id": "om_fake"} + + async def close(self): + pass + + +@pytest_asyncio.fixture +async def env(tmp_path): + ca_key, ca = issue_ca() + ca_path = tmp_path / "ca.pem" + ca_path.write_bytes(ca.public_bytes(serialization.Encoding.PEM)) + connector_key = tmp_path / "connector.key" + connector_key.write_bytes(pem_private(ed25519.Ed25519PrivateKey.generate())) + keyring = tmp_path / "keyring.json" + keyring.write_text(json.dumps({"active_key_id": "test-key", "keys": { + "test-key": base64.b64encode(os.urandom(32)).decode()}})) + settings = Settings(environment="test", admin_secret="a"*40, internal_secret="b"*40, + ca_cert=ca_path, connector_key=connector_key, data_dir=tmp_path, feishu_enabled=True, + feishu_app_id="cli_test", feishu_app_secret="fake-app-secret", credential_mode="broker", + credential_keyring_file=keyring, credential_permission_profiles={"product-v1": ["read"], "product-v2": ["read", "write"]}, + public_url="https://relay.example.test", feishu_api_base="https://work.example.test", + feishu_authorize_url="https://work.example.test/open-apis/authen/v1/index") + redis = fakeredis.aioredis.FakeRedis(server=fakeredis.FakeServer(), decode_responses=True) + platform = Platform() + app = create_app(settings, redis, platform) + keys = {} + for host, owner in [("a"*32, "ou_a"), ("b"*32, "ou_b")]: + key = ed25519.Ed25519PrivateKey.generate() + cert = issue_device(ca_key, ca, host, key.public_key()).public_bytes(serialization.Encoding.PEM).decode() + app.state.store.add_device(host, cert, fingerprint(cert)) + app.state.store.bind(host, owner) + keys[host] = key + async with app.router.lifespan_context(app): + async with httpx.AsyncClient(transport=httpx.ASGITransport(app), base_url="https://relay.example.test") as client: + yield dict(client=client, app=app, store=app.state.store, api=app.state.oauth, + settings=settings, redis=redis, platform=platform, keys=keys) + await redis.aclose() + + +def wire(env, path, host="a"*32, *, request_id=None, proof_changes=None, **fields): + owner = "ou_a" if host == "a"*32 else "ou_b" + generation = env["store"].device(host)["binding_generation"] + payload = {**dict(host_id=host, binding_owner=owner, binding_generation=generation, + request_id=request_id or secrets.token_hex(8)), **fields} + if path == "/work/v2/status" and "binding_generation" not in fields: + payload["binding_generation"] = None + proof = dict(version=2, environment=env["settings"].environment, app_id=env["settings"].feishu_app_id, + host_id=host, purpose="work-v2", method="POST", path=path, ts=time.time(), + nonce=secrets.token_hex(16), payload_hash=hashlib.sha256(canonical(payload)).hexdigest()) + proof.update(proof_changes or {}) + return {**payload, "proof": sign(proof, env["keys"][host])} + + +async def start(env, **fields): + path = "/work/v2/authorizations" + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", **fields)) + assert result.status_code == 201, result.text + data = result.json() + data["state"] = parse_qs(urlsplit(data["authorization_url"]).query)["state"][0] + return data + + +async def authorize(env): + item = await start(env) + result = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + assert result.status_code == 200, result.text + return item + + +async def test_full_flow_only_access_and_no_store_secrets_in_responses(env): + item = await authorize(env) + path = "/work/v2/credentials/access-token" + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1")) + assert result.status_code == 200 + body = result.json() + assert set(body) == {"access_token", "token_type", "expires_at", "expires_in", "server_time", "credential_version", "granted_scopes", "app_id"} + assert body["access_token"] == "fake-access-v2" and 0 < body["expires_in"] <= 3600 + assert result.headers["cache-control"] == "no-store" + path = "/work/v2/authorizations/status" + status = await env["client"].post(path, json=wire(env, path, transaction_id=item["transaction_id"])) + assert status.json()["status"] == "authorized" + assert "fake-access" not in status.text and "fake-refresh" not in status.text + assert "fake-refresh" not in result.text + dump = "\n".join(env["store"].db.iterdump()) + assert "fake-refresh" not in dump and "fake-code" not in dump and item["state"] not in dump + actions = env["store"].db.execute("SELECT action FROM audit WHERE actor='ou_a'").fetchall() + assert {row["action"] for row in actions} >= {"credential_issue", "credential_authorized"} + + +async def test_bootstrap_status_verifies_owner_and_returns_current_generation(env): + path = "/work/v2/status" + response = await env["client"].post(path, json=wire(env, path)) + assert response.status_code == 200 and response.json()["binding_generation"] == 1 + assert response.json()["credential_status"] == "authorization_required" + wrong = wire(env, path, binding_owner="ou_b") + assert (await env["client"].post(path, json=wrong)).status_code == 403 + + +@pytest.mark.parametrize("changes", [{"purpose": "other"}, {"method": "GET"}, {"path": "/work/v2/status"}, + {"ts": 1}, {"ts": True}, {"nonce": "bad"}, {"payload_hash": "bad"}, + {"environment": "other"}, {"app_id": "other-app"}, {"version": 1}, {"version": True}]) +async def test_invalid_proof_rejected(env, changes): + path = "/work/v2/credentials/access-token" + response = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", proof_changes=changes)) + assert response.status_code == 403 + + +async def test_tampering_replay_and_extra_owner_or_refresh_fields(env): + path = "/work/v2/authorizations" + body = wire(env, path, permission_profile="product-v1") + tampered = {**body, "permission_profile": "product-v2"} + assert (await env["client"].post(path, json=tampered)).status_code == 403 + assert (await env["client"].post(path, json=body)).status_code == 201 + assert (await env["client"].post(path, json=body)).status_code == 409 + for name in ["open_id", "owner", "refresh_token", "access_token", "scope"]: + body = wire(env, path, permission_profile="product-v1") + response = await env["client"].post(path, json={**body, name: "fake-private-extra"}) + assert response.status_code == 422 and "fake-private-extra" not in response.text + + +async def test_unapproved_profile_and_missing_authorization(env): + path = "/work/v2/credentials/access-token" + assert (await env["client"].post(path, json=wire(env, path, permission_profile="unapproved"))).status_code == 403 + assert (await env["client"].post(path, json=wire(env, path, permission_profile="product-v1"))).status_code == 401 + + +async def test_wrong_authorized_user_not_saved_and_state_cannot_retry(env): + item = await start(env) + env["platform"].owner = "ou_b" + for expected in (502, 400): + result = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + assert result.status_code == expected + assert "fake-code" not in result.text and "fake-refresh" not in result.text + assert env["platform"].exchanges == 1 + assert env["api"].broker.store.get("test", "cli_test", "ou_a") is None + + +async def test_callback_state_invalid_expired_cancelled_and_replayed(env): + client = env["client"] + assert (await client.get("/oauth/cb", params={"state": "v2_" + "a"*43, "code": "fake-code"})).status_code == 400 + expired = await start(env) + # Advancing the injected clock preserves the authenticated state ciphertext. + old_clock = env["api"].clock + env["api"].clock = lambda: old_clock() + 601 + assert (await client.get("/oauth/cb", params={"state": expired["state"], "code": "fake-code"})).status_code == 410 + env["api"].clock = old_clock + cancelled = await start(env) + assert (await client.get("/oauth/cb", params={"state": cancelled["state"], "error": "access_denied"})).status_code == 400 + assert (await client.get("/oauth/cb", params={"state": cancelled["state"], "code": "fake-code"})).status_code == 400 + item = await authorize(env) + assert (await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})).status_code == 400 + assert env["platform"].exchanges == 1 + + +async def test_callback_claim_is_atomic_under_duplicate_delivery(env): + item = await start(env) + env["platform"].resume = asyncio.Event() + first = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) + await asyncio.wait_for(env["platform"].exchanging.wait(), 2) + duplicate = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + env["platform"].resume.set() + assert duplicate.status_code == 400 and (await first).status_code == 200 + assert env["platform"].exchanges == 1 + + +async def test_cross_user_transactions_and_token_are_isolated(env): + item = await authorize(env) + path = "/work/v2/authorizations/status" + other = await env["client"].post(path, json=wire(env, path, host="b"*32, transaction_id=item["transaction_id"])) + assert other.status_code == 404 + path = "/work/v2/credentials/access-token" + other = await env["client"].post(path, json=wire(env, path, host="b"*32, permission_profile="product-v1")) + assert other.status_code == 401 and "fake-access" not in other.text + + +async def test_request_id_idempotence_and_profile_collision(env): + first = await start(env, request_id="stable-request") + second = await start(env, request_id="stable-request") + assert first["transaction_id"] == second["transaction_id"] and first["state"] == second["state"] + path = "/work/v2/authorizations" + response = await env["client"].post(path, json=wire(env, path, request_id="stable-request", permission_profile="product-v2")) + assert response.status_code == 409 + + +async def test_persistent_authorization_survives_api_and_database_reopen(env): + item = await start(env) + other_store = Store(env["settings"].data_dir / "metadata.sqlite") + try: + api = CredentialAPI(env["settings"], env["redis"], other_store, env["api"].broker) + assert api.authorization_url_for_notification("ou_a", "a"*32, item["transaction_id"]) == item["authorization_url"] + assert api._row(item["transaction_id"])["status"] == "waiting" + finally: + other_store.close() + + +async def test_unbind_rebind_invalidates_old_signature_and_authorization(env): + item = await start(env) + path = "/work/v2/credentials/access-token" + old = wire(env, path, permission_profile="product-v1") + env["store"].unbind("ou_a") + env["store"].bind("a"*32, "ou_a") + assert env["store"].device("a"*32)["binding_generation"] == 3 + assert (await env["client"].post(path, json=old)).status_code == 403 + assert (await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})).status_code == 403 + assert env["platform"].exchanges == 0 + + +async def test_binding_changes_during_exchange_prevent_credential_commit(env): + item = await start(env) + env["platform"].resume = asyncio.Event() + task = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) + await asyncio.wait_for(env["platform"].exchanging.wait(), 2) + env["store"].unbind("ou_a") + env["platform"].resume.set() + response = await task + assert response.status_code == 502 + assert env["api"].broker.store.get("test", "cli_test", "ou_a") is None + + +async def test_binding_changes_during_refresh_keep_rotated_pair_but_never_issue(env): + vault = env["api"].broker.store + vault.save("test", "cli_test", "ou_a", expected_version=0, access_token="fake-access-old", + refresh_token="fake-refresh-old", access_expires_at=time.time()+10, + refresh_expires_at=time.time()+86400, scopes=["read"]) + env["platform"].resume = asyncio.Event() + path = "/work/v2/credentials/access-token" + task = asyncio.create_task(env["client"].post(path, json=wire(env, path, permission_profile="product-v1"))) + await asyncio.wait_for(env["platform"].exchanging.wait(), 2) + env["store"].unbind("ou_a") + env["platform"].resume.set() + response = await task + assert response.status_code == 403 and "fake-access" not in response.text + assert vault.get("test", "cli_test", "ou_a").refresh_token == "fake-refresh-rotated" + + +async def test_scope_missing_requires_product_permission_update(env): + await authorize(env) + path = "/work/v2/credentials/access-token" + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v2")) + assert result.status_code == 403 and result.json()["detail"] == "permission_update_required" + assert env["platform"].refreshes == 0 + + +async def test_old_refresh_claim_start_proxy_routes_absent_in_broker_mode(env): + for path in ("/oauth/start", "/oauth/refresh", "/oauth/claim", "/oauth/pending", "/work/api"): + response = await env["client"].post(path, json={"refresh_token": "fake-refresh-old"}) + assert response.status_code == 404 and "fake-refresh" not in response.text + assert (await env["client"].get("/oauth/claim?state=anything")).status_code == 404 + + +async def test_authorization_notification_uses_server_owned_transaction(env): + item = await start(env) + host = "a"*32 + fields = dict(request_id="notify-test-001", kind="authorization", text="", title="", + oauth_state=item["transaction_id"], binding_owner="ou_a") + proof = sign(dict(host_id=host, purpose="work-notify-v1", ts=int(time.time()), nonce=secrets.token_hex(16), + payload_hash=hashlib.sha256(canonical(fields)).hexdigest()), env["keys"][host]) + response = await env["client"].post("/device/notifications", json={**fields, "host_id": host, "proof": proof}) + assert response.status_code == 200 and response.json()["status"] == "sent" + assert env["platform"].sent[-1][0] == "ou_a" + assert item["authorization_url"] in json.dumps(env["platform"].sent[-1][1]) + assert "fake-refresh" not in response.text + + +async def test_second_connector_can_complete_persisted_authorization(env): + item = await start(env) + second = create_app(env["settings"], env["redis"], env["platform"]) + try: + async with httpx.AsyncClient(transport=httpx.ASGITransport(second), base_url="https://relay.example.test") as client: + response = await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + assert response.status_code == 200 + path = "/work/v2/credentials/access-token" + result = await client.post(path, json=wire(env, path, permission_profile="product-v1")) + assert result.status_code == 200 and "fake-refresh" not in result.text + assert env["api"].broker.store.get("test", "cli_test", "ou_a").version == 1 + finally: + second.state.store.close() + + +async def test_two_connectors_duplicate_callback_only_exchange_once(env): + item = await start(env) + second = create_app(env["settings"], env["redis"], env["platform"]) + env["platform"].resume = asyncio.Event() + try: + async with httpx.AsyncClient(transport=httpx.ASGITransport(second), base_url="https://relay.example.test") as client: + first = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) + await asyncio.wait_for(env["platform"].exchanging.wait(), 2) + response = await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + env["platform"].resume.set() + assert response.status_code == 400 and (await first).status_code == 200 + assert env["platform"].exchanges == 1 + finally: + second.state.store.close() + + +async def test_revoked_unbound_and_wrong_private_key_rejected(env): + path = "/work/v2/status" + wrong = wire(env, path) + plain = {k: v for k, v in wrong["proof"].items() if k != "signature"} + wrong["proof"] = sign(plain, env["keys"]["b"*32]) + assert (await env["client"].post(path, json=wrong)).status_code == 403 + env["store"].unbind("ou_a") + assert (await env["client"].post(path, json=wire(env, path))).status_code == 403 + env["store"].revoke("b"*32) + assert (await env["client"].post(path, json=wire(env, path, host="b"*32))).status_code == 403 + + +async def test_state_ciphertext_and_profile_changes_fail_closed(env): + item = await start(env) + with env["store"].db: + env["store"].db.execute("UPDATE work_authorizations_v2 SET state_ciphertext=? WHERE transaction_id=?", + ("broken-ciphertext", item["transaction_id"])) + response = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) + assert response.status_code == 503 and env["platform"].exchanges == 0 + second = await start(env) + env["settings"].credential_permission_profiles["product-v1"] = ["other-scope"] + response = await env["client"].get("/oauth/cb", params={"state": second["state"], "code": "fake-code"}) + assert response.status_code == 409 and env["platform"].exchanges == 0 + + +async def test_capacity_is_bounded_and_no_reused_request_extends_expiry(env): + env["settings"].max_pending = 1 + first = await start(env, request_id="capacity-test") + repeated = await start(env, request_id="capacity-test") + assert first["expires_at"] == repeated["expires_at"] + path = "/work/v2/authorizations" + response = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1")) + assert response.status_code == 503 + + +async def test_partial_scope_authorization_has_explicit_status(env): + path = "/work/v2/authorizations" + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v2")) + assert result.status_code == 201 + data = result.json() + state = parse_qs(urlsplit(data["authorization_url"]).query)["state"][0] + result = await env["client"].get("/oauth/cb", params={"state": state, "code": "fake-code"}) + assert result.status_code == 200 + path = "/work/v2/authorizations/status" + result = await env["client"].post(path, json=wire(env, path, transaction_id=data["transaction_id"])) + assert result.json()["status"] == "permission_update_required" + assert result.json()["granted_scopes"] == ["read"] + + +async def test_new_authorization_supersedes_older_intent(env): + older = await start(env) + newer = await start(env) + response = await env["client"].get("/oauth/cb", params={"state": older["state"], "code": "fake-old-code"}) + assert response.status_code == 400 and env["platform"].exchanges == 0 + response = await env["client"].get("/oauth/cb", params={"state": newer["state"], "code": "fake-new-code"}) + assert response.status_code == 200 + + +async def test_new_intent_during_exchange_prevents_old_consent_commit(env): + older = await start(env) + env["platform"].resume = asyncio.Event() + task = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": older["state"], "code": "fake-old-code"})) + await asyncio.wait_for(env["platform"].exchanging.wait(), 2) + newer = await start(env) + env["platform"].resume.set() + response = await task + assert response.status_code == 502 and env["api"].broker.store.get("test", "cli_test", "ou_a") is None + assert env["api"]._row(older["transaction_id"])["status"] == "superseded" + response = await env["client"].get("/oauth/cb", params={"state": newer["state"], "code": "fake-new-code"}) + assert response.status_code == 200 + + +async def test_expiry_and_generation_are_not_client_selectable(env): + path = "/work/v2/credentials/access-token" + for generation in (-1, True, "1", None): + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", + binding_generation=generation)) + assert result.status_code == 422 + result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", binding_generation=999)) + assert result.status_code == 403 + + +@pytest.mark.skipif(not os.environ.get("HN_TEST_DATABASE_URL"), reason="real PostgreSQL URL not supplied") +async def test_postgres_authorization_and_binding_generation(env, tmp_path): + import uuid + import psycopg + from psycopg import sql + from psycopg.conninfo import make_conninfo + url = os.environ["HN_TEST_DATABASE_URL"] + schema = "hn_oauth_test_" + uuid.uuid4().hex + admin = psycopg.connect(url, autocommit=True) + admin.execute(sql.SQL("CREATE SCHEMA {}").format(sql.Identifier(schema))) + app = None + try: + settings = env["settings"].model_copy(update={"database_url": make_conninfo(url, options=f"-c search_path={schema}"), + "data_dir": tmp_path / "pg"}) + app = create_app(settings, env["redis"], env["platform"]) + device = env["store"].device("a"*32) + app.state.store.add_device(device["host_id"], device["cert"], device["fingerprint"]) + app.state.store.bind(device["host_id"], "ou_a") + async with httpx.AsyncClient(transport=httpx.ASGITransport(app), base_url="https://relay.example.test") as client: + pg_env = {**env, "client": client, "app": app, "store": app.state.store, "api": app.state.oauth} + await authorize(pg_env) + path = "/work/v2/credentials/access-token" + result = await client.post(path, json=wire(pg_env, path, permission_profile="product-v1")) + assert result.status_code == 200 and "fake-refresh" not in result.text + app.state.store.unbind("ou_a") + assert app.state.store.device("a"*32)["binding_generation"] == 2 + finally: + if app: + app.state.store.close() + admin.execute(sql.SQL("DROP SCHEMA {} CASCADE").format(sql.Identifier(schema))) + admin.close() + + +def test_settings_require_explicit_key_and_product_profiles(tmp_path): + common = dict(environment="test", admin_secret="a"*40, internal_secret="b"*40, credential_mode="broker", + feishu_enabled=True, feishu_app_id="test-app", feishu_app_secret="fake-secret") + with pytest.raises(ValueError): + Settings(**common) + for profile in ({"bad profile": ["read"]}, {"v1": []}, {"v1": ["bad scope"]}): + with pytest.raises(ValueError): + Settings(**common, credential_keyring_file=tmp_path/"keys.json", credential_permission_profiles=profile) + + +def test_existing_sqlite_schema_migration_preserves_device_and_increments_generations(tmp_path): + path = tmp_path / "old.sqlite" + conn = sqlite3.connect(path) + conn.execute("CREATE TABLE devices(host_id TEXT PRIMARY KEY,cert TEXT NOT NULL,fingerprint TEXT NOT NULL,owner TEXT UNIQUE,revoked INTEGER NOT NULL DEFAULT 0)") + conn.execute("INSERT INTO devices VALUES('host','cert','fp','owner',0)") + conn.commit() + conn.close() + store = Store(path) + try: + assert store.device("host")["owner"] == "owner" and store.device("host")["binding_generation"] == 0 + store.unbind("owner") + store.bind("host", "owner") + assert store.device("host")["binding_generation"] == 2 + store.revoke("host") + store.revoke("host") + assert store.device("host")["binding_generation"] == 3 + finally: + store.close()