Skip to content

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

2.9 KB
Raw Blame
(ns metabase-enterprise.sso.api.interface
  (:require
   [metabase-enterprise.sso.settings :as ee-sso-settings]
   [metabase.util.i18n :refer [tru]]))

(defn request-jwt
  "JWT from the SSO request. GET reads `:params` (query string). POST reads JSON `:body` only."
  [req]
  (case (:request-method req)
    :get  (get-in req [:params :jwt])
    :post (or (get-in req [:body "jwt"])
              (get-in req [:body :jwt]))
    nil))

(defn- select-sso-backend
  [req]
  (let [preferred-method (get-in req [:params :preferred_method])]
    (cond
      preferred-method (case preferred-method
                         "jwt"  :jwt
                         "saml" :saml
                         (throw (ex-info "Invalid auth method"
                                         {:preferred-method preferred-method
                                          :available        [:jwt :saml]})))
      (some? (request-jwt req)) :jwt
      :else :saml)))

(defn- sso-backend
  "Function that powers the defmulti in figuring out which SSO backend to use. It might be that we need to have more
  complex logic around this, but now it's just a simple priority. If multiple SSO methods are enabled, uses
  preferred_method parameter if provided."
  [req]
  (let [enabled-count (count (filter identity
                                     [(ee-sso-settings/saml-enabled)
                                      (ee-sso-settings/jwt-enabled-and-configured)]))]
    (cond
      ;; Multiple SSO methods enabled - use preferred_method or selection logic
      (> enabled-count 1) (select-sso-backend req)

      ;; Single SSO method enabled
      (ee-sso-settings/saml-enabled) :saml
      (ee-sso-settings/jwt-enabled-and-configured)  :jwt

      ;; No SSO method enabled
      :else nil)))

(defmulti sso-get
  "Multi-method for supporting the first part of an SSO signin request. An implementation of this method will usually
  result in a redirect to an SSO backend"
  {:arglists '([request])}
  sso-backend)

(defmulti sso-post
  "Multi-method for supporting a POST-back from an SSO signin request. An implementation of this method will need to
  validate the POST from the SSO backend and successfully log the user into Metabase."
  {:arglists '([request])}
  sso-backend)

(defmulti sso-handle-slo
  "Multi-method for handling a SLO request from an SSO backend. An implementation of this method will need to validate
  the SLO request and log the user out of Metabase."
  {:arglists '([request])}
  sso-backend)

(defn- throw-not-configured-error []
  (throw
   (ex-info (tru "SSO has not been enabled and/or configured")
            {:status-code   400
             :status        "error-sso-disabled"
             :response/keys #{:status}})))

(defmethod sso-get :default
  [_]
  (throw-not-configured-error))

(defmethod sso-post :default
  [_]
  (throw-not-configured-error))

(defmethod sso-handle-slo :default
  [_]
  (throw-not-configured-error))