Discourse OAuth2 基础

:discourse2: 摘要 Discourse OAuth2 Basic 支持基础的 OAuth2 提供商,前提是它们提供可通过令牌获取用户详情的 JSON API 端点。
:open_book: 安装指南 该插件已内置于 Discourse 核心中。无需单独安装该插件。

功能特性

该插件允许您使用基础的 OAuth2 提供商作为 Discourse 的身份验证方式。它应能与许多提供商配合使用,但前提是这些提供商必须提供用于检索登录用户信息的 JSON 端点。

这对于使用不太流行的登录提供商的用户特别有用。如果您想使用 Google、Facebook 或 Twitter,这些功能已内置,无需此插件。您也可以在我们的 GitHub 仓库 中查找其他登录提供商。

配置

基础配置

  1. 首先,向您的 OAuth2 提供商注册您的 Discourse 应用。这将需要一个 重定向 URI,其格式为:

    http://DISCOURSE_HOST/auth/oauth2_basic/callback

:information_source:DISCOURSE_HOST 替换为相应的值,并确保如果启用了 https 则使用 https。OAuth2 提供商应为您提供 client IDsecret,以及若干 URL。

  1. 访问您的 管理后台设置OAuth2 登录,并填写 OAuth2 提供商的基础配置:
  • oauth2_enabled - 勾选此项以启用该功能
  • oauth2_client_id - 来自提供商的客户端 ID
  • oauth2_client_secret - 来自提供商的客户端密钥
  • oauth2_authorize_url - 提供商的授权 URL
  • oauth2_token_url - 提供商的令牌 URL

:information_source: 如果您无法确定上述设置的值,请查阅提供商的开发文档或联系其客户支持。

配置 JSON 用户端点

现在,Discourse 已能够从您的 OAuth2 提供商接收授权令牌。不幸的是,Discourse 需要更多信息才能完成身份验证。

我们需要一个可通过 API 调用的端点,以便根据令牌检索用户信息。

例如,OAuth2 提供商 SoundCloud 提供了这样的 URL。如果您拥有 SoundCloud 的 OAuth2 令牌,可以向 https://api.soundcloud.com/me?oauth_token=A_VALID_TOKEN 发送 GET 请求,并将返回包含用户信息的 JSON 对象。

要在 Discourse 上配置此项,我们需要设置 oauth2_user_json_url 设置的值。在这种情况下,我们将输入以下值:

https://api.soundcloud.com/me?oauth_token=:token

其中 :token 部分告诉 Discourse 需要在身份验证完成后用接收到的授权令牌替换该值。

最后一步是完成配置。我们需要告诉 Discourse 在接收到的 JSON 中哪些属性是可用的。以下是来自 SoundCloud 的示例响应:

{
  "id": 3207,
  "permalink": "jwagener",
  "username": "Johannes Wagener",
  "uri": "https://api.soundcloud.com/users/3207",
  "permalink_url": "http://soundcloud.com/jwagener",
  "avatar_url": "http://i1.sndcdn.com/avatars-000001552142-pbw8yd-large.jpg?142a848",
  "country": "Germany",
  "full_name": "Johannes Wagener",
  "city": "Berlin"
}

oauth2_json_user_id_pathoauth2_json_username_pathoauth2_json_name_pathoauth2_json_email_path 变量应设置为指向 JSON 中的相应属性。

唯一必需的属性是 id —— 我们需要它以便在用户未来登录时能够找到正确的账户。其他属性如果可用也非常好——它们可以加快用户的注册流程,因为这些信息将在表单中预先填充。

以下是我配置 JSON 路径设置的方式:

  oauth2_json_user_id_path: 'id'
  oauth2_json_username_path: 'permalink'
  oauth2_json_name_path: 'full_name'

我使用了 permalink,因为它看起来比 JSON 中的 username 更接近 Discourse 对用户名格式的预期。注意我省略了邮箱路径:SoundCloud 不提供邮箱,因此用户首次注册 Discourse 时必须提供并验证邮箱。

如果您需要的 JSON 对象属性是嵌套的,可以使用点号。例如,如果 API 返回如下结构:

{
  "user": {
    "id": 1234,
    "email": {
      "address": "test@example.com"
    }
  }
}

您可以使用 user.id 作为 oauth2_json_user_id_path,使用 user.email.address 作为 oauth2_json_email_path

如果键本身包含点号,则需要用双引号将其括起来,或使用反斜杠转义点号。例如,给定以下 JSON:

{
  "example.com/uid": "myuid"
}

您可以将路径指定为 example\.com/uid"example.com/uid"

群组与用户字段同步

  • oauth2_json_groups_path:包含用户所属群组(字符串数组)的 JSON 路径。在首位成员登录后,这些群组将可在 Discourse 群组设置的“关联群组”部分中选择。留空以禁用群组同步。

  • oauth2_user_field_mappings:将存储到 Discourse 用户字段的 JSON 路径映射。用户字段通过其数字 ID 进行标识,该 ID 可通过管理面板编辑用户字段时在 URL 中找到。

:warning: 如果您设置了 oauth2_json_email_path,OAuth2 提供商 必须 确认用户确实拥有该邮箱地址。 若不这样做,可能导致 Discourse 中的账户被接管!

:discourse2: 由我们托管?此插件可在我们的商业版和企业版计划中使用。OAuth 2.0 & OpenID Connect Support | Discourse - Civilized Discussion

:spiral_notepad: 需要自动化用户注册?请参阅 Auto-provisioning user accounts when SSO is enabled

28 个赞
Keycloak with Discourse
Discourse SSO with OAuth2
Login from another user database
Shopify Integration
How can we enable Auth0 SSO in Discourse
OAuth2 integration with Drupal
OAuth connection of discourse
Login flow (Flask -> Discourse -> Flask) with OAuth
How to use Oauth2 service provided by discourse?
How to login to discourse from external website
Is "partial" SSO possible?
Set up Salesforce auth using OAuth2 basic support plugin
How to force users link phone number when they using Discourse?
OAuth2 Custom Redirects Plugin
Custom Login / Registration from another API
Login on discourse using mastodon credentials
Open source will support customized provider SSO
Oauth2 with fusionauth cert issues
Auto-provisioning user accounts when SSO is enabled
Configure sign up and log in with Auth0 using the OAuth2 Basic Plugin
SSO with TownNews CMS
What is supposed to go in “DISCOURSE_HOST”?
Custom Provider log-in with OAuth only sign-up/log-in
Discord, Google and Microsoft login, is oAuth2 enough?
Populating email field on login page
CodeBerg support
Gate our community to just members of our Shopify site?
Intergrate Discourse with keycloak
Integration into custom auth system where emails are not unique?
Twitter login doesn't work on meta
Setup DiscourseConnect - Official Single-Sign-On for Discourse (sso)
Question about Docker Manager?
Discourse OpenID Connect (OIDC)
🧩 How to Build an Android App User Community with Discourse? [HeyApks Project]
Bundling more popular plugins with Discourse core
Drupal 8 and Discourse shared SSO
Discourse for self hosting
Discourse + Intercom (Current User Id)
Error during SSO integration - Wholistic Minds
ADFS Authentication
Pulling user auth0 sub from OAuth2.0 plugin
Suggestion for improving Integrated Authentication development
Github and Twitter Login/Sign-Up Functionality?
Automatically creating a user when logging in with Webflow/Memberspace
Switching out authentication for a passwordless alternative
Removing Yahoo login from Core, and deprecating OpenID 2.0
Shopify Integration
SSO and Auth0
Migrate a Jive Clearspace forum to Discourse