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
(ns metabase-enterprise.sso.api.sso
"`/auth/sso` Routes.
Implements the SSO routes needed for SAML and JWT. This namespace primarily provides hooks for those two backends so
we can have a uniform interface both via the API and code"
(:require
[metabase-enterprise.sso.api.interface :as sso.i]
[metabase-enterprise.sso.db :as sso.db]
[metabase-enterprise.sso.integrations.jwt :as jwt]
[metabase-enterprise.sso.integrations.oidc :as oidc-integration]
[metabase-enterprise.sso.integrations.saml]
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.api.macros :as api.macros]
[metabase.request.core :as request]
[metabase.session.core :as session]
[metabase.system.core :as system]
[metabase.util :as u]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[saml20-clj.core :as saml]
[stencil.core :as stencil]))
(set! *warn-on-reflection* true)
;; load the SSO integrations so their implementations for the multimethods below are available.
;; `jwt` is actually used here, but keeping it below to document the requirement in case it's removed someday
(comment jwt/keep-me
metabase-enterprise.sso.integrations.saml/keep-me)
;; GET /auth/sso
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema
:metabase/validate-defendpoint-query-params-use-kebab-case]}
(api.macros/defendpoint :get "/"
"SSO entry-point for an SSO user that has not logged in yet"
[_route-params
_query-params :- [:map {:closed true}
[:jwt {:optional true} [:maybe :string]]
[:preferred_method {:optional true} [:maybe :string]]
[:redirect {:optional true} [:maybe :string]]
[:return_to {:optional true} [:maybe :string]]]
_body request]
(try
(sso.i/sso-get request)
(catch Throwable e
(log/error #_e "Error returning SSO entry point")
(throw e))))
(mu/defn- sso-error-page
[^Throwable e :- (ms/InstanceOfClass Throwable) log-direction :- [:enum :in :out]]
{:status (get (ex-data e) :status-code 500)
:headers {"Content-Type" "text/html"}
:body (stencil/render-file "metabase_enterprise/sandbox/api/error_page"
(let [message (.getMessage e)
data (u/pprint-to-str (ex-data e))]
{:logDirection (name log-direction)
:errorMessage message
:exceptionClass (.getName Exception)
:additionalData data}))})
;; POST /auth/sso
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/"
"Route the SSO backends call with successful login details"
[_route-params
_query-params :- [:map {:closed true}
[:SAMLResponse {:optional true} [:maybe :string]]
[:RelayState {:optional true} [:maybe :string]]]
_body :- [:maybe [:map {:closed true}
[:jwt {:optional true} [:maybe :string]]
[:SAMLResponse {:optional true} [:maybe :string]]
[:RelayState {:optional true} [:maybe :string]]]]
request]
(try
(sso.i/sso-post request)
(catch Throwable e
(log/errorf "Error logging in: %s" (ex-message e))
(sso-error-page e :in))))
;; ------------------------------ Single Logout aka SLO ------------------------------
(def metabase-slo-redirect-url
"The url that the IdP should respond to. Not all IdPs support this, but it's a good idea to send it just in case."
"/auth/sso/handle_slo")
;; POST /auth/sso/logout
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/logout"
"Logout."
[_route-params _query-params _body {cookies :cookies, :as _request}]
(let [metabase-session-key (get-in cookies [request/metabase-session-cookie :value])
metabase-session-key-hashed (session/hash-session-key metabase-session-key)
{:keys [email sso_source saml_session_index saml_name_id saml_name_id_format]}
(sso.db/session-user-email-and-source metabase-session-key-hashed)]
;; If a user doesn't have SLO setup on their IdP,
;; they will never hit "/handle_slo" so we must end the session here:
(when-not (sso-settings/saml-slo-enabled)
(session/end-sessions! {:key_hashed metabase-session-key-hashed} "sso-logout" :self))
{:saml-logout-url
(when (and (sso-settings/saml-slo-enabled)
(= sso_source "saml"))
(saml/logout-redirect-location
:credential (metabase-enterprise.sso.integrations.saml/sp-cert-keystore-details)
:idp-url (sso-settings/saml-identity-provider-slo-uri)
:issuer (sso-settings/saml-application-name)
;; Name the subject the way the IdP does. Its NameID is not necessarily the user's email
;; - Auth0 sends an opaque `auth0|<id>` - and a LogoutRequest naming an identifier the IdP
;; never issued matches no session. Fall back to email for sessions predating this column.
:user-email (or saml_name_id email)
:name-id-format saml_name_id_format
;; Identifies which session to end. IdPs that map several sessions to one subject (Auth0)
;; reject a LogoutRequest without it. Omitted when we have none, which keeps the request
;; unchanged for IdPs that never sent one.
:session-index saml_session_index
:relay-state (u/encode-base64
(str (system/site-url) metabase-slo-redirect-url))))}))
;; POST /auth/sso/to_session
;;
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-route-uses-kebab-case
:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/to_session"
"If a user wants to simply convert a JWT to a session token (which they'll manage)
this provides a path for them to do so."
[_route-params
_query-params
{:keys [jwt]} :- [:map {:closed true}
[:jwt ms/NonBlankString]]
request]
(when-not (sso-settings/jwt-enabled-and-configured)
(throw (ex-info "JWT authentication is not enabled"
{:status-code 400})))
{:session_token (jwt/jwt->session jwt request)})
;; POST /auth/sso/handle_slo
;;
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-route-uses-kebab-case
:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/handle_slo"
"Handles client confirmation of saml logout via slo"
[_route-params
_query-params :- [:map {:closed true}
[:SAMLRequest {:optional true} [:maybe :string]]
[:SAMLResponse {:optional true} [:maybe :string]]
[:RelayState {:optional true} [:maybe :string]]]
_body :- [:maybe [:map {:closed true}
[:SAMLRequest {:optional true} [:maybe :string]]
[:SAMLResponse {:optional true} [:maybe :string]]
[:RelayState {:optional true} [:maybe :string]]]]
request]
(try
(if (sso-settings/saml-slo-enabled)
(sso.i/sso-handle-slo request)
(throw (ex-info "SAML Single Logout is not enabled, request forbidden."
{:status-code 403})))
(catch Throwable e
(log/errorf "Error handling SLO: %s" (ex-message e))
(sso-error-page e :out))))
;; Key schema that excludes `/` so /:key does not greedily match /:key/callback
(def ^:private ProviderKey
[:string {:api/regex #"[a-z0-9][a-z0-9-]*"}])
;; GET /auth/sso/:key
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :get "/:key"
"Initiate OIDC SSO for a specific provider."
[{provider-key :key} :- [:map {:closed true}
[:key ProviderKey]]
_query-params :- [:map {:closed true}
[:redirect {:optional true} [:maybe :string]]]
_body request]
(try
(oidc-integration/sso-initiate provider-key request)
(catch Throwable e
(log/errorf "Error initiating OIDC SSO: %s" (ex-message e))
(throw e))))
;; GET /auth/sso/:key/callback
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :get "/:key/callback"
"OIDC callback for a specific provider."
[{provider-key :key} :- [:map {:closed true}
[:key ProviderKey]]
_query-params :- [:map {:closed true}
[:code {:optional true} [:maybe :string]]
[:state {:optional true} [:maybe :string]]]
_body request]
(try
(oidc-integration/sso-callback provider-key request)
(catch Throwable e
(log/errorf "Error handling OIDC callback: %s" (ex-message e))
(throw e))))