import type CertificateRepresentation from "@keycloak/keycloak-admin-client/lib/defs/certificateRepresentation"; import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig"; import { AlertVariant, Button, ButtonVariant, Flex, FlexItem, Form, FormGroup, Modal, ModalVariant, Radio, Split, SplitItem, Text, TextContent, Title, } from "@patternfly/react-core"; import { saveAs } from "file-saver"; import { useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { HelpItem } from "ui-shared"; import { adminClient } from "../../admin-client"; import { useAlerts } from "../../components/alert/Alerts"; import { Certificate } from "./Certificate"; import { KeyForm } from "./GenerateKeyDialog"; import type { KeyTypes } from "./SamlKeys"; type SamlKeysDialogProps = { id: string; attr: KeyTypes; onClose: () => void; onCancel: () => void; }; export type SamlKeysDialogForm = KeyStoreConfig & { file: File; }; export const submitForm = async ( form: SamlKeysDialogForm, id: string, attr: KeyTypes, callback: (error?: unknown) => void, ) => { try { const formData = new FormData(); const { file, ...rest } = form; Object.entries(rest).map(([key, value]) => formData.append( key === "format" ? "keystoreFormat" : key, value.toString(), ), ); formData.append("file", file); await adminClient.clients.uploadKey({ id, attr }, formData); callback(); } catch (error) { callback(error); } }; export const SamlKeysDialog = ({ id, attr, onClose, onCancel, }: SamlKeysDialogProps) => { const { t } = useTranslation(); const [type, setType] = useState(false); const [keys, setKeys] = useState(); const form = useForm({ mode: "onChange" }); const { handleSubmit, formState: { isValid }, } = form; const { addAlert, addError } = useAlerts(); const submit = (form: SamlKeysDialogForm) => { submitForm(form, id, attr, (error) => { if (error) { addError("importError", error); } else { addAlert(t("importSuccess"), AlertVariant.success); } }); }; const generate = async () => { try { const key = await adminClient.clients.generateKey({ id, attr, }); setKeys(key); saveAs( new Blob([key.privateKey!], { type: "application/octet-stream", }), "private.key", ); addAlert(t("generateSuccess"), AlertVariant.success); } catch (error) { addError("generateError", error); } }; return ( {t("enableClientSignatureRequired")} {t("enableClientSignatureRequiredExplain")} } isOpen={true} onClose={onClose} actions={[ , , ]} >
setType(false)} label={t("selectMethodType.generate")} id="selectMethodType-generate" /> setType(true)} label={t("selectMethodType.import")} id="selectMethodType-import" /> {!type && ( } > )}
{type && }
); };