/* eslint-disable */ // @ts-nocheck import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import { HelpItem, NumberControl, SelectControl, SwitchControl, useAlerts, } from "../../../shared/keycloak-ui-shared"; import { ActionGroup, AlertVariant, Button, ButtonVariant, Chip, ChipGroup, FormGroup, PageSection, Radio, } from "../../../shared/@patternfly/react-core"; import { useMemo } from "react"; import { Controller, FormProvider, useForm, useWatch } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { FormAccess } from "../../components/form/FormAccess"; import { TimeSelectorControl } from "../../components/time-selector/TimeSelectorControl"; import { useRealm } from "../../context/realm-context/RealmContext"; import useLocaleSort from "../../utils/useLocaleSort"; import "./otp-policy.css"; const POLICY_TYPES = ["totp", "hotp"] as const; const OTP_HASH_ALGORITHMS = ["SHA1", "SHA256", "SHA512"] as const; const NUMBER_OF_DIGITS = [6, 8] as const; type OtpPolicyProps = { realm: RealmRepresentation; realmUpdated: (realm: RealmRepresentation) => void; }; type FormFields = Omit< RealmRepresentation, "clients" | "components" | "groups" | "users" | "federatedUsers" >; export const OtpPolicy = ({ realm, realmUpdated }: OtpPolicyProps) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const form = useForm({ mode: "onChange", defaultValues: { otpPolicyType: realm.otpPolicyType ?? POLICY_TYPES[0], otpPolicyAlgorithm: realm.otpPolicyAlgorithm ?? `Hmac${OTP_HASH_ALGORITHMS[0]}`, otpPolicyDigits: realm.otpPolicyDigits ?? NUMBER_OF_DIGITS[0], otpPolicyLookAheadWindow: realm.otpPolicyLookAheadWindow ?? 1, otpPolicyPeriod: realm.otpPolicyPeriod ?? 30, otpPolicyInitialCounter: realm.otpPolicyInitialCounter ?? 30, otpPolicyCodeReusable: realm.otpPolicyCodeReusable ?? false, }, }); const { control, reset, handleSubmit, formState: { isValid, isDirty }, } = form; const { realm: realmName } = useRealm(); const { addAlert, addError } = useAlerts(); const localeSort = useLocaleSort(); const otpType = useWatch({ name: "otpPolicyType", control }); const setupForm = (formValues: FormFields) => reset(formValues); const supportedApplications = useMemo(() => { const labels = (realm.otpSupportedApplications ?? []).map((key) => t(`otpSupportedApplications.${key}`), ); return localeSort(labels, (label) => label); }, [realm.otpSupportedApplications]); const onSubmit = async (formValues: FormFields) => { try { await adminClient.realms.update({ realm: realmName }, formValues); const updatedRealm = await adminClient.realms.findOne({ realm: realmName, }); realmUpdated(updatedRealm!); setupForm(updatedRealm!); addAlert(t("updateOtpSuccess"), AlertVariant.success); } catch (error) { addError("updateOtpError", error); } }; return ( } hasNoPaddingTop > ( <> {POLICY_TYPES.map((type) => ( onChange(type)} label={t(`policyType.${type}`)} className="keycloak__otp_policies_authentication__policy-type" /> ))} )} /> ({ key: `Hmac${type}`, value: type, }))} controller={{ defaultValue: `Hmac${OTP_HASH_ALGORITHMS[0]}` }} /> } hasNoPaddingTop > ( <> {NUMBER_OF_DIGITS.map((type) => ( field.onChange(type)} label={type} className="keycloak__otp_policies_authentication__number-of-digits" /> ))} )} /> {otpType === POLICY_TYPES[0] && ( )} {otpType === POLICY_TYPES[1] && ( )} } > {supportedApplications.map((label) => ( {label} ))} {otpType === POLICY_TYPES[0] && ( )} ); };