2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-05-04 08:11:58 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-03-16 10:37:45 +00:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
FormProvider,
|
|
|
|
useForm,
|
|
|
|
useFormContext,
|
|
|
|
} from "react-hook-form";
|
2021-05-04 08:11:58 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2021-08-26 08:39:35 +00:00
|
|
|
import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig";
|
2021-05-04 08:11:58 +00:00
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { StoreSettings } from "./StoreSettings";
|
2022-09-12 09:05:27 +00:00
|
|
|
import { FileUpload } from "../../components/json-file-upload/patternfly/FileUpload";
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
type GenerateKeyDialogProps = {
|
2022-03-16 10:37:45 +00:00
|
|
|
clientId: string;
|
2021-05-04 08:11:58 +00:00
|
|
|
toggleDialog: () => void;
|
|
|
|
save: (keyStoreConfig: KeyStoreConfig) => void;
|
|
|
|
};
|
|
|
|
|
2021-10-12 09:28:55 +00:00
|
|
|
type KeyFormProps = {
|
|
|
|
useFile?: boolean;
|
2022-10-26 13:31:33 +00:00
|
|
|
isSaml?: boolean;
|
2021-10-12 09:28:55 +00:00
|
|
|
};
|
|
|
|
|
2022-10-26 13:31:33 +00:00
|
|
|
export const KeyForm = ({ isSaml = false, useFile = false }: KeyFormProps) => {
|
2021-10-12 09:28:55 +00:00
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
|
|
|
|
const [filename, setFilename] = useState<string>();
|
|
|
|
const [openArchiveFormat, setOpenArchiveFormat] = useState(false);
|
|
|
|
|
2022-03-16 10:37:45 +00:00
|
|
|
const { control } = useFormContext<KeyStoreConfig>();
|
|
|
|
|
2021-10-12 09:28:55 +00:00
|
|
|
return (
|
|
|
|
<Form className="pf-u-pt-lg">
|
|
|
|
<FormGroup
|
|
|
|
label={t("archiveFormat")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:archiveFormat"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:archiveFormat"
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="archiveFormat"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="format"
|
|
|
|
defaultValue="JKS"
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="archiveFormat"
|
2021-11-30 13:07:44 +00:00
|
|
|
onToggle={setOpenArchiveFormat}
|
2021-10-12 09:28:55 +00:00
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value.toString());
|
|
|
|
setOpenArchiveFormat(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("archiveFormat")}
|
|
|
|
isOpen={openArchiveFormat}
|
|
|
|
>
|
|
|
|
{["JKS", "PKCS12"].map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={option === value}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{useFile && (
|
|
|
|
<FormGroup
|
|
|
|
label={t("importFile")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:importFile"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:importFile"
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="importFile"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="file"
|
|
|
|
defaultValue=""
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<FileUpload
|
|
|
|
id="importFile"
|
|
|
|
value={value}
|
|
|
|
filename={filename}
|
|
|
|
browseButtonText={t("browse")}
|
|
|
|
onChange={(value, filename) => {
|
|
|
|
setFilename(filename);
|
|
|
|
onChange(value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2022-10-26 13:31:33 +00:00
|
|
|
<StoreSettings hidePassword={useFile} isSaml={isSaml} />
|
2021-10-12 09:28:55 +00:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-04 08:11:58 +00:00
|
|
|
export const GenerateKeyDialog = ({
|
2022-03-16 10:37:45 +00:00
|
|
|
clientId,
|
2021-05-04 08:11:58 +00:00
|
|
|
save,
|
|
|
|
toggleDialog,
|
|
|
|
}: GenerateKeyDialogProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
2022-03-16 10:37:45 +00:00
|
|
|
const form = useForm<KeyStoreConfig>({
|
|
|
|
defaultValues: { keyAlias: clientId },
|
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isValid },
|
|
|
|
} = form;
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.medium}
|
|
|
|
title={t("generateKeys")}
|
|
|
|
isOpen
|
|
|
|
onClose={toggleDialog}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
|
|
|
key="confirm"
|
|
|
|
data-testid="confirm"
|
2022-03-16 10:37:45 +00:00
|
|
|
isDisabled={!isValid}
|
2021-05-04 08:11:58 +00:00
|
|
|
onClick={() => {
|
|
|
|
handleSubmit((config) => {
|
|
|
|
save(config);
|
|
|
|
toggleDialog();
|
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("generate")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
|
|
|
data-testid="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<TextContent>
|
|
|
|
<Text>{t("clients-help:generateKeysDescription")}</Text>
|
|
|
|
</TextContent>
|
2022-03-16 10:37:45 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<KeyForm />
|
|
|
|
</FormProvider>
|
2021-05-04 08:11:58 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|