2021-11-01 07:49:23 +00:00
|
|
|
import React from "react";
|
2021-08-19 14:41:27 +00:00
|
|
|
import { useFormContext } from "react-hook-form";
|
2021-11-01 07:49:23 +00:00
|
|
|
import { FormGroup, TextInput, Title } from "@patternfly/react-core";
|
2021-08-19 14:41:27 +00:00
|
|
|
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type IdentityProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/identityProviderRepresentation";
|
2021-08-19 14:41:27 +00:00
|
|
|
|
2021-11-01 07:49:23 +00:00
|
|
|
import { FileUploadForm } from "../../components/json-file-upload/FileUploadForm";
|
2021-08-19 14:41:27 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
|
|
|
import { DescriptorSettings } from "./DescriptorSettings";
|
|
|
|
import { getBaseUrl } from "../../util";
|
2021-11-01 07:49:23 +00:00
|
|
|
import { DiscoveryEndpointField } from "../component/DiscoveryEndpointField";
|
2022-02-21 13:55:02 +00:00
|
|
|
import environment from "../../environment";
|
2021-08-19 14:41:27 +00:00
|
|
|
|
|
|
|
export const SamlConnectSettings = () => {
|
|
|
|
const { t } = useTranslation("identity-providers");
|
|
|
|
const id = "saml";
|
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm } = useRealm();
|
2022-02-21 16:06:35 +00:00
|
|
|
const { setValue, register, errors, setError, clearErrors } =
|
|
|
|
useFormContext();
|
2021-08-19 14:41:27 +00:00
|
|
|
|
|
|
|
const setupForm = (result: IdentityProviderRepresentation) => {
|
|
|
|
Object.entries(result).map(([key, value]) =>
|
|
|
|
setValue(`config.${key}`, value)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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(
|
|
|
|
`${getBaseUrl(
|
|
|
|
adminClient
|
|
|
|
)}admin/realms/${realm}/identity-provider/import-config`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
headers: {
|
|
|
|
Authorization: `bearer ${await adminClient.getAccessToken()}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
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 (
|
|
|
|
<>
|
|
|
|
<Title headingLevel="h4" size="xl" className="kc-form-panel__title">
|
|
|
|
{t("samlSettings")}
|
|
|
|
</Title>
|
|
|
|
|
|
|
|
<FormGroup
|
|
|
|
label={t("serviceProviderEntityId")}
|
|
|
|
fieldId="kc-service-provider-entity-id"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="identity-providers-help:serviceProviderEntityId"
|
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
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={errors.config?.entityId ? "error" : "default"}
|
2021-08-19 14:41:27 +00:00
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
type="text"
|
|
|
|
name="config.entityId"
|
|
|
|
data-testid="serviceProviderEntityId"
|
|
|
|
id="kc-service-provider-entity-id"
|
2022-02-21 13:55:02 +00:00
|
|
|
ref={register({ required: true })}
|
|
|
|
validated={errors.config?.entityId ? "error" : "default"}
|
|
|
|
defaultValue={`${environment.authServerUrl}/realms/${realm}`}
|
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
|
|
|
|
helpText="identity-providers-help:importConfig"
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|