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
Pass scopes through the oidc check function (#75416)
Nathan Voxland73cd4c7c1e07
diff --git a/enterprise/backend/src/metabase_enterprise/sso/api/oidc.clj b/enterprise/backend/src/metabase_enterprise/sso/api/oidc.cljindex ce2cb74cb70..182e131cee9 100644--- a/enterprise/backend/src/metabase_enterprise/sso/api/oidc.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/api/oidc.clj@@ -74,8 +74,8 @@(defn- check-oidc-connection!"Run OIDC configuration check and throw a 400 if it fails. Returns the check result on success."- [issuer-uri client-id client-secret]- (let [result (sso/check-oidc-configuration issuer-uri client-id client-secret)]+ [issuer-uri client-id client-secret scopes]+ (let [result (sso/check-oidc-configuration issuer-uri client-id client-secret scopes)](api/check-400 (:ok result)(or (get-in result [:credentials :error])(get-in result [:discovery :error])@@ -110,11 +110,11 @@(let [provider-key (:key body)](api/check-400 (u/valid-slug? provider-key) "Provider key must be a URL-safe slug (lowercase letters, numbers, hyphens, must start with letter or number)")(api/check-400 (nil? (sso-settings/get-oidc-provider provider-key)) (format "An OIDC provider with key '%s' already exists" provider-key))- (check-oidc-connection! (:issuer-uri body) (:client-id body) (:client-secret body))- (let [providers (sso-settings/oidc-providers)+ (let [providers (sso-settings/oidc-providers)new-provider (merge {:enabled false- :scopes ["openid" "email" "profile"]}+ :scopes ["openid" "email" "profile"]}body)]+ (check-oidc-connection! (:issuer-uri new-provider) (:client-id new-provider) (:client-secret new-provider) (:scopes new-provider))(sso-settings/oidc-providers! (conj (vec providers) new-provider))(sanitize-response new-provider))))@@ -136,7 +136,7 @@(dissoc body :client-secret)body)updated (merge existing body)]- (check-oidc-connection! (:issuer-uri updated) (:client-id updated) (:client-secret updated))+ (check-oidc-connection! (:issuer-uri updated) (:client-id updated) (:client-secret updated) (:scopes updated))(let [providers (assoc (vec providers) idx updated)](sso-settings/oidc-providers! providers)(sanitize-response updated)))))@@ -171,14 +171,13 @@(premium-features/assert-has-feature :sso-oidc (tru "OIDC authentication"))(let [issuer-uri (:issuer-uri body)client-id (:client-id body)+ stored (some-> body :key sso-settings/get-oidc-provider)client-secret (let [s (:client-secret body)](if (str/blank? s)- ;; If no secret provided, look up the stored one by provider key- (when-let [provider-key (:key body)]- (when-let [provider (sso-settings/get-oidc-provider provider-key)]- (:client-secret provider)))- s))]- (check-oidc-connection! issuer-uri client-id client-secret)))+ (:client-secret stored)+ s))+ scopes (or (:scopes stored) ["openid"])]+ (check-oidc-connection! issuer-uri client-id client-secret scopes)));; DELETE /api/ee/sso/oidc/:key(api.macros/defendpoint :delete "/:key" :- :nildiff --git a/src/metabase/sso/oidc/check.clj b/src/metabase/sso/oidc/check.cljindex 9e6fd8f1ec1..5a62cf099ea 100644--- a/src/metabase/sso/oidc/check.clj+++ b/src/metabase/sso/oidc/check.clj@@ -4,6 +4,7 @@Validates OIDC provider configuration by probing the discovery documentand testing client credentials against the token endpoint."(:require+ [clojure.string :as str][metabase.sso.oidc.discovery :as discovery][metabase.sso.oidc.http :as oidc.http][metabase.util.log :as log]))@@ -21,17 +22,17 @@(discovery/invalidate-cache! issuer-uri)(let [doc (discovery/discover-oidc-configuration issuer-uri)](if (nil? doc)- {:step :discovery+ {:step :discovery:success false- :error (str "Could not fetch OIDC discovery document from " issuer-uri)}+ :error (str "Could not fetch OIDC discovery document from " issuer-uri)}(let [config {:discovery-document doc}](if (discovery/validate-configuration config)- {:step :discovery- :success true+ {:step :discovery+ :success true:token-endpoint (discovery/get-token-endpoint config)}- {:step :discovery+ {:step :discovery:success false- :error "Discovery document is missing required endpoints (authorization, token, or JWKS)"})))))+ :error "Discovery document is missing required endpoints (authorization, token, or JWKS)"})))))(defn check-credentials"Validate client credentials by POSTing a client_credentials grant to the token endpoint.@@ -44,17 +45,18 @@Returns `{:step :credentials, :success bool, :verified bool, :error str?}`.When `:verified` is false, the IdP did not confirm the credentials but didn't reject them either."- [token-endpoint client-id client-secret]+ [token-endpoint client-id client-secret scopes](try- (let [response (oidc.http/oidc-post token-endpoint- {:form-params {:grant_type "client_credentials"- :client_id client-id- :client_secret client-secret}- :coerce :always})- status (:status response)- body (:body response)- error-code (or (:error body)- (when (string? body) body))]+ (let [response (oidc.http/oidc-post token-endpoint+ {:form-params {:grant_type "client_credentials"+ :client_id client-id+ :client_secret client-secret+ :scope (str/join " " scopes)}+ :coerce :always})+ status (:status response)+ body (:body response)+ error-code (or (:error body)+ (when (string? body) body))](cond(= 200 status){:step :credentials :success true :verified true}@@ -66,17 +68,17 @@{:step :credentials :success false :verified true :error "Invalid client ID or client secret"}:else- {:step :credentials- :success false+ {:step :credentials+ :success false:verified true- :error (str "Unexpected response from token endpoint (HTTP " status "): "- (or (:error_description body) error-code body))}))+ :error (str "Unexpected response from token endpoint (HTTP " status "): "+ (or (:error_description body) error-code body))}))(catch Exception e(log/warnf e "OIDC credential check failed for %s" token-endpoint)- {:step :credentials- :success false+ {:step :credentials+ :success false:verified false- :error (str "Could not connect to token endpoint: " (.getMessage e))})))+ :error (str "Could not connect to token endpoint: " (.getMessage e))})))(defn check-oidc-configuration"Run full OIDC configuration validation: discovery then credentials.@@ -85,13 +87,14 @@- issuer-uri: The OIDC issuer URL- client-id: OAuth2 client ID- client-secret: OAuth2 client secret+ - scopes: OAuth2 scopes to request (e.g. `[\"openid\" \"email\" \"profile\"]`)Returns `{:ok bool, :discovery {...}, :credentials {...}}`."- [issuer-uri client-id client-secret]+ [issuer-uri client-id client-secret scopes](let [disc (check-discovery issuer-uri)](if-not (:success disc){:ok false :discovery disc}- (let [cred (check-credentials (:token-endpoint disc) client-id client-secret)]- {:ok (:success cred)- :discovery disc+ (let [cred (check-credentials (:token-endpoint disc) client-id client-secret scopes)]+ {:ok (:success cred)+ :discovery disc:credentials cred}))))diff --git a/test/metabase/sso/oidc/check_test.clj b/test/metabase/sso/oidc/check_test.cljnew file mode 100644index 00000000000..e9a542c1826--- /dev/null+++ b/test/metabase/sso/oidc/check_test.clj@@ -0,0 +1,59 @@+(ns metabase.sso.oidc.check-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.sso.oidc.check :as check]+ [metabase.sso.oidc.http :as oidc.http]+ [metabase.test :as mt]))++(def ^:private token-endpoint "https://login.microsoftonline.com/tenant-id/oauth2/v2.0/token")++(deftest ^:parallel check-credentials-includes-scope-test+ (testing "scope param is always included in the token endpoint request"+ (let [captured (atom nil)]+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post (fn [_url opts]+ (reset! captured (:form-params opts))+ {:status 200 :body {:access_token "tok"}})]+ (check/check-credentials token-endpoint "client-id" "client-secret" ["openid"])+ (is (contains? @captured :scope)+ "form-params must include :scope — Entra ID rejects requests without it (AADSTS90014)")))))++(deftest ^:parallel check-credentials-uses-provided-scopes-test+ (testing "configured scopes are sent space-joined"+ (let [captured (atom nil)]+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post (fn [_url opts]+ (reset! captured (:form-params opts))+ {:status 200 :body {:access_token "tok"}})]+ (check/check-credentials token-endpoint "client-id" "client-secret"+ ["openid" "email" "profile"])+ (is (= "openid email profile" (:scope @captured)))))))++(deftest ^:parallel check-credentials-entra-missing-scope-error-test+ (testing "Entra AADSTS90014 error is surfaced correctly when scope list is empty"+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post+ (fn [_url _opts]+ {:status 400+ :body {:error "invalid_request"+ :error_description "AADSTS90014: The required field 'scope' is missing from the credential."}})]+ (let [result (check/check-credentials token-endpoint "client-id" "client-secret" [])]+ (is (false? (:success result)))+ (is (str/includes? (:error result) "AADSTS90014"))))))++(deftest ^:parallel check-credentials-success-test+ (testing "HTTP 200 → success"+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post (fn [_url _opts]+ {:status 200 :body {:access_token "tok"}})]+ (is (= {:step :credentials :success true :verified true}+ (check/check-credentials token-endpoint "client-id" "client-secret" ["openid"])))))+ (testing "unsupported_grant_type → inconclusive success"+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post (fn [_url _opts]+ {:status 400 :body {:error "unsupported_grant_type"}})]+ (let [result (check/check-credentials token-endpoint "client-id" "client-secret" ["openid"])]+ (is (true? (:success result)))+ (is (false? (:verified result))))))+ (testing "invalid_client → failure"+ (mt/with-dynamic-fn-redefs [oidc.http/oidc-post (fn [_url _opts]+ {:status 401 :body {:error "invalid_client"}})]+ (let [result (check/check-credentials token-endpoint "client-id" "wrong-secret" ["openid"])]+ (is (false? (:success result)))+ (is (= "Invalid client ID or client secret" (:error result)))))))