2023-05-03 13:51:02 +00:00
|
|
|
import type CertificateRepresentation from "@keycloak/keycloak-admin-client/lib/defs/certificateRepresentation";
|
2021-10-12 09:28:55 +00:00
|
|
|
import {
|
2022-12-02 14:54:30 +00:00
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Card,
|
2021-10-12 09:28:55 +00:00
|
|
|
CardBody,
|
2022-12-02 14:54:30 +00:00
|
|
|
Form,
|
2021-10-12 09:28:55 +00:00
|
|
|
FormGroup,
|
2022-12-02 14:54:30 +00:00
|
|
|
PageSection,
|
2021-10-12 09:28:55 +00:00
|
|
|
Switch,
|
2022-12-02 14:54:30 +00:00
|
|
|
Text,
|
|
|
|
TextContent,
|
2021-10-12 09:28:55 +00:00
|
|
|
} from "@patternfly/react-core";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { saveAs } from "file-saver";
|
|
|
|
import { Fragment, useState } from "react";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2021-10-12 09:28:55 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
2023-05-24 12:11:06 +00:00
|
|
|
import { FormAccess } from "../../components/form/FormAccess";
|
2021-10-12 09:28:55 +00:00
|
|
|
import { FormPanel } from "../../components/scroll-form/FormPanel";
|
2022-08-19 11:57:25 +00:00
|
|
|
import { convertAttributeNameToForm } from "../../util";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-10-26 13:31:33 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { FormFields } from "../ClientDetails";
|
|
|
|
import { Certificate } from "./Certificate";
|
2022-10-26 13:31:33 +00:00
|
|
|
import { ExportSamlKeyDialog } from "./ExportSamlKeyDialog";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { SamlImportKeyDialog } from "./SamlImportKeyDialog";
|
|
|
|
import { SamlKeysDialog } from "./SamlKeysDialog";
|
2021-10-12 09:28:55 +00:00
|
|
|
|
|
|
|
type SamlKeysProps = {
|
|
|
|
clientId: string;
|
|
|
|
save: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const KEYS = ["saml.signing", "saml.encryption"] as const;
|
2023-01-09 11:53:43 +00:00
|
|
|
export type KeyTypes = (typeof KEYS)[number];
|
2021-10-12 09:28:55 +00:00
|
|
|
|
|
|
|
const KEYS_MAPPING: { [key in KeyTypes]: { [index: string]: string } } = {
|
|
|
|
"saml.signing": {
|
2022-08-19 11:57:25 +00:00
|
|
|
name: convertAttributeNameToForm("attributes.saml.client.signature"),
|
2021-10-12 09:28:55 +00:00
|
|
|
title: "signingKeysConfig",
|
|
|
|
key: "clientSignature",
|
|
|
|
},
|
|
|
|
"saml.encryption": {
|
2022-08-19 11:57:25 +00:00
|
|
|
name: convertAttributeNameToForm("attributes.saml.encrypt"),
|
2021-10-12 09:28:55 +00:00
|
|
|
title: "encryptionKeysConfig",
|
|
|
|
key: "encryptAssertions",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
type KeySectionProps = {
|
2022-10-26 13:31:33 +00:00
|
|
|
clientId: string;
|
2021-10-12 09:28:55 +00:00
|
|
|
keyInfo?: CertificateRepresentation;
|
|
|
|
attr: KeyTypes;
|
|
|
|
onChanged: (key: KeyTypes) => void;
|
|
|
|
onGenerate: (key: KeyTypes, regenerate: boolean) => void;
|
|
|
|
onImport: (key: KeyTypes) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const KeySection = ({
|
2022-10-26 13:31:33 +00:00
|
|
|
clientId,
|
2021-10-12 09:28:55 +00:00
|
|
|
keyInfo,
|
|
|
|
attr,
|
|
|
|
onChanged,
|
|
|
|
onGenerate,
|
|
|
|
onImport,
|
|
|
|
}: KeySectionProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-12-02 14:54:30 +00:00
|
|
|
const { control, watch } = useFormContext<FormFields>();
|
2021-10-12 09:28:55 +00:00
|
|
|
const title = KEYS_MAPPING[attr].title;
|
|
|
|
const key = KEYS_MAPPING[attr].key;
|
|
|
|
const name = KEYS_MAPPING[attr].name;
|
|
|
|
|
2022-10-26 13:31:33 +00:00
|
|
|
const [showImportDialog, toggleImportDialog] = useToggle();
|
|
|
|
|
2022-12-02 14:54:30 +00:00
|
|
|
const section = watch(name as keyof FormFields);
|
2021-10-12 09:28:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-10-26 13:31:33 +00:00
|
|
|
{showImportDialog && (
|
|
|
|
<ExportSamlKeyDialog clientId={clientId} close={toggleImportDialog} />
|
|
|
|
)}
|
2021-10-12 09:28:55 +00:00
|
|
|
<FormPanel title={t(title)} className="kc-form-panel__panel">
|
|
|
|
<TextContent className="pf-u-pb-lg">
|
|
|
|
<Text>{t(`${title}Explain`)}</Text>
|
|
|
|
</TextContent>
|
|
|
|
<FormAccess role="manage-clients" isHorizontal>
|
|
|
|
<FormGroup
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-03-07 09:29:40 +00:00
|
|
|
helpText={t(`clients-help:${key}`)}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId={`clients:${key}`}
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={t(key)}
|
|
|
|
fieldId={key}
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Controller
|
2022-12-02 14:54:30 +00:00
|
|
|
name={name as keyof FormFields}
|
2021-10-12 09:28:55 +00:00
|
|
|
control={control}
|
|
|
|
defaultValue="false"
|
2022-12-02 14:54:30 +00:00
|
|
|
render={({ field }) => (
|
2021-10-12 09:28:55 +00:00
|
|
|
<Switch
|
|
|
|
data-testid={key}
|
|
|
|
id={key}
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2022-12-02 14:54:30 +00:00
|
|
|
isChecked={field.value === "true"}
|
2021-10-12 09:28:55 +00:00
|
|
|
onChange={(value) => {
|
|
|
|
const v = value.toString();
|
|
|
|
if (v === "true") {
|
|
|
|
onChanged(attr);
|
2022-12-02 14:54:30 +00:00
|
|
|
field.onChange(v);
|
2021-10-12 09:28:55 +00:00
|
|
|
} else {
|
|
|
|
onGenerate(attr, false);
|
|
|
|
}
|
|
|
|
}}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t(key)}
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
{keyInfo?.certificate && section === "true" && (
|
|
|
|
<Card isFlat>
|
|
|
|
<CardBody className="kc-form-panel__body">
|
|
|
|
<Form isHorizontal>
|
|
|
|
<Certificate keyInfo={keyInfo} />
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => onGenerate(attr, true)}
|
|
|
|
>
|
|
|
|
{t("regenerate")}
|
|
|
|
</Button>
|
|
|
|
<Button variant="secondary" onClick={() => onImport(attr)}>
|
|
|
|
{t("importKey")}
|
|
|
|
</Button>
|
2022-10-26 13:31:33 +00:00
|
|
|
<Button variant="tertiary" onClick={toggleImportDialog}>
|
|
|
|
{t("common:export")}
|
|
|
|
</Button>
|
2021-10-12 09:28:55 +00:00
|
|
|
</ActionGroup>
|
|
|
|
</Form>
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SamlKeys = ({ clientId, save }: SamlKeysProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-10-12 09:28:55 +00:00
|
|
|
const [isChanged, setIsChanged] = useState<KeyTypes>();
|
|
|
|
const [keyInfo, setKeyInfo] = useState<CertificateRepresentation[]>();
|
|
|
|
const [selectedType, setSelectedType] = useState<KeyTypes>();
|
2023-01-16 16:41:11 +00:00
|
|
|
const [openImport, setImportOpen] = useState<KeyTypes>();
|
2021-10-12 09:28:55 +00:00
|
|
|
const [refresh, setRefresh] = useState(0);
|
|
|
|
|
|
|
|
const { setValue } = useFormContext();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
Promise.all(
|
|
|
|
KEYS.map((attr) =>
|
2023-07-11 14:03:21 +00:00
|
|
|
adminClient.clients.getKeyInfo({ id: clientId, attr }),
|
|
|
|
),
|
2021-10-12 09:28:55 +00:00
|
|
|
),
|
|
|
|
(info) => setKeyInfo(info),
|
2023-07-11 14:03:21 +00:00
|
|
|
[refresh],
|
2021-10-12 09:28:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const generate = async (attr: KeyTypes) => {
|
|
|
|
const index = KEYS.indexOf(attr);
|
|
|
|
try {
|
|
|
|
const info = [...(keyInfo || [])];
|
|
|
|
info[index] = await adminClient.clients.generateKey({
|
|
|
|
id: clientId,
|
|
|
|
attr,
|
|
|
|
});
|
|
|
|
|
|
|
|
setKeyInfo(info);
|
2022-11-18 16:13:39 +00:00
|
|
|
saveAs(
|
2021-10-12 09:28:55 +00:00
|
|
|
new Blob([info[index].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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = selectedType ? KEYS_MAPPING[selectedType].key : "";
|
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("disableSigning", {
|
|
|
|
key: t(key),
|
|
|
|
}),
|
|
|
|
messageKey: t("disableSigningExplain", {
|
|
|
|
key: t(key),
|
|
|
|
}),
|
|
|
|
continueButtonLabel: "common:yes",
|
|
|
|
cancelButtonLabel: "common:no",
|
|
|
|
onConfirm: () => {
|
|
|
|
setValue(KEYS_MAPPING[selectedType!].name, "false");
|
|
|
|
save();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleReGenerateDialog, ReGenerateConfirm] = useConfirmDialog({
|
2023-09-13 14:05:17 +00:00
|
|
|
titleKey: "reGenerateSigning",
|
|
|
|
messageKey: "reGenerateSigningExplain",
|
2021-10-12 09:28:55 +00:00
|
|
|
continueButtonLabel: "common:yes",
|
|
|
|
cancelButtonLabel: "common:no",
|
|
|
|
onConfirm: () => {
|
|
|
|
generate(selectedType!);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light" className="keycloak__form">
|
|
|
|
{isChanged && (
|
|
|
|
<SamlKeysDialog
|
|
|
|
id={clientId}
|
|
|
|
attr={isChanged}
|
|
|
|
onClose={() => {
|
|
|
|
setIsChanged(undefined);
|
|
|
|
save();
|
|
|
|
setRefresh(refresh + 1);
|
|
|
|
}}
|
2021-11-15 07:30:53 +00:00
|
|
|
onCancel={() => {
|
|
|
|
setValue(KEYS_MAPPING[selectedType!].name, "false");
|
|
|
|
setIsChanged(undefined);
|
|
|
|
}}
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<DisableConfirm />
|
|
|
|
<ReGenerateConfirm />
|
|
|
|
{KEYS.map((attr, index) => (
|
2021-10-14 10:30:49 +00:00
|
|
|
<Fragment key={attr}>
|
2023-01-16 16:41:11 +00:00
|
|
|
{openImport === attr && (
|
2021-10-12 09:28:55 +00:00
|
|
|
<SamlImportKeyDialog
|
|
|
|
id={clientId}
|
|
|
|
attr={attr}
|
2023-01-16 16:41:11 +00:00
|
|
|
onClose={() => setImportOpen(undefined)}
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<KeySection
|
2022-10-26 13:31:33 +00:00
|
|
|
clientId={clientId}
|
2021-10-12 09:28:55 +00:00
|
|
|
keyInfo={keyInfo?.[index]}
|
|
|
|
attr={attr}
|
|
|
|
onChanged={setIsChanged}
|
|
|
|
onGenerate={(type, isNew) => {
|
|
|
|
setSelectedType(type);
|
|
|
|
if (!isNew) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
|
|
|
toggleReGenerateDialog();
|
|
|
|
}
|
|
|
|
}}
|
2023-01-16 16:41:11 +00:00
|
|
|
onImport={() => setImportOpen(attr)}
|
2021-10-12 09:28:55 +00:00
|
|
|
/>
|
2021-10-14 10:30:49 +00:00
|
|
|
</Fragment>
|
2021-10-12 09:28:55 +00:00
|
|
|
))}
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|