Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Support group sync in OAuth2 plugin (#40039)

David Taylorc43c69e1116b
diff --git a/plugins/discourse-oauth2-basic/config/locales/server.en.yml b/plugins/discourse-oauth2-basic/config/locales/server.en.yml
index d77dccf0aef..d395b81f1d7 100644
--- a/plugins/discourse-oauth2-basic/config/locales/server.en.yml
+++ b/plugins/discourse-oauth2-basic/config/locales/server.en.yml
@@ -21,6 +21,7 @@ en:
oauth2_json_email_path: "Path in the OAuth2 User JSON to the user's email. eg: user.email"
oauth2_json_email_verified_path: "Path in the OAuth2 User JSON to the user's email verification state. eg: user.email.verified. oauth2_email_verified must be disabled for this setting to have any effect"
oauth2_json_avatar_path: "Path in the Oauth2 User JSON to the user's avatar. eg: user.avatar_url"
+ oauth2_json_groups_path: "Path in the OAuth2 User JSON to an array of group names the user belongs to. Leave blank to disable group syncing."
oauth2_user_field_mappings: "Map paths in the OAuth2 User JSON onto Discourse user fields. Each entry pairs a JSON path with the numeric ID of a Discourse user field."
oauth2_email_verified: "Check this if the OAuth2 site has verified the email"
oauth2_overrides_email: "Override the Discourse email with the remote email on every login. Works the same as the `auth_overrides_email` setting, but is specific to OAuth2 logins."
diff --git a/plugins/discourse-oauth2-basic/config/settings.yml b/plugins/discourse-oauth2-basic/config/settings.yml
index 44206f83065..f33a1190fa9 100644
--- a/plugins/discourse-oauth2-basic/config/settings.yml
+++ b/plugins/discourse-oauth2-basic/config/settings.yml
@@ -66,6 +66,9 @@ login:
oauth2_json_avatar_path:
default: ""
area: "oauth2"
+ oauth2_json_groups_path:
+ default: ""
+ area: "oauth2"
oauth2_user_field_mappings:
type: objects
default: []
diff --git a/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb b/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
index 556171663da..d215cb0a546 100644
--- a/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
+++ b/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
@@ -13,6 +13,10 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
SiteSetting.oauth2_allow_association_change
end
+ def provides_groups?
+ SiteSetting.oauth2_json_groups_path.present?
+ end
+
def register_middleware(omniauth)
omniauth.provider :oauth2_basic,
name: name,
@@ -118,6 +122,22 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
end
end
+ def groups_from(user_json)
+ path = SiteSetting.oauth2_json_groups_path
+ expanded = path.gsub(".[].", ".").gsub(".[", "[")
+ value = walk_path(user_json, parse_segments(expanded))
+
+ if value.is_a?(Array)
+ value
+ elsif value.present?
+ log("groups path '#{path}' did not resolve to an array (got #{value.class})")
+ []
+ else
+ log("groups path '#{path}' did not resolve to anything")
+ []
+ end
+ end
+
def user_field_values_from(user_json)
mappings = JSON.parse(SiteSetting.oauth2_user_field_mappings.presence || "[]")
return {} if mappings.blank?
@@ -205,6 +225,7 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
end
result[:user_field_values] = user_field_values_from(user_json)
+ result[:groups] = groups_from(user_json) if provides_groups?
end
result
else
@@ -274,7 +295,13 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
end
result = super(auth, existing_account: existing_account)
- result.user_field_values = fetched_user_details[:user_field_values] if fetched_user_details
+ if fetched_user_details
+ result.user_field_values = fetched_user_details[:user_field_values]
+ if provides_groups?
+ groups = fetched_user_details[:groups] || []
+ result.associated_groups = groups.map { |g| { id: g, name: g } }
+ end
+ end
result
end
diff --git a/plugins/discourse-oauth2-basic/spec/plugin_spec.rb b/plugins/discourse-oauth2-basic/spec/plugin_spec.rb
index cb179bdb1dd..c75626d94f0 100644
--- a/plugins/discourse-oauth2-basic/spec/plugin_spec.rb
+++ b/plugins/discourse-oauth2-basic/spec/plugin_spec.rb
@@ -123,6 +123,53 @@ describe OAuth2BasicAuthenticator do
end
end
+ describe "group syncing" do
+ before { SiteSetting.oauth2_json_groups_path = "account.groups" }
+
+ it "sets associated_groups from the configured path" do
+ body = { account: { email: "[email protected]", groups: %w[admins editors] } }.to_json
+ stub_request(:get, SiteSetting.oauth2_user_json_url).to_return(status: 200, body: body)
+
+ result = authenticator.after_authenticate(auth)
+ expect(result.associated_groups).to eq(
+ [{ id: "admins", name: "admins" }, { id: "editors", name: "editors" }],
+ )
+ end
+
+ it "clears associated_groups when the path resolves to an empty array" do
+ body = { account: { email: "[email protected]", groups: [] } }.to_json
+ stub_request(:get, SiteSetting.oauth2_user_json_url).to_return(status: 200, body: body)
+
+ result = authenticator.after_authenticate(auth)
+ expect(result.associated_groups).to eq([])
+ end
+
+ it "clears associated_groups when the path doesn't resolve" do
+ body = { account: { email: "[email protected]" } }.to_json
+ stub_request(:get, SiteSetting.oauth2_user_json_url).to_return(status: 200, body: body)
+
+ result = authenticator.after_authenticate(auth)
+ expect(result.associated_groups).to eq([])
+ end
+
+ it "clears associated_groups and logs when the path resolves to a non-array" do
+ body = { account: { email: "[email protected]", groups: "admins" } }.to_json
+ stub_request(:get, SiteSetting.oauth2_user_json_url).to_return(status: 200, body: body)
+
+ result = authenticator.after_authenticate(auth)
+ expect(result.associated_groups).to eq([])
+ end
+
+ it "leaves associated_groups nil when no path is configured" do
+ SiteSetting.oauth2_json_groups_path = ""
+ body = { account: { email: "[email protected]", groups: %w[admins editors] } }.to_json
+ stub_request(:get, SiteSetting.oauth2_user_json_url).to_return(status: 200, body: body)
+
+ result = authenticator.after_authenticate(auth)
+ expect(result.associated_groups).to be_nil
+ end
+ end
+
describe "user field mappings" do
fab!(:user_field)