Skip to content

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

FIX: Don't advertise auth providers with missing credentials (#42484)

Régis Hanol74f839fb7f39
diff --git a/app/services/problem_check/auth_provider_config.rb b/app/services/problem_check/auth_provider_config.rb
new file mode 100644
index 00000000000..0b59d4ac1b6
--- /dev/null
+++ b/app/services/problem_check/auth_provider_config.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class ProblemCheck::AuthProviderConfig < ProblemCheck
+ def call
+ return no_problem if !SiteSetting.get(authenticator.enable_setting)
+ return no_problem if authenticator.configured?
+
+ problem
+ end
+
+ private
+
+ def authenticator
+ raise NotImplementedError
+ end
+
+ def translation_data
+ {
+ missing_settings:
+ SiteSettings::LabelFormatter.setting_markers(authenticator.missing_settings),
+ }
+ end
+end
diff --git a/app/services/problem_check/facebook_config.rb b/app/services/problem_check/facebook_config.rb
index 931e43d2b6d..f4fb85b2030 100644
--- a/app/services/problem_check/facebook_config.rb
+++ b/app/services/problem_check/facebook_config.rb
@@ -1,18 +1,9 @@
# frozen_string_literal: true
-class ProblemCheck::FacebookConfig < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if !SiteSetting.enable_facebook_logins
- return no_problem if facebook_credentials_present?
-
- problem
- end
-
+class ProblemCheck::FacebookConfig < ProblemCheck::AuthProviderConfig
private
- def facebook_credentials_present?
- SiteSetting.facebook_app_id.present? && SiteSetting.facebook_app_secret.present?
+ def authenticator
+ @authenticator ||= Auth::FacebookAuthenticator.new
end
end
diff --git a/app/services/problem_check/github_config.rb b/app/services/problem_check/github_config.rb
index 7ce3bd4e658..3f908001f37 100644
--- a/app/services/problem_check/github_config.rb
+++ b/app/services/problem_check/github_config.rb
@@ -1,18 +1,9 @@
# frozen_string_literal: true
-class ProblemCheck::GithubConfig < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if !SiteSetting.enable_github_logins
- return no_problem if github_credentials_present?
-
- problem
- end
-
+class ProblemCheck::GithubConfig < ProblemCheck::AuthProviderConfig
private
- def github_credentials_present?
- SiteSetting.github_client_id.present? && SiteSetting.github_client_secret.present?
+ def authenticator
+ @authenticator ||= Auth::GithubAuthenticator.new
end
end
diff --git a/app/services/problem_check/google_oauth2_config.rb b/app/services/problem_check/google_oauth2_config.rb
index d149124e79b..0edcfc80e1d 100644
--- a/app/services/problem_check/google_oauth2_config.rb
+++ b/app/services/problem_check/google_oauth2_config.rb
@@ -1,18 +1,9 @@
# frozen_string_literal: true
-class ProblemCheck::GoogleOauth2Config < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if !SiteSetting.enable_google_oauth2_logins
- return no_problem if google_oauth2_credentials_present?
-
- problem
- end
-
+class ProblemCheck::GoogleOauth2Config < ProblemCheck::AuthProviderConfig
private
- def google_oauth2_credentials_present?
- SiteSetting.google_oauth2_client_id.present? && SiteSetting.google_oauth2_client_secret.present?
+ def authenticator
+ @authenticator ||= Auth::GoogleOAuth2Authenticator.new
end
end
diff --git a/app/services/problem_check/s3_backup_config.rb b/app/services/problem_check/s3_backup_config.rb
index d455d493d86..ed8c6c1a4ee 100644
--- a/app/services/problem_check/s3_backup_config.rb
+++ b/app/services/problem_check/s3_backup_config.rb
@@ -1,21 +1,13 @@
# frozen_string_literal: true
-class ProblemCheck::S3BackupConfig < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if GlobalSetting.use_s3?
- return no_problem if SiteSetting.backup_location != BackupLocationSiteSetting::S3
- return no_problem if !missing_keys? && SiteSetting.s3_backup_bucket.present?
-
- problem
- end
-
+class ProblemCheck::S3BackupConfig < ProblemCheck::S3Config
private
- def missing_keys?
- return false if SiteSetting.s3_use_iam_profile
+ def s3_enabled?
+ SiteSetting.backup_location == BackupLocationSiteSetting::S3
+ end
- SiteSetting.s3_access_key_id.blank? || SiteSetting.s3_secret_access_key.blank?
+ def bucket_setting
+ :s3_backup_bucket
end
end
diff --git a/app/services/problem_check/s3_config.rb b/app/services/problem_check/s3_config.rb
new file mode 100644
index 00000000000..af520a07b87
--- /dev/null
+++ b/app/services/problem_check/s3_config.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class ProblemCheck::S3Config < ProblemCheck
+ def call
+ return no_problem if GlobalSetting.use_s3?
+ return no_problem if !s3_enabled?
+ return no_problem if missing_settings.empty?
+
+ problem
+ end
+
+ private
+
+ def s3_enabled?
+ raise NotImplementedError
+ end
+
+ def bucket_setting
+ raise NotImplementedError
+ end
+
+ # An IAM profile stands in for the access keys.
+ def missing_settings
+ settings = SiteSetting.s3_use_iam_profile ? [] : %i[s3_access_key_id s3_secret_access_key]
+
+ (settings << bucket_setting).select { SiteSetting.get(it).blank? }
+ end
+
+ def translation_data
+ { missing_settings: SiteSettings::LabelFormatter.setting_markers(missing_settings) }
+ end
+end
diff --git a/app/services/problem_check/s3_upload_config.rb b/app/services/problem_check/s3_upload_config.rb
index 764ce1430c5..4278afa2fa5 100644
--- a/app/services/problem_check/s3_upload_config.rb
+++ b/app/services/problem_check/s3_upload_config.rb
@@ -1,21 +1,13 @@
# frozen_string_literal: true
-class ProblemCheck::S3UploadConfig < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if GlobalSetting.use_s3?
- return no_problem if !SiteSetting.enable_s3_uploads?
- return no_problem if !missing_keys? && SiteSetting.s3_upload_bucket.present?
-
- problem
- end
-
+class ProblemCheck::S3UploadConfig < ProblemCheck::S3Config
private
- def missing_keys?
- return false if SiteSetting.s3_use_iam_profile
+ def s3_enabled?
+ SiteSetting.enable_s3_uploads?
+ end
- SiteSetting.s3_access_key_id.blank? || SiteSetting.s3_secret_access_key.blank?
+ def bucket_setting
+ :s3_upload_bucket
end
end
diff --git a/app/services/problem_check/twitter_config.rb b/app/services/problem_check/twitter_config.rb
index e6d0ca95600..b93c9ac8fb7 100644
--- a/app/services/problem_check/twitter_config.rb
+++ b/app/services/problem_check/twitter_config.rb
@@ -1,18 +1,9 @@
# frozen_string_literal: true
-class ProblemCheck::TwitterConfig < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if !SiteSetting.enable_twitter_logins
- return no_problem if twitter_credentials_present?
-
- problem
- end
-
+class ProblemCheck::TwitterConfig < ProblemCheck::AuthProviderConfig
private
- def twitter_credentials_present?
- SiteSetting.twitter_consumer_key.present? && SiteSetting.twitter_consumer_secret.present?
+ def authenticator
+ @authenticator ||= Auth::TwitterAuthenticator.new
end
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 73a5be8b8f0..e736403b25a 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1980,13 +1980,13 @@ en:
sidekiq_check: 'Sidekiq is not running. Many tasks, like sending emails, are executed asynchronously by Sidekiq. Please ensure at least one Sidekiq process is running. <a href="https://github.com/mperham/sidekiq" target="_blank">Learn about Sidekiq here</a>.'
queue_size: "The number of queued jobs is %{queue_size}, which is high. This could indicate a problem with the Sidekiq process(es), or you may need to add more Sidekiq workers."
ram: "Your server is running with less than 1 GB of total memory. At least 1 GB of memory is recommended."
- google_oauth2_config: 'The server is configured to allow signup and login with Google OAuth2 (enable_google_oauth2_logins), but the client id and client secret values are not set. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.'
- facebook_config: 'The server is configured to allow signup and login with Facebook (enable_facebook_logins), but the app id and app secret values are not set. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.'
- twitter_config: 'The server is configured to allow signup and login with Twitter (enable_twitter_logins), but the key and secret values are not set. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.'
- github_config: 'The server is configured to allow signup and login with GitHub (enable_github_logins), but the client id and secret values are not set. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.'
+ google_oauth2_config: '{{setting:enable_google_oauth2_logins}} is on, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.'
+ facebook_config: '{{setting:enable_facebook_logins}} is on, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.'
+ twitter_config: '{{setting:enable_twitter_logins}} is on, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.'
+ github_config: '{{setting:enable_github_logins}} is on, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.'
github_onebox_backoff: "GitHub is rate-limiting the server, so GitHub links may not preview until the limit resets. Configuring {{setting:github_onebox_access_tokens}} raises the limit significantly."
- s3_upload_config: 'The server is configured to upload files to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_upload_bucket. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.'
- s3_backup_config: 'The server is configured to upload backups to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_backup_bucket. Go to <a href="%{base_path}/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.'
+ s3_upload_config: '{{setting:enable_s3_uploads}} is on, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.'
+ s3_backup_config: '{{setting:backup_location}} is set to S3, but %{missing_settings} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.'
s3_cdn: 'The server is configured to upload files to S3, but there is no S3 CDN configured. This can lead to expensive S3 costs and slower site performance. <a href="https://meta.discourse.org/t/-/148916" target="_blank">See "Using Object Storage for Uploads" to learn more</a>.'
image_magick: 'The server is configured to create thumbnails of large images, but ImageMagick is not installed. Install ImageMagick using your favorite package manager or <a href="https://www.imagemagick.org/script/download.php" target="_blank">download the latest release</a>.'
failing_emails: 'There are %{num_failed_jobs} email jobs that failed. Check your app.yml and ensure that the mail server settings are correct. <a href="%{base_path}/sidekiq/retries" target="_blank">See the failed jobs in Sidekiq</a>.'
@@ -3217,7 +3217,7 @@ en:
markdown_linkify_tlds: "You cannot include a value of '*'."
google_oauth2_hd_groups: "You must configure all 'google oauth2 hd' settings before enabling this setting."
discourse_id_registration: "Failed to automatically register with Discourse ID. This could be due to network connectivity issues, firewall restrictions, or the Discourse ID service being unreachable. Please check server logs for more details or contact support."
- linkedin_oidc_credentials: "You must configure LinkedIn OIDC credentials ('linkedin_oidc_client_id' and 'linkedin_oidc_client_secret') before enabling this setting."
+ auth_provider_credentials_missing: "You must set %{settings} before enabling this setting."
search_tokenize_chinese_enabled: "You must disable 'search_tokenize_chinese' before enabling this setting."
search_tokenize_japanese_enabled: "You must disable 'search_tokenize_japanese' before enabling this setting."
discourse_connect_cannot_be_enabled_if_second_factor_enforced: "You cannot enable DiscourseConnect if 2FA is enforced."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index e7abea37b25..7a783a90af5 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -757,6 +757,7 @@ login:
hidden: true
enable_google_oauth2_logins:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
google_oauth2_client_id:
default: ""
@@ -794,6 +795,7 @@ login:
area: "authenticators"
enable_twitter_logins:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
twitter_consumer_key:
default: ""
@@ -806,6 +808,7 @@ login:
area: "authenticators"
enable_facebook_logins:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
facebook_app_id:
client: true
@@ -819,6 +822,7 @@ login:
area: "authenticators"
enable_github_logins:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
github_client_id:
default: ""
@@ -831,6 +835,7 @@ login:
area: "authenticators"
enable_discord_logins:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
discord_client_id:
default: ""
@@ -846,7 +851,7 @@ login:
area: "authenticators"
enable_linkedin_oidc_logins:
default: false
- validator: "LinkedinOidcCredentialsValidator"
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
linkedin_oidc_client_id:
default: ""
diff --git a/docs/developer-guides/docs/03-code-internals/17-authentication-method.md b/docs/developer-guides/docs/03-code-internals/17-authentication-method.md
index 73518497a4f..92ce92fbaf8 100644
--- a/docs/developer-guides/docs/03-code-internals/17-authentication-method.md
+++ b/docs/developer-guides/docs/03-code-internals/17-authentication-method.md
@@ -16,7 +16,21 @@ Each authenticator **must** implement a subclass of [Auth::Authenticator](https:
https://github.com/discourse/discourse/blob/main/lib/auth/facebook_authenticator.rb
-`name`, `enabled?` and `register_middleware` must be overridden by implementing classes.
+`name` and `register_middleware` must be overridden by implementing classes, along with `enable_setting` — the boolean site setting an admin uses to turn the provider on.
+
+An authenticator **should** also declare `required_settings`: the site settings that must have a value before an authentication can succeed. The base class uses them for `configured?`, and `enabled?` is `enable_setting && configured?`, so a provider with missing credentials is never advertised on the login page and its `/auth/<name>` route stays closed — otherwise clicking the button strands the user on the provider's own error page with no way back. Declaring `required_settings` also lets `AuthProviderCredentialsValidator` refuse to enable the provider in the first place; wire it up with `validator: "AuthProviderCredentialsValidator"` on the enable setting.
+
+```rb
+def enable_setting
+ :enable_google_oauth2_logins
+end
+
+def required_settings
+ %i[google_oauth2_client_id google_oauth2_client_secret]
+end
+```
+
+An authenticator that overrides `enabled?` directly opts out of both gates.
> :information_source: **Aside:** for multisite compatibility, it is important that any site-specific information is supplied to omniauth in a `setup` lambda, rather than being fixed at the time of definition. See all core authenticators for examples of this.
diff --git a/lib/auth/authenticator.rb b/lib/auth/authenticator.rb
index 9d55191124d..dd72cb47284 100644
--- a/lib/auth/authenticator.rb
+++ b/lib/auth/authenticator.rb
@@ -18,8 +18,28 @@ class Auth::Authenticator
nil
end
+ def enable_setting
+ nil
+ end
+
+ def required_settings
+ []
+ end
+
+ def missing_settings
+ required_settings.select { SiteSetting.get(it).blank? }
+ end
+
+ def configured?
+ missing_settings.empty?
+ end
+
def enabled?
- raise NotImplementedError
+ if enable_setting.nil?
+ raise NotImplementedError, "#{self.class} must implement `enable_setting` or `enabled?`"
+ end
+
+ SiteSetting.get(enable_setting) && configured?
end
# run once the user has completed authentication on the third party system. Should return an instance of Auth::Result.
diff --git a/lib/auth/discord_authenticator.rb b/lib/auth/discord_authenticator.rb
index ff7d9cba894..cc239ba6963 100644
--- a/lib/auth/discord_authenticator.rb
+++ b/lib/auth/discord_authenticator.rb
@@ -50,8 +50,12 @@ class Auth::DiscordAuthenticator < Auth::ManagedAuthenticator
"https://discord.com"
end
- def enabled?
- SiteSetting.enable_discord_logins?
+ def enable_setting
+ :enable_discord_logins
+ end
+
+ def required_settings
+ %i[discord_client_id discord_secret]
end
def register_middleware(omniauth)
diff --git a/lib/auth/discourse_id_authenticator.rb b/lib/auth/discourse_id_authenticator.rb
index 21837673bf6..b01e7a8311d 100644
--- a/lib/auth/discourse_id_authenticator.rb
+++ b/lib/auth/discourse_id_authenticator.rb
@@ -43,9 +43,12 @@ class Auth::DiscourseIdAuthenticator < Auth::ManagedAuthenticator
site
end
- def enabled?
- SiteSetting.enable_discourse_id && SiteSetting.discourse_id_client_id.present? &&
- SiteSetting.discourse_id_client_secret.present?
+ def enable_setting
+ :enable_discourse_id
+ end
+
+ def required_settings
+ %i[discourse_id_client_id discourse_id_client_secret]
end
def site
diff --git a/lib/auth/facebook_authenticator.rb b/lib/auth/facebook_authenticator.rb
index 4c7ab11670e..480f6c25797 100644
--- a/lib/auth/facebook_authenticator.rb
+++ b/lib/auth/facebook_authenticator.rb
@@ -15,8 +15,12 @@ class Auth::FacebookAuthenticator < Auth::ManagedAuthenticator
"https://www.facebook.com"
end
- def enabled?
- SiteSetting.enable_facebook_logins
+ def enable_setting
+ :enable_facebook_logins
+ end
+
+ def required_settings
+ %i[facebook_app_id facebook_app_secret]
end
def register_middleware(omniauth)
diff --git a/lib/auth/github_authenticator.rb b/lib/auth/github_authenticator.rb
index 6b1b4212ddd..7d833e087df 100644
--- a/lib/auth/github_authenticator.rb
+++ b/lib/auth/github_authenticator.rb
@@ -15,8 +15,12 @@ class Auth::GithubAuthenticator < Auth::ManagedAuthenticator
"https://github.com"
end
- def enabled?
- SiteSetting.enable_github_logins
+ def enable_setting
+ :enable_github_logins
+ end
+
+ def required_settings
+ %i[github_client_id github_client_secret]
end
def after_authenticate(auth_token, existing_account: nil)
diff --git a/lib/auth/google_oauth2_authenticator.rb b/lib/auth/google_oauth2_authenticator.rb
index c383c165994..80199875f86 100644
--- a/lib/auth/google_oauth2_authenticator.rb
+++ b/lib/auth/google_oauth2_authenticator.rb
@@ -18,8 +18,12 @@ class Auth::GoogleOAuth2Authenticator < Auth::ManagedAuthenticator
"https://accounts.google.com"
end
- def enabled?
- SiteSetting.enable_google_oauth2_logins
+ def enable_setting
+ :enable_google_oauth2_logins
+ end
+
+ def required_settings
+ %i[google_oauth2_client_id google_oauth2_client_secret]
end
def primary_email_verified?(auth_token)
diff --git a/lib/auth/linkedin_oidc_authenticator.rb b/lib/auth/linkedin_oidc_authenticator.rb
index 246015a49f1..4f316876f4a 100644
--- a/lib/auth/linkedin_oidc_authenticator.rb
+++ b/lib/auth/linkedin_oidc_authenticator.rb
@@ -53,8 +53,12 @@ class Auth::LinkedInOidcAuthenticator < Auth::ManagedAuthenticator
"https://www.linkedin.com"
end
- def enabled?
- SiteSetting.enable_linkedin_oidc_logins
+ def enable_setting
+ :enable_linkedin_oidc_logins
+ end
+
+ def required_settings
+ %i[linkedin_oidc_client_id linkedin_oidc_client_secret]
end
def register_middleware(omniauth)
diff --git a/lib/auth/twitter_authenticator.rb b/lib/auth/twitter_authenticator.rb
index 4778ef3b80c..272d86daa58 100644
--- a/lib/auth/twitter_authenticator.rb
+++ b/lib/auth/twitter_authenticator.rb
@@ -13,19 +13,21 @@ class Auth::TwitterAuthenticator < Auth::ManagedAuthenticator
"https://x.com"
end
- def enabled?
- SiteSetting.enable_twitter_logins
+ def enable_setting
+ :enable_twitter_logins
+ end
+
+ def required_settings
+ %i[twitter_consumer_key twitter_consumer_secret]
end
def healthy?
require "omniauth-twitter"
- consumer_key = SiteSetting.twitter_consumer_key
- consumer_secret = SiteSetting.twitter_consumer_secret
- return false if consumer_key.blank? || consumer_secret.blank?
+ return false if !configured?
OmniAuth::Strategies::Twitter
- .new(nil, consumer_key, consumer_secret)
+ .new(nil, SiteSetting.twitter_consumer_key, SiteSetting.twitter_consumer_secret)
.consumer
.get_request_token(oauth_callback: "oob")
diff --git a/lib/site_settings/label_formatter.rb b/lib/site_settings/label_formatter.rb
index a781f52b141..828174e271f 100644
--- a/lib/site_settings/label_formatter.rb
+++ b/lib/site_settings/label_formatter.rb
@@ -148,6 +148,10 @@ module SiteSettings
expand_setting_links(desc)
end
+ def setting_markers(settings)
+ Array(settings).map { "{{setting:#{it}}}" }.join(", ")
+ end
+
def settings_filter_href(filter)
"#{Discourse.base_path}/admin/site_settings/category/all_results?filter=#{CGI.escape(filter)}"
end
diff --git a/lib/validators/auth_provider_credentials_validator.rb b/lib/validators/auth_provider_credentials_validator.rb
new file mode 100644
index 00000000000..f14aea53ce6
--- /dev/null
+++ b/lib/validators/auth_provider_credentials_validator.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class AuthProviderCredentialsValidator
+ def initialize(opts = {})
+ @opts = opts
+ end
+
+ def valid_value?(val)
+ return true if val == "f"
+ missing_settings.empty?
+ end
+
+ def error_message
+ I18n.t(
+ "site_settings.errors.auth_provider_credentials_missing",
+ settings: SiteSettings::LabelFormatter.setting_markers(missing_settings),
+ )
+ end
+
+ private
+
+ def missing_settings
+ authenticator&.missing_settings.to_a
+ end
+
+ def authenticator
+ @authenticator ||= Discourse.authenticators.find { it.enable_setting == @opts[:name] }
+ end
+end
diff --git a/lib/validators/enable_discourse_id_validator.rb b/lib/validators/enable_discourse_id_validator.rb
index 425ccc0b1cd..f5ea78da013 100644
--- a/lib/validators/enable_discourse_id_validator.rb
+++ b/lib/validators/enable_discourse_id_validator.rb
@@ -27,6 +27,6 @@ class EnableDiscourseIdValidator
private
def credentials_missing?
- SiteSetting.discourse_id_client_id.blank? || SiteSetting.discourse_id_client_secret.blank?
+ !Auth::DiscourseIdAuthenticator.new.configured?
end
end
diff --git a/lib/validators/linkedin_oidc_credentials_validator.rb b/lib/validators/linkedin_oidc_credentials_validator.rb
deleted file mode 100644
index fab7faf68b3..00000000000
--- a/lib/validators/linkedin_oidc_credentials_validator.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-# frozen_string_literal: true
-
-class LinkedinOidcCredentialsValidator
- def initialize(opts = {})
- @opts = opts
- end
-
- def valid_value?(val)
- return true if val == "f"
- return false if credentials_missing?
- true
- end
-
- def error_message
- I18n.t("site_settings.errors.linkedin_oidc_credentials") if credentials_missing?
- end
-
- private
-
- def credentials_missing?
- SiteSetting.linkedin_oidc_client_id.blank? || SiteSetting.linkedin_oidc_client_secret.blank?
- end
-end
diff --git a/plugins/discourse-apple-auth/config/settings.yml b/plugins/discourse-apple-auth/config/settings.yml
index d2c2fb2b84d..cfc118f848d 100644
--- a/plugins/discourse-apple-auth/config/settings.yml
+++ b/plugins/discourse-apple-auth/config/settings.yml
@@ -2,6 +2,7 @@ plugins:
sign_in_with_apple_enabled:
default: false
area: "authenticators"
+ validator: "AuthProviderCredentialsValidator"
apple_client_id:
default: ''
area: "authenticators"
diff --git a/plugins/discourse-apple-auth/plugin.rb b/plugins/discourse-apple-auth/plugin.rb
index 78a3687426b..b204ca07b2d 100644
--- a/plugins/discourse-apple-auth/plugin.rb
+++ b/plugins/discourse-apple-auth/plugin.rb
@@ -18,8 +18,12 @@ class AppleAuthenticator < ::Auth::ManagedAuthenticator
"apple"
end
- def enabled?
- SiteSetting.sign_in_with_apple_enabled?
+ def enable_setting
+ :sign_in_with_apple_enabled
+ end
+
+ def required_settings
+ %i[apple_client_id apple_team_id apple_key_id apple_pem]
end
def provider_url
diff --git a/plugins/discourse-apple-auth/spec/requests/auth_apple_spec.rb b/plugins/discourse-apple-auth/spec/requests/auth_apple_spec.rb
index 2aff75ff0f4..ee942cb7943 100644
--- a/plugins/discourse-apple-auth/spec/requests/auth_apple_spec.rb
+++ b/plugins/discourse-apple-auth/spec/requests/auth_apple_spec.rb
@@ -9,11 +9,11 @@ describe "sign in with apple" do
before do
Discourse.cache.delete("sign-in-with-apple-jwks")
- SiteSetting.sign_in_with_apple_enabled = true
SiteSetting.apple_client_id = "myclientid"
SiteSetting.apple_team_id = "myteamid"
SiteSetting.apple_key_id = "mykeyid"
SiteSetting.apple_pem = pem
+ SiteSetting.sign_in_with_apple_enabled = true
stub_request(:get, "https://appleid.apple.com/auth/keys").to_return(
body: { keys: [jwk.export] }.to_json,
diff --git a/plugins/discourse-login-with-amazon/config/locales/server.en.yml b/plugins/discourse-login-with-amazon/config/locales/server.en.yml
index 615864556fa..f7837aef781 100644
--- a/plugins/discourse-login-with-amazon/config/locales/server.en.yml
+++ b/plugins/discourse-login-with-amazon/config/locales/server.en.yml
@@ -7,7 +7,3 @@ en:
login_with_amazon_overrides_email: "Overrides the email address on the Discourse user account."
login_with_amazon_user_can_connect: "Allow existing users to connect their Amazon account in preferences."
login_with_amazon_user_can_revoke: "Allow users to revoke their associated Amazon account in preferences."
-
- errors:
- login_with_amazon_client_id_is_blank: "You must set 'login with amazon client id' before enabling this setting."
- login_with_amazon_client_secret_is_blank: "You must set 'login with amazon client secret' before enabling this setting."
diff --git a/plugins/discourse-login-with-amazon/config/settings.yml b/plugins/discourse-login-with-amazon/config/settings.yml
index 9d73bcb29dd..2e5a17ed1c3 100644
--- a/plugins/discourse-login-with-amazon/config/settings.yml
+++ b/plugins/discourse-login-with-amazon/config/settings.yml
@@ -1,7 +1,7 @@
discourse_login_with_amazon:
enable_login_with_amazon:
default: false
- validator: "EnableLoginWithAmazonValidator"
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
login_with_amazon_client_id:
default: ""
diff --git a/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb b/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
index 60bb5fdceb7..36b8d27378b 100644
--- a/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
+++ b/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
@@ -5,8 +5,12 @@ class Auth::LoginWithAmazonAuthenticator < ::Auth::ManagedAuthenticator
"amazon"
end
- def enabled?
- SiteSetting.enable_login_with_amazon
+ def enable_setting
+ :enable_login_with_amazon
+ end
+
+ def required_settings
+ %i[login_with_amazon_client_id login_with_amazon_client_secret]
end
def register_middleware(omniauth)
diff --git a/plugins/discourse-login-with-amazon/lib/validators/enable_login_with_amazon_validator.rb b/plugins/discourse-login-with-amazon/lib/validators/enable_login_with_amazon_validator.rb
deleted file mode 100644
index 1c5c8d534f0..00000000000
--- a/plugins/discourse-login-with-amazon/lib/validators/enable_login_with_amazon_validator.rb
+++ /dev/null
@@ -1,26 +0,0 @@
-# frozen_string_literal: true
-
-class EnableLoginWithAmazonValidator
- def initialize(opts = {})
- @opts = opts
- end
-
- def valid_value?(val)
- return true if val == "f"
-
- if SiteSetting.login_with_amazon_client_id.blank? ||
- SiteSetting.login_with_amazon_client_secret.blank?
- return false
- end
-
- true
- end
-
- def error_message
- if SiteSetting.login_with_amazon_client_id.blank?
- I18n.t("site_settings.errors.login_with_amazon_client_id_is_blank")
- elsif SiteSetting.login_with_amazon_client_secret.blank?
- I18n.t("site_settings.errors.login_with_amazon_client_secret_is_blank")
- end
- end
-end
diff --git a/plugins/discourse-login-with-amazon/plugin.rb b/plugins/discourse-login-with-amazon/plugin.rb
index c49d296797c..ee28d78ff5e 100644
--- a/plugins/discourse-login-with-amazon/plugin.rb
+++ b/plugins/discourse-login-with-amazon/plugin.rb
@@ -12,7 +12,6 @@ enabled_site_setting :enable_login_with_amazon
register_svg_icon "fab-amazon"
require_relative "lib/auth/login_with_amazon_authenticator"
-require_relative "lib/validators/enable_login_with_amazon_validator"
require_relative "lib/omniauth/strategies/amazon"
auth_provider authenticator: Auth::LoginWithAmazonAuthenticator.new, icon: "fab-amazon"
diff --git a/plugins/discourse-login-with-amazon/spec/lib/validators/enable_login_with_amazon_validator_spec.rb b/plugins/discourse-login-with-amazon/spec/lib/validators/enable_login_with_amazon_validator_spec.rb
deleted file mode 100644
index 42ea9970125..00000000000
--- a/plugins/discourse-login-with-amazon/spec/lib/validators/enable_login_with_amazon_validator_spec.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe EnableLoginWithAmazonValidator do
- subject(:validator) { described_class.new }
-
- describe "#valid_value?" do
- describe "when login_with_amazon_client_id and login_with_amazon_client_secret has not been set" do
- it "returns true when value is false" do
- expect(validator.valid_value?("f")).to eq(true)
- end
-
- it "returns false when value is true" do
- expect(validator.valid_value?("t")).to eq(false)
-
- expect(validator.error_message).to eq(
- I18n.t("site_settings.errors.login_with_amazon_client_id_is_blank"),
- )
-
- SiteSetting.login_with_amazon_client_id = "somekey"
-
- expect(validator.valid_value?("t")).to eq(false)
-
- expect(validator.error_message).to eq(
- I18n.t("site_settings.errors.login_with_amazon_client_secret_is_blank"),
- )
- end
- end
-
- describe "when login_with_amazon_client_id and login_with_amazon_client_secret has been set" do
- before do
- SiteSetting.login_with_amazon_client_id = "somekey"
- SiteSetting.login_with_amazon_client_secret = "somesecretkey"
- end
-
- it "returns true when value is false" do
- expect(validator.valid_value?("f")).to eq(true)
- end
-
- it "returns true when value is true" do
- expect(validator.valid_value?("t")).to eq(true)
- end
- end
- end
-end
diff --git a/plugins/discourse-lti/config/settings.yml b/plugins/discourse-lti/config/settings.yml
index addc248428c..85af518f7a7 100644
--- a/plugins/discourse-lti/config/settings.yml
+++ b/plugins/discourse-lti/config/settings.yml
@@ -1,6 +1,7 @@
plugins:
lti_enabled:
default: false
+ validator: "AuthProviderCredentialsValidator"
lti_authorization_endpoint:
default: ""
lti_client_ids:
diff --git a/plugins/discourse-lti/lib/discourse_lti/lti_authenticator.rb b/plugins/discourse-lti/lib/discourse_lti/lti_authenticator.rb
index aad37df3305..525f6d796d6 100644
--- a/plugins/discourse-lti/lib/discourse_lti/lti_authenticator.rb
+++ b/plugins/discourse-lti/lib/discourse_lti/lti_authenticator.rb
@@ -15,8 +15,12 @@ class DiscourseLti::LtiAuthenticator < Auth::ManagedAuthenticator
true
end
- def enabled?
- SiteSetting.lti_enabled
+ def enable_setting
+ :lti_enabled
+ end
+
+ def required_settings
+ %i[lti_client_ids lti_authorization_endpoint lti_platform_issuer_id lti_platform_public_key]
end
def primary_email_verified?(auth)
diff --git a/plugins/discourse-lti/spec/requests/lti_spec.rb b/plugins/discourse-lti/spec/requests/lti_spec.rb
index c1fa3a69ab7..f370c58cf7d 100644
--- a/plugins/discourse-lti/spec/requests/lti_spec.rb
+++ b/plugins/discourse-lti/spec/requests/lti_spec.rb
@@ -17,12 +17,12 @@ describe "LTI Plugin" do
end
before do
- SiteSetting.lti_enabled = true
SiteSetting.lti_authorization_endpoint = authorize_url
SiteSetting.lti_platform_public_key = platform_public_key.to_s
SiteSetting.lti_platform_issuer_id = platform_issuer_id
SiteSetting.lti_client_ids = tool_client_id
SiteSetting.lti_email_verified = true
+ SiteSetting.lti_enabled = true
end
it "shows an error if auth is started on Discourse side" do
diff --git a/plugins/discourse-microsoft-auth/config/settings.yml b/plugins/discourse-microsoft-auth/config/settings.yml
index 47986fb5b72..69bf8021de1 100644
--- a/plugins/discourse-microsoft-auth/config/settings.yml
+++ b/plugins/discourse-microsoft-auth/config/settings.yml
@@ -2,6 +2,7 @@ plugins:
microsoft_auth_enabled:
client: true
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "authenticators"
microsoft_auth_client_id:
client: false
diff --git a/plugins/discourse-microsoft-auth/lib/microsoft_authenticator.rb b/plugins/discourse-microsoft-auth/lib/microsoft_authenticator.rb
index ea7755d843d..0d4eaf22e42 100644
--- a/plugins/discourse-microsoft-auth/lib/microsoft_authenticator.rb
+++ b/plugins/discourse-microsoft-auth/lib/microsoft_authenticator.rb
@@ -23,8 +23,12 @@ class MicrosoftAuthenticator < ::Auth::ManagedAuthenticator
}
end
- def enabled?
- SiteSetting.microsoft_auth_enabled
+ def enable_setting
+ :microsoft_auth_enabled
+ end
+
+ def required_settings
+ %i[microsoft_auth_client_id microsoft_auth_client_secret]
end
def primary_email_verified?(auth_token)
diff --git a/plugins/discourse-microsoft-auth/spec/integration/microsoft_auth_spec.rb b/plugins/discourse-microsoft-auth/spec/integration/microsoft_auth_spec.rb
index fac14852e72..c62cb38ba6a 100644
--- a/plugins/discourse-microsoft-auth/spec/integration/microsoft_auth_spec.rb
+++ b/plugins/discourse-microsoft-auth/spec/integration/microsoft_auth_spec.rb
@@ -9,9 +9,9 @@ describe "Microsoft OAuth2" do
fab!(:user1, :user)
before do
- SiteSetting.microsoft_auth_enabled = true
SiteSetting.microsoft_auth_client_id = client_id
SiteSetting.microsoft_auth_client_secret = client_secret
+ SiteSetting.microsoft_auth_enabled = true
stub_request(:post, "https://login.microsoftonline.com/common/oauth2/v2.0/token").with(
headers: {
diff --git a/plugins/discourse-oauth2-basic/config/settings.yml b/plugins/discourse-oauth2-basic/config/settings.yml
index f33a1190fa9..c04cc61fcfa 100644
--- a/plugins/discourse-oauth2-basic/config/settings.yml
+++ b/plugins/discourse-oauth2-basic/config/settings.yml
@@ -2,6 +2,7 @@ login:
oauth2_enabled:
default: false
client: true
+ validator: "AuthProviderCredentialsValidator"
area: "oauth2"
oauth2_client_id:
default: ""
diff --git a/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb b/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
index 1f0b65b4aee..906b4f0c35c 100644
--- a/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
+++ b/plugins/discourse-oauth2-basic/lib/oauth2_basic_authenticator.rb
@@ -5,6 +5,14 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
"oauth2_basic"
end
+ def enable_setting
+ :oauth2_enabled
+ end
+
+ def required_settings
+ %i[oauth2_client_id oauth2_client_secret oauth2_authorize_url oauth2_token_url]
+ end
+
def can_revoke?
SiteSetting.oauth2_allow_association_change
end
@@ -324,10 +332,6 @@ class OAuth2BasicAuthenticator < Auth::ManagedAuthenticator
result
end
- def enabled?
- SiteSetting.oauth2_enabled
- end
-
def request_timeout_seconds
GlobalSetting.oauth2_request_timeout_seconds
end
diff --git a/plugins/discourse-oauth2-basic/spec/integration/overrides_email_spec.rb b/plugins/discourse-oauth2-basic/spec/integration/overrides_email_spec.rb
index 689c9bf6cb4..30da15ab5e1 100644
--- a/plugins/discourse-oauth2-basic/spec/integration/overrides_email_spec.rb
+++ b/plugins/discourse-oauth2-basic/spec/integration/overrides_email_spec.rb
@@ -9,6 +9,10 @@ describe "OAuth2 Overrides Email", type: :request do
end
before do
+ SiteSetting.oauth2_client_id = "id"
+ SiteSetting.oauth2_client_secret = "secret"
+ SiteSetting.oauth2_authorize_url = "https://provider.com/authorize"
+ SiteSetting.oauth2_token_url = "https://provider.com/token"
SiteSetting.oauth2_enabled = true
SiteSetting.oauth2_callback_user_id_path = "uid"
SiteSetting.oauth2_fetch_user_details = false
diff --git a/plugins/discourse-oauth2-basic/spec/integration/token_request_failure_spec.rb b/plugins/discourse-oauth2-basic/spec/integration/token_request_failure_spec.rb
index d3037246409..cca29f57d8f 100644
--- a/plugins/discourse-oauth2-basic/spec/integration/token_request_failure_spec.rb
+++ b/plugins/discourse-oauth2-basic/spec/integration/token_request_failure_spec.rb
@@ -2,11 +2,11 @@
describe "OAuth2 Basic token request failures" do
before do
- SiteSetting.oauth2_enabled = true
SiteSetting.oauth2_client_id = "client"
SiteSetting.oauth2_client_secret = "secret"
SiteSetting.oauth2_authorize_url = "https://id.example.com/authorize"
SiteSetting.oauth2_token_url = "https://id.example.com/token"
+ SiteSetting.oauth2_enabled = true
stub_request(:post, "https://id.example.com/token").to_timeout
end
diff --git a/plugins/discourse-openid-connect/config/settings.yml b/plugins/discourse-openid-connect/config/settings.yml
index 0f3e5b0c6b8..cb3092dd856 100644
--- a/plugins/discourse-openid-connect/config/settings.yml
+++ b/plugins/discourse-openid-connect/config/settings.yml
@@ -1,6 +1,7 @@
discourse_openid_connect:
openid_connect_enabled:
default: false
+ validator: "AuthProviderCredentialsValidator"
area: "oidc"
openid_connect_discovery_document:
default: ""
diff --git a/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb b/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
index 0208f99bf22..603a9716d1b 100644
--- a/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
+++ b/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
@@ -15,8 +15,18 @@ class OpenIDConnectAuthenticator < Auth::ManagedAuthenticator
SiteSetting.openid_connect_allow_association_change
end
- def enabled?
- SiteSetting.openid_connect_enabled
+ def enable_setting
+ :openid_connect_enabled
+ end
+
+ # PKCE and mTLS client authentication each replace the client secret, so
+ # demanding it unconditionally would disable working installations.
+ def required_settings
+ settings = %i[openid_connect_discovery_document openid_connect_client_id]
+ if !SiteSetting.openid_connect_use_pkce && !mtls_configured?
+ settings << :openid_connect_client_secret
+ end
+ settings
end
def primary_email_verified?(auth)
@@ -206,11 +216,16 @@ class OpenIDConnectAuthenticator < Auth::ManagedAuthenticator
Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)).tr("+/", "-_").tr("=", "")
end
+ def mtls_configured?
+ SiteSetting.openid_connect_mtls_client_cert.present? &&
+ SiteSetting.openid_connect_mtls_client_key.present?
+ end
+
def mtls_ssl_options
+ return {} if !mtls_configured?
+
cert_pem = SiteSetting.openid_connect_mtls_client_cert
key_pem = SiteSetting.openid_connect_mtls_client_key
- return {} if cert_pem.blank? || key_pem.blank?
-
key_passcode = SiteSetting.openid_connect_mtls_client_key_passcode.presence
{
diff --git a/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb b/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
index 37bad64aa86..67f81681ec2 100644
--- a/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
+++ b/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
@@ -203,6 +203,58 @@ describe OpenIDConnectAuthenticator do
end
end
+ describe "#required_settings" do
+ it "requires a client secret by default" do
+ expect(authenticator.required_settings).to contain_exactly(
+ :openid_connect_discovery_document,
+ :openid_connect_client_id,
+ :openid_connect_client_secret,
+ )
+ end
+
+ it "does not require a client secret when using PKCE" do
+ SiteSetting.openid_connect_use_pkce = true
+ expect(authenticator.required_settings).not_to include(:openid_connect_client_secret)
+ end
+
+ it "does not require a client secret when using mTLS" do
+ SiteSetting.openid_connect_mtls_client_cert = "cert-pem"
+ SiteSetting.openid_connect_mtls_client_key = "key-pem"
+ expect(authenticator.required_settings).not_to include(:openid_connect_client_secret)
+ end
+
+ it "requires a client secret when only half of the mTLS pair is set" do
+ SiteSetting.openid_connect_mtls_client_cert = "cert-pem"
+ expect(authenticator.required_settings).to include(:openid_connect_client_secret)
+ end
+ end
+
+ describe "enabling the plugin" do
+ before do
+ SiteSetting.openid_connect_discovery_document =
+ "https://id.example.com/.well-known/openid-configuration"
+ SiteSetting.openid_connect_client_id = "my-client-id"
+ end
+
+ it "is refused while credentials are missing" do
+ expect { SiteSetting.openid_connect_enabled = true }.to raise_error(
+ Discourse::InvalidHTMLParameters,
+ )
+ end
+
+ it "is allowed once credentials are set" do
+ SiteSetting.openid_connect_client_secret = "my-client-secret"
+ SiteSetting.openid_connect_enabled = true
+ expect(authenticator.enabled?).to eq(true)
+ end
+
+ it "is allowed without a client secret when using PKCE" do
+ SiteSetting.openid_connect_use_pkce = true
+ SiteSetting.openid_connect_enabled = true
+ expect(authenticator.enabled?).to eq(true)
+ end
+ end
+
describe "mTLS support" do
let!(:mtls_key) { OpenSSL::PKey::RSA.new(2048) }
let!(:mtls_cert) do
diff --git a/plugins/discourse-openid-connect/spec/requests/rp_initiated_logout_spec.rb b/plugins/discourse-openid-connect/spec/requests/rp_initiated_logout_spec.rb
index b5a2f062c01..ee5d62d1ab6 100644
--- a/plugins/discourse-openid-connect/spec/requests/rp_initiated_logout_spec.rb
+++ b/plugins/discourse-openid-connect/spec/requests/rp_initiated_logout_spec.rb
@@ -1,10 +1,7 @@
# frozen_string_literal: true
describe "OIDC RP-Initiated Logout" do
- let(:document_url) do
- SiteSetting.openid_connect_discovery_document =
- "https://id.example.com/.well-known/openid-configuration"
- end
+ let(:document_url) { "https://id.example.com/.well-known/openid-configuration" }
let(:document) do
{
issuer: "https://id.example.com/",
@@ -18,6 +15,9 @@ describe "OIDC RP-Initiated Logout" do
fab!(:user)
before do
+ SiteSetting.openid_connect_discovery_document = document_url
+ SiteSetting.openid_connect_client_id = "my-client-id"
+ SiteSetting.openid_connect_client_secret = "my-client-secret"
SiteSetting.openid_connect_enabled = true
SiteSetting.openid_connect_rp_initiated_logout = true
stub_request(:get, document_url).to_return(body: lambda { |r| document.to_json })
diff --git a/plugins/discourse-patreon/config/locales/server.en.yml b/plugins/discourse-patreon/config/locales/server.en.yml
index 2fa24068155..4890014036e 100644
--- a/plugins/discourse-patreon/config/locales/server.en.yml
+++ b/plugins/discourse-patreon/config/locales/server.en.yml
@@ -16,8 +16,6 @@ en:
patreon_donation_prompt_show_after_topics: "Show donation prompt after n topics"
patreon_api_version: "Patreon API version. Version 1 is deprecated — create a new OAuth client on Patreon and switch to version 2."
patreon_donation_prompt_campaign_url: "Donation prompt campaign URL"
- errors:
- patreon_creator_username_not_set: "You need to set the Patreon Creator Discourse Username setting before enabling Patreon Login."
dashboard:
problem:
access_token_invalid: "Patreon Creator's access and refresh token values are incorrect. You must copy-paste new tokens from <a href='https://www.patreon.com/platform/documentation/clients'>Patreon website</a> to <a href='%{base_path}/admin/site_settings/category/all_results?filter=patreon'>site settings</a>."
diff --git a/plugins/discourse-patreon/config/settings.yml b/plugins/discourse-patreon/config/settings.yml
index a16a304fecd..35e68e42b34 100644
--- a/plugins/discourse-patreon/config/settings.yml
+++ b/plugins/discourse-patreon/config/settings.yml
@@ -5,7 +5,7 @@ discourse_patreon:
patreon_login_enabled:
default: false
client: true
- validator: 'PatreonLoginEnabledValidator'
+ validator: 'AuthProviderCredentialsValidator'
patreon_api_version:
default: "1"
type: enum
diff --git a/plugins/discourse-patreon/lib/validators/patreon_login_enabled_validator.rb b/plugins/discourse-patreon/lib/validators/patreon_login_enabled_validator.rb
deleted file mode 100644
index c57079e42a3..00000000000
--- a/plugins/discourse-patreon/lib/validators/patreon_login_enabled_validator.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-# frozen_string_literal: true
-
-class PatreonLoginEnabledValidator
- def initialize(opts = {})
- @opts = opts
- end
-
- def valid_value?(val)
- return true if val == "f"
- return false if SiteSetting.patreon_creator_discourse_username.blank?
- true
- end
-
- def error_message
- I18n.t("site_settings.errors.patreon_creator_username_not_set")
- end
-end
diff --git a/plugins/discourse-patreon/plugin.rb b/plugins/discourse-patreon/plugin.rb
index 10cea8311fe..581600598c9 100644
--- a/plugins/discourse-patreon/plugin.rb
+++ b/plugins/discourse-patreon/plugin.rb
@@ -16,9 +16,6 @@ register_asset "stylesheets/patreon.scss"
register_svg_icon "fab-patreon"
register_svg_icon "patreon-new"
-# Site setting validators must be loaded before initialize
-require_relative "lib/validators/patreon_login_enabled_validator"
-
module ::Patreon
PLUGIN_NAME = "discourse-patreon"
end
@@ -195,8 +192,12 @@ class Auth::PatreonAuthenticator < Auth::ManagedAuthenticator
result
end
- def enabled?
- SiteSetting.patreon_login_enabled
+ def enable_setting
+ :patreon_login_enabled
+ end
+
+ def required_settings
+ %i[patreon_client_id patreon_client_secret patreon_creator_discourse_username]
end
def primary_email_verified?(auth_token)
diff --git a/plugins/discourse-patreon/spec/integration/patreon_auth_spec.rb b/plugins/discourse-patreon/spec/integration/patreon_auth_spec.rb
index 81e0880bf82..e602beacbcc 100644
--- a/plugins/discourse-patreon/spec/integration/patreon_auth_spec.rb
+++ b/plugins/discourse-patreon/spec/integration/patreon_auth_spec.rb
@@ -11,9 +11,9 @@ describe "Patreon Oauth2" do
before do
SiteSetting.patreon_creator_discourse_username = user2.username
- SiteSetting.patreon_login_enabled = true
SiteSetting.patreon_client_id = client_id
SiteSetting.patreon_client_secret = client_secret
+ SiteSetting.patreon_login_enabled = true
end
shared_examples "patreon oauth" do
diff --git a/spec/integration/discord_omniauth_spec.rb b/spec/integration/discord_omniauth_spec.rb
index 574d985bf71..9bf37b774d5 100644
--- a/spec/integration/discord_omniauth_spec.rb
+++ b/spec/integration/discord_omniauth_spec.rb
@@ -36,9 +36,9 @@ describe "Discord OAuth2" do
end
before do
- SiteSetting.enable_discord_logins = true
SiteSetting.discord_client_id = client_id
SiteSetting.discord_secret = client_secret
+ SiteSetting.enable_discord_logins = true
stub_request(:post, "https://discord.com/api/oauth2/token").with(
headers: {
diff --git a/spec/integration/facebook_omniauth_spec.rb b/spec/integration/facebook_omniauth_spec.rb
index e5b657e944a..c1596a9a165 100644
--- a/spec/integration/facebook_omniauth_spec.rb
+++ b/spec/integration/facebook_omniauth_spec.rb
@@ -33,9 +33,9 @@ describe "Facebook OAuth2" do
end
before do
- SiteSetting.enable_facebook_logins = true
SiteSetting.facebook_app_id = app_id
SiteSetting.facebook_app_secret = app_secret
+ SiteSetting.enable_facebook_logins = true
stub_request(:post, "https://graph.facebook.com/v19.0/oauth/access_token").with(
body:
diff --git a/spec/integration/github_omniauth_spec.rb b/spec/integration/github_omniauth_spec.rb
index 1633939e7b2..0dfac09021b 100644
--- a/spec/integration/github_omniauth_spec.rb
+++ b/spec/integration/github_omniauth_spec.rb
@@ -24,9 +24,9 @@ describe "GitHub Oauth2" do
end
before do
- SiteSetting.enable_github_logins = true
SiteSetting.github_client_id = client_id
SiteSetting.github_client_secret = client_secret
+ SiteSetting.enable_github_logins = true
stub_request(:post, "https://github.com/login/oauth/access_token").with(
body:
diff --git a/spec/integration/twitter_omniauth_spec.rb b/spec/integration/twitter_omniauth_spec.rb
index 55e00b08be7..32c4647235e 100644
--- a/spec/integration/twitter_omniauth_spec.rb
+++ b/spec/integration/twitter_omniauth_spec.rb
@@ -64,9 +64,9 @@ describe "Twitter OAuth 1.0a" do
end
before do
- SiteSetting.enable_twitter_logins = true
SiteSetting.twitter_consumer_key = consumer_key
SiteSetting.twitter_consumer_secret = consumer_secret
+ SiteSetting.enable_twitter_logins = true
stub_request(:post, "https://api.x.com/oauth/request_token").to_return(
status: 200,
diff --git a/spec/integrity/site_setting_spec.rb b/spec/integrity/site_setting_spec.rb
index c6e4d074a0d..00be3933f8f 100644
--- a/spec/integrity/site_setting_spec.rb
+++ b/spec/integrity/site_setting_spec.rb
@@ -20,6 +20,33 @@ RSpec.describe "site setting integrity checks" do
expect(duplicates).to be_empty
end
+ it "guards every auth provider enable setting, and nothing else" do
+ files = [site_setting_file]
+ files.concat(Discourse.plugins.map { File.join(it.directory, "config/settings.yml") })
+
+ validators = {}
+ files
+ .select { File.exist?(it) }
+ .each do |file|
+ YAML
+ .load_file(file, aliases: true)
+ .each_value do |category|
+ next if !category.is_a?(Hash)
+ category.each do |name, setting|
+ validators[name.to_sym] = setting["validator"] if setting.is_a?(Hash)
+ end
+ end
+ end
+
+ guarded =
+ Discourse.authenticators.select { it.required_settings.present? }.map(&:enable_setting)
+
+ expect(guarded.reject { validators[it].present? }).to be_empty
+ expect(
+ validators.select { |_, v| v == "AuthProviderCredentialsValidator" }.keys - guarded,
+ ).to be_empty
+ end
+
it "no locale default has different type than default or invalid key" do
yaml.each_value do |category|
category.each do |setting_name, setting|
diff --git a/spec/lib/auth/google_oauth2_authenticator_spec.rb b/spec/lib/auth/google_oauth2_authenticator_spec.rb
index 64acb5e8bbb..a1f9c80e55a 100644
--- a/spec/lib/auth/google_oauth2_authenticator_spec.rb
+++ b/spec/lib/auth/google_oauth2_authenticator_spec.rb
@@ -29,6 +29,39 @@ RSpec.describe Auth::GoogleOAuth2Authenticator do
expect(result.user).to eq(nil)
end
+ describe "#enabled?" do
+ let(:authenticator) { described_class.new }
+
+ before do
+ SiteSetting.google_oauth2_client_id = "client_id"
+ SiteSetting.google_oauth2_client_secret = "client_secret"
+ end
+
+ it "is disabled when the site setting is off" do
+ expect(authenticator.enabled?).to eq(false)
+ end
+
+ context "when the site setting is on" do
+ before { SiteSetting.enable_google_oauth2_logins = true }
+
+ it "is enabled when both credentials are present" do
+ expect(authenticator.enabled?).to eq(true)
+ end
+
+ it "is disabled when the client id is blank" do
+ SiteSetting.google_oauth2_client_id = ""
+
+ expect(authenticator.enabled?).to eq(false)
+ end
+
+ it "is disabled when the client secret is blank" do
+ SiteSetting.google_oauth2_client_secret = ""
+
+ expect(authenticator.enabled?).to eq(false)
+ end
+ end
+ end
+
describe "after_authenticate" do
it "can authenticate and create a user record for already existing users" do
authenticator = Auth::GoogleOAuth2Authenticator.new
diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rb
index d15be6b1935..e1b6d737c89 100644
--- a/spec/lib/discourse_spec.rb
+++ b/spec/lib/discourse_spec.rb
@@ -250,6 +250,9 @@ RSpec.describe Discourse do
describe "enabled_authenticators" do
it "only returns enabled authenticators" do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = "consumer_secret"
+
expect(Discourse.enabled_authenticators.length).to be(0)
expect { SiteSetting.enable_twitter_logins = true }.to change {
Discourse.enabled_authenticators.length
@@ -257,6 +260,19 @@ RSpec.describe Discourse do
expect(Discourse.enabled_authenticators.length).to be(1)
expect(Discourse.enabled_authenticators.first).to be_instance_of(Auth::TwitterAuthenticator)
end
+
+ it "does not return an enabled authenticator once its credentials are removed" do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = "consumer_secret"
+ SiteSetting.enable_twitter_logins = true
+
+ expect { SiteSetting.twitter_consumer_secret = "" }.to change {
+ Discourse.enabled_authenticators.length
+ }.by(-1)
+
+ expect(SiteSetting.enable_twitter_logins).to eq(true)
+ expect(Discourse.enabled_authenticators).to be_empty
+ end
end
describe "#user_agent" do
diff --git a/spec/lib/validators/auth_provider_credentials_validator_spec.rb b/spec/lib/validators/auth_provider_credentials_validator_spec.rb
new file mode 100644
index 00000000000..0c75b8335ad
--- /dev/null
+++ b/spec/lib/validators/auth_provider_credentials_validator_spec.rb
@@ -0,0 +1,125 @@
+# frozen_string_literal: true
+
+RSpec.describe AuthProviderCredentialsValidator do
+ subject(:validator) { described_class.new(name: :enable_twitter_logins) }
+
+ describe "#valid_value?" do
+ context "when the provider's credentials are configured" do
+ before do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = "consumer_secret"
+ end
+
+ describe "when value is false" do
+ it "is valid" do
+ expect(validator.valid_value?("f")).to eq(true)
+ end
+ end
+
+ describe "when value is true" do
+ it "is valid" do
+ expect(validator.valid_value?("t")).to eq(true)
+ end
+ end
+ end
+
+ context "when the provider's credentials are not configured" do
+ before do
+ SiteSetting.twitter_consumer_key = ""
+ SiteSetting.twitter_consumer_secret = ""
+ end
+
+ describe "when value is false" do
+ it "is valid" do
+ expect(validator.valid_value?("f")).to eq(true)
+ end
+ end
+
+ describe "when value is true" do
+ it "is not valid" do
+ expect(validator.valid_value?("t")).to eq(false)
+
+ expect(validator.error_message).to eq(
+ I18n.t(
+ "site_settings.errors.auth_provider_credentials_missing",
+ settings: "{{setting:twitter_consumer_key}}, {{setting:twitter_consumer_secret}}",
+ ),
+ )
+ end
+ end
+ end
+
+ context "when only one of the provider's credentials is missing" do
+ before do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = ""
+ end
+
+ it "is not valid, and should only name the missing credential" do
+ expect(validator.valid_value?("t")).to eq(false)
+
+ expect(validator.error_message).to eq(
+ I18n.t(
+ "site_settings.errors.auth_provider_credentials_missing",
+ settings: "{{setting:twitter_consumer_secret}}",
+ ),
+ )
+ end
+ end
+
+ context "when no authenticator declares the setting" do
+ subject(:validator) { described_class.new(name: :enable_local_logins) }
+
+ it "is valid" do
+ expect(validator.valid_value?("t")).to eq(true)
+ end
+ end
+
+ # Every setting default is validated as the specs boot, so turning a
+ # provider off must never depend on the authenticator registry.
+ it "does not look up authenticators when the setting is being turned off" do
+ allow(Discourse).to receive(:authenticators).and_call_original
+
+ expect(validator.valid_value?("f")).to eq(true)
+ expect(Discourse).not_to have_received(:authenticators)
+ end
+ end
+
+ describe "enabling the site setting" do
+ it "is allowed once the credentials are set" do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = "consumer_secret"
+
+ SiteSetting.enable_twitter_logins = true
+
+ expect(SiteSetting.enable_twitter_logins).to eq(true)
+ end
+
+ it "is refused with a linkified error when a credential is missing" do
+ SiteSetting.twitter_consumer_key = "consumer_key"
+ SiteSetting.twitter_consumer_secret = ""
+
+ expect { SiteSetting.enable_twitter_logins = true }.to raise_error(
+ Discourse::InvalidHTMLParameters,
+ ) do |error|
+ expect(error.message).to include("Twitter consumer secret")
+ expect(error.message).not_to include("<a", "{{settings")
+ expect(error.html_message).to include(
+ 'class="site-setting-link"',
+ ">Twitter consumer secret</a>",
+ )
+ end
+
+ expect(SiteSetting.enable_twitter_logins).to eq(false)
+ end
+
+ it "is allowed when the setting is being turned off" do
+ SiteSetting.twitter_consumer_key = ""
+ SiteSetting.twitter_consumer_secret = ""
+
+ SiteSetting.enable_twitter_logins = false
+
+ expect(SiteSetting.enable_twitter_logins).to eq(false)
+ end
+ end
+end
diff --git a/spec/lib/validators/linkedin_oidc_credentials_validator_spec.rb b/spec/lib/validators/linkedin_oidc_credentials_validator_spec.rb
deleted file mode 100644
index 63f7f088e16..00000000000
--- a/spec/lib/validators/linkedin_oidc_credentials_validator_spec.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe LinkedinOidcCredentialsValidator do
- subject(:validator) { described_class.new }
-
- describe "#valid_value?" do
- describe "when OIDC authentication credentials are configured" do
- before do
- SiteSetting.linkedin_oidc_client_id = "foo"
- SiteSetting.linkedin_oidc_client_secret = "bar"
- end
-
- describe "when val is false" do
- it "accepts the setting value" do
- expect(validator.valid_value?("f")).to eq(true)
- end
- end
-
- describe "when value is true" do
- it "accepts the setting value" do
- expect(validator.valid_value?("t")).to eq(true)
- end
- end
- end
-
- describe "when OIDC authentication credentials are not configured" do
- before do
- SiteSetting.linkedin_oidc_client_id = ""
- SiteSetting.linkedin_oidc_client_secret = ""
- end
-
- describe "when value is false" do
- it "accepts the setting value" do
- expect(validator.valid_value?("f")).to eq(true)
- end
- end
-
- describe "when value is true" do
- it "rejects the setting value" do
- expect(validator.valid_value?("t")).to eq(false)
-
- expect(validator.error_message).to eq(
- I18n.t("site_settings.errors.linkedin_oidc_credentials"),
- )
- end
- end
- end
- end
-end
diff --git a/spec/models/associated_group_spec.rb b/spec/models/associated_group_spec.rb
index 404b129562a..850f836d993 100644
--- a/spec/models/associated_group_spec.rb
+++ b/spec/models/associated_group_spec.rb
@@ -11,7 +11,7 @@ RSpec.describe AssociatedGroup do
end
it "detects whether any auth providers provide associated groups" do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.google_oauth2_hd = "domain.com"
SiteSetting.google_oauth2_hd_groups = false
SiteSetting.google_oauth2_hd_groups_service_account_admin_email = "[email protected]"
diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb
index 1bc6302d143..d43e9b28adf 100644
--- a/spec/models/site_spec.rb
+++ b/spec/models/site_spec.rb
@@ -276,15 +276,33 @@ RSpec.describe Site do
end
it "includes all enabled authentication providers" do
+ SiteSetting.twitter_consumer_key = "twitter_consumer_key"
+ SiteSetting.twitter_consumer_secret = "twitter_consumer_secret"
SiteSetting.enable_twitter_logins = true
+ SiteSetting.facebook_app_id = "432489234823984"
+ SiteSetting.facebook_app_secret = "adddcccdddd99922"
SiteSetting.enable_facebook_logins = true
data = JSON.parse(Site.json_for(Guardian.new))
expect(data["auth_providers"].map { |a| a["name"] }).to contain_exactly("facebook", "twitter")
end
+ it "excludes enabled authentication providers that are missing credentials" do
+ SiteSetting.twitter_consumer_key = "twitter_consumer_key"
+ SiteSetting.twitter_consumer_secret = "twitter_consumer_secret"
+ SiteSetting.enable_twitter_logins = true
+ SiteSetting.twitter_consumer_secret = ""
+
+ data = JSON.parse(Site.json_for(Guardian.new))
+ expect(data["auth_providers"].map { |a| a["name"] }).to be_empty
+ end
+
it "includes all enabled authentication providers for anon when login_required" do
SiteSetting.login_required = true
+ SiteSetting.twitter_consumer_key = "twitter_consumer_key"
+ SiteSetting.twitter_consumer_secret = "twitter_consumer_secret"
SiteSetting.enable_twitter_logins = true
+ SiteSetting.facebook_app_id = "432489234823984"
+ SiteSetting.facebook_app_secret = "adddcccdddd99922"
SiteSetting.enable_facebook_logins = true
data = JSON.parse(Site.json_for(Guardian.new))
expect(data["auth_providers"].map { |a| a["name"] }).to contain_exactly("facebook", "twitter")
diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb
index 98df95de1cd..8cfac11f4e9 100644
--- a/spec/requests/api/users_spec.rb
+++ b/spec/requests/api/users_spec.rb
@@ -179,7 +179,7 @@ RSpec.describe "users" do
let(:external_id) { "myuid" }
before do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
UserAssociatedAccount.create!(
user: user,
provider_uid: "myuid",
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index fc44c046869..1e8a70da93d 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -155,7 +155,7 @@ RSpec.describe ApplicationController do
it "redirects to the sole authenticator when local logins are disabled" do
# Local logins and google enabled, show login UI
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
get "/"
expect(response).not_to redirect_to("/login")
expect(response.status).to eq(200)
@@ -166,7 +166,7 @@ RSpec.describe ApplicationController do
expect(response).to redirect_to("/auth/google_oauth2")
# Google and GitHub enabled, direct to login UI
- SiteSetting.enable_github_logins = true
+ enable_auth_provider(:github)
get "/"
expect(response).not_to redirect_to("/login")
expect(response.status).to eq(200)
@@ -185,7 +185,7 @@ RSpec.describe ApplicationController do
it "does not redirect to the authenticator when auth_immediately is disabled" do
SiteSetting.auth_immediately = false
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.enable_local_logins = false
get "/"
@@ -215,7 +215,7 @@ RSpec.describe ApplicationController do
it "does not redirect to the authenticator during registration" do
SiteSetting.enable_local_logins = false
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
get "/"
expect(response).to redirect_to("/auth/google_oauth2")
diff --git a/spec/requests/associate_accounts_controller_spec.rb b/spec/requests/associate_accounts_controller_spec.rb
index cf8854d36f2..fc4c2f02182 100644
--- a/spec/requests/associate_accounts_controller_spec.rb
+++ b/spec/requests/associate_accounts_controller_spec.rb
@@ -13,7 +13,7 @@ RSpec.describe Users::AssociateAccountsController do
context "when attempting reconnect" do
before do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
provider: "google_oauth2",
uid: "12345",
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index bca2f0e8472..7cdc7d693a1 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -75,7 +75,7 @@ RSpec.describe InvitesController do
it "verifies legacy email tokens when #{setting} is disabled after enabling codes" do
SiteSetting.enable_local_logins_via_code = true
SiteSetting.public_send("#{setting}=", false)
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
get "/invites/#{invite.invite_key}?t=#{invite.email_token}"
@@ -1543,7 +1543,7 @@ RSpec.describe InvitesController do
)
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
get "/auth/google_oauth2/callback.json"
expect(response.status).to eq(302)
diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rb
index 5b5f9cca755..706d0849dac 100644
--- a/spec/requests/omniauth_callbacks_controller_spec.rb
+++ b/spec/requests/omniauth_callbacks_controller_spec.rb
@@ -31,7 +31,7 @@ RSpec.describe Users::OmniauthCallbacksController do
end
it "finds an authenticator when enabled" do
- SiteSetting.enable_twitter_logins = true
+ enable_auth_provider(:twitter)
expect(Users::OmniauthCallbacksController.find_authenticator("twitter")).not_to eq(nil)
end
@@ -78,7 +78,7 @@ RSpec.describe Users::OmniauthCallbacksController do
end
describe "Google Oauth2" do
- before { SiteSetting.enable_google_oauth2_logins = true }
+ before { enable_auth_provider(:google_oauth2) }
describe "#failure" do
it "defaults to the provider when only one is enabled" do
@@ -96,7 +96,7 @@ RSpec.describe Users::OmniauthCallbacksController do
end
it "uses the provider parameter when matching an enabled authenticator" do
- SiteSetting.enable_facebook_logins = true
+ enable_auth_provider(:facebook)
get "/auth/failure", params: { provider: "facebook" }
expect(response.status).to eq(200)
@@ -106,7 +106,7 @@ RSpec.describe Users::OmniauthCallbacksController do
end
it "uses the strategy parameter when matching an enabled authenticator" do
- SiteSetting.enable_facebook_logins = true
+ enable_auth_provider(:facebook)
get "/auth/failure", params: { strategy: "facebook" }
expect(response.status).to eq(200)
@@ -116,7 +116,7 @@ RSpec.describe Users::OmniauthCallbacksController do
end
it "shows a generic error message when there are more than one enabled providers" do
- SiteSetting.enable_facebook_logins = true
+ enable_auth_provider(:facebook)
get "/auth/failure"
expect(response.status).to eq(200)
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 13209903150..daafcf7fe73 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -979,7 +979,7 @@ RSpec.describe UsersController do
context "with local logins disabled" do
before do
SiteSetting.enable_local_logins = false
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
end
it "blocks registration without authenticator information" do
@@ -1462,7 +1462,7 @@ RSpec.describe UsersController do
)
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
- SiteSetting.enable_twitter_logins = true
+ enable_auth_provider(:twitter)
get "/auth/twitter/callback.json"
end
@@ -1544,7 +1544,7 @@ RSpec.describe UsersController do
info: OmniAuth::AuthHash::InfoHash.new(nickname: "testosama", name: "Osama Test"),
)
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
- SiteSetting.enable_twitter_logins = true
+ enable_auth_provider(:twitter)
get "/auth/twitter/callback.json"
end
@@ -5466,7 +5466,7 @@ RSpec.describe UsersController do
context "for an external provider" do
before do
sign_in(admin)
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
UserAssociatedAccount.create!(
user: user1,
provider_uid: "myuid",
diff --git a/spec/services/problem_check/facebook_config_spec.rb b/spec/services/problem_check/facebook_config_spec.rb
index 36823dbbf83..e36cfad8a8d 100644
--- a/spec/services/problem_check/facebook_config_spec.rb
+++ b/spec/services/problem_check/facebook_config_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe ProblemCheck::FacebookConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Facebook (enable_facebook_logins), but the app id and app secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_facebook_logins}} is on, but {{setting:facebook_app_id}} must still be set. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.',
)
end
end
@@ -48,7 +48,7 @@ RSpec.describe ProblemCheck::FacebookConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Facebook (enable_facebook_logins), but the app id and app secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_facebook_logins}} is on, but {{setting:facebook_app_secret}} must still be set. <a href="https://meta.discourse.org/t/configuring-facebook-login-for-discourse/13394" target="_blank">See this guide to learn more</a>.',
)
end
end
diff --git a/spec/services/problem_check/github_config_spec.rb b/spec/services/problem_check/github_config_spec.rb
index 1fa171ad75a..d403a9df3ba 100644
--- a/spec/services/problem_check/github_config_spec.rb
+++ b/spec/services/problem_check/github_config_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe ProblemCheck::GithubConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with GitHub (enable_github_logins), but the client id and secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_github_logins}} is on, but {{setting:github_client_id}} must still be set. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.',
)
end
end
@@ -48,7 +48,7 @@ RSpec.describe ProblemCheck::GithubConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with GitHub (enable_github_logins), but the client id and secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_github_logins}} is on, but {{setting:github_client_secret}} must still be set. <a href="https://meta.discourse.org/t/configuring-github-login-for-discourse/13745" target="_blank">See this guide to learn more</a>.',
)
end
end
diff --git a/spec/services/problem_check/google_oauth2_config_spec.rb b/spec/services/problem_check/google_oauth2_config_spec.rb
index 3f489947bd7..088e6e73197 100644
--- a/spec/services/problem_check/google_oauth2_config_spec.rb
+++ b/spec/services/problem_check/google_oauth2_config_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe ProblemCheck::GoogleOauth2Config do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Google OAuth2 (enable_google_oauth2_logins), but the client id and client secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_google_oauth2_logins}} is on, but {{setting:google_oauth2_client_id}} must still be set. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.',
)
end
end
@@ -48,7 +48,7 @@ RSpec.describe ProblemCheck::GoogleOauth2Config do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Google OAuth2 (enable_google_oauth2_logins), but the client id and client secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_google_oauth2_logins}} is on, but {{setting:google_oauth2_client_secret}} must still be set. <a href="https://meta.discourse.org/t/configuring-google-login-for-discourse/15858" target="_blank">See this guide to learn more</a>.',
)
end
end
diff --git a/spec/services/problem_check/s3_backup_config_spec.rb b/spec/services/problem_check/s3_backup_config_spec.rb
index 2d536ed657e..bb2d50d8699 100644
--- a/spec/services/problem_check/s3_backup_config_spec.rb
+++ b/spec/services/problem_check/s3_backup_config_spec.rb
@@ -60,7 +60,7 @@ RSpec.describe ProblemCheck::S3BackupConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to upload backups to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_backup_bucket. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
+ '{{setting:backup_location}} is set to S3, but {{setting:s3_secret_access_key}} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
)
end
end
@@ -72,7 +72,7 @@ RSpec.describe ProblemCheck::S3BackupConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to upload backups to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_backup_bucket. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
+ '{{setting:backup_location}} is set to S3, but {{setting:s3_backup_bucket}} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
)
end
end
diff --git a/spec/services/problem_check/s3_upload_config_spec.rb b/spec/services/problem_check/s3_upload_config_spec.rb
index 656e4d8eb7a..8111961f706 100644
--- a/spec/services/problem_check/s3_upload_config_spec.rb
+++ b/spec/services/problem_check/s3_upload_config_spec.rb
@@ -61,7 +61,7 @@ RSpec.describe ProblemCheck::S3UploadConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to upload files to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_upload_bucket. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
+ '{{setting:enable_s3_uploads}} is on, but {{setting:s3_secret_access_key}} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
)
end
end
@@ -73,7 +73,7 @@ RSpec.describe ProblemCheck::S3UploadConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to upload files to S3, but at least one the following setting is not set: s3_access_key_id, s3_secret_access_key, s3_use_iam_profile, or s3_upload_bucket. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
+ '{{setting:enable_s3_uploads}} is on, but {{setting:s3_upload_bucket}} must still be set. <a href="https://meta.discourse.org/t/how-to-set-up-image-uploads-to-s3/7229" target="_blank">See "How to set up image uploads to S3?" to learn more</a>.',
)
end
end
diff --git a/spec/services/problem_check/twitter_config_spec.rb b/spec/services/problem_check/twitter_config_spec.rb
index 9a01d92a66a..4a5d4534db8 100644
--- a/spec/services/problem_check/twitter_config_spec.rb
+++ b/spec/services/problem_check/twitter_config_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe ProblemCheck::TwitterConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Twitter (enable_twitter_logins), but the key and secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_twitter_logins}} is on, but {{setting:twitter_consumer_key}} must still be set. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.',
)
end
end
@@ -48,7 +48,7 @@ RSpec.describe ProblemCheck::TwitterConfig do
it do
expect(check).to have_a_problem.with_priority("low").with_message(
- 'The server is configured to allow signup and login with Twitter (enable_twitter_logins), but the key and secret values are not set. Go to <a href="/admin/site_settings">the Site Settings</a> and update the settings. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.',
+ '{{setting:enable_twitter_logins}} is on, but {{setting:twitter_consumer_secret}} must still be set. <a href="https://meta.discourse.org/t/configuring-twitter-login-for-discourse/13395" target="_blank">See this guide to learn more</a>.',
)
end
end
diff --git a/spec/services/user_authenticator_spec.rb b/spec/services/user_authenticator_spec.rb
index 604f1a67c9d..ef2a30bc5c5 100644
--- a/spec/services/user_authenticator_spec.rb
+++ b/spec/services/user_authenticator_spec.rb
@@ -17,7 +17,7 @@ RSpec.describe UserAuthenticator do
}
end
- before { SiteSetting.enable_github_logins = true }
+ before { enable_auth_provider(:github) }
describe "#start" do
describe "without authentication session" do
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb
index 14f60db71f9..7583f2b6189 100644
--- a/spec/support/helpers.rb
+++ b/spec/support/helpers.rb
@@ -336,9 +336,24 @@ module Helpers
def enable_current_plugin
plugin = Discourse.plugins_by_name[directory_from_caller.split("/").last]
return if plugin.enabled?
+ return if enable_auth_provider(plugin.enabled_site_setting)
SiteSetting.public_send("#{plugin.enabled_site_setting}=", true)
end
+ # Only blank settings are filled, so a spec can set anything it reads first.
+ # Ten digits is the one placeholder every credential `regex:` accepts.
+ def enable_auth_provider(name)
+ authenticator =
+ Discourse.authenticators.find { it.enable_setting == name.to_sym || it.name == name.to_s }
+ return false if authenticator.nil?
+
+ authenticator.required_settings.each do |setting|
+ SiteSetting.set(setting, "1234567890") if SiteSetting.get(setting).blank?
+ end
+ SiteSetting.set(authenticator.enable_setting, true)
+ true
+ end
+
def mock_upcoming_change_metadata(metadata)
# Without ||= here nested blocks would further mutate the instance var so
# resetting in clear_mocked_upcoming_change_metadata would not work.
diff --git a/spec/system/login_spec.rb b/spec/system/login_spec.rb
index b49144373e9..96a09c26032 100644
--- a/spec/system/login_spec.rb
+++ b/spec/system/login_spec.rb
@@ -286,7 +286,7 @@ shared_examples "login scenarios" do
before do
OmniAuth.config.test_mode = true
SiteSetting.auth_skip_create_confirm = true
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.enable_local_logins = false
end
diff --git a/spec/system/omniauth_confirm_styling_spec.rb b/spec/system/omniauth_confirm_styling_spec.rb
index 192b62b4f54..12ae8f96692 100644
--- a/spec/system/omniauth_confirm_styling_spec.rb
+++ b/spec/system/omniauth_confirm_styling_spec.rb
@@ -7,9 +7,9 @@ RSpec.describe "OmniAuth Confirm Page" do
context "with Google OAuth" do
before do
- SiteSetting.enable_google_oauth2_logins = true
SiteSetting.google_oauth2_client_id = "fake_client_id"
SiteSetting.google_oauth2_client_secret = "fake_secret"
+ SiteSetting.enable_google_oauth2_logins = true
end
it "displays the styled confirmation page with correct content" do
diff --git a/spec/system/social_authentication_spec.rb b/spec/system/social_authentication_spec.rb
index e2d50c2920e..d55ccead8e5 100644
--- a/spec/system/social_authentication_spec.rb
+++ b/spec/system/social_authentication_spec.rb
@@ -14,7 +14,7 @@ shared_examples "social authentication scenarios" do
context "when user does not exist" do
context "with Facebook" do
- before { SiteSetting.enable_facebook_logins = true }
+ before { enable_auth_provider(:facebook) }
after { reset_omniauth_config(:facebook) }
it "fills the signup form" do
@@ -34,7 +34,7 @@ shared_examples "social authentication scenarios" do
end
context "with Google" do
- before { SiteSetting.enable_google_oauth2_logins = true }
+ before { enable_auth_provider(:google_oauth2) }
after { reset_omniauth_config(:google_oauth2) }
it "fills the signup form" do
@@ -71,7 +71,7 @@ shared_examples "social authentication scenarios" do
end
context "with Github" do
- before { SiteSetting.enable_github_logins = true }
+ before { enable_auth_provider(:github) }
after { reset_omniauth_config(:github) }
it "fills the signup form" do
@@ -146,7 +146,7 @@ shared_examples "social authentication scenarios" do
end
context "with Twitter" do
- before { SiteSetting.enable_twitter_logins = true }
+ before { enable_auth_provider(:twitter) }
after { reset_omniauth_config(:twitter) }
it "fills the signup form" do
@@ -182,7 +182,7 @@ shared_examples "social authentication scenarios" do
end
context "with Discord" do
- before { SiteSetting.enable_discord_logins = true }
+ before { enable_auth_provider(:discord) }
after { reset_omniauth_config(:discord) }
it "fills the signup form" do
@@ -201,11 +201,7 @@ shared_examples "social authentication scenarios" do
end
context "with Linkedin" do
- before do
- SiteSetting.linkedin_oidc_client_id = "12345"
- SiteSetting.linkedin_oidc_client_secret = "abcde"
- SiteSetting.enable_linkedin_oidc_logins = true
- end
+ before { enable_auth_provider(:linkedin_oidc) }
after { reset_omniauth_config(:linkedin_oidc) }
@@ -226,7 +222,7 @@ shared_examples "social authentication scenarios" do
# These tests use Google, but they should be the same for all providers
context "when opening the external auth from /login" do
- before { SiteSetting.enable_google_oauth2_logins = true }
+ before { enable_auth_provider(:google_oauth2) }
after { reset_omniauth_config(:google_oauth2) }
it "fills the signup form" do
@@ -246,7 +242,7 @@ shared_examples "social authentication scenarios" do
context "when overriding local fields" do
before do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.auth_overrides_name = true
SiteSetting.auth_overrides_username = true
end
@@ -272,7 +268,7 @@ shared_examples "social authentication scenarios" do
context "when skipping the signup form" do
before do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.auth_skip_create_confirm = true
end
@@ -300,7 +296,7 @@ shared_examples "social authentication scenarios" do
context "when there is only one external login method enabled" do
before do
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:google_oauth2)
SiteSetting.enable_local_logins = false
end
@@ -442,7 +438,7 @@ shared_examples "social authentication scenarios" do
end
context "with Facebook" do
- before { SiteSetting.enable_facebook_logins = true }
+ before { enable_auth_provider(:facebook) }
after { reset_omniauth_config(:facebook) }
it "logs in user" do
@@ -505,7 +501,7 @@ shared_examples "social authentication scenarios" do
end
context "with Google" do
- before { SiteSetting.enable_google_oauth2_logins = true }
+ before { enable_auth_provider(:google_oauth2) }
after { reset_omniauth_config(:google_oauth2) }
it "logs in user" do
@@ -518,7 +514,7 @@ shared_examples "social authentication scenarios" do
end
context "with Github" do
- before { SiteSetting.enable_github_logins = true }
+ before { enable_auth_provider(:github) }
after { reset_omniauth_config(:github) }
it "logs in user" do
@@ -531,7 +527,7 @@ shared_examples "social authentication scenarios" do
end
context "with Twitter" do
- before { SiteSetting.enable_twitter_logins = true }
+ before { enable_auth_provider(:twitter) }
after { reset_omniauth_config(:twitter) }
it "logs in user" do
@@ -550,7 +546,7 @@ shared_examples "social authentication scenarios" do
end
context "with Discord" do
- before { SiteSetting.enable_discord_logins = true }
+ before { enable_auth_provider(:discord) }
after { reset_omniauth_config(:discord) }
it "logs in user" do
@@ -563,11 +559,7 @@ shared_examples "social authentication scenarios" do
end
context "with Linkedin" do
- before do
- SiteSetting.linkedin_oidc_client_id = "12345"
- SiteSetting.linkedin_oidc_client_secret = "abcde"
- SiteSetting.enable_linkedin_oidc_logins = true
- end
+ before { enable_auth_provider(:linkedin_oidc) }
after { reset_omniauth_config(:linkedin_oidc) }
@@ -582,7 +574,7 @@ shared_examples "social authentication scenarios" do
end
context "when the provider returns an error" do
- before { SiteSetting.enable_google_oauth2_logins = true }
+ before { enable_auth_provider(:google_oauth2) }
it "shows the unauthorized error message" do
visit("/auth/failure?message=unauthorized")
diff --git a/spec/system/user_page/user_preferences_account_spec.rb b/spec/system/user_page/user_preferences_account_spec.rb
index 0530e1b4b5e..9952fe5103f 100644
--- a/spec/system/user_page/user_preferences_account_spec.rb
+++ b/spec/system/user_page/user_preferences_account_spec.rb
@@ -97,10 +97,10 @@ describe "User preferences | Account" do
describe "external login provider URLs" do
it "shows provider URLs as links when available" do
- SiteSetting.enable_discord_logins = true
- SiteSetting.enable_facebook_logins = true
- SiteSetting.enable_github_logins = true
- SiteSetting.enable_google_oauth2_logins = true
+ enable_auth_provider(:discord)
+ enable_auth_provider(:facebook)
+ enable_auth_provider(:github)
+ enable_auth_provider(:google_oauth2)
# Let's connect at least 1 external account
UserAssociatedAccount.create!(