Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

SSO users can reset password if provider disabled (#70219)

John Swanson330fb2727e26
diff --git a/enterprise/backend/test/metabase_enterprise/sso/settings_test.clj b/enterprise/backend/test/metabase_enterprise/sso/settings_test.clj
index 32a869eb5ab..8c816527cd9 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/settings_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/settings_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.session.core :as session]
+ [metabase.sso.settings :as sso.settings]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.test.util :as tu]))
@@ -222,3 +223,39 @@
(session/enable-password-login! false)
(is (= false
(session/enable-password-login))))))))
+
+(deftest sso-source-enabled?-saml-test
+ (testing "sso-source-enabled? for SAML"
+ (mt/with-premium-features #{:sso-saml}
+ (tu/with-temporary-setting-values [saml-identity-provider-uri default-idp-uri
+ saml-identity-provider-certificate default-idp-cert
+ saml-enabled true]
+ (testing "returns true when SAML is configured, enabled, and licensed"
+ (is (true? (sso.settings/sso-source-enabled? :saml))))
+ (testing "returns false when SAML feature is unlicensed (e.g., license downgrade)"
+ (mt/with-premium-features #{}
+ (is (false? (sso.settings/sso-source-enabled? :saml)))))))))
+
+(deftest sso-source-enabled?-jwt-test
+ (testing "sso-source-enabled? for JWT"
+ (mt/with-premium-features #{:sso-jwt}
+ (tu/with-temporary-setting-values [jwt-identity-provider-uri default-idp-uri
+ jwt-shared-secret "01234"
+ jwt-enabled true]
+ (testing "returns true when JWT is configured, enabled, and licensed"
+ (is (true? (sso.settings/sso-source-enabled? :jwt))))
+ (testing "returns false when JWT feature is unlicensed (e.g., license downgrade)"
+ (mt/with-premium-features #{}
+ (is (false? (sso.settings/sso-source-enabled? :jwt)))))))))
+
+(deftest sso-source-enabled?-google-test
+ (testing "sso-source-enabled? for Google"
+ (mt/with-temporary-setting-values [google-auth-client-id "pretend-client-id.apps.googleusercontent.com"
+ google-auth-enabled true]
+ (is (true? (sso.settings/sso-source-enabled? :google))))
+ (mt/with-temporary-setting-values [google-auth-enabled false]
+ (is (false? (sso.settings/sso-source-enabled? :google))))))
+
+(deftest sso-source-enabled?-unknown-test
+ (testing "sso-source-enabled? returns false for unknown sources"
+ (is (false? (sso.settings/sso-source-enabled? :unknown-provider)))))
diff --git a/src/metabase/session/api.clj b/src/metabase/session/api.clj
index 96b65f98220..bba7d639409 100644
--- a/src/metabase/session/api.clj
+++ b/src/metabase/session/api.clj
@@ -165,15 +165,16 @@
:ip-address (throttle/make-throttler :email :attempts-threshold 50)})
(defn- password-reset-disabled?
- "Disable password reset for all users created with SSO logins, unless those Users were created with Google SSO
- in which case disable reset for them as long as the Google SSO feature is enabled.
+ "Disable password reset for users whose SSO provider is still active — they should use SSO.
+ When a provider is no longer available (e.g., after license downgrade), allow password reset
+ so users aren't locked out.
- Disable password reset for any support users -- users with a auth-identity of type `support-access-request`."
+ Always disable password reset for support-access users."
[user-id sso-source]
(cond
(t2/exists? :model/AuthIdentity :user_id user-id :provider "support-access-grant") true
- (and (= sso-source :google) (not (sso/sso-enabled?))) (sso/google-auth-enabled)
- :else (some? sso-source)))
+ (some? sso-source) (sso/sso-source-enabled? sso-source)
+ :else false))
(defn- forgot-password-impl
[email]
@@ -184,9 +185,8 @@
(t2/select-one [:model/User :id :sso_source :is_active]
:%lower.email
(u/lower-case-en email))]
+ ;; If user uses any *enabled* SSO method to log in, no need to generate a reset token.
(if (password-reset-disabled? user-id sso-source)
- ;; If user uses any SSO method to log in, no need to generate a reset token. Some cases for Google SSO
- ;; are exempted see `password-reset-allowed?`
(messages/send-password-reset-email! email sso-source nil is-active?)
(let [reset-token (auth-identity/create-password-reset! user-id)
password-reset-url (str (system/site-url) "/auth/reset_password/" reset-token)]
diff --git a/src/metabase/sso/core.clj b/src/metabase/sso/core.clj
index e415aaada81..f20e7977a09 100644
--- a/src/metabase/sso/core.clj
+++ b/src/metabase/sso/core.clj
@@ -25,7 +25,8 @@
google-auth-enabled
ldap-enabled
send-new-sso-user-admin-email?
- sso-enabled?])
+ sso-enabled?
+ sso-source-enabled?])
#_{:clj-kondo/ignore [:missing-docstring]}
(p/import-def metabase.sso.ldap.default-implementation/UserInfo LDAPUserInfo)
diff --git a/src/metabase/sso/settings.clj b/src/metabase/sso/settings.clj
index fade67b3591..d611ba8f869 100644
--- a/src/metabase/sso/settings.clj
+++ b/src/metabase/sso/settings.clj
@@ -208,6 +208,28 @@
(ldap-enabled)
(ee-sso-configured?)))
+(defn sso-source-enabled?
+ "Returns true if the given SSO source's authentication provider is currently enabled.
+ When a provider is disabled (e.g., after license downgrade), returns false so users
+ can fall back to password authentication."
+ [sso-source]
+ (boolean
+ (case (keyword sso-source)
+ :google (google-auth-enabled)
+ :ldap (ldap-enabled)
+ ;; Enterprise SSO providers: setting/get respects the :feature flag on each
+ ;; setting — returning the default (false) when the feature is unlicensed (e.g.,
+ ;; after license downgrade), so users aren't locked out of password reset.
+ ;; Exception: :scim-enabled has no :feature guard, so it returns the stored value
+ ;; regardless of license status.
+ :saml (setting/get :saml-enabled)
+ :jwt (setting/get :jwt-enabled)
+ :oidc (setting/get :oidc-enabled?)
+ :slack (setting/get :slack-connect-enabled)
+ :scim (setting/get :scim-enabled)
+ ;; Unknown sso_source -- treat as disabled to allow password reset
+ false)))
+
(define-multi-setting google-auth-auto-create-accounts-domain
(deferred-tru "When set, allow users to sign up on their own if their Google account email address is from this domain.")
(fn [] (if (premium-features/enable-sso-google?) :ee :oss))
diff --git a/test/metabase/session/api_test.clj b/test/metabase/session/api_test.clj
index db40180947d..5dab846c1ec 100644
--- a/test/metabase/session/api_test.clj
+++ b/test/metabase/session/api_test.clj
@@ -13,6 +13,7 @@
[metabase.settings.models.setting]
[metabase.sso.ldap-test-util :as ldap.test]
[metabase.sso.ldap.default-implementation]
+ [metabase.sso.settings]
[metabase.test :as mt]
[metabase.test.data.users :as test.users]
[metabase.test.fixtures :as fixtures]
@@ -311,6 +312,48 @@
(:email g-user)
(re-pattern "Click the button below to reset the password"))))))))))
+(deftest forgot-password-ee-sso-user-can-reset-when-provider-disabled-test
+ (testing "POST /api/session/forgot_password - enterprise SSO users can reset password when their provider is disabled"
+ (doseq [sso-source [:saml :jwt :oidc :slack :scim]]
+ (testing (str "sso_source = " sso-source)
+ ;; Mock sso-source-enabled? to return false (provider is disabled, e.g., after downgrade)
+ (with-redefs [api.session/forgot-password-impl
+ (let [orig @#'api.session/forgot-password-impl]
+ (fn [& args] (u/deref-with-timeout (apply orig args) 1000)))
+ metabase.sso.settings/sso-source-enabled? (constantly false)]
+ (mt/with-temp [:model/User sso-user {:first_name "sso"
+ :last_name "user"
+ :email (str (name sso-source) "[email protected]")
+ :sso_source sso-source}]
+ (mt/with-temporary-setting-values [site-url "http://test.example.com"]
+ (mt/with-fake-inbox
+ (mt/user-http-request sso-user :post 204 "session/forgot_password"
+ {:email (:email sso-user)})
+ (is (mt/received-email-body?
+ (:email sso-user)
+ (re-pattern "Click the button below to reset the password")))))))))))
+
+(deftest forgot-password-ee-sso-user-cannot-reset-when-provider-enabled-test
+ (testing "POST /api/session/forgot_password - enterprise SSO users cannot reset password when their provider is enabled"
+ (doseq [sso-source [:saml :jwt :oidc :slack :scim]]
+ (testing (str "sso_source = " sso-source)
+ ;; Mock sso-source-enabled? to return true (provider is active)
+ (with-redefs [api.session/forgot-password-impl
+ (let [orig @#'api.session/forgot-password-impl]
+ (fn [& args] (u/deref-with-timeout (apply orig args) 1000)))
+ metabase.sso.settings/sso-source-enabled? (fn [src] (= (keyword src) sso-source))]
+ (mt/with-temp [:model/User sso-user {:first_name "sso"
+ :last_name "user"
+ :email (str (name sso-source) "[email protected]")
+ :sso_source sso-source}]
+ (mt/with-temporary-setting-values [site-url "http://test.example.com"]
+ (mt/with-fake-inbox
+ (mt/user-http-request sso-user :post 204 "session/forgot_password"
+ {:email (:email sso-user)})
+ (is (mt/received-email-body?
+ (:email sso-user)
+ (re-pattern "single sign-on")))))))))))
+
(deftest forgot-password-event-test
(mt/with-premium-features #{:audit-app}
(with-redefs [api.session/forgot-password-impl