Repository navigation
feat(cofco): 千问办公免登换票(统一认证令牌换只读令牌或网页会话) - #2845
Closed
laiyinyizao007 wants to merge 12 commits into
Closed
laiyinyizao007 wants to merge 12 commits into
laiyinyizao007 wants to merge 12 commits into
Conversation
An employee can now hold two personal tokens at once: the one they sign for their own AI assistant, and the one an external connector (Qwen Office) issues on their behalf. Issuing a token revokes only the previous token of the same purpose, so the connector no longer kicks out the self-signed token. - New /api/v1/me/mcp-token GET/POST/DELETE; token responses carry Cache-Control: no-store. - The purpose travels in the existing api_credential.name column: no schema change, no migration; legacy tokens remain the self slot. - Audit actions are prefixed by purpose (open_api.pat.* / open_api.mcp_token.*) with purpose in metadata, including admin revoke-by-id and tenant migration. - Admin ledger gains a Purpose column; the AI-access pane lists the connector token, revocable even when the tenant switch is off. (ref dataelement#2836) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Qwen Office hands the employee's enterprise SSO access token to embedded pages (jsBridge auth.getToken) and to the agent runtime (QWENWORK_SSO_TOKEN). POST /api/v1/open-api/qwenwork/exchange verifies it at the SSO's userinfo endpoint, matches the employee to an existing account by (source, external_id) without provisioning anything, and issues either a short-lived read-only token in a new "qwenwork" slot or a short web session (off by default). - open_api.qwenwork_sso config, off by default; https-only userinfo; accepted and rejected answers cached by token hash. - Error codes 26060-26063; path exempt from the JWT middleware. - Both grants check tenant status; audits carry the source IP; the web session also writes the standard login audit. - AuthJwt.create_access_token gains an expires_in that can only shorten. - client /qwenwork/sso landing page: same-origin exchange, web sign-in, read-only token written into the Qwen Office connector via connector.saveConfig (same-origin URL, plain names only), renewal with retry, relative-only redirects. - Admin ledger labels the new slot. (ref dataelement#2844) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rote Checked against the COFCO environment: the platform's own SSO sync writes user.source="wecom" (DEFAULT_SSO_SYNC_SOURCE), and the gateway's org sync writes the same value, so a default of "sso" matched no employee at all and every exchange would have failed with 26062. The two syncs also carry different id spaces: the SSO profile holds the account number the login flow reads, the org sync stores the directory id. Which one lands in user.external_id depends on which sync ran, so user_id_field now takes a comma separated list of candidates, tried in order; the first one naming exactly one account wins. A candidate that names an account without a tenant of ours is skipped rather than issued a credential for, and an id matching two accounts is refused, never guessed. - user_source default "sso" -> "wecom"; user_id_field accepts several candidates, blanks and repeats dropped. - resolve_employee tries each candidate, skipping accounts with no active tenant binding; ambiguity is refused. - 5 more backend cases (25 total); design.md gains the COFCO findings. (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
The candidate walk took the first id that named an account with a tenant of ours. When the SSO answer carried two ids and both named an employee here, that was a choice between two people made on the customer's behalf, which is exactly what this endpoint must not do. Now every candidate is tried: only when exactly one of them names an account with a tenant of ours does the exchange go on. Zero or more than one is refused as before. The case "the id names a leftover row without a tenant" keeps the walk going to the next candidate. - resolve_employee collects the accounts the candidates name and refuses unless there is exactly one. - Cases: two candidates naming two employees -> refused; a candidate naming an account without a tenant -> skipped. (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
无有效租户归属的行被跳过后,下一个候选会顶上:持有前者令牌的人会被 签成后者的员工。改为先把各候选命中的账号计数,恰好一个时才检查它的 租户归属;命中两个及以上一律 26063,与「知源不认识这个人」区分开, 运维看日志能定位是应答指向了多个账号。 - 两个候选指向同一个账号仍算一个账号 - 候选一次查完(一条 IN 查询),不再逐个查库 - 缓存满时先进先出淘汰一半,不再整份清空 - 空 user_id_field 启动即报错 - 进程内记录一次生效的 user_source 与 user_id_field - 文档同步:spec AC-03/AC-04、design §3/§4/§7/§8/§9/§10、配置模板注释 (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
配置字段与统一认证实际应答对不上时,每次换票都会打一次统一认证, 这个端点就成了打统一认证的放大器。改成先缓存再判断:应答里取不出 可用 id 时把它按空候选缓存下来,缓存期内直接回 26063。 (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
登录同步为新员工预建的那条行,有效租户归属是根租户;组织同步写的那条 行,挂的是员工真实租户。换票取的是匹配行的有效租户归属,与密码登录同一 套逻辑,所以候选字段要指向组织同步写的那个员工号。 (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
数据库用 utf8mb4_unicode_ci 比较员工号:不区分大小写、忽略尾随空格。 原来按原文归组,数据库认为相等的两行会被拆成两组,接口就在两行里 挑了一行——那个员工号本该被拒绝。 - 入库行与候选值按同样的规则归组;组内多于一行即拒绝 - 换票接口挂在 /api/v1 下,被拒也回 HTTP 200:文档改成「业务码 + HTTP 200(平台 v1 约定)」,用例改挂平台自己的异常处理器,钉住真实契约 - 被拒另写审计 open_api.qwenwork.exchange_refused(业务码、原因、来源 IP) - 每种拒绝原因各带一句说明;日志写明命中的行数与员工号 - 候选字段超过 8 个启动即报错;清缓存时连同生效配置一起重置 - 上线前提写进 design §10,附上线前的核对 SQL (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
codex 第四轮评审的处置: - 员工号的比较交给数据库做:候选逐个在 external_id 列上用 CASE WHEN 打标记,命中的行按标记归组。删掉 Python 里那套近似规则(lower + 去尾随空格)——它与列的 collation 不一定一致,数据库认为相等的两行 可能被拆成两组,接口就在两行里挑了一行。 - 拒绝审计的两个动作进运维审计页(后端白名单、前端动作列表与模块 筛选「开放平台」、三语文案),否则被拒的换票在页面上根本查不到。 - 审计只写「关于账号的决定」(26062、应答指向多个账号);统一认证 不可用不写审计,另加机器可读的原因码 reason_code。 - 被拒的应答补上 Cache-Control: no-store;成功路径的审计加异常保护, 审计写不进去不能影响已经签发的凭据。 - 文档与代码对齐:design §1/§3、spec AC-03 改成「按数据库的方式 比较」;上线前的核对 SQL 补一条 SHOW FULL COLUMNS,确认列的 collation 与库的类型。 后端 46 例、审计可见性 2 例、platform 前端 5 例全绿。 (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
第五轮 codex 复核第四轮的修复,改了五处: - 「应答指向多个账号」与「账号没有有效租户归属」各自成类,审计原因 码按类型判(no_account / no_active_tenant / ambiguous_account); 改一次消息文案不会静默丢掉审计 - 这个接口的每个应答都带 Cache-Control: no-store,包括不是本功能的 拒绝(租户停用、只读令牌位关掉)——应答可能带凭据 - 审计模块筛选的前缀把 '_' 转义,不再把别的命名空间的动作也匹配进来 - 加一条用例钉住查询的形状(每个候选一个 CASE WHEN 标记、比较留在列 上),防止比较挪回 Python - 拒绝审计写在根租户只有全局超管看得到:写进注释与文档,不猜租户 后端用例 47 例、审计可见性 2 例、platform 前端 5 例、client 13 例全绿; test/open_api 全量的 11 个失败项与改动前基线逐项一致(Windows 路径 分隔符、子进程超时、skill_pack 环境)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
G8 双轮验证的第一轮(codereview 工作流)复核第五轮的修复:把改动连同 平台既有的异常处理、路由挂载、错误码登记表一起看,外部专家模型再验一遍。 结论是没有新缺陷——无 Critical/High/Medium,四条 Low 全部接受不改, 理由逐条写进 design §9。 顺带确认第二轮那条「取不出员工号也缓存」确实已修:verify_sso_token 在 判空前先记缓存,空候选命中缓存直接抛 26063,不再打统一认证。 (ref dataelement#2844) Co-Authored-By: Claude Code <noreply@anthropic.com>
此前按 MySQL utf8mb4_unicode_ci 写的比较口径作废。105 只读实测:DM8 区分大小写、区分重音、忽略尾随空格;员工号的大小写变体按换票同样的 CASE WHEN 查询命中 0 行,加尾随空格仍命中原行。代码不用改——比较一直 交给数据库做;带两个候选的换票查询与审计模块筛选的 LIKE ... ESCAPE 在 DM8 上也都执行正确。 - §3、§7:比较口径改成 DM8;新增风险「大小写对不上得到 26062」 - §10:核对 SQL 改成达梦写法("user"、"delete" 加引号,SHOW FULL COLUMNS 换成比较方式的探针),新增「只差大小写的员工号」,四条都已 在 105 执行;「写两行大小写变体应被拒」在 DM8 上不成立,删掉 - §9:第四、五轮里的 MySQL 口径加更正 (ref dataelement#2844) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
本次中粮只做免登授权(网关授权服务 + 代签位 #2842),免登换票暂不合并,先关闭;分支 feat/cofco-qwenwork-sso-exchange 保留,需要时可重开。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2844
背景
中粮希望员工在千问办公里使用知源时不出现任何认证步骤。千问办公企业版在员工用企业统一认证登录后,把统一认证签发的访问令牌交给第三方:内嵌应用通过 jsBridge
auth.getToken,agent 运行时通过环境变量QWENWORK_USER_SSO_TOKEN(客户端源码核实,两者是同一把third_party_access_token)。千问没有公开的令牌核验接口,所以由知源直接向企业统一认证核实。改动
POST /api/v1/open-api/qwenwork/exchange,请求头Authorization: Bearer <SSO 令牌>,请求体只接受{"grant": "pat" | "session"}:(source, external_id)匹配已有账号;不开户、不改部门(没有复用login-sync,因为它缺省主部门时会把员工挪回根租户)。CASE WHEN external_id = <候选>打标记后一次查回(中粮的知源库是达梦 DM8:105 实测区分大小写与重音、忽略尾随空格;换成 MySQL 又是另一套规则),Python 不另立比较规则,命中几行以数据库的答案为准;命中多于一行即拒绝(26063),不让接口在数据库认为相等的两行里挑一行。grant=pat:第三个用途位「千问免登」的只读令牌,有效期 ≤ 12 小时;grant=session:网页会话,默认关闭,有效期默认 120 分钟,放行判断与密码登录一致。/api/v1下,按平台 v1 约定失败也回 HTTP 200,业务码在响应体的status_code里(只有/api/v2给真实 HTTP 状态,来源open_api/api/exception_handlers.py)。落地页与套件脚本读的本来就是业务码,不受影响;代价是被拒的换票在 HTTP 状态上看不出来,因此另写审计open_api.qwenwork.exchange_refused(业务码、原因、来源 IP,不含员工号与令牌),日志写明命中了几行与员工号。审计只记关于账号的决定(26062 无对应账号、26062 账号没有有效租户归属、26063 应答指向多个账号),统一认证不可用不写审计(那是对方的问题,不是这次换票的决定);三类各带机器可读的reason_code,按错误的类型判而不是认消息文本(改一次文案就会静默丢审计)。这个接口的每个应答都带Cache-Control: no-store,包括不是本功能的拒绝(租户停用、只读令牌位关掉)。审计动作在知源运维审计页可见(模块筛选「开放平台」);但拒绝的行写在根租户,只有全局超管看得到(被拒常常没有租户可写,猜一个租户等于把记录给错的人看)。open_api.qwenwork_sso,默认关闭;userinfo 只允许 https(本机回环除外);user_id_field候选最多 8 个,超过启动即报错;配置模板加了注释示例。AuthJwt.create_access_token增加expires_in,只能缩短不能延长。/qwenwork/sso落地页:内嵌 WebView 里取令牌 → 网页登录 → 只读令牌经connector.saveConfig写进千问的知源连接器(客户端源码核实接受headers);换票与连接器地址都必须同源,续期失败 5 分钟重试。features/v3.0.0-beta1/088-cofco-qwenwork-sso-exchange/;CHANGELOG 已补。安全评审
codex 评审五轮的 2 高 8 中 5 低已逐条处置,见 design.md §9(第四、五轮的发现也记在同一节)。主要修正:会话改为短时且默认关闭;两种 grant 都查租户状态;被拒令牌也缓存;审计带来源 IP 并补标准登录审计;连接器地址同源校验;账号匹配收紧为「整个应答只能命中一个账号」;员工号比较交给数据库做、Python 不另立规则;被拒的换票写审计与日志,审计只记关于账号的决定并带
reason_code(按错误类型判,三类原因码),统一认证不可用不写审计;这个接口的每个应答都带no-store;审计写失败不致换票失败。保留未改的三项(租户取当前有效归属、并发换票、缓存窗口内的吊销)写明了理由。另做了一轮工作流复核(codereview):把改动连同平台既有的异常处理、路由挂载、错误码登记表一起复核,外部专家模型再验一遍,没有发现新的缺陷(无 Critical/High/Medium),四条 Low 全部接受不改,理由记在 design.md §9 同一节。
仍需中粮确认:统一认证 userinfo 地址与员工号字段、能否做令牌内省校验
client_id、安全侧是否同意开启环境变量。上线前在中粮的 DM8 上跑 design.md §10 的四条核对 SQL(达梦写法,"user"、"delete"加引号,已在 105 执行过):有没有一名员工两行、库的比较方式、有没有只差大小写的员工号。DM8 区分大小写,收窄user_id_field时要拿统一认证给的值与库里external_id的写法逐字对照,大小写不一致会得到 26062。验证
test_qwenwork_exchange.py47 passed;用例挂平台自己的异常处理器,钉住「HTTP 200 + 业务码」的真实回执契约。其中一条按数据库自己的答案断言换票结果,不把测试库的比较规则写死成生产库的行为;另有一条钉住查询的形状(每个候选一个CASE WHEN标记、比较留在列上)。test/open_api与test/user全量的失败项与改动前基线逐项一致(均为环境依赖)。两个后端测试文件要分开跑:放在同一个 pytest session 里,6 个审计用例会因共享库被改过而失败(既有现象,b509d5b757上同样复现,与本功能无关)。test/audit/test_audit_log_v2.py2 passed(换票审计在运维页可见、模块归到open_api)。match_0、match_1标记)与审计页模块筛选的LIKE … ESCAPE ''都执行正确;员工号的大小写变体命中 0 行、加尾随空格仍命中原行。qwenworkSignIn.test.ts13 passed;eslint 干净;tsc-strict 无新增错误;check-i18n无漂移。🤖 Generated with Claude Code