Skip to content

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

feat: endpoint for retrieve embedding token from jwt (#61540)

Roman Abdulmanov064110eaa222
diff --git a/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj b/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj
index 0d7c9049624..8275d33fb9a 100644
--- a/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj
+++ b/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj
@@ -5,7 +5,7 @@
we can have a uniform interface both via the API and code"
(:require
[metabase-enterprise.sso.api.interface :as sso.i]
- [metabase-enterprise.sso.integrations.jwt]
+ [metabase-enterprise.sso.integrations.jwt :as jwt]
[metabase-enterprise.sso.integrations.saml]
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.api.macros :as api.macros]
@@ -15,6 +15,7 @@
[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]
[toucan2.core :as t2]))
@@ -22,7 +23,8 @@
(set! *warn-on-reflection* true)
;; load the SSO integrations so their implementations for the multimethods below are available.
-(comment metabase-enterprise.sso.integrations.jwt/keep-me
+;; `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
@@ -90,6 +92,19 @@
:relay-state (u/encode-base64
(str (urls/site-url) metabase-slo-redirect-url))))}))
+;; POST /auth/sso/to_session
+(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 [:jwt ms/NonBlankString]]
+ request]
+ (when-not (sso-settings/jwt-enabled)
+ (throw (ex-info "JWT authentication is not enabled"
+ {:status-code 400})))
+ {:session_token (jwt/jwt->session jwt request)})
+
;; POST /auth/sso/handle_slo
(api.macros/defendpoint :post "/handle_slo"
"Handles client confirmation of saml logout via slo"
diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/jwt.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/jwt.clj
index 90ac2ef38fb..40eb08db243 100644
--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/jwt.clj
+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/jwt.clj
@@ -107,8 +107,7 @@
(catch Throwable e
(throw
(ex-info (ex-message e)
- {:status "error-jwt-bad-unsigning"
- :status-code 401}))))
+ {:status-code 401}))))
login-attrs (jwt-data->login-attributes jwt-data)
email (get jwt-data (jwt-attribute-email))
first-name (get jwt-data (jwt-attribute-firstname))
@@ -118,6 +117,11 @@
(sync-groups! user jwt-data)
{:session session, :redirect-url redirect-url, :jwt-data jwt-data})))
+(defn jwt->session
+ "Given a JWT, return a valid session token for the associated user (creating the user if necessary)."
+ [jwt request]
+ (-> (session-data jwt request) :session :key))
+
(defn- throw-react-sdk-embedding-disabled
[]
(throw
diff --git a/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj b/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj
index f80d1f2142f..ab213d63092 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj
@@ -1,11 +1,20 @@
(ns metabase-enterprise.sso.api.sso-test
(:require
+ [buddy.sign.jwt :as jwt]
+ [clojure.string :as str]
[clojure.test :refer :all]
+ [crypto.random :as crypto-random]
+ [metabase.config.core :as config]
[metabase.request.core :as request]
[metabase.session.core :as session]
[metabase.test :as mt]
+ [metabase.test.fixtures :as fixtures]
[metabase.test.http-client :as client]))
+(set! *warn-on-reflection* true)
+
+(use-fixtures :once (fixtures/initialize :test-users))
+
(deftest saml-logout
(testing "with slo enabled and configured"
(mt/with-premium-features #{:sso-saml}
@@ -44,3 +53,60 @@
"W6HSXlA%2FP2W1xFSMV7isYbynM%2BTPZ1vp88zQCpb0xVzDWt"
"%2FYw11yAqadb8%3D")
(:saml-logout-url response))))))))))))))
+
+(def ^:private default-jwt-secret (crypto-random/hex 32))
+
+(defn- with-jwt-settings! [f]
+ (mt/with-additional-premium-features #{:sso-jwt}
+ (mt/with-temporary-setting-values
+ [jwt-enabled true
+ jwt-identity-provider-uri "http://test.idp.metabase.com"
+ jwt-shared-secret default-jwt-secret
+ site-url (format "http://localhost:%s" (config/config-str :mb-jetty-port))]
+ (f))))
+
+(defn- create-valid-jwt-token
+ ([]
+ (create-valid-jwt-token {}))
+ ([claims]
+ (let [now (int (/ (System/currentTimeMillis) 1000))
+ default-claims {:email "[email protected]"
+ :first_name "Test"
+ :last_name "User"
+ :iat now
+ :exp (+ now 3600)}]
+ (jwt/sign (merge default-claims claims) default-jwt-secret))))
+
+(deftest jwt-to-session-test
+ (testing "POST /auth/sso/to_session"
+ (binding [client/*url-prefix* ""]
+ (with-jwt-settings!
+ (fn []
+ (testing "successful JWT to session conversion"
+ (let [jwt-token (create-valid-jwt-token)
+ response (mt/client :post 200 "/auth/sso/to_session" {:jwt jwt-token})]
+ (is (string? (:session_token response)))))
+ (testing "missing JWT token"
+ (is (=? {:errors {:jwt "value must be a non-blank string."}}
+ (mt/client :post 400 "/auth/sso/to_session" {}))))
+ (testing "JWT disabled"
+ (mt/with-temporary-setting-values [jwt-enabled false]
+ (let [jwt-token (create-valid-jwt-token)]
+ (is (= "JWT authentication is not enabled"
+ (mt/client :post 400 "/auth/sso/to_session" {:jwt jwt-token}))))))
+ (testing "invalid JWT token"
+ (is (= "Message seems corrupt or manipulated"
+ (mt/client :post 401 "/auth/sso/to_session" {:jwt "invalid-token"}))))
+ (testing "expired JWT token"
+ (let [expired-token (create-valid-jwt-token {:exp (- (int (/ (System/currentTimeMillis) 1000)) 3600)})]
+ (is (str/starts-with? (mt/client :post 401 "/auth/sso/to_session" {:jwt expired-token})
+ "Token is expired"))))
+ (testing "JWT with different secret"
+ (let [wrong-secret-token (jwt/sign {:email "[email protected]"
+ :first_name "Test"
+ :last_name "User"
+ :iat (int (/ (System/currentTimeMillis) 1000))
+ :exp (+ (int (/ (System/currentTimeMillis) 1000)) 3600)}
+ "wrong-secret")]
+ (is (= "Message seems corrupt or manipulated"
+ (mt/client :post 401 "/auth/sso/to_session" {:jwt wrong-secret-token}))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
index 699146ccf5d..a4108be5f00 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
@@ -296,16 +296,15 @@
(with-jwt-default-setup!
(is
(= "Token is older than max-age (180)"
- (:message
- (client/client :get 401 "/auth/sso" {:request-options {:redirect-strategy :none}}
- :return_to default-redirect-uri
- :jwt
- (jwt/sign
- {:email "[email protected]",
- :first_name "Test"
- :last_name "User"
- :iat (- (buddy-util/now) (u/minutes->seconds 5))}
- default-jwt-secret))))))))
+ (client/client :get 401 "/auth/sso" {:request-options {:redirect-strategy :none}}
+ :return_to default-redirect-uri
+ :jwt
+ (jwt/sign
+ {:email "[email protected]",
+ :first_name "Test"
+ :last_name "User"
+ :iat (- (buddy-util/now) (u/minutes->seconds 5))}
+ default-jwt-secret)))))))
(defmacro with-users-with-email-deleted {:style/indent 1} [user-email & body]
`(try