当前桌面端已经有多 provider OAuth 架构,ZAI 登录通过 ZaiProviderAdapter 直接向三方 ZAI token 与 userinfo 接口换取 access token。新需求要求桌面端只负责打开 ZAI 授权页并接收 deep link 回调;拿到 code 和 state 后调用自有后端 https://zcode.z.ai/api/v1/oauth/token,由后端完成向 z.ai 换 token、拉取 userinfo、创建用户并签发 JWT。
本次改造目标是最终把后端返回的 JWT 保存为 ZAI 登录 token,同时保留现有 UI、deep link、凭据仓储和多 provider service 架构。
OAuthService 与 provider adapter 机制。oauthService.startOAuth("zai")。ZaiProviderAdapter.buildAuthorizeUrl 生成授权 URL:
https://chat.z.ai/api/oauth/authorizeclient_id=client_P8X5CMWmlaRO9gyO-KSqtgredirect_uri=zcode://zai-auth/callbackstate=<random-state>zcode://zai-auth/callback?code=<code>&state=<state>OAuthService.handleCallback 校验 pending state 后调用 ZAI adapter。ZAI adapter 调用后端:
POST https://zcode.z.ai/api/v1/oauth/token
Content-Type: application/json
{
"code": "<code>",
"redirect_uri": "zcode://zai-auth/callback",
"state": "<state>"
}
后端返回:
{
"code": 0,
"msg": "",
"data": {
"token": "eyJ...",
"expires_in": 86400,
"user": {
"user_id": "u_xxxxx",
"email": "user@example.com",
"avatar": "https://...",
"created_at": "2026-04-27T10:00:00Z"
}
}
}
客户端保存:
oauth:zai:access_token = data.tokenoauth:zai:user_info = 映射后的用户信息oauth:active_provider = zaiZAI provider 配置:
authorizeUrl: https://chat.z.ai/api/oauth/authorizetokenUrl: https://zcode.z.ai/api/v1/oauth/tokenuserinfoUrl: 保留配置字段但本次登录成功路径不再调用appId: 使用 client_P8X5CMWmlaRO9gyO-KSqtgredirectUri: zcode://zai-auth/callbacklegacyRedirectUri: 继续保留 zcode://oauth/callback 兼容旧 deep link 解析能力Token 映射:
OAuthTokenSet.accessToken 使用 data.tokenOAuthTokenSet.expiresAt 使用 now() + data.expires_in * 1000refreshTokenUser 映射:
OAuthUserProfile.id 使用 data.user.user_idOAuthUserProfile.username 优先使用 data.user.email,缺失时回落为 user idOAuthUserProfile.displayName 同 usernameOAuthUserProfile.avatarUrl 使用 data.user.avatarreadApiJson 抛出 ApiError。code !== 0 视为 token 交换失败,错误消息优先使用 msg。data.token 时抛出明确错误,避免保存空登录态。data.user 不阻断登录,沿用现有 fallback profile;原因是本次核心目标是拿到后端 JWT,用户展示信息可后续补齐。更新 packages/services/test/oauthService.test.ts:
code、redirect_uri、state。data.token 到 oauth:zai:access_token。expires_in 被换算为 expiresAt,后续可用于过期判断扩展。code !== 0 和缺少 data.token 会失败。最终验证必须执行:
pnpm typecheckpnpm lint功能完成后更新 docs/oauth/oauth-multi-provider-implementation.md,记录 ZAI 登录已经改为后端 JWT token 交换链路。