UNPKG

@keycloakify/keycloak-admin-ui

Version:
60 lines 3.54 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { fetchWithError } from "@keycloak/keycloak-admin-client"; import { FormErrorText, HelpItem, TextControl, useEnvironment, } from "../../ui-shared"; import { FormGroup, Title } from "@patternfly/react-core"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { FileUploadForm } from "../../components/json-file-upload/FileUploadForm"; import { useRealm } from "../../context/realm-context/RealmContext"; import { addTrailingSlash } from "../../util"; import { getAuthorizationHeaders } from "../../utils/getAuthorizationHeaders"; import { DiscoveryEndpointField } from "../../identity-providers/component/DiscoveryEndpointField"; import { DescriptorSettings } from "../../identity-providers/add/DescriptorSettings"; export const SamlConnectSettings = () => { const { adminClient } = useAdminClient(); const { environment } = useEnvironment(); const { t } = useTranslation(); const id = "saml"; const { realm } = useRealm(); const { setValue, setError, clearErrors, formState: { errors }, } = useFormContext(); const setupForm = (result) => { Object.entries(result).map(([key, value]) => setValue(`config.${key}`, value)); }; const fileUpload = async (xml) => { clearErrors("discoveryError"); if (!xml) { return; } const formData = new FormData(); formData.append("providerId", id); formData.append("file", new Blob([xml])); try { const response = await fetchWithError(`${addTrailingSlash(adminClient.baseUrl)}admin/realms/${realm}/identity-provider/import-config`, { method: "POST", body: formData, headers: getAuthorizationHeaders(await adminClient.getAccessToken()), }); if (response.ok) { const result = await response.json(); setupForm(result); } else { setError("discoveryError", { type: "manual", message: response.statusText, }); } } catch (error) { setError("discoveryError", { type: "manual", message: error.message, }); } }; return (_jsxs(_Fragment, { children: [_jsx(Title, { headingLevel: "h2", size: "xl", className: "kc-form-panel__title", children: t("samlSettings") }), _jsx(TextControl, { name: "config.entityId", label: t("serviceProviderEntityId"), labelIcon: t("serviceProviderEntityIdHelp"), defaultValue: `${environment.serverBaseUrl}/realms/${realm}`, rules: { required: t("required"), } }), _jsx(DiscoveryEndpointField, { id: "saml", fileUpload: _jsxs(FormGroup, { label: t("importConfig"), fieldId: "kc-import-config", labelIcon: _jsx(HelpItem, { helpText: t("importConfigHelp"), fieldLabelId: "importConfig" }), children: [_jsx(FileUploadForm, { id: "kc-import-config", extension: ".xml", hideDefaultPreview: true, unWrap: true, validated: errors.discoveryError ? "error" : "default", onChange: (value) => fileUpload(value) }), errors.discoveryError && (_jsx(FormErrorText, { message: errors.discoveryError.message }))] }), children: (readonly) => _jsx(DescriptorSettings, { readOnly: readonly }) })] })); }; //# sourceMappingURL=SamlConnectSettings.js.map