2023-05-03 13:51:02 +00:00
|
|
|
import type IdentityProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/identityProviderRepresentation";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { FormGroup, Title } from "@patternfly/react-core";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useFormContext } from "react-hook-form";
|
2021-08-19 14:41:27 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2021-08-19 14:41:27 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2021-11-01 07:49:23 +00:00
|
|
|
import { FileUploadForm } from "../../components/json-file-upload/FileUploadForm";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2022-12-21 17:39:24 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2022-02-21 13:55:02 +00:00
|
|
|
import environment from "../../environment";
|
2022-07-14 13:02:28 +00:00
|
|
|
import { addTrailingSlash } from "../../util";
|
2022-08-04 11:08:11 +00:00
|
|
|
import { getAuthorizationHeaders } from "../../utils/getAuthorizationHeaders";
|
2022-12-21 17:39:24 +00:00
|
|
|
import { DiscoveryEndpointField } from "../component/DiscoveryEndpointField";
|
|
|
|
import { DescriptorSettings } from "./DescriptorSettings";
|
|
|
|
|
|
|
|
type FormFields = IdentityProviderRepresentation & {
|
|
|
|
discoveryError: string;
|
|
|
|
};
|
2021-08-19 14:41:27 +00:00
|
|
|
|
|
|
|
export const SamlConnectSettings = () => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-08-19 14:41:27 +00:00
|
|
|
const id = "saml";
|
|
|
|
|
|
|
|
const { realm } = useRealm();
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
setValue,
|
|
|
|
register,
|
|
|
|
setError,
|
|
|
|
clearErrors,
|
|
|
|
formState: { errors },
|
2022-12-21 17:39:24 +00:00
|
|
|
} = useFormContext<FormFields>();
|
2021-08-19 14:41:27 +00:00
|
|
|
|
|
|
|
const setupForm = (result: IdentityProviderRepresentation) => {
|
|
|
|
Object.entries(result).map(([key, value]) =>
|
2023-07-11 14:03:21 +00:00
|
|
|
setValue(`config.${key}`, value),
|
2021-08-19 14:41:27 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-11-01 07:49:23 +00:00
|
|
|
const fileUpload = async (xml: string) => {
|
2022-02-21 16:06:35 +00:00
|
|
|
clearErrors("discoveryError");
|
|
|
|
if (!xml) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-13 09:17:00 +00:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("providerId", id);
|
2021-11-01 07:49:23 +00:00
|
|
|
formData.append("file", new Blob([xml]));
|
2021-09-13 09:17:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
2022-07-14 13:02:28 +00:00
|
|
|
`${addTrailingSlash(
|
2023-07-11 14:03:21 +00:00
|
|
|
adminClient.baseUrl,
|
2021-09-13 09:17:00 +00:00
|
|
|
)}admin/realms/${realm}/identity-provider/import-config`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
2022-08-04 11:08:11 +00:00
|
|
|
headers: getAuthorizationHeaders(await adminClient.getAccessToken()),
|
2023-07-11 14:03:21 +00:00
|
|
|
},
|
2021-09-13 09:17:00 +00:00
|
|
|
);
|
2022-02-21 16:06:35 +00:00
|
|
|
if (response.ok) {
|
|
|
|
const result = await response.json();
|
|
|
|
setupForm(result);
|
|
|
|
} else {
|
|
|
|
setError("discoveryError", {
|
|
|
|
type: "manual",
|
|
|
|
message: response.statusText,
|
|
|
|
});
|
|
|
|
}
|
2021-11-01 07:49:23 +00:00
|
|
|
} catch (error) {
|
|
|
|
setError("discoveryError", {
|
|
|
|
type: "manual",
|
|
|
|
message: (error as Error).message,
|
|
|
|
});
|
2021-08-19 14:41:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-06-29 12:00:06 +00:00
|
|
|
<Title headingLevel="h2" size="xl" className="kc-form-panel__title">
|
2021-08-19 14:41:27 +00:00
|
|
|
{t("samlSettings")}
|
|
|
|
</Title>
|
|
|
|
|
|
|
|
<FormGroup
|
|
|
|
label={t("serviceProviderEntityId")}
|
|
|
|
fieldId="kc-service-provider-entity-id"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-09-08 13:17:17 +00:00
|
|
|
helpText={t("serviceProviderEntityIdHelp")}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="identity-providers:serviceProviderEntityId"
|
2021-08-19 14:41:27 +00:00
|
|
|
/>
|
|
|
|
}
|
2022-02-21 13:55:02 +00:00
|
|
|
isRequired
|
2023-09-14 09:01:15 +00:00
|
|
|
helperTextInvalid={t("required")}
|
2022-02-21 13:55:02 +00:00
|
|
|
validated={errors.config?.entityId ? "error" : "default"}
|
2021-08-19 14:41:27 +00:00
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-08-19 14:41:27 +00:00
|
|
|
data-testid="serviceProviderEntityId"
|
|
|
|
id="kc-service-provider-entity-id"
|
2022-02-21 13:55:02 +00:00
|
|
|
validated={errors.config?.entityId ? "error" : "default"}
|
|
|
|
defaultValue={`${environment.authServerUrl}/realms/${realm}`}
|
2022-12-21 17:39:24 +00:00
|
|
|
{...register("config.entityId", { required: true })}
|
2021-08-19 14:41:27 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
|
2021-11-01 07:49:23 +00:00
|
|
|
<DiscoveryEndpointField
|
|
|
|
id="saml"
|
|
|
|
fileUpload={
|
|
|
|
<FormGroup
|
|
|
|
label={t("importConfig")}
|
|
|
|
fieldId="kc-import-config"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-09-08 13:17:17 +00:00
|
|
|
helpText={t("importConfigHelp")}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="identity-providers:importConfig"
|
2021-11-01 07:49:23 +00:00
|
|
|
/>
|
2021-08-19 14:41:27 +00:00
|
|
|
}
|
2021-11-01 07:49:23 +00:00
|
|
|
validated={errors.discoveryError ? "error" : "default"}
|
2022-02-21 16:06:35 +00:00
|
|
|
helperTextInvalid={errors.discoveryError?.message}
|
2021-11-01 07:49:23 +00:00
|
|
|
>
|
|
|
|
<FileUploadForm
|
|
|
|
id="kc-import-config"
|
|
|
|
extension=".xml"
|
|
|
|
hideDefaultPreview
|
|
|
|
unWrap
|
|
|
|
validated={errors.discoveryError ? "error" : "default"}
|
|
|
|
onChange={(value) => fileUpload(value)}
|
2021-08-19 14:41:27 +00:00
|
|
|
/>
|
2021-11-01 07:49:23 +00:00
|
|
|
</FormGroup>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{(readonly) => <DescriptorSettings readOnly={readonly} />}
|
|
|
|
</DiscoveryEndpointField>
|
2021-08-19 14:41:27 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|