跳到主内容

添加自定义 OAuth 提供商

本指南介绍如何将 Casdoor 连接到没有内置类型的 OAuth 2.0 身份提供商,例如内部身份提供商或自托管服务。您可以添加最多十个自定义提供商,类型分别为 Custom、Custom2,依此类推直至 Custom10,每个提供商都有独立的配置。对于支持 OpenID Connect (OIDC) 发现的提供商,请改用 OIDC 类型。


学习目标​

  • 配置自定义 OAuth 提供商。
  • 了解 Casdoor 发送哪些请求以及期望哪些响应。

所需条件​

  • 支持标准三方 OAuth 2.0 授权码流程的身份提供商
  • 提供商的授权端点、令牌端点和用户信息端点,以及从提供商获取的 Client ID 和 Client Secret

创建提供商​

  1. 在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。

  2. 将 Category 设置为 OAuth,将 Type 设置为 Custom 到 Custom10 中的一种。

  3. 填写以下字段:

    FieldDescription
    Client ID, Client secretCredentials that the identity provider issued
    Auth URLAuthorization endpoint of the identity provider
    ScopeScopes to request, as the identity provider documents them
    Enable PKCEAdds Proof Key for Code Exchange (PKCE) to the flow
    Token URLToken endpoint of the identity provider
    UserInfo URLUser info endpoint of the identity provider. Optional if the token response has an ID token
    Logout URLOptional. When a user signs out, Casdoor sends a POST form with token, client_id, and client_secret to it
    User mappingClaim name for each user field. See User info
    FaviconURL of the logo that the Casdoor sign-in page shows

    Custom provider in Casdoor

  4. 保存提供商.

请求与响应​

身份提供商必须处理以下请求并返回以下响应。示例中使用 Casdoor 本身作为身份提供商。

Casbin权限管理​

Casdoor 将浏览器重定向到 Auth URL:

https://door.casdoor.com/login/oauth/authorize?client_id={ClientID}&redirect_uri=https://{your-casdoor-hostname}/callback&state={State_generated_by_Casdoor}&response_type=code&scope={Scope}` 

启用 Enable PKCE 后,Casdoor 会追加以下内容:

&code_challenge={code_challenge}&code_challenge_method=S256

用户登录后,身份提供商会携带 code 重定向到 Casdoor 的回调地址:

https://{your-casdoor-hostname}/callback?code={code}

使用 PKCE 时,Casdoor 会为每次登录生成一个新的随机代码验证器(code verifier),计算 S256 挑战值,并将验证器存储在 OAuth state 对应的 localStorage 中。使用后,它会删除该验证器。

令牌​

Casdoor 在 Token URL 处交换 code:

curl -X POST -u "{ClientID}:{ClientSecret}" --data-binary "code={code}&grant_type=authorization_code&redirect_uri=https://{your-casdoor-hostname}/callback" https://door.casdoor.com/api/login/oauth/access_token

使用 PKCE 时,请求包含 code verifier:

curl -X POST -u "{ClientID}:{ClientSecret}" --data-binary "code={code}&grant_type=authorization_code&redirect_uri=https://{your-casdoor-hostname}/callback&code_verifier={code_verifier}" https://door.casdoor.com/api/login/oauth/access_token

响应必须至少包含:

{
"access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ixxxxxxxxxxxxxx",
"refresh_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6xxxxxxxxxxxxxx",
"token_type": "Bearer",
"expires_in": 10080,
"scope": "openid profile email"
}

用户信息​

Casdoor 使用访问令牌从 UserInfo URL 读取用户:

curl -X GET -H "Authorization: Bearer {accessToken}" https://door.casdoor.com/api/userinfo

例如:

{
"sub": "8a7b9c4e-1234-5678-9abc-def012345678",
"name": "admin",
"preferred_username": "Admin",
"email": "admin@example.com",
"email_verified": true,
"picture": "https://casbin.org/img/casbin.svg",
"phone": "+1234567890"
}

如果令牌响应包含 id_token,Casdoor 也会读取其声明。它会检查是否存在 sub,exp 是否未过期,以及 aud 是否包含客户端 ID。UserInfo URL 的声明会覆盖同名的 ID 令牌声明。

Casdoor 从 User mapping 指定的声明中读取每个用户字段。点号分隔的名称(如 data.user.id)用于读取嵌套声明。如果声明缺失或为空,Casdoor 会使用标准的 OIDC 声明,例如用 sub 作为 ID,用 preferred_username 作为用户名,用 picture 作为头像,用 phone_number 作为电话。参见 Casdoor 读取的声明。由 ID 指定的声明是必需的:如果响应中不包含它,登录将失败。对于上面的示例,请将 ID 设置为 sub。

用户映射键 emailVerified 和 phoneVerified 决定电子邮件地址和电话号码是否被视为已验证。参见 已验证的邮箱和电话。

下一步​

将提供商添加到应用。参见 向应用添加提供商。

另请参阅​