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
Accept JWT in the body or in parameters according to the method (#75656)
da6108e0182ee8
diff --git a/docs/embedding/authentication.md b/docs/embedding/authentication.mdindex c4e214cf64b..f305791ecd8 100644--- a/docs/embedding/authentication.md+++ b/docs/embedding/authentication.md@@ -281,6 +281,8 @@ app.get("/sso/metabase", async (req, res) => {});```+For modular embedding, Metabase exchanges the JWT via `POST /auth/sso` with a JSON body (not a `GET` redirect). Full app embedding typically uses the `GET` redirect shown above. See [JWT-based authentication](../people-and-groups/authenticating-with-jwt.md).+## Embedding Metabase in a different domain{% include plans-blockquote.html feature="Authenticated modular embeds" convert_pro_link_to_embedding=true is_plural=true %}diff --git a/docs/embedding/full-app-embedding.md b/docs/embedding/full-app-embedding.mdindex 13020ba248c..78ff0f6e808 100644--- a/docs/embedding/full-app-embedding.md+++ b/docs/embedding/full-app-embedding.md@@ -109,6 +109,8 @@ If you're using [JWT](../people-and-groups/authenticating-with-jwt.md), you canhttps://metabase.example.com/auth/sso?jwt=<token>&return_to=%2Fdashboard%2F1```+To avoid putting the JWT in the URL, you can also authenticate with a `POST` request and a JSON body. See [JWT-based authentication](../people-and-groups/authenticating-with-jwt.md).+You must URL encode (or double encode, depending on your web setup) all of the parameters in your redirect link, including parameters for filters (e.g., `filter=value`) and [UI settings](#showing-or-hiding-metabase-ui-components) (e.g., `top_nav=true`). For example, if you added two filter parameters to the JWT example shown above, your `src` link would become:```diff --git a/docs/people-and-groups/authenticating-with-jwt.md b/docs/people-and-groups/authenticating-with-jwt.mdindex 3e7ec686437..a0545fab44a 100644--- a/docs/people-and-groups/authenticating-with-jwt.md+++ b/docs/people-and-groups/authenticating-with-jwt.md@@ -22,7 +22,30 @@ Assuming your site is localhost serving on port 3000:5. In the event of a successful sign-in, your authentication app should issue a GET request to your Metabase endpoint with the token and the "return to" URI: `http://localhost:3000/auth/sso?jwt=TOKEN_GOES_HERE&return_to=/question/1-superb-question`.6. Metabase verifies the JSON Web Token, logs the person in, then redirects the person to their original destination, `/question/1-superb-question`.-As an alternative to putting the JWT in the URL query string (which some environments log or cache), you can send the same token in the body of a `POST` request to `/auth/sso` with `Content-Type: application/json` and a JSON body like `{"jwt": "TOKEN_GOES_HERE"}`. You can still pass `return_to` as a query parameter on that URL (for example `POST /auth/sso?return_to=/question/1-superb-question`). The login behavior is otherwise the same as the GET flow.+- For full app embeds, use a `GET` request.+- For modular embeds use a `POST` request with a JSON body (to avoid putting the JWT in the URL). You'll also want to use `POST` requests in contexts that log or cache URLs, such as server-side integrations or testing.++In both cases the login behavior is the same, and you can pass `return_to` as a query parameter in both `GET` and `POST` requests.++An example `POST` request with curl:++```bash+curl -X POST "http://localhost:3000/auth/sso?return_to=/question/1-superb-question" \+ -H "Content-Type: application/json" \+ -d '{"jwt": "TOKEN_GOES_HERE"}'+```++Or with JavaScript:++```js+await fetch(`${METABASE_URL}/auth/sso?return_to=/question/1-superb-question`, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ jwt: token }),+ credentials: "include",+});+```+## Set up JWT authenticationdiff --git a/enterprise/backend/src/metabase_enterprise/sso/api/interface.clj b/enterprise/backend/src/metabase_enterprise/sso/api/interface.cljindex dd7678fabf1..18079d5be44 100644--- a/enterprise/backend/src/metabase_enterprise/sso/api/interface.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/api/interface.clj@@ -4,10 +4,12 @@[metabase.util.i18n :refer [tru]]))(defn request-jwt- "JWT from the request `:params` (which includes both query string and form body) or JSON body (`:body`), in that order."+ "JWT from the SSO request. GET reads `:params` (query string). POST reads JSON `:body` only."[req]- (or (get-in req [:params :jwt])- (get-in req [:body :jwt])))+ (case (:request-method req)+ :get (get-in req [:params :jwt])+ :post (get-in req [:body :jwt])+ nil))(defn- select-sso-backend[req]diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.cljindex 6a22edf5fdf..3818666b505 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj@@ -4,6 +4,7 @@[buddy.sign.util :as buddy-util][clojure.string :as str][clojure.test :refer :all]+ [metabase-enterprise.sso.api.interface :as sso.i][metabase-enterprise.sso.integrations.token-utils :as token-utils][metabase-enterprise.sso.settings :as sso-settings][metabase-enterprise.sso.test-setup :as sso.test-setup]@@ -234,6 +235,21 @@(= {"extra" "keypairs", "are" "also present"}(t2/select-one-fn :jwt_attributes :model/User :email "[email protected]"))))))))+(deftest request-jwt-test+ (let [token "some.jwt.token"]+ (testing "GET reads jwt from query params"+ (is (= token (sso.i/request-jwt {:request-method :get :params {:jwt token}}))))+ (testing "GET ignores jwt in body"+ (is (nil? (sso.i/request-jwt {:request-method :get :body {:jwt token}}))))+ (testing "POST reads jwt from JSON body"+ (is (= token (sso.i/request-jwt {:request-method :post :body {:jwt token}}))))+ (testing "POST ignores jwt in query params"+ (is (nil? (sso.i/request-jwt {:request-method :post :params {:jwt token}}))))+ (testing "other methods return nil"+ (is (nil? (sso.i/request-jwt {:request-method :put+ :params {:jwt token}+ :body {:jwt token}}))))))+(deftest post-jwt-body-happy-path-test(testing "Happy path login via POST /auth/sso with JSON body {:jwt ...}, same behavior as GET with query param"(with-jwt-default-setup!@@ -261,6 +277,51 @@(= {"extra" "keypairs", "are" "also present"}(t2/select-one-fn :jwt_attributes :model/User :email "[email protected]"))))))))+(deftest post-jwt-query-param-ignored-test+ (testing "POST /auth/sso does not authenticate when jwt is only in the query string"+ (with-jwt-default-setup!+ (let [jwt-payload (jwt/sign+ {:email "[email protected]"+ :first_name "Rasta"+ :last_name "Toucan"+ :exp (+ (buddy-util/now) 3600)+ :iat (buddy-util/now)}+ default-jwt-secret)+ response (client/client-real-response :post 302 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ {}+ :return_to default-redirect-uri+ :jwt jwt-payload)]+ (is (not (sso.test-setup/successful-login? response)))))))++(deftest post-jwt-body-precedence-test+ (testing "POST /auth/sso uses jwt from JSON body, not from query params"+ (with-jwt-default-setup!+ (let [body-jwt (jwt/sign+ {:email "[email protected]"+ :first_name "Rasta"+ :last_name "Toucan"+ :source "body"+ :exp (+ (buddy-util/now) 3600)+ :iat (buddy-util/now)}+ default-jwt-secret)+ query-jwt (jwt/sign+ {:email "[email protected]"+ :first_name "Crowberto"+ :last_name "Bird"+ :source "query"+ :exp (+ (buddy-util/now) 3600)+ :iat (buddy-util/now)}+ default-jwt-secret)+ response (client/client-real-response :post 302 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ {:jwt body-jwt}+ :return_to default-redirect-uri+ :jwt query-jwt)]+ (is (sso.test-setup/successful-login? response))+ (is (= {"source" "body"}+ (t2/select-one-fn :jwt_attributes :model/User :email "[email protected]")))))))+(deftest no-open-redirect-test(testing "Check that we prevent open redirects to untrusted sites"(with-jwt-default-setup!