2023-05-03 13:51:02 +00:00
|
|
|
import type CertificateRepresentation from "@keycloak/keycloak-admin-client/lib/defs/certificateRepresentation";
|
|
|
|
import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig";
|
2021-10-12 09:28:55 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Flex,
|
|
|
|
FlexItem,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Radio,
|
|
|
|
Split,
|
|
|
|
SplitItem,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
Title,
|
|
|
|
} from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { saveAs } from "file-saver";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2023-05-03 13:51:02 +00:00
|
|
|
|
|
|
|
import { adminClient } from "../../admin-client";
|
2021-10-12 09:28:55 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
|
|
|
import { Certificate } from "./Certificate";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { KeyForm } from "./GenerateKeyDialog";
|
|
|
|
import type { KeyTypes } from "./SamlKeys";
|
2021-10-12 09:28:55 +00:00
|
|
|
|
|
|
|
type SamlKeysDialogProps = {
|
|
|
|
id: string;
|
|
|
|
attr: KeyTypes;
|
|
|
|
onClose: () => void;
|
2021-11-15 07:30:53 +00:00
|
|
|
onCancel: () => void;
|
2021-10-12 09:28:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type SamlKeysDialogForm = KeyStoreConfig & {
|
|
|
|
file: File;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const submitForm = async (
|
|
|
|
form: SamlKeysDialogForm,
|
|
|
|
id: string,
|
|
|
|
attr: KeyTypes,
|
2023-07-11 14:03:21 +00:00
|
|
|
callback: (error?: unknown) => void,
|
2021-10-12 09:28:55 +00:00
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const formData = new FormData();
|
|
|
|
const { file, ...rest } = form;
|
|
|
|
Object.entries(rest).map(([key, value]) =>
|
|
|
|
formData.append(
|
|
|
|
key === "format" ? "keystoreFormat" : key,
|
2023-07-11 14:03:21 +00:00
|
|
|
value.toString(),
|
|
|
|
),
|
2021-10-12 09:28:55 +00:00
|
|
|
);
|
|
|
|
formData.append("file", file);
|
|
|
|
|
|
|
|
await adminClient.clients.uploadKey({ id, attr }, formData);
|
|
|
|
callback();
|
|
|
|
} catch (error) {
|
|
|
|
callback(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-15 07:30:53 +00:00
|
|
|
export const SamlKeysDialog = ({
|
|
|
|
id,
|
|
|
|
attr,
|
|
|
|
onClose,
|
|
|
|
onCancel,
|
|
|
|
}: SamlKeysDialogProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-10-12 09:28:55 +00:00
|
|
|
const [type, setType] = useState(false);
|
|
|
|
const [keys, setKeys] = useState<CertificateRepresentation>();
|
2023-01-12 09:23:48 +00:00
|
|
|
const form = useForm<SamlKeysDialogForm>({ mode: "onChange" });
|
2021-11-15 07:30:53 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
2023-01-12 09:23:48 +00:00
|
|
|
formState: { isValid },
|
2022-03-16 10:37:45 +00:00
|
|
|
} = form;
|
2021-10-12 09:28:55 +00:00
|
|
|
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const submit = (form: SamlKeysDialogForm) => {
|
2023-05-03 13:51:02 +00:00
|
|
|
submitForm(form, id, attr, (error) => {
|
2021-10-12 09:28:55 +00:00
|
|
|
if (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("importError", error);
|
2021-10-12 09:28:55 +00:00
|
|
|
} else {
|
|
|
|
addAlert(t("importSuccess"), AlertVariant.success);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const generate = async () => {
|
|
|
|
try {
|
|
|
|
const key = await adminClient.clients.generateKey({
|
|
|
|
id,
|
|
|
|
attr,
|
|
|
|
});
|
|
|
|
setKeys(key);
|
2022-11-18 16:13:39 +00:00
|
|
|
saveAs(
|
2021-10-12 09:28:55 +00:00
|
|
|
new Blob([key.privateKey!], {
|
|
|
|
type: "application/octet-stream",
|
|
|
|
}),
|
2023-07-11 14:03:21 +00:00
|
|
|
"private.key",
|
2021-10-12 09:28:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
addAlert(t("generateSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("generateError", error);
|
2021-10-12 09:28:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.medium}
|
|
|
|
aria-labelledby={t("enableClientSignatureRequired")}
|
|
|
|
header={
|
|
|
|
<TextContent>
|
|
|
|
<Title headingLevel="h1">{t("enableClientSignatureRequired")}</Title>
|
|
|
|
<Text>{t("enableClientSignatureRequiredExplain")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={onClose}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
|
|
|
key="confirm"
|
|
|
|
data-testid="confirm"
|
|
|
|
variant="primary"
|
2023-01-12 09:23:48 +00:00
|
|
|
isDisabled={!isValid && !keys}
|
2021-10-12 09:28:55 +00:00
|
|
|
onClick={() => {
|
|
|
|
if (type) {
|
|
|
|
handleSubmit(submit)();
|
|
|
|
}
|
|
|
|
onClose();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("confirm")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
|
|
|
data-testid="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
2021-11-15 07:30:53 +00:00
|
|
|
onClick={onCancel}
|
2021-10-12 09:28:55 +00:00
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
2022-12-11 23:46:19 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<Form isHorizontal>
|
2021-10-12 09:28:55 +00:00
|
|
|
<FormGroup
|
2022-12-11 23:46:19 +00:00
|
|
|
label={t("selectMethod")}
|
|
|
|
fieldId="selectMethod"
|
|
|
|
hasNoPaddingTop
|
2021-10-12 09:28:55 +00:00
|
|
|
>
|
2022-12-11 23:46:19 +00:00
|
|
|
<Flex>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={!type}
|
|
|
|
name="selectMethodType"
|
|
|
|
onChange={() => setType(false)}
|
|
|
|
label={t("selectMethodType.generate")}
|
|
|
|
id="selectMethodType-generate"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={type}
|
|
|
|
name="selectMethodType"
|
|
|
|
onChange={() => setType(true)}
|
|
|
|
label={t("selectMethodType.import")}
|
|
|
|
id="selectMethodType-import"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
2021-10-12 09:28:55 +00:00
|
|
|
</FormGroup>
|
2023-01-12 09:23:48 +00:00
|
|
|
{!type && (
|
2022-12-11 23:46:19 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("certificate")}
|
|
|
|
fieldId="certificate"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-09-08 13:17:17 +00:00
|
|
|
helpText={t("certificateHelp")}
|
2023-09-13 14:05:17 +00:00
|
|
|
fieldLabelId="certificate"
|
2022-12-11 23:46:19 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Split hasGutter>
|
|
|
|
<SplitItem isFilled>
|
|
|
|
<Certificate plain keyInfo={keys} />
|
|
|
|
</SplitItem>
|
|
|
|
<SplitItem>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
data-testid="generate"
|
|
|
|
onClick={generate}
|
|
|
|
>
|
|
|
|
{t("generate")}
|
|
|
|
</Button>
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
</FormGroup>
|
2023-01-12 09:23:48 +00:00
|
|
|
)}
|
|
|
|
</Form>
|
2022-12-11 23:46:19 +00:00
|
|
|
{type && <KeyForm useFile />}
|
|
|
|
</FormProvider>
|
2021-10-12 09:28:55 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|