2022-12-21 17:39:24 +00:00
|
|
|
import { FormGroup, Switch } from "@patternfly/react-core";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { ReactNode, useEffect, useState } from "react";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useFormContext } from "react-hook-form";
|
2021-11-01 07:49:23 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2021-11-01 07:49:23 +00:00
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
2022-12-21 17:39:24 +00:00
|
|
|
import environment from "../../environment";
|
2021-11-01 07:49:23 +00:00
|
|
|
|
|
|
|
type DiscoveryEndpointFieldProps = {
|
|
|
|
id: string;
|
|
|
|
fileUpload: ReactNode;
|
|
|
|
children: (readOnly: boolean) => ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DiscoveryEndpointField = ({
|
|
|
|
id,
|
|
|
|
fileUpload,
|
|
|
|
children,
|
|
|
|
}: DiscoveryEndpointFieldProps) => {
|
|
|
|
const { t } = useTranslation("identity-providers");
|
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-11-01 07:49:23 +00:00
|
|
|
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
setValue,
|
|
|
|
register,
|
|
|
|
setError,
|
|
|
|
watch,
|
|
|
|
clearErrors,
|
|
|
|
formState: { errors },
|
|
|
|
} = useFormContext();
|
2021-11-01 07:49:23 +00:00
|
|
|
const discoveryUrl = watch("discoveryEndpoint");
|
|
|
|
|
|
|
|
const [discovery, setDiscovery] = useState(true);
|
|
|
|
const [discovering, setDiscovering] = useState(false);
|
|
|
|
const [discoveryResult, setDiscoveryResult] =
|
|
|
|
useState<Record<string, string>>();
|
|
|
|
|
|
|
|
const setupForm = (result: Record<string, string>) => {
|
|
|
|
Object.keys(result).map((k) => setValue(`config.${k}`, result[k]));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!discoveryUrl) {
|
|
|
|
setDiscovering(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
clearErrors("discoveryError");
|
|
|
|
try {
|
|
|
|
const result = await adminClient.identityProviders.importFromUrl({
|
|
|
|
providerId: id,
|
|
|
|
fromUrl: discoveryUrl,
|
|
|
|
});
|
|
|
|
setupForm(result);
|
|
|
|
setDiscoveryResult(result);
|
|
|
|
} catch (error) {
|
|
|
|
setError("discoveryError", {
|
|
|
|
type: "manual",
|
|
|
|
message: (error as Error).message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setDiscovering(false);
|
|
|
|
})();
|
|
|
|
}, [discovering]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
2022-02-21 13:55:02 +00:00
|
|
|
label={t(
|
|
|
|
id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor"
|
|
|
|
)}
|
|
|
|
fieldId="kc-discovery-endpoint"
|
2021-11-01 07:49:23 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2022-02-21 13:55:02 +00:00
|
|
|
helpText={`identity-providers-help:${
|
|
|
|
id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor"
|
|
|
|
}`}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="identity-providers:discoveryEndpoint"
|
2021-11-01 07:49:23 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Switch
|
|
|
|
id="kc-discovery-endpoint-switch"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={discovery}
|
2022-02-21 16:06:35 +00:00
|
|
|
onChange={(checked) => {
|
|
|
|
clearErrors("discoveryError");
|
|
|
|
setDiscovery(checked);
|
|
|
|
}}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t(
|
|
|
|
id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor"
|
|
|
|
)}
|
2021-11-01 07:49:23 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{discovery && (
|
|
|
|
<FormGroup
|
2022-02-21 13:55:02 +00:00
|
|
|
label={t(
|
|
|
|
id === "oidc" ? "discoveryEndpoint" : "samlEntityDescriptor"
|
|
|
|
)}
|
2021-11-01 07:49:23 +00:00
|
|
|
fieldId="kc-discovery-endpoint"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2022-02-21 13:55:02 +00:00
|
|
|
helpText={`identity-providers-help:${
|
|
|
|
id === "oidc" ? "discoveryEndpoint" : "samlEntityDescriptor"
|
|
|
|
}`}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="identity-providers:discoveryEndpoint"
|
2021-11-01 07:49:23 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
validated={
|
|
|
|
errors.discoveryError || errors.discoveryEndpoint
|
|
|
|
? "error"
|
|
|
|
: !discoveryResult
|
|
|
|
? "default"
|
|
|
|
: "success"
|
|
|
|
}
|
|
|
|
helperTextInvalid={
|
|
|
|
errors.discoveryEndpoint
|
|
|
|
? t("common:required")
|
2022-02-21 16:06:35 +00:00
|
|
|
: t("noValidMetaDataFound", {
|
|
|
|
error: errors.discoveryError?.message,
|
|
|
|
})
|
2021-11-01 07:49:23 +00:00
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-12-07 09:04:37 +00:00
|
|
|
type="url"
|
2021-11-01 07:49:23 +00:00
|
|
|
data-testid="discoveryEndpoint"
|
|
|
|
id="kc-discovery-endpoint"
|
|
|
|
placeholder={
|
|
|
|
id === "oidc"
|
|
|
|
? "https://hostname/auth/realms/master/.well-known/openid-configuration"
|
2022-02-21 13:55:02 +00:00
|
|
|
: ""
|
2021-11-01 07:49:23 +00:00
|
|
|
}
|
|
|
|
validated={
|
|
|
|
errors.discoveryError || errors.discoveryEndpoint
|
|
|
|
? "error"
|
|
|
|
: !discoveryResult
|
|
|
|
? "default"
|
|
|
|
: "success"
|
|
|
|
}
|
|
|
|
customIconUrl={
|
|
|
|
discovering
|
2022-02-18 11:41:44 +00:00
|
|
|
? environment.resourceUrl + "/discovery-load-indicator.svg"
|
2021-11-01 07:49:23 +00:00
|
|
|
: ""
|
|
|
|
}
|
2022-12-21 17:39:24 +00:00
|
|
|
{...register("discoveryEndpoint", {
|
|
|
|
required: true,
|
|
|
|
onBlur: () => setDiscovering(true),
|
|
|
|
})}
|
2021-11-01 07:49:23 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
{!discovery && fileUpload}
|
|
|
|
{discovery && !errors.discoveryError && children(true)}
|
|
|
|
{!discovery && children(false)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|