2021-05-04 08:11:58 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardHeader,
|
|
|
|
CardTitle,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Switch,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
} from "@patternfly/react-core";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { saveAs } from "file-saver";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-05-04 08:11:58 +00:00
|
|
|
|
2021-08-26 08:39:35 +00:00
|
|
|
import type CertificateRepresentation from "@keycloak/keycloak-admin-client/lib/defs/certificateRepresentation";
|
|
|
|
import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { Controller, useFormContext, useWatch } from "react-hook-form-v7";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2021-05-04 08:11:58 +00:00
|
|
|
import { FormAccess } from "../../components/form-access/FormAccess";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
2022-08-09 08:32:16 +00:00
|
|
|
import { convertAttributeNameToForm } from "../../util";
|
2022-12-02 14:54:30 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
|
|
|
import { FormFields } from "../ClientDetails";
|
2021-10-12 09:28:55 +00:00
|
|
|
import { Certificate } from "./Certificate";
|
2022-12-02 14:54:30 +00:00
|
|
|
import { GenerateKeyDialog, getFileExtension } from "./GenerateKeyDialog";
|
|
|
|
import { ImportFile, ImportKeyDialog } from "./ImportKeyDialog";
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
type KeysProps = {
|
|
|
|
save: () => void;
|
|
|
|
clientId: string;
|
2022-05-30 09:23:24 +00:00
|
|
|
hasConfigureAccess?: boolean;
|
2021-05-04 08:11:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const attr = "jwt.credential";
|
|
|
|
|
2022-05-30 09:23:24 +00:00
|
|
|
export const Keys = ({ clientId, save, hasConfigureAccess }: KeysProps) => {
|
2021-05-04 08:11:58 +00:00
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
register,
|
2022-03-16 10:37:45 +00:00
|
|
|
getValues,
|
2021-05-04 08:11:58 +00:00
|
|
|
formState: { isDirty },
|
2022-12-02 14:54:30 +00:00
|
|
|
} = useFormContext<FormFields>();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
const [keyInfo, setKeyInfo] = useState<CertificateRepresentation>();
|
2021-11-30 13:07:44 +00:00
|
|
|
const [openGenerateKeys, toggleOpenGenerateKeys, setOpenGenerateKeys] =
|
|
|
|
useToggle();
|
|
|
|
const [openImportKeys, toggleOpenImportKeys, setOpenImportKeys] = useToggle();
|
2022-10-11 11:08:47 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(key + 1);
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
const useJwksUrl = useWatch({
|
|
|
|
control,
|
2022-12-06 15:58:43 +00:00
|
|
|
name: convertAttributeNameToForm<FormFields>("attributes.use.jwks.url"),
|
2021-05-04 08:11:58 +00:00
|
|
|
defaultValue: "false",
|
|
|
|
});
|
2021-04-29 15:01:27 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.clients.getKeyInfo({ id: clientId, attr }),
|
|
|
|
(info) => setKeyInfo(info),
|
2022-10-11 11:08:47 +00:00
|
|
|
[key]
|
2021-05-04 08:11:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const generate = async (config: KeyStoreConfig) => {
|
|
|
|
try {
|
|
|
|
const keyStore = await adminClient.clients.generateAndDownloadKey(
|
|
|
|
{
|
|
|
|
id: clientId,
|
|
|
|
attr,
|
|
|
|
},
|
|
|
|
config
|
|
|
|
);
|
2022-11-18 16:13:39 +00:00
|
|
|
saveAs(
|
2021-05-04 08:11:58 +00:00
|
|
|
new Blob([keyStore], { type: "application/octet-stream" }),
|
2022-11-24 16:59:02 +00:00
|
|
|
`keystore.${getFileExtension(config.format ?? "")}`
|
2021-05-04 08:11:58 +00:00
|
|
|
);
|
|
|
|
addAlert(t("generateSuccess"), AlertVariant.success);
|
2022-10-11 11:08:47 +00:00
|
|
|
refresh();
|
2021-05-04 08:11:58 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("clients:generateError", error);
|
2021-05-04 08:11:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const importKey = async (importFile: ImportFile) => {
|
|
|
|
try {
|
|
|
|
const formData = new FormData();
|
|
|
|
const { file, ...rest } = importFile;
|
|
|
|
Object.entries(rest).map((entry) =>
|
|
|
|
formData.append(entry[0], entry[1] as string)
|
|
|
|
);
|
|
|
|
formData.append("file", file.value);
|
|
|
|
|
|
|
|
await adminClient.clients.uploadCertificate(
|
|
|
|
{ id: clientId, attr },
|
|
|
|
formData
|
|
|
|
);
|
|
|
|
addAlert(t("importSuccess"), AlertVariant.success);
|
2022-10-11 11:08:47 +00:00
|
|
|
refresh();
|
2021-05-04 08:11:58 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("clients:importError", error);
|
2021-05-04 08:11:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light" className="keycloak__form">
|
|
|
|
{openGenerateKeys && (
|
|
|
|
<GenerateKeyDialog
|
2022-03-16 10:37:45 +00:00
|
|
|
clientId={getValues("clientId")!}
|
2021-11-30 13:07:44 +00:00
|
|
|
toggleDialog={toggleOpenGenerateKeys}
|
2021-05-04 08:11:58 +00:00
|
|
|
save={generate}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{openImportKeys && (
|
2021-11-30 13:07:44 +00:00
|
|
|
<ImportKeyDialog toggleDialog={toggleOpenImportKeys} save={importKey} />
|
2021-05-04 08:11:58 +00:00
|
|
|
)}
|
|
|
|
<Card isFlat>
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle>{t("jwksUrlConfig")}</CardTitle>
|
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
|
|
|
<TextContent>
|
|
|
|
<Text>{t("keysIntro")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
</CardBody>
|
|
|
|
<CardBody>
|
2022-05-30 09:23:24 +00:00
|
|
|
<FormAccess
|
|
|
|
role="manage-clients"
|
|
|
|
fineGrainedAccess={hasConfigureAccess}
|
|
|
|
isHorizontal
|
|
|
|
>
|
2021-05-04 08:11:58 +00:00
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t("useJwksUrl")}
|
|
|
|
fieldId="useJwksUrl"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:useJwksUrl"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:useJwksUrl"
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
2022-08-09 08:32:16 +00:00
|
|
|
name={convertAttributeNameToForm("attributes.use.jwks.url")}
|
2021-05-04 08:11:58 +00:00
|
|
|
control={control}
|
2022-12-02 14:54:30 +00:00
|
|
|
render={({ field }) => (
|
2021-05-04 08:11:58 +00:00
|
|
|
<Switch
|
|
|
|
data-testid="useJwksUrl"
|
2021-07-12 14:09:14 +00:00
|
|
|
id="useJwksUrl-switch"
|
2021-05-04 08:11:58 +00:00
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2022-12-02 14:54:30 +00:00
|
|
|
isChecked={field.value === "true"}
|
|
|
|
onChange={(value) => field.onChange(`${value}`)}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t("useJwksUrl")}
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-08-26 12:15:28 +00:00
|
|
|
{useJwksUrl !== "true" &&
|
|
|
|
(keyInfo ? (
|
2021-10-12 09:28:55 +00:00
|
|
|
<Certificate plain keyInfo={keyInfo} />
|
2021-08-26 12:15:28 +00:00
|
|
|
) : (
|
|
|
|
"No client certificate configured"
|
|
|
|
))}
|
2021-05-04 08:11:58 +00:00
|
|
|
{useJwksUrl === "true" && (
|
|
|
|
<FormGroup
|
|
|
|
label={t("jwksUrl")}
|
|
|
|
fieldId="jwksUrl"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:jwksUrl"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:jwksUrl"
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-05-04 08:11:58 +00:00
|
|
|
id="jwksUrl"
|
2022-12-02 14:54:30 +00:00
|
|
|
{...register(
|
|
|
|
convertAttributeNameToForm("attributes.jwks.url")
|
|
|
|
)}
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
data-testid="saveKeys"
|
|
|
|
onClick={save}
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
data-testid="generate"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => setOpenGenerateKeys(true)}
|
|
|
|
>
|
|
|
|
{t("generateNewKeys")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
data-testid="import"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => setOpenImportKeys(true)}
|
|
|
|
isDisabled={useJwksUrl === "true"}
|
|
|
|
>
|
|
|
|
{t("import")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|