2023-01-26 09:31:07 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
2021-07-09 14:23:49 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
NumberInput,
|
|
|
|
Switch,
|
|
|
|
} from "@patternfly/react-core";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { Controller, FormProvider, useForm, useWatch } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-07-09 14:23:49 +00:00
|
|
|
|
2023-05-24 12:11:06 +00:00
|
|
|
import { FormAccess } from "../../components/form/FormAccess";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2022-06-15 12:57:39 +00:00
|
|
|
import { convertToFormValues } from "../../util";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Time } from "./Time";
|
2021-07-09 14:23:49 +00:00
|
|
|
|
|
|
|
type BruteForceDetectionProps = {
|
2022-06-15 12:57:39 +00:00
|
|
|
realm: RealmRepresentation;
|
2021-07-09 14:23:49 +00:00
|
|
|
save: (realm: RealmRepresentation) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BruteForceDetection = ({
|
2022-06-15 12:57:39 +00:00
|
|
|
realm,
|
2021-07-09 14:23:49 +00:00
|
|
|
save,
|
|
|
|
}: BruteForceDetectionProps) => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
2023-01-26 09:31:07 +00:00
|
|
|
const form = useForm();
|
2021-07-09 14:23:49 +00:00
|
|
|
const {
|
2022-06-15 12:57:39 +00:00
|
|
|
setValue,
|
2021-07-09 14:23:49 +00:00
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
formState: { isDirty },
|
2022-06-15 12:57:39 +00:00
|
|
|
} = form;
|
2021-07-09 14:23:49 +00:00
|
|
|
|
|
|
|
const enable = useWatch({
|
|
|
|
control,
|
|
|
|
name: "bruteForceProtected",
|
|
|
|
});
|
|
|
|
|
|
|
|
const permanentLockout = useWatch({
|
|
|
|
control,
|
|
|
|
name: "permanentLockout",
|
|
|
|
});
|
|
|
|
|
2022-06-15 12:57:39 +00:00
|
|
|
const setupForm = () => convertToFormValues(realm, setValue);
|
|
|
|
useEffect(setupForm, []);
|
|
|
|
|
2021-07-09 14:23:49 +00:00
|
|
|
return (
|
2022-06-15 12:57:39 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<FormAccess
|
|
|
|
role="manage-realm"
|
|
|
|
isHorizontal
|
|
|
|
onSubmit={handleSubmit(save)}
|
2021-07-09 14:23:49 +00:00
|
|
|
>
|
2022-06-15 12:57:39 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("common:enabled")}
|
|
|
|
fieldId="bruteForceProtected"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="bruteForceProtected"
|
|
|
|
defaultValue={false}
|
|
|
|
control={control}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) => (
|
2022-06-15 12:57:39 +00:00
|
|
|
<Switch
|
|
|
|
id="bruteForceProtected"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2023-01-26 09:31:07 +00:00
|
|
|
isChecked={field.value}
|
|
|
|
onChange={field.onChange}
|
2022-06-15 12:57:39 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-09-23 07:48:23 +00:00
|
|
|
/>
|
2022-06-15 12:57:39 +00:00
|
|
|
</FormGroup>
|
|
|
|
{enable && (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
|
|
|
label={t("failureFactor")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-03-07 09:29:40 +00:00
|
|
|
helpText={t("realm-settings-help:failureFactor")}
|
2022-06-15 12:57:39 +00:00
|
|
|
fieldLabelId="realm-settings:failureFactor"
|
|
|
|
/>
|
2021-09-23 07:48:23 +00:00
|
|
|
}
|
2022-06-15 12:57:39 +00:00
|
|
|
fieldId="failureFactor"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="failureFactor"
|
|
|
|
defaultValue={0}
|
|
|
|
control={control}
|
|
|
|
rules={{ required: true }}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) => (
|
2022-06-15 12:57:39 +00:00
|
|
|
<NumberInput
|
|
|
|
type="text"
|
|
|
|
id="failureFactor"
|
2023-01-26 09:31:07 +00:00
|
|
|
value={field.value}
|
|
|
|
onPlus={() => field.onChange(field.value + 1)}
|
|
|
|
onMinus={() => field.onChange(field.value - 1)}
|
2022-06-15 12:57:39 +00:00
|
|
|
onChange={(event) =>
|
2023-01-26 09:31:07 +00:00
|
|
|
field.onChange(
|
|
|
|
Number((event.target as HTMLInputElement).value)
|
|
|
|
)
|
2022-06-15 12:57:39 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("permanentLockout")}
|
|
|
|
fieldId="permanentLockout"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="permanentLockout"
|
|
|
|
defaultValue={false}
|
|
|
|
control={control}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) => (
|
2022-06-15 12:57:39 +00:00
|
|
|
<Switch
|
|
|
|
id="permanentLockout"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2023-01-26 09:31:07 +00:00
|
|
|
isChecked={field.value}
|
|
|
|
onChange={field.onChange}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t("permanentLockout")}
|
2022-06-15 12:57:39 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-09-23 07:48:23 +00:00
|
|
|
|
2022-06-15 12:57:39 +00:00
|
|
|
{!permanentLockout && (
|
|
|
|
<>
|
|
|
|
<Time name="waitIncrementSeconds" />
|
|
|
|
<Time name="maxFailureWaitSeconds" />
|
|
|
|
<Time name="maxDeltaTimeSeconds" />
|
|
|
|
</>
|
|
|
|
)}
|
2021-07-09 14:23:49 +00:00
|
|
|
|
2022-06-15 12:57:39 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("quickLoginCheckMilliSeconds")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-03-07 09:29:40 +00:00
|
|
|
helpText={t(
|
|
|
|
"realm-settings-help:quickLoginCheckMilliSeconds"
|
|
|
|
)}
|
2022-06-15 12:57:39 +00:00
|
|
|
fieldLabelId="realm-settings:quickLoginCheckMilliSeconds"
|
|
|
|
/>
|
2021-09-23 07:48:23 +00:00
|
|
|
}
|
2022-06-15 12:57:39 +00:00
|
|
|
fieldId="quickLoginCheckMilliSeconds"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="quickLoginCheckMilliSeconds"
|
|
|
|
defaultValue={0}
|
|
|
|
control={control}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) => (
|
2022-06-15 12:57:39 +00:00
|
|
|
<NumberInput
|
|
|
|
type="text"
|
|
|
|
id="quickLoginCheckMilliSeconds"
|
2023-01-26 09:31:07 +00:00
|
|
|
value={field.value}
|
|
|
|
onPlus={() => field.onChange(field.value + 1)}
|
|
|
|
onMinus={() => field.onChange(field.value - 1)}
|
2022-06-15 12:57:39 +00:00
|
|
|
onChange={(event) =>
|
2023-01-26 09:31:07 +00:00
|
|
|
field.onChange(
|
|
|
|
Number((event.target as HTMLInputElement).value)
|
|
|
|
)
|
2022-06-15 12:57:39 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-07-09 14:23:49 +00:00
|
|
|
|
2022-06-15 12:57:39 +00:00
|
|
|
<Time name="minimumQuickLoginWaitSeconds" />
|
|
|
|
</>
|
|
|
|
)}
|
2021-07-09 14:23:49 +00:00
|
|
|
|
2022-06-15 12:57:39 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="brute-force-tab-save"
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button variant="link" onClick={setupForm}>
|
|
|
|
{t("common:revert")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormProvider>
|
2021-07-09 14:23:49 +00:00
|
|
|
);
|
|
|
|
};
|