Skip to content

OAuth 2.0 安全认证体系 ​

天枢AI-TokenHub 提供完整的企业级 OAuth 2.0 认证能力,支持安全的用户委托访问和第三方集成,完全符合 OAuth 2.0 标准规范。

核心特性 ​

✨ 内置 OAuth 能力 ​

天枢AI-TokenHub 原生支持完整的 OAuth 2.0 协议栈,开箱即用:

  • 自动 Token 刷新:令牌过期前自动刷新,无需业务代码处理
  • PKCE 支持:支持公共客户端无需客户端密钥即可安全认证
  • 动态客户端注册:自动完成客户端注册,符合 RFC 7591 标准
  • OAuth 自动发现:从服务端 URL 自动发现认证端点
  • Token 全生命周期管理:支持令牌存储、查询、吊销等操作
  • 企业级安全:符合 OAuth 2.0 安全最佳实践,支持多种加密算法

OAuth 授权流程 ​

天枢AI-TokenHub 实现了业界最安全、应用最广泛的 Authorization Code 授权码流程:

Client                  Tianshu AI-TokenHub                  Authorization Server
  │                             │                                   │
  ├─ Create MCP client config ──►│                                   │
  │                             │ Return authorization URL          │
  │                             │                                   │
  ├─ Redirect user to auth URL ─┼──────────────────────────────────►│
  │                             │                                   │ User logs in and authorizes
  │                             │◄──────────────────────────────────┤
  │                             │ Authorization callback            │
  │                             │                                   │
  │                             │ Request token                     │
  │                             │◄──────────────────────────────────┤ Return token
  │                             │                                   │
  │◄───────────────────────────┤ MCP client created, ready to call  │

配置方式 ​

基础 OAuth 配置 ​

创建 MCP 客户端时配置 OAuth 认证:

json
{
  "name": "authenticated-service",
  "connection_type": "http",
  "connection_string": "https://api.example.com/mcp",
  "auth_type": "oauth",
  "oauth_config": {
    "client_id": "your-client-id",
    "client_secret": "your-client-secret",
    "authorize_url": "https://auth.example.com/oauth/authorize",
    "token_url": "https://auth.example.com/oauth/token",
    "scopes": ["mcp:read", "mcp:write"]
  },
  "tools_to_execute": ["*"]
}

创建成功返回:

json
{
  "status": "pending_oauth",
  "message": "OAuth authorization required",
  "oauth_config_id": "oauth_cfg_abc123",
  "authorize_url": "https://auth.example.com/oauth/authorize?client_id=...&state=xyz",
  "expires_at": "2026-01-24T12:30:00Z",
  "mcp_client_id": "mcp_client_abc123"
}

将用户重定向到 authorize_url 完成授权后,调用完成接口:

bash
curl -X POST https://ai-tokenhub.com/api/mcp/client/mcp_client_abc123/complete-oauth

PKCE 公共客户端配置 ​

对于没有客户端密钥的应用,可使用 PKCE(Proof Key for Code Exchange)模式:

json
{
  "name": "public-client-service",
  "connection_type": "http",
  "connection_string": "https://api.example.com/mcp",
  "auth_type": "oauth",
  "oauth_config": {
    "client_id": "your-public-client-id",
    "authorize_url": "https://auth.example.com/oauth/authorize",
    "token_url": "https://auth.example.com/oauth/token",
    "scopes": ["mcp:read"]
  },
  "tools_to_execute": ["*"]
}

天枢AI-TokenHub 会自动生成和管理 PKCE 代码验证器,无需额外配置。

动态客户端注册 ​

如果 OAuth 服务端支持 RFC 7591,天枢AI-TokenHub 可以自动注册客户端:

json
{
  "name": "auto-registered-service",
  "connection_type": "http",
  "connection_string": "https://api.example.com/mcp",
  "auth_type": "oauth",
  "oauth_config": {
    "registration_url": "https://auth.example.com/oauth/register",
    "server_url": "https://api.example.com",
    "scopes": ["mcp:read", "mcp:write"]
  },
  "tools_to_execute": ["*"]
}

OAuth 自动发现 ​

天枢AI-TokenHub 可以从服务端元数据自动发现 OAuth 端点:

json
{
  "name": "discovered-service",
  "connection_type": "http",
  "connection_string": "https://api.example.com/mcp",
  "auth_type": "oauth",
  "oauth_config": {
    "client_id": "your-client-id",
    "server_url": "https://api.example.com",
    "scopes": ["mcp:read"]
  },
  "tools_to_execute": ["*"]
}

如果没有显式提供 OAuth 端点,系统会自动检查:

  • /.well-known/oauth-authorization-server (RFC 8414)
  • /.well-known/openid-configuration
  • 服务端 MCP 元数据

Token 管理 ​

查询 Token 状态 ​

bash
curl https://ai-tokenhub.com/api/oauth/config/oauth_cfg_abc123/status

返回结果:

json
{
  "id": "oauth_cfg_abc123",
  "status": "authorized",
  "created_at": "2026-01-24T10:00:00Z",
  "expires_at": "2026-01-31T10:00:00Z",
  "token_id": "oauth_token_xyz",
  "token_expires_at": "2026-01-25T10:00:00Z",
  "token_scopes": ["mcp:read", "mcp:write"]
}

状态说明 ​

状态说明
pending用户尚未完成授权
authorizedToken 有效且处于激活状态
failed授权失败或 Token 已失效

自动 Token 刷新 ​

天枢AI-TokenHub 会自动监控 Token 有效期,在过期前自动完成刷新,无需业务系统介入,保证服务连续性。

适用场景 ​

  • 第三方集成:安全地授权第三方应用访问平台资源
  • 用户委托访问:支持用户授权的安全访问模式
  • 企业内部系统集成:不同业务系统之间的安全调用
  • 合规场景:需要符合 OAuth 2.0 安全标准的业务场景
  • 定期授权场景:需要周期性重新授权的安全敏感业务