From 911663b65ce77c37600d90e5cd48065c373f61ca Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Mon, 12 Jun 2023 12:50:53 +0200 Subject: [PATCH] use admin client instead of fetch (#20774) --- .../add/OpenIdConnectSettings.tsx | 25 +++---------------- .../src/resources/agent.ts | 21 ++++++++-------- .../src/resources/identityProviders.ts | 9 ++++--- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/js/apps/admin-ui/src/identity-providers/add/OpenIdConnectSettings.tsx b/js/apps/admin-ui/src/identity-providers/add/OpenIdConnectSettings.tsx index df01804b5a..2214571070 100644 --- a/js/apps/admin-ui/src/identity-providers/add/OpenIdConnectSettings.tsx +++ b/js/apps/admin-ui/src/identity-providers/add/OpenIdConnectSettings.tsx @@ -5,9 +5,6 @@ import { HelpItem } from "ui-shared"; import { adminClient } from "../../admin-client"; import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload"; -import { useRealm } from "../../context/realm-context/RealmContext"; -import { addTrailingSlash } from "../../util"; -import { getAuthorizationHeaders } from "../../utils/getAuthorizationHeaders"; import { DiscoveryEndpointField } from "../component/DiscoveryEndpointField"; import { DiscoverySettings } from "./DiscoverySettings"; @@ -15,7 +12,6 @@ export const OpenIdConnectSettings = () => { const { t } = useTranslation("identity-providers"); const id = "oidc"; - const { realm } = useRealm(); const { setValue, setError, @@ -38,25 +34,10 @@ export const OpenIdConnectSettings = () => { formData.append("file", new Blob([JSON.stringify(obj)])); try { - const response = await fetch( - `${addTrailingSlash( - adminClient.baseUrl - )}admin/realms/${realm}/identity-provider/import-config`, - { - method: "POST", - body: formData, - headers: getAuthorizationHeaders(await adminClient.getAccessToken()), - } + const result = await adminClient.identityProviders.importFromUrl( + formData ); - if (response.ok) { - const result = await response.json(); - setupForm(result); - } else { - setError("discoveryError", { - type: "manual", - message: response.statusText, - }); - } + setupForm(result); } catch (error) { setError("discoveryError", { type: "manual", diff --git a/js/libs/keycloak-admin-client/src/resources/agent.ts b/js/libs/keycloak-admin-client/src/resources/agent.ts index 52f2ace1f2..f71affffaa 100644 --- a/js/libs/keycloak-admin-client/src/resources/agent.ts +++ b/js/libs/keycloak-admin-client/src/resources/agent.ts @@ -82,22 +82,23 @@ export class Agent { const baseParams = this.getBaseParams?.() ?? {}; // Filter query parameters by queryParamKeys - const queryParams = queryParamKeys - ? pick(payload, queryParamKeys) - : undefined; + const queryParams = + queryParamKeys.length > 0 ? pick(payload, queryParamKeys) : undefined; // Add filtered payload parameters to base parameters const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys]; const urlParams = { ...baseParams, ...pick(payload, allUrlParamKeys) }; - // Omit url parameters and query parameters from payload - const omittedKeys = ignoredKeys - ? [...allUrlParamKeys, ...queryParamKeys].filter( - (key) => !ignoredKeys.includes(key) - ) - : [...allUrlParamKeys, ...queryParamKeys]; + if (!(payload instanceof FormData)) { + // Omit url parameters and query parameters from payload + const omittedKeys = ignoredKeys + ? [...allUrlParamKeys, ...queryParamKeys].filter( + (key) => !ignoredKeys.includes(key) + ) + : [...allUrlParamKeys, ...queryParamKeys]; - payload = omit(payload, omittedKeys); + payload = omit(payload, omittedKeys); + } // Transform keys of both payload and queryParams if (keyTransform) { diff --git a/js/libs/keycloak-admin-client/src/resources/identityProviders.ts b/js/libs/keycloak-admin-client/src/resources/identityProviders.ts index 49f56000d9..79cb8a93df 100644 --- a/js/libs/keycloak-admin-client/src/resources/identityProviders.ts +++ b/js/libs/keycloak-admin-client/src/resources/identityProviders.ts @@ -116,10 +116,11 @@ export class IdentityProviders extends Resource<{ realm?: string }> { }); public importFromUrl = this.makeRequest< - { - fromUrl: string; - providerId: string; - }, + | { + fromUrl: string; + providerId: string; + } + | FormData, Record >({ method: "POST",