跳到主内容

OAuth提供方

OAuth 提供商允许用户使用外部身份提供商(如 Google、GitHub 或微信)的账户登录 Casdoor。该提供商的图标会显示在使用它的应用的登录和注册页面上。

支持的提供商​

提供者Logo提供者Logo提供者Logo提供者Logo
ADFS支付宝亚马逊苹果
Auth0Azure ADAzure AD B2C百度
BilibiliBitbucket盒子Casdoor
Cloud FoundryDailymotionDeezerDigitalOcean
钉钉DiscordTiktokDropbox
Eve OnlineFacebookFitbitGitea
GiteeGitHubGitLabGoogle
HerokuInfluxCloud信息流Instagram
IntercomKakaoLarkLastfm
线LinkedInMailruMeetup
微软NaverNextcloudOkta
OneDriveOuraPatreonPayPal
QQSalesforceShopifySlack
SoundCloudSpotifySteamStrava
StripeTelegramTikTokTumblr
TwitchTwitterTypetalkUber
VK微信WeCom微博
WePayXeroYahooYammer
YandexZoom电子邮件短信
Battle.netOIDC

本节为每种提供商类型提供了单独的指南。对于未列出的提供商,如果支持 OpenID Connect (OIDC),请使用 OIDC 类型;否则,请使用自定义 OAuth 提供商。

添加 OAuth 提供商​

  1. 在身份提供商处,注册一个 OAuth 应用:

    • 将其回调 URL(提供商可能称为重定向 URI)设置为 Casdoor 的回调 URL:https://<your-casdoor-host>/callback。
    • 选择作用域,这决定了 Casdoor 接收哪些用户数据。
    • Copy the client ID and the client secret. 妥善保管 client secret。

    提供商处的回调 URL 是 Casdoor 的 URL,而不是你自己应用的 URL。请参阅 重定向 URL 和回调 URL。

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

  3. 将 Category 设置为 OAuth 并选择 Type,例如 Google 或 GitHub。

  4. 输入从身份提供商获取的 Client ID 和 Client secret。

  5. 保存提供商.

将提供商添加到应用​

  1. 打开应用的编辑页面并转到 Providers 选项卡。
  2. 添加提供商并选择用户是否可以与其进行注册、登录和解绑。请参阅 将提供商添加到应用。
  3. 保存应用。

Casdoor 如何关联账户​

当用户使用 OAuth 提供商登录时,Casdoor 首先查找已与该提供商身份关联的 Casdoor 用户。如果没有找到,它会按顺序检查提供商的 Binding rule 字段(在应用的 Providers 选项卡上设置),并关联第一个匹配的用户。因此,你可以将 OAuth 提供商添加到现有用户库中,而无需手动关联账户。

RuleLinks the user when
Email提供商报告电子邮件地址已验证,且组织中的某个用户拥有相同的电子邮件地址。该用户必须具有已验证的电子邮件地址,或者不是通过应用注册创建的
PhoneThe provider reports the phone number as verified, and a user of the organization has the same phone number and country code
NameA user of the organization has the same username, without regard to case

The default Binding rule is Email, Phone, and Email, Phone, Name for DingTalk, where Name only matches the unionid from the address book of your organization, not the nickname. For other providers, add Name only if usernames at the provider and in Casdoor belong to the same people. An empty Binding rule turns off linking. If the existing user has multi-factor authentication (MFA) turned on, Casdoor asks for the second factor before the sign-in completes.

电子邮件地址或电话号码是否被视为已验证取决于提供商类型。例如,Google 报告 email_verified 声明,OIDC 类型读取 email_verified 和 phone_number_verified,而 GitHub、GitLab 和 DingTalk 将其返回的所有电子邮件地址都视为已验证。自定义提供商读取用户映射键 emailVerified 和 phoneVerified。请参阅 将 OAuth 声明映射到用户字段。

使用提供商注册​

如果没有匹配的用户且应用允许使用该提供商注册,Casdoor 会创建一个用户:

  • Casdoor 会将电子邮件地址转换为小写。
  • 如果组织中的另一个用户已经拥有该电子邮件地址,注册将失败并提示 Email already exists。如果另一个用户拥有相同国家代码的电话号码,则失败并提示 Phone already exists。Casdoor 不会创建具有相同电子邮件地址或电话号码的第二个用户。只有 Binding rule 决定登录是否属于现有用户。
  • 如果提供商报告新用户的电子邮件地址已验证,则该地址被视为已验证。因此,Casdoor 签发的令牌中的 email_verified 声明遵循提供商的状态。

映射额外的用户字段​

Casdoor 从提供商读取用户名、电子邮件地址和头像。要填充更多字段(如电话号码或职位),请将提供商的声明映射到用户字段。请参阅 将 OAuth 声明映射到用户字段。

使用提供商的访问令牌​

OAuth 登录后,Casdoor 将提供商的访问令牌存储在用户身上。你的应用可以从 /api/get-account 读取它,并代表用户调用提供商的 API,例如 GitHub API 或 Google Drive API。只有用户和组织管理员可以看到该令牌。请参阅 获取外部提供商的访问令牌。

通过代理路由请求​

如果身份提供商只能通过代理访问,请在提供商上开启 Enable proxy。然后 Casdoor 将通过 conf/app.conf 中 socks5Proxy 设置的 SOCKS5 代理发送登录流程的请求。某些提供商类型始终使用代理,无论此设置如何:GitHub, Google, Facebook, LinkedIn, Steam, Line, Amazon, Instagram, TikTok, Twitter, Uber 和 Yahoo。

A provider that belongs to an organization other than admin, built-in, and the trusted organizations sends the requests of the sign-in flow only to Internet addresses, and Enable proxy has no effect on it. The provider types in the list above are the exception: they always use the proxy. See Which addresses a provider can reach.

另请参阅​