2020-10-30 20:15:37 +00:00
|
|
|
import {
|
2021-05-06 13:03:25 +00:00
|
|
|
AlertVariant,
|
2020-10-30 20:15:37 +00:00
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
2020-11-25 14:50:40 +00:00
|
|
|
SelectVariant,
|
2020-10-30 20:15:37 +00:00
|
|
|
Switch,
|
|
|
|
TextInput,
|
2021-05-07 13:15:14 +00:00
|
|
|
ValidatedOptions,
|
2020-10-30 20:15:37 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-01-26 01:41:14 +00:00
|
|
|
import React, { useState } from "react";
|
2021-05-05 21:27:39 +00:00
|
|
|
import _ from "lodash";
|
2021-05-06 13:03:25 +00:00
|
|
|
|
|
|
|
import TestLdapConnectionRepresentation from "keycloak-admin/lib/defs/testLdapConnection";
|
2020-12-16 07:02:41 +00:00
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
2021-05-05 21:27:39 +00:00
|
|
|
import { Controller, UseFormMethods, useWatch } from "react-hook-form";
|
2020-12-16 07:02:41 +00:00
|
|
|
import { FormAccess } from "../../components/form-access/FormAccess";
|
2021-01-04 21:33:18 +00:00
|
|
|
import { WizardSectionHeader } from "../../components/wizard-section-header/WizardSectionHeader";
|
2021-05-04 08:11:58 +00:00
|
|
|
import { PasswordInput } from "../../components/password-input/PasswordInput";
|
2021-05-06 13:03:25 +00:00
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2020-10-30 20:15:37 +00:00
|
|
|
|
2021-01-04 21:33:18 +00:00
|
|
|
export type LdapSettingsConnectionProps = {
|
2021-01-26 01:41:14 +00:00
|
|
|
form: UseFormMethods;
|
2021-01-04 21:33:18 +00:00
|
|
|
showSectionHeading?: boolean;
|
|
|
|
showSectionDescription?: boolean;
|
|
|
|
};
|
|
|
|
|
2021-05-06 13:03:25 +00:00
|
|
|
const testLdapProperties: Array<keyof TestLdapConnectionRepresentation> = [
|
|
|
|
"connectionUrl",
|
|
|
|
"bindDn",
|
|
|
|
"bindCredential",
|
|
|
|
"useTruststoreSpi",
|
|
|
|
"connectionTimeout",
|
|
|
|
"startTls",
|
|
|
|
"authType",
|
|
|
|
];
|
|
|
|
|
2021-01-04 21:33:18 +00:00
|
|
|
export const LdapSettingsConnection = ({
|
2021-01-26 01:41:14 +00:00
|
|
|
form,
|
2021-01-04 21:33:18 +00:00
|
|
|
showSectionHeading = false,
|
|
|
|
showSectionDescription = false,
|
|
|
|
}: LdapSettingsConnectionProps) => {
|
2020-10-30 20:15:37 +00:00
|
|
|
const { t } = useTranslation("user-federation");
|
2021-05-06 13:03:25 +00:00
|
|
|
const { t: helpText } = useTranslation("user-federation-help");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
|
|
|
|
const testLdap = async () => {
|
|
|
|
try {
|
|
|
|
const settings: TestLdapConnectionRepresentation = {};
|
|
|
|
|
|
|
|
testLdapProperties.forEach((key) => {
|
|
|
|
const value = _.get(form.getValues(), `config.${key}`);
|
|
|
|
settings[key] = _.isArray(value) ? value[0] : "";
|
|
|
|
});
|
|
|
|
await adminClient.realms.testLDAPConnection(
|
|
|
|
{ realm },
|
|
|
|
{ ...settings, action: "testConnection" }
|
|
|
|
);
|
|
|
|
addAlert(t("testSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("testError"), AlertVariant.danger);
|
|
|
|
console.error(error.response?.data?.errorMessage);
|
|
|
|
}
|
|
|
|
};
|
2020-10-30 20:15:37 +00:00
|
|
|
|
2020-11-25 14:50:40 +00:00
|
|
|
const [
|
|
|
|
isTruststoreSpiDropdownOpen,
|
|
|
|
setIsTruststoreSpiDropdownOpen,
|
|
|
|
] = useState(false);
|
2020-11-25 16:17:50 +00:00
|
|
|
|
2020-11-25 14:50:40 +00:00
|
|
|
const [isBindTypeDropdownOpen, setIsBindTypeDropdownOpen] = useState(false);
|
2020-11-25 16:17:50 +00:00
|
|
|
|
2021-05-05 21:27:39 +00:00
|
|
|
const ldapBindType = useWatch({
|
|
|
|
control: form.control,
|
|
|
|
name: "config.authType",
|
|
|
|
});
|
|
|
|
|
2020-10-30 20:15:37 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-01-04 21:33:18 +00:00
|
|
|
{showSectionHeading && (
|
|
|
|
<WizardSectionHeader
|
|
|
|
title={t("connectionAndAuthenticationSettings")}
|
|
|
|
description={helpText(
|
|
|
|
"ldapConnectionAndAuthorizationSettingsDescription"
|
|
|
|
)}
|
|
|
|
showDescription={showSectionDescription}
|
|
|
|
/>
|
|
|
|
)}
|
2020-11-25 16:17:50 +00:00
|
|
|
<FormAccess role="manage-realm" isHorizontal>
|
2020-10-30 20:15:37 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("connectionURL")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("consoleDisplayConnectionUrlHelp")}
|
|
|
|
forLabel={t("connectionURL")}
|
2020-11-25 14:50:40 +00:00
|
|
|
forID="kc-console-connection-url"
|
2020-10-30 20:15:37 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-11-25 14:50:40 +00:00
|
|
|
fieldId="kc-console-connection-url"
|
2020-10-30 20:15:37 +00:00
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
isRequired
|
|
|
|
type="text"
|
2020-11-25 14:50:40 +00:00
|
|
|
id="kc-console-connection-url"
|
2021-02-23 20:49:57 +00:00
|
|
|
data-testid="ldap-connection-url"
|
2021-01-26 01:41:14 +00:00
|
|
|
name="config.connectionUrl[0]"
|
|
|
|
ref={form.register({
|
|
|
|
required: {
|
|
|
|
value: true,
|
|
|
|
message: `${t("validateConnectionUrl")}`,
|
|
|
|
},
|
|
|
|
})}
|
2020-10-30 20:15:37 +00:00
|
|
|
/>
|
2021-01-26 01:41:14 +00:00
|
|
|
{form.errors.config &&
|
|
|
|
form.errors.config.connectionUrl &&
|
|
|
|
form.errors.config.connectionUrl[0] && (
|
|
|
|
<div className="error">
|
|
|
|
{form.errors.config.connectionUrl[0].message}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-10-30 20:15:37 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2020-11-25 14:50:40 +00:00
|
|
|
label={t("enableStartTls")}
|
2020-10-30 20:15:37 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2020-11-25 14:50:40 +00:00
|
|
|
helpText={helpText("enableStartTlsHelp")}
|
|
|
|
forLabel={t("enableStartTls")}
|
2020-10-30 20:15:37 +00:00
|
|
|
forID="kc-enable-start-tls"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-enable-start-tls"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
2020-11-25 14:50:40 +00:00
|
|
|
<Controller
|
2020-12-16 07:02:41 +00:00
|
|
|
name="config.startTls"
|
2021-01-26 01:41:14 +00:00
|
|
|
defaultValue={["false"]}
|
|
|
|
control={form.control}
|
2020-11-25 14:50:40 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id={"kc-enable-start-tls"}
|
|
|
|
isDisabled={false}
|
2021-01-26 01:41:14 +00:00
|
|
|
onChange={(value) => onChange([`${value}`])}
|
|
|
|
isChecked={value[0] === "true"}
|
2020-11-25 14:50:40 +00:00
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
></Controller>
|
2020-10-30 20:15:37 +00:00
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
<FormGroup
|
|
|
|
label={t("useTruststoreSpi")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("useTruststoreSpiHelp")}
|
|
|
|
forLabel={t("useTruststoreSpi")}
|
|
|
|
forID="kc-use-truststore-spi"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-use-truststore-spi"
|
|
|
|
>
|
2020-11-25 14:50:40 +00:00
|
|
|
<Controller
|
2021-01-26 01:41:14 +00:00
|
|
|
name="config.useTruststoreSpi[0]"
|
2020-11-25 14:50:40 +00:00
|
|
|
defaultValue=""
|
2021-01-26 01:41:14 +00:00
|
|
|
control={form.control}
|
2020-11-25 14:50:40 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-use-truststore-spi"
|
|
|
|
onToggle={() =>
|
|
|
|
setIsTruststoreSpiDropdownOpen(!isTruststoreSpiDropdownOpen)
|
|
|
|
}
|
|
|
|
isOpen={isTruststoreSpiDropdownOpen}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setIsTruststoreSpiDropdownOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
>
|
2021-01-26 01:41:14 +00:00
|
|
|
<SelectOption key={0} value="always">
|
|
|
|
{t("always")}
|
|
|
|
</SelectOption>
|
|
|
|
<SelectOption key={1} value="ldapsOnly">
|
|
|
|
{t("onlyLdaps")}
|
|
|
|
</SelectOption>
|
|
|
|
<SelectOption key={2} value="never">
|
|
|
|
{t("never")}
|
|
|
|
</SelectOption>
|
2020-11-25 14:50:40 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
></Controller>
|
2020-10-30 20:15:37 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("connectionPooling")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("connectionPoolingHelp")}
|
|
|
|
forLabel={t("connectionPooling")}
|
|
|
|
forID="kc-connection-pooling"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-connection-pooling"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
2020-11-25 14:50:40 +00:00
|
|
|
<Controller
|
2020-12-16 07:02:41 +00:00
|
|
|
name="config.connectionPooling"
|
2021-02-19 23:13:07 +00:00
|
|
|
defaultValue={["false"]}
|
2021-01-26 01:41:14 +00:00
|
|
|
control={form.control}
|
2020-11-25 14:50:40 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id={"kc-connection-pooling"}
|
|
|
|
isDisabled={false}
|
2021-01-26 01:41:14 +00:00
|
|
|
onChange={(value) => onChange([`${value}`])}
|
2020-12-16 07:02:41 +00:00
|
|
|
isChecked={value[0] === "true"}
|
2020-11-25 14:50:40 +00:00
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
></Controller>
|
2020-10-30 20:15:37 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("connectionTimeout")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("connectionTimeoutHelp")}
|
|
|
|
forLabel={t("connectionTimeout")}
|
2020-11-25 14:50:40 +00:00
|
|
|
forID="kc-console-connection-timeout"
|
2020-10-30 20:15:37 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-11-25 14:50:40 +00:00
|
|
|
fieldId="kc-console-connection-timeout"
|
2020-10-30 20:15:37 +00:00
|
|
|
>
|
|
|
|
<TextInput
|
2021-02-22 19:48:03 +00:00
|
|
|
type="number"
|
2021-03-02 15:37:51 +00:00
|
|
|
min={0}
|
2020-11-25 14:50:40 +00:00
|
|
|
id="kc-console-connection-timeout"
|
2021-01-26 01:41:14 +00:00
|
|
|
name="config.connectionTimeout[0]"
|
|
|
|
ref={form.register}
|
2020-10-30 20:15:37 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("bindType")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("bindTypeHelp")}
|
|
|
|
forLabel={t("bindType")}
|
|
|
|
forID="kc-bind-type"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-bind-type"
|
|
|
|
isRequired
|
|
|
|
>
|
2020-11-25 14:50:40 +00:00
|
|
|
<Controller
|
2021-01-26 01:41:14 +00:00
|
|
|
name="config.authType[0]"
|
2021-05-06 13:03:25 +00:00
|
|
|
defaultValue="none"
|
2021-01-26 01:41:14 +00:00
|
|
|
control={form.control}
|
2020-11-25 14:50:40 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-bind-type"
|
|
|
|
required
|
|
|
|
onToggle={() =>
|
|
|
|
setIsBindTypeDropdownOpen(!isBindTypeDropdownOpen)
|
|
|
|
}
|
|
|
|
isOpen={isBindTypeDropdownOpen}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setIsBindTypeDropdownOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
2021-02-23 20:49:57 +00:00
|
|
|
data-testid="ldap-bind-type"
|
2020-11-25 14:50:40 +00:00
|
|
|
>
|
2021-05-05 21:27:39 +00:00
|
|
|
<SelectOption key={0} value="simple" />
|
|
|
|
<SelectOption key={1} value="none" isPlaceholder />
|
2020-11-25 14:50:40 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
></Controller>
|
2020-10-30 20:15:37 +00:00
|
|
|
</FormGroup>
|
2021-05-05 21:27:39 +00:00
|
|
|
|
2021-05-07 13:15:14 +00:00
|
|
|
{_.isEqual(ldapBindType, ["simple"]) && (
|
2021-05-05 21:27:39 +00:00
|
|
|
<>
|
|
|
|
<FormGroup
|
|
|
|
label={t("bindDn")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("bindDnHelp")}
|
|
|
|
forLabel={t("bindDn")}
|
|
|
|
forID="kc-console-bind-dn"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-console-bind-dn"
|
2021-05-07 13:15:14 +00:00
|
|
|
helperTextInvalid={t("validateBindDn")}
|
|
|
|
validated={
|
|
|
|
form.errors.config?.bindDn
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
|
|
|
}
|
2021-05-05 21:27:39 +00:00
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
type="text"
|
|
|
|
id="kc-console-bind-dn"
|
|
|
|
data-testid="ldap-bind-dn"
|
|
|
|
name="config.bindDn[0]"
|
2021-05-07 13:15:14 +00:00
|
|
|
ref={form.register({ required: true })}
|
2021-05-05 21:27:39 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("bindCredentials")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={helpText("bindCredentialsHelp")}
|
|
|
|
forLabel={t("bindCredentials")}
|
|
|
|
forID="kc-console-bind-credentials"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="kc-console-bind-credentials"
|
2021-05-07 13:15:14 +00:00
|
|
|
helperTextInvalid={t("validateBindCredentials")}
|
|
|
|
validated={
|
|
|
|
form.errors.config?.bindCredential
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
|
|
|
}
|
2021-05-05 21:27:39 +00:00
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<PasswordInput
|
|
|
|
isRequired
|
|
|
|
id="kc-console-bind-credentials"
|
|
|
|
data-testid="ldap-bind-credentials"
|
|
|
|
name="config.bindCredential[0]"
|
|
|
|
ref={form.register({
|
2021-05-07 13:15:14 +00:00
|
|
|
required: true,
|
2021-05-05 21:27:39 +00:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup fieldId="kc-test-button">
|
2021-05-06 13:03:25 +00:00
|
|
|
<Button
|
|
|
|
isDisabled={!form.formState.isValid}
|
|
|
|
variant="secondary"
|
|
|
|
id="kc-test-button"
|
|
|
|
onClick={() => testLdap()}
|
|
|
|
>
|
2021-05-05 21:27:39 +00:00
|
|
|
{t("common:test")}
|
|
|
|
</Button>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
2020-11-25 16:17:50 +00:00
|
|
|
</FormAccess>
|
2020-10-30 20:15:37 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|