Skip to content

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

5.8 KB
Raw Blame
(ns metabase-enterprise.support-access-grants.provider
  "Auth provider for support access grants with time-limited passwords.

  This provider extends the emailed-secret provider to support time-limited passwords for customer support access.
  When a support access grant is active, password reset tokens created for support access will track the grant's
  end time and automatically set the user's password to expire when the grant expires."
  (:require
   [java-time.api :as t]
   [metabase-enterprise.support-access-grants.db :as support-access-grants.db]
   [metabase-enterprise.support-access-grants.schema :as schema]
   [metabase.auth-identity.core :as auth-identity]
   [metabase.util :as u]
   [metabase.util.log :as log]
   [metabase.util.malli :as mu]
   [metabase.util.malli.schema :as ms]
   [metabase.util.password :as u.password]
   [methodical.core :as methodical]
   [toucan2.core :as t2]))

(set! *warn-on-reflection* true)

(mu/defn create-support-access-reset-token-credentials
  "Creates a credentials map for a support access password reset token with grant end time tracking.

  Similar to [[emailed-secret/create-reset-token-credentials]], but adds a `:grant_ends_at` timestamp that
  tracks when the support access grant expires. The token itself expires in 48 hours (or as specified), but
  the grant end time is used to set the password expiration when the user logs in.

  Parameters:
  - `token`: The plaintext reset token that will be hashed using bcrypt
  - `grant-ends-at`: The instant when the support access grant expires
  - `expires-in-ms`: Optional token TTL in milliseconds (defaults to 48 hours)

  Returns a map containing:
  - `:token_hash` - The bcrypt hash of the token
  - `:expires_at` - When the token itself expires
  - `:grant_ends_at` - When the support access grant expires
  - `:consumed_at` - Initially nil, set when token is used"
  [token :- :string
   grant-ends-at :- ::schema/timestamp
   & {:keys [expires-in-ms]
      :or {expires-in-ms (* 48 60 60 1000)}} :- [:maybe [:map {:closed true} [:expires-in-ms {:optional true} [:maybe :int]]]]]
  {:token_hash (u.password/hash-bcrypt token)
   :expires_at (t/plus (t/instant) (t/millis expires-in-ms))
   :grant_ends_at grant-ends-at
   :consumed_at nil})

(mu/defn create-support-access-reset!
  "Creates or updates a support access password reset token for a user and returns the plaintext token.

  Generates a new reset token for the user identified by `user-id` and stores it in an [[AuthIdentity]] record with
  provider `support-access-grant`. The token includes the support access grant's end time, which will be used to set
  the user's password expiration when they log in.

  Parameters:
  - `user-id`: The ID of the user to create a password reset token for
  - `grant`: The support access grant map containing at minimum `:grant_end_timestamp`

  Throws an exception if:
  - The grant is nil
  - The grant has expired

  Returns the plaintext token string that should be sent to the user via email."
  [user-id :- ms/PositiveInt
   grant :- [:maybe [:or ::schema/support-access-grant-log ::schema/grant-response]]]
  (when-not grant
    (throw (ex-info "Cannot create support access reset token: no grant provided"
                    {:status-code 400})))
  (let [grant-ends-at (:grant_end_timestamp grant)]
    (when (t/before? grant-ends-at (t/offset-date-time))
      (throw (ex-info "Cannot create support access reset token: grant has expired"
                      {:status-code 400})))
    (u/prog1 (auth-identity/generate-reset-token user-id)
      (t2/with-transaction [_]
        (let [user (support-access-grants.db/user user-id)
              auth-identity {:user_id user-id
                             :provider "support-access-grant"
                             :provider_id (:email user)
                             :expires_at grant-ends-at
                             :credentials (create-support-access-reset-token-credentials <> grant-ends-at)
                             :metadata (auth-identity/create-reset-token-metadata (:email user))}]
          (if-let [auth-identity-id (support-access-grants.db/support-access-auth-identity-id user-id)]
            (support-access-grants.db/update-auth-identity! auth-identity-id auth-identity)
            (support-access-grants.db/insert-auth-identity! auth-identity)))))))

;;; -------------------------------------------------- Provider Registration --------------------------------------------------

;; Register support-access-grant provider in the hierarchy, deriving from emailed-secret
(auth-identity/derive! :provider/support-access-grant :provider/emailed-secret)

;;; -------------------------------------------------- Multimethod Implementations --------------------------------------------------

(methodical/defmethod auth-identity/login! :after :provider/support-access-grant
  "Completes the support access password reset process after successful authentication.

  After a successful support access authentication, this method:
  - Marks the reset token as consumed in the [[AuthIdentity]]
  - Updates the user's password
  - Sets the user's expires_at based on the grant's end time

  All operations are performed within a transaction to ensure consistency."
  [_provider {:keys [user password auth-identity] :as result}]
  (when (:success? result)
    (let [grant-ends-at (get-in auth-identity [:credentials :grant_ends_at])]
      (log/infof "Setting password expiration for user %s to %s based on support access grant"
                 (:id user) grant-ends-at)
      (t2/with-transaction [_]
        (support-access-grants.db/update-auth-identity! (:id auth-identity) (auth-identity/mark-token-consumed auth-identity))
        ;; the support user is who is changing their own password here, so they are who ends their sessions
        (auth-identity/set-password! (:id user) password {:expires-at grant-ends-at, :ended-by (:id user)}))))
  result)