Discourse OAuth2 の基礎

:discourse2: | 概要 | Discourse OAuth2 Basic は、ユーザー詳細をトークンで取得できる JSON API エンドポイントを持つと仮定した、基本的な OAuth2 プロバイダーをサポートします。
: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 を使用していることを確認してください。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"
    }
  }
}

oauth2_json_user_id_pathuser.idoauth2_json_email_pathuser.email.address を使用できます。

キー自体にピリオドが含まれている場合は、ダブルクォートで囲むか、バックスラッシュでピリオドをエスケープする必要があります。例えば、以下の JSON が与えられた場合。

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

パスを example\.com/uid または "example.com/uid" として指定します。

グループとユーザーフィールドの同期

  • oauth2_json_groups_path: ユーザーのグループを文字列の配列として含む JSON パス。最初のメンバーがログインした後、これらのグループは Discourse のグループ設定の「関連するグループ」セクションで選択可能になります。グループ同期を無効にする場合は空白のままにしてください。

  • oauth2_user_field_mappings: Discourse のユーザーフィールドに保存される JSON パスのマッピング。ユーザーフィールドは数値 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