2021-11-01 07:49:23 +00:00
|
|
|
import React, { ReactNode, useEffect, useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useFormContext } from "react-hook-form";
|
|
|
|
import { FormGroup, TextInput, Switch } from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import environment from "../../environment";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
|
|
|
|
|
|
|
type DiscoveryEndpointFieldProps = {
|
|
|
|
id: string;
|
|
|
|
fileUpload: ReactNode;
|
|
|
|
children: (readOnly: boolean) => ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DiscoveryEndpointField = ({
|
|
|
|
id,
|
|
|
|
fileUpload,
|
|
|
|
children,
|
|
|
|
}: DiscoveryEndpointFieldProps) => {
|
|
|
|
const { t } = useTranslation("identity-providers");
|
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
|
|
|
|
const { setValue, register, errors, setError, watch, clearErrors } =
|
|
|
|
useFormContext();
|
|
|
|
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}
|
|
|
|
onChange={setDiscovery}
|
|
|
|
/>
|
|
|
|
</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")
|
|
|
|
: t("noValidMetaDataFound")
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
type="text"
|
|
|
|
name="discoveryEndpoint"
|
|
|
|
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
|
|
|
}
|
|
|
|
onBlur={() => setDiscovering(true)}
|
|
|
|
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
|
|
|
: ""
|
|
|
|
}
|
|
|
|
ref={register({ required: true })}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
{!discovery && fileUpload}
|
|
|
|
{discovery && !errors.discoveryError && children(true)}
|
|
|
|
{!discovery && children(false)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|