2021-06-03 18:31:47 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
FileUpload,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Switch,
|
|
|
|
TextInput,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import type ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2021-07-12 18:19:50 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
2021-06-03 18:31:47 +00:00
|
|
|
|
|
|
|
type RSAModalProps = {
|
2021-07-12 18:19:50 +00:00
|
|
|
providerType: string;
|
|
|
|
handleModalToggle: () => void;
|
|
|
|
refresh: () => void;
|
2021-06-03 18:31:47 +00:00
|
|
|
open: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const RSAModal = ({
|
|
|
|
providerType,
|
|
|
|
handleModalToggle,
|
|
|
|
open,
|
|
|
|
refresh,
|
2021-07-01 16:44:36 +00:00
|
|
|
}: RSAModalProps) => {
|
2021-07-12 18:19:50 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2021-06-03 18:31:47 +00:00
|
|
|
const serverInfo = useServerInfo();
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
const { handleSubmit, control } = useForm({});
|
|
|
|
const [isRSAalgDropdownOpen, setIsRSAalgDropdownOpen] = useState(false);
|
2021-07-12 18:19:50 +00:00
|
|
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-06-03 18:31:47 +00:00
|
|
|
|
|
|
|
const [keyFileName, setKeyFileName] = useState("");
|
|
|
|
const [certificateFileName, setCertificateFileName] = useState("");
|
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
const allComponentTypes =
|
2021-07-12 18:19:50 +00:00
|
|
|
serverInfo.componentTypes?.["org.keycloak.keys.KeyProvider"] ?? [];
|
2021-06-03 18:31:47 +00:00
|
|
|
|
|
|
|
const save = async (component: ComponentRepresentation) => {
|
|
|
|
try {
|
2021-07-12 18:19:50 +00:00
|
|
|
if (id) {
|
|
|
|
await adminClient.components.update(
|
|
|
|
{ id },
|
|
|
|
{
|
|
|
|
...component,
|
|
|
|
parentId: component.parentId,
|
|
|
|
providerId: providerType,
|
|
|
|
providerType: "org.keycloak.keys.KeyProvider",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
addAlert(t("saveProviderSuccess"), AlertVariant.success);
|
|
|
|
} else {
|
|
|
|
await adminClient.components.create({
|
|
|
|
...component,
|
|
|
|
parentId: component.parentId,
|
|
|
|
providerId: providerType,
|
|
|
|
providerType: "org.keycloak.keys.KeyProvider",
|
|
|
|
});
|
|
|
|
handleModalToggle();
|
|
|
|
addAlert(t("saveProviderSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
}
|
2021-06-03 18:31:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(
|
2021-07-12 18:19:50 +00:00
|
|
|
t("saveProviderError", {
|
|
|
|
error: error.response?.data?.errorMessage || error,
|
|
|
|
}),
|
2021-06-03 18:31:47 +00:00
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
className="add-provider-modal"
|
|
|
|
variant={ModalVariant.medium}
|
2021-07-12 18:19:50 +00:00
|
|
|
title={t("addProvider")}
|
2021-06-03 18:31:47 +00:00
|
|
|
isOpen={open}
|
|
|
|
onClose={handleModalToggle}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
data-testid="add-provider-button"
|
|
|
|
key="confirm"
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
form="add-provider"
|
|
|
|
>
|
|
|
|
{t("common:Add")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={() => {
|
|
|
|
handleModalToggle!();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
isHorizontal
|
|
|
|
id="add-provider"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save!)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
2021-07-12 18:19:50 +00:00
|
|
|
label={t("consoleDisplayName")}
|
2021-06-03 18:31:47 +00:00
|
|
|
fieldId="kc-console-display-name"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:displayName"
|
|
|
|
forLabel={t("loginTheme")}
|
|
|
|
forID="kc-console-display-name"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="name"
|
|
|
|
control={control}
|
|
|
|
defaultValue={providerType}
|
|
|
|
render={({ onChange }) => (
|
|
|
|
<TextInput
|
|
|
|
aria-label={t("consoleDisplayName")}
|
|
|
|
defaultValue={providerType}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange(value);
|
|
|
|
}}
|
|
|
|
data-testid="display-name-input"
|
|
|
|
></TextInput>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("common:enabled")}
|
|
|
|
fieldId="kc-enabled"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("realm-settings-help:enabled")}
|
|
|
|
forLabel={t("enabled")}
|
|
|
|
forID="kc-enabled"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="config.enabled"
|
|
|
|
control={control}
|
|
|
|
defaultValue={["true"]}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="kc-enabled"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={value[0] === "true"}
|
|
|
|
data-testid={
|
|
|
|
value[0] === "true"
|
|
|
|
? "internationalization-enabled"
|
|
|
|
: "internationalization-disabled"
|
|
|
|
}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange([value + ""]);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2021-07-12 18:19:50 +00:00
|
|
|
label={t("active")}
|
2021-06-03 18:31:47 +00:00
|
|
|
fieldId="kc-active"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:active"
|
|
|
|
forLabel={t("active")}
|
|
|
|
forID="kc-active"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="config.active"
|
|
|
|
control={control}
|
|
|
|
defaultValue={["true"]}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="kc-active"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={value[0] === "true"}
|
|
|
|
data-testid={
|
|
|
|
value[0] === "true"
|
|
|
|
? "internationalization-enabled"
|
|
|
|
: "internationalization-disabled"
|
|
|
|
}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange([value + ""]);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{providerType === "rsa" && (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
2021-07-12 18:19:50 +00:00
|
|
|
label={t("algorithm")}
|
2021-06-03 18:31:47 +00:00
|
|
|
fieldId="kc-algorithm"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:algorithm"
|
|
|
|
forLabel={t("algorithm")}
|
|
|
|
forID="kc-algorithm"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="algorithm"
|
|
|
|
defaultValue=""
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-rsa-algorithm"
|
|
|
|
onToggle={() =>
|
|
|
|
setIsRSAalgDropdownOpen(!isRSAalgDropdownOpen)
|
|
|
|
}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setIsRSAalgDropdownOpen(false);
|
|
|
|
}}
|
|
|
|
selections={[value + ""]}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("algorithm")}
|
|
|
|
isOpen={isRSAalgDropdownOpen}
|
|
|
|
data-testid="select-rsa-algorithm"
|
|
|
|
>
|
|
|
|
{allComponentTypes[4].properties[3].options!.map(
|
|
|
|
(p, idx) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={p === value}
|
|
|
|
key={`rsa-algorithm-${idx}`}
|
|
|
|
value={p}
|
|
|
|
></SelectOption>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2021-07-12 18:19:50 +00:00
|
|
|
label={t("privateRSAKey")}
|
2021-06-03 18:31:47 +00:00
|
|
|
fieldId="kc-private-rsa-key"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:privateRSAKey"
|
|
|
|
forLabel={t("privateRSAKey")}
|
|
|
|
forID="kc-rsa-key"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="config.privateKey"
|
|
|
|
control={control}
|
|
|
|
defaultValue={[]}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<FileUpload
|
|
|
|
id="importPrivateKey"
|
|
|
|
type="text"
|
|
|
|
value={value[0]}
|
|
|
|
filenamePlaceholder="Upload a PEM file or paste key below"
|
|
|
|
filename={keyFileName}
|
|
|
|
onChange={(value, fileName) => {
|
|
|
|
setKeyFileName(fileName);
|
|
|
|
onChange([value]);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
2021-07-12 18:19:50 +00:00
|
|
|
label={t("x509Certificate")}
|
2021-06-03 18:31:47 +00:00
|
|
|
fieldId="kc-aes-keysize"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:x509Certificate"
|
|
|
|
forLabel={t("x509Certificate")}
|
|
|
|
forID="kc-x509-certificatw"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="config.certificate"
|
|
|
|
control={control}
|
|
|
|
defaultValue={[]}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<FileUpload
|
|
|
|
id="importCertificate"
|
|
|
|
type="text"
|
|
|
|
value={value[0]}
|
|
|
|
filenamePlaceholder="Upload a PEM file or paste key below"
|
|
|
|
filename={certificateFileName}
|
|
|
|
onChange={(value, fileName) => {
|
|
|
|
setCertificateFileName(fileName);
|
|
|
|
onChange([value]);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|