Skip to content

当前用户

获取当前认证用户的身份信息,以及管理「我的凭证」(API Key、Agent Token、OAuth 授权)。

源码: apps/backend/src/routes/my.ts

GET /my

返回当前认证方式及其关联信息。

认证方式

JWT Token / API Key / Agent Token(combinedAuth

请求参数

响应格式

状态码: 200

响应内容取决于认证方式:

JWT 认证响应

json
{
  "method": "jwt",
  "userId": "uuid",
  "email": "user@example.com",
  "metadata": {
    "display_name": "用户名",
    "avatar_url": "https://..."
  }
}
字段类型说明
methodstring固定值 "jwt"
userIdstring用户 UUID
emailstring用户邮箱
metadataobject用户元数据(user_metadata

API Key 认证响应

json
{
  "method": "api-key",
  "userId": "uuid",
  "teamId": "uuid",
  "apiKeyId": "uuid"
}
字段类型说明
methodstring固定值 "api-key"
userIdstringAPI Key 创建者的用户 ID
teamIdstringAPI Key 所属团队 ID
apiKeyIdstringAPI Key 记录 ID

Agent Token 认证响应

json
{
  "method": "agent",
  "userId": "uuid",
  "teamId": "uuid",
  "bindingId": "uuid"
}
字段类型说明
methodstring固定值 "agent"
userIdstringAgent 关联的用户 ID
teamIdstringAgent 所属团队 ID
bindingIdstringagent_bindings 记录 ID

Agent Token 需在其权限组中显式授予 GET /my 访问权限才会到达本端点;未授权时由 combinedAuthcheckTokenPermission 拦截。

Schedule 认证响应

json
{
  "method": "schedule",
  "scheduleId": "uuid",
  "teamId": "uuid",
  "deploymentId": "uuid"
}
字段类型说明
methodstring固定值 "schedule"
scheduleIdstring定时任务 ID
teamIdstring所属团队 ID
deploymentIdstring目标部署 ID

请求示例

bash
# JWT 认证
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  https://block2-api.wainao.chat/my

# API Key 认证
curl -H "Authorization: Bearer wn-your-api-key" \
  https://block2-api.wainao.chat/my

# Agent Token 认证
curl -H "Authorization: Bearer wna-your-agent-token" \
  https://block2-api.wainao.chat/my

错误码

状态码错误说明
401Missing or invalid authorization header缺少认证信息
401Invalid API key / Invalid token认证失败
403Permission denied: ...Token 权限组不包含 GET /my

我的凭证管理(/my/credentials/*

「我的凭证」管理子路由组,仅接受 JWT 认证。其他认证方式(API Key / Agent Token)通过 combinedAuth 后会被 jwtOnly guard 拦截返回 403;无效 token 在 combinedAuth 阶段直接 401 拒绝。

设计意图:用户对自己持有的机器凭证(Agent Token、OAuth 授权)的管理能力,必须使用人类身份(JWT)操作,避免机器凭证自我撤销。

API Key 列表与撤销复用既有 /teams/:teamId/api-keys 链路,未在此暴露独立 REST 端点。

GET /my/credentials/agent-tokens

列出当前用户持有的 active Agent Token 绑定。

过滤条件: owner_user_id = auth.userId AND token_hash IS NOT NULL AND deleted_at IS NULL,按创建时间倒序。

响应格式

json
{
  "items": [
    {
      "binding_id": "uuid",
      "agent_user_id": "uuid",
      "agent_display_name": "我的 Agent",
      "agent_avatar_url": "https://...",
      "team_id": "uuid",
      "team_name": "团队名",
      "team_slug": "team-slug",
      "can_send": true,
      "can_receive": true,
      "created_at": "2026-04-25T..."
    }
  ]
}
字段类型说明
binding_idstringagent_bindings 主键,吊销时使用
agent_user_idstringAgent 对应的虚拟用户 ID
agent_display_namestring | nullAgent 在 profiles 表中的展示名
agent_avatar_urlstring | nullAgent 头像 URL
team_id / team_name / team_slugstring | nullAgent 所属团队(可能为空,代表个人 Agent)
can_send / can_receivebooleanAgent 当前的双向通信开关
created_atstringAgent 绑定创建时间(ISO 8601)

DELETE /my/credentials/agent-tokens/:bindingId

吊销指定 Agent Token,写 token_hash = null 保留绑定本体(与 /team-agents/.../revoke-token 行为一致)。

响应格式

成功(首次撤销):

json
{ "success": true }

幂等(已撤销过):

json
{ "success": true, "alreadyRevoked": true }

错误码

状态码错误说明
401Missing or invalid authorization header / Invalid token缺少认证或 token 无效(在 combinedAuth 阶段拦截)
403This endpoint only accepts JWT authentication凭证有效但非 JWT(API Key / Agent Token 通过 combinedAuth 后由 jwtOnly guard 拒绝)
403Forbiddenbinding 不属于当前用户
404Agent binding not foundbinding 不存在或已软删除
500Failed to revoke agent token数据库写入失败

GET /my/credentials/oauth-authorizations

列出当前用户授权过的第三方 OAuth 应用,按 app_id 聚合(仅展示 active token,且应用未被软删除)。

过滤条件: oauth_tokens.user_id = auth.userId AND oauth_tokens.revoked_at IS NULL AND oauth_apps.deleted_at IS NULL

聚合规则: 同一应用的多枚 token 合并为一条;scopes 取并集;authorized_at 取最新;active_token_count 计数。

响应格式

json
{
  "items": [
    {
      "app_id": "uuid",
      "app_name": "我的应用",
      "app_logo_url": "https://...",
      "scopes": ["documents:read", "runs:write"],
      "authorized_at": "2026-04-25T...",
      "active_token_count": 1
    }
  ]
}

已禁用的应用(disabled_at IS NOT NULL)仍会展示,让用户感知后主动撤销。已删除的应用(deleted_at IS NOT NULL)不展示。

DELETE /my/credentials/oauth-authorizations/:appId

撤销该应用下当前用户的所有 active token。严格按 user_id + app_id 过滤,不会影响其他用户的授权(与 admin 端的 revoke_all_oauth_tokens_for_app RPC 语义不同)。

响应格式

json
{ "success": true, "revokedCount": 2 }

幂等:若该应用下当前用户已无 active token,返回 revokedCount: 0

错误码

状态码错误说明
401Missing or invalid authorization header / Invalid token缺少认证或 token 无效(在 combinedAuth 阶段拦截)
403This endpoint only accepts JWT authentication凭证有效但非 JWT(API Key / Agent Token 通过 combinedAuth 后由 jwtOnly guard 拒绝)
500Failed to revoke oauth authorization数据库写入失败

AI Workflow Editor