The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix password resets for Helping Hand instance access account (#74024)
Braden Shepherdsoned9fb8a0c8b5
diff --git a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.cljindex 2624e3218bd..e1fc4f7e045 100644--- a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj+++ b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj@@ -310,8 +310,8 @@(is (true? (:success response)) "Reset should succeed")(is (some? (:session_id response)) "Should return session ID"))))))))))-(deftest forgot-password-support-user-disabled-test- (testing "POST /api/session/forgot_password - Support Users cannot reset passwords"+(deftest forgot-password-support-user-no-active-grant-test+ (testing "POST /api/session/forgot_password - Support user with no active grant gets no email"(with-redefs [api.session/forgot-password-impl(let [orig @#'api.session/forgot-password-impl](fn [& args] (u/deref-with-timeout (apply orig args) 1000)))]@@ -320,8 +320,97 @@:email "[email protected]"}:model/AuthIdentity _ {:user_id (:id user):provider "support-access-grant"}]- (let [my-url "abcdefghij"]- (mt/with-temporary-setting-values [site-url my-url]- (mt/with-fake-inbox- (mt/user-http-request user :post 204 "session/forgot_password" {:email (:email user)})- (is (not (t2/exists? :model/AuthIdentity :user_id (:id user) :provider "emailed-secret-password-reset"))))))))))+ (mt/with-temporary-setting-values [site-url "http://localhost"]+ (mt/with-fake-inbox+ (mt/user-http-request user :post 204 "session/forgot_password" {:email (:email user)})+ (is (not (t2/exists? :model/AuthIdentity :user_id (:id user) :provider "emailed-secret-password-reset"))+ "Should not create a normal password reset token")+ (is (empty? @mt/inbox)+ "No email should be sent when the grant has no active credentials")))))))++(deftest forgot-password-support-user-active-grant-test+ (testing "POST /api/session/forgot_password - Support user with active grant gets a refreshed token"+ (with-redefs [api.session/forgot-password-impl+ (let [orig @#'api.session/forgot-password-impl]+ (fn [& args] (u/deref-with-timeout (apply orig args) 1000)))]+ (mt/with-temp [:model/User {creator-id :id} {}]+ (mt/with-model-cleanup [:model/SupportAccessGrantLog :model/AuthIdentity :model/User]+ (with-redefs [sag.settings/support-access-grant-email (constantly "[email protected]")+ sag.settings/support-access-grant-first-name (constantly "Support")+ sag.settings/support-access-grant-last-name (constantly "User")]+ (let [grant (grants/create-grant! creator-id 60 "TICKET-FORGOT" "Test forgot password")+ original-token (:token grant)]+ (is (some? original-token) "Grant should create a token")+ (mt/with-temporary-setting-values [site-url "http://test.example.com"]+ (mt/with-fake-inbox+ (mt/client :post 204 "session/forgot_password" {:email "[email protected]"})+ (testing "Email is sent with a valid reset URL"+ (let [emails (get @mt/inbox "[email protected]")]+ (is (= 1 (count emails)) "Should send exactly one email")+ (is (mt/received-email-body? "[email protected]"+ #"http://test.example.com/auth/reset_password/\d+_")+ "Email should contain a reset URL")))+ (testing "No normal password reset token is created"+ (let [support-user (t2/select-one :model/User :email "[email protected]")]+ (is (not (t2/exists? :model/AuthIdentity+ :user_id (:id support-user)+ :provider "emailed-secret-password-reset"))+ "Should not create a normal password reset token")))+ (testing "The refreshed token is different from the original"+ (let [support-user (t2/select-one :model/User :email "[email protected]")+ auth-identity (t2/select-one :model/AuthIdentity+ :user_id (:id support-user)+ :provider "support-access-grant")]+ (is (nil? (get-in auth-identity [:credentials :consumed_at]))+ "Token should not be marked as consumed")+ (is (some? (get-in auth-identity [:credentials :grant_ends_at]))+ "Grant end time should still be present in credentials"))))))))))))++(deftest forgot-password-then-reset-preserves-support-provider-test+ (testing "Consuming a refreshed support-access token via reset_password keeps the support-access-grant provider"+ (with-redefs [api.session/forgot-password-impl+ (let [orig @#'api.session/forgot-password-impl]+ (fn [& args] (u/deref-with-timeout (apply orig args) 1000)))]+ (mt/with-temp [:model/User {creator-id :id} {}]+ (mt/with-model-cleanup [:model/SupportAccessGrantLog :model/AuthIdentity :model/User]+ (with-redefs [sag.settings/support-access-grant-email (constantly "[email protected]")+ sag.settings/support-access-grant-first-name (constantly "Support")+ sag.settings/support-access-grant-last-name (constantly "User")]+ (let [grant (grants/create-grant! creator-id 60 "TICKET-PROVIDER" "Test provider preservation")]+ (is (some? (:token grant)) "Grant should create a token")+ (mt/with-temporary-setting-values [site-url "http://test.example.com"]+ (mt/with-fake-inbox+ ;; Step 1: Request a password reset for the support user.+ (mt/client :post 204 "session/forgot_password" {:email "[email protected]"})+ ;; Step 2: Extract the refreshed token from the email.+ (let [emails (get @mt/inbox "[email protected]")+ _ (is (= 1 (count emails)) "Should send exactly one email")+ email-body (-> emails first :body first :content)+ [_ reset-token] (re-find #"/auth/reset_password/(\d+_[\w_-]+)" email-body)+ _ (is (some? reset-token) "Should find reset token in email")+ new-password "ResetViaForgot123!"+ support-user (t2/select-one :model/User :email "[email protected]")]+ ;; Step 3: Use the token to reset the password.+ (let [response (mt/client :post 200 "session/reset_password"+ {:token reset-token :password new-password})]+ (is (true? (:success response)) "Reset should succeed")+ (is (some? (:session_id response)) "Should return session ID"))+ ;; Step 4: Verify the support-access-grant AuthIdentity is consumed, not replaced.+ (testing "support-access-grant AuthIdentity is consumed"+ (let [sag-auth (t2/select-one :model/AuthIdentity+ :user_id (:id support-user)+ :provider "support-access-grant")]+ (is (some? sag-auth) "support-access-grant AuthIdentity should still exist")+ (is (some? (get-in sag-auth [:credentials :consumed_at]))+ "Token should be marked as consumed")))+ (testing "No emailed-secret-password-reset AuthIdentity was created"+ (is (not (t2/exists? :model/AuthIdentity+ :user_id (:id support-user)+ :provider "emailed-secret-password-reset"))+ "Should not create a normal password reset AuthIdentity"))+ (testing "Password AuthIdentity has grant expiration, not unlimited"+ (let [pw-auth (t2/select-one :model/AuthIdentity+ :user_id (:id support-user)+ :provider "password")]+ (is (some? (:expires_at pw-auth))+ "Password AuthIdentity should have an expiration from the grant")))))))))))))diff --git a/src/metabase/session/api.clj b/src/metabase/session/api.cljindex 95225cac9ea..25719760c3a 100644--- a/src/metabase/session/api.clj+++ b/src/metabase/session/api.clj@@ -20,6 +20,7 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.schema :as ms]+ [metabase.util.password :as u.password][throttle.core :as throttle][toucan2.core :as t2]))@@ -164,17 +165,30 @@{:email (throttle/make-throttler :email :attempts-threshold 3 :attempt-ttl-ms 1000):ip-address (throttle/make-throttler :email :attempts-threshold 50)})-(defn- password-reset-disabled?+(defn- sso-password-reset-disabled?"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.+ so users aren't locked out."+ [sso-source]+ (and (some? sso-source)+ (sso/sso-source-enabled? sso-source)))- 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- (some? sso-source) (sso/sso-source-enabled? sso-source)- :else false))+(defn- refresh-support-access-token!+ "Refresh the reset token on an existing support-access-grant AuthIdentity, preserving the grant+ binding. Returns the new plaintext token, or nil if the grant has expired."+ [user-id]+ (when-let [auth-identity (t2/select-one :model/AuthIdentity+ :user_id user-id+ :provider "support-access-grant")]+ (let [grant-ends-at (get-in auth-identity [:credentials :grant_ends_at])]+ (when (and grant-ends-at (t/before? (t/instant) (t/instant grant-ends-at)))+ (let [token (auth-identity/generate-reset-token user-id)]+ (t2/update! :model/AuthIdentity (:id auth-identity)+ {:credentials {:token_hash (u.password/hash-bcrypt token)+ :expires_at (t/plus (t/instant) (t/hours 48))+ :grant_ends_at grant-ends-at+ :consumed_at nil}})+ token)))))(defn- forgot-password-impl[email]@@ -185,9 +199,21 @@(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)+ (cond+ ;; SSO users should use their SSO provider, not password reset.+ (sso-password-reset-disabled? sso-source)(messages/send-password-reset-email! email sso-source nil is-active?)++ ;; Support-access users get a refreshed token bound to the grant.+ ;; If the grant has expired, refresh-support-access-token! returns nil and we silently+ ;; do nothing (same as a nonexistent account).+ (t2/exists? :model/AuthIdentity :user_id user-id :provider "support-access-grant")+ (when-let [reset-token (refresh-support-access-token! user-id)]+ (let [password-reset-url (str (system/site-url) "/auth/reset_password/" reset-token)]+ (messages/send-password-reset-email! email nil password-reset-url is-active?)))++ ;; Normal password reset.+ :else(let [reset-token (auth-identity/create-password-reset! user-id)password-reset-url (str (system/site-url) "/auth/reset_password/" reset-token)](messages/send-password-reset-email! email nil password-reset-url is-active?)))