2021-05-14 18:58:08 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useHistory, useRouteMatch } from "react-router-dom";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Button, ButtonVariant, PageSection } from "@patternfly/react-core";
|
2021-05-27 07:01:55 +00:00
|
|
|
import { cellWidth } from "@patternfly/react-table";
|
|
|
|
|
2021-05-04 17:58:18 +00:00
|
|
|
import type { KeyMetadataRepresentation } from "keycloak-admin/lib/defs/keyMetadataRepresentation";
|
2021-05-27 07:01:55 +00:00
|
|
|
import type ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation";
|
2021-05-14 18:58:08 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { emptyFormatter } from "../util";
|
2021-05-27 07:01:55 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-05-14 18:58:08 +00:00
|
|
|
|
|
|
|
import "./RealmSettingsSection.css";
|
|
|
|
|
|
|
|
type KeyData = KeyMetadataRepresentation & {
|
|
|
|
provider?: string;
|
|
|
|
};
|
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
type KeysListTabProps = {
|
|
|
|
realmComponents: ComponentRepresentation[];
|
2021-05-14 18:58:08 +00:00
|
|
|
};
|
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
export const KeysListTab = ({ realmComponents }: KeysListTabProps) => {
|
2021-05-14 18:58:08 +00:00
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const history = useHistory();
|
|
|
|
const { url } = useRouteMatch();
|
|
|
|
|
|
|
|
const [publicKey, setPublicKey] = useState("");
|
|
|
|
const [certificate, setCertificate] = useState("");
|
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm: realmName } = useRealm();
|
|
|
|
|
2021-05-14 18:58:08 +00:00
|
|
|
const loader = async () => {
|
2021-05-27 07:01:55 +00:00
|
|
|
const keysMetaData = await adminClient.realms.getKeys({
|
|
|
|
realm: realmName,
|
|
|
|
});
|
|
|
|
const keys = keysMetaData.keys;
|
2021-05-14 18:58:08 +00:00
|
|
|
|
2021-05-27 07:01:55 +00:00
|
|
|
return keys?.map((key) => {
|
|
|
|
const provider = realmComponents.find(
|
|
|
|
(component: ComponentRepresentation) => component.id === key.providerId
|
|
|
|
);
|
|
|
|
return { ...key, provider: provider?.name } as KeyData;
|
|
|
|
})!;
|
|
|
|
};
|
2021-05-14 18:58:08 +00:00
|
|
|
|
|
|
|
const [togglePublicKeyDialog, PublicKeyDialog] = useConfirmDialog({
|
|
|
|
titleKey: t("realm-settings:publicKeys").slice(0, -1),
|
|
|
|
messageKey: publicKey,
|
|
|
|
continueButtonLabel: "common:close",
|
|
|
|
continueButtonVariant: ButtonVariant.primary,
|
|
|
|
onConfirm: async () => {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleCertificateDialog, CertificateDialog] = useConfirmDialog({
|
|
|
|
titleKey: t("realm-settings:certificate"),
|
|
|
|
messageKey: certificate,
|
|
|
|
continueButtonLabel: "common:close",
|
|
|
|
continueButtonVariant: ButtonVariant.primary,
|
|
|
|
onConfirm: async () => {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const goToCreate = () => history.push(`${url}/add-role`);
|
|
|
|
|
|
|
|
const ProviderRenderer = ({ provider }: KeyData) => {
|
|
|
|
return <>{provider}</>;
|
|
|
|
};
|
|
|
|
|
2021-05-18 15:17:52 +00:00
|
|
|
const ButtonRenderer = ({ type, publicKey, certificate }: KeyData) => {
|
|
|
|
if (type === "EC") {
|
2021-04-30 18:48:57 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
togglePublicKeyDialog();
|
|
|
|
setPublicKey(publicKey!);
|
|
|
|
}}
|
|
|
|
variant="secondary"
|
|
|
|
id="kc-public-key"
|
|
|
|
>
|
|
|
|
{t("realm-settings:publicKeys").slice(0, -1)}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
2021-05-18 15:17:52 +00:00
|
|
|
} else if (type === "RSA") {
|
2021-05-14 18:58:08 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-05-18 15:41:38 +00:00
|
|
|
<div className="button-wrapper">
|
2021-04-30 18:48:57 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
togglePublicKeyDialog();
|
|
|
|
setPublicKey(publicKey!);
|
|
|
|
}}
|
|
|
|
variant="secondary"
|
|
|
|
id="kc-rsa-public-key"
|
|
|
|
>
|
|
|
|
{t("realm-settings:publicKeys").slice(0, -1)}
|
|
|
|
</Button>
|
2021-05-14 18:58:08 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
toggleCertificateDialog();
|
|
|
|
setCertificate(certificate!);
|
|
|
|
}}
|
|
|
|
variant="secondary"
|
|
|
|
id="kc-certificate"
|
|
|
|
>
|
|
|
|
{t("realm-settings:certificate")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageSection variant="light" padding={{ default: "noPadding" }}>
|
|
|
|
<PublicKeyDialog />
|
|
|
|
<CertificateDialog />
|
|
|
|
<KeycloakDataTable
|
2021-04-30 18:48:57 +00:00
|
|
|
isNotCompact={true}
|
2021-05-14 18:58:08 +00:00
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="realm-settings:keysList"
|
|
|
|
searchPlaceholderKey="realm-settings:searchKey"
|
|
|
|
canSelectAll
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "algorithm",
|
|
|
|
displayKey: "realm-settings:algorithm",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
transforms: [cellWidth(15)],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "type",
|
|
|
|
displayKey: "realm-settings:type",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
transforms: [cellWidth(10)],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "kid",
|
|
|
|
displayKey: "realm-settings:kid",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provider",
|
|
|
|
displayKey: "realm-settings:provider",
|
|
|
|
cellRenderer: ProviderRenderer,
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "publicKeys",
|
|
|
|
displayKey: "realm-settings:publicKeys",
|
|
|
|
cellRenderer: ButtonRenderer,
|
|
|
|
cellFormatters: [],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noRoles")}
|
|
|
|
instructions={t("noRolesInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|