2020-12-07 19:23:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Badge,
|
|
|
|
Button,
|
2021-02-24 07:59:12 +00:00
|
|
|
ButtonVariant,
|
2020-12-07 19:23:18 +00:00
|
|
|
PageSection,
|
2021-03-03 07:02:40 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
2021-07-21 15:08:40 +00:00
|
|
|
ToolbarItem,
|
2020-12-07 19:23:18 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-10-01 10:52:24 +00:00
|
|
|
import { cellWidth, IRowData, TableText } from "@patternfly/react-table";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
2021-10-20 14:26:05 +00:00
|
|
|
import type { ClientQuery } from "@keycloak/keycloak-admin-client/lib/resources/clients";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-18 12:09:49 +00:00
|
|
|
import { Link } from "react-router-dom";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-06-24 07:35:13 +00:00
|
|
|
import { formattedLinkTableCell } from "../components/external-link/FormattedLink";
|
2021-10-01 10:52:24 +00:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
KeycloakDataTable,
|
|
|
|
} from "../components/table-toolbar/KeycloakDataTable";
|
2020-09-25 17:29:03 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-06-24 07:35:13 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2022-08-22 15:09:41 +00:00
|
|
|
import { emptyFormatter, exportClient } from "../util";
|
|
|
|
import { convertClientToUrl } from "../utils/client-url";
|
2021-03-04 08:20:37 +00:00
|
|
|
import { InitialAccessTokenList } from "./initial-access/InitialAccessTokenList";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { toAddClient } from "./routes/AddClient";
|
|
|
|
import { toClient } from "./routes/Client";
|
|
|
|
import { toImportClient } from "./routes/ImportClient";
|
2021-10-01 14:36:36 +00:00
|
|
|
import { isRealmClient, getProtocolName } from "./utils";
|
2021-12-21 15:32:53 +00:00
|
|
|
import helpUrls from "../help-urls";
|
2022-05-17 07:52:19 +00:00
|
|
|
import { useAccess } from "../context/access/Access";
|
2022-06-22 11:35:10 +00:00
|
|
|
import {
|
|
|
|
RoutableTabs,
|
2023-01-04 10:33:43 +00:00
|
|
|
useRoutableTab,
|
2022-06-22 11:35:10 +00:00
|
|
|
} from "../components/routable-tabs/RoutableTabs";
|
|
|
|
import { ClientsTab, toClients } from "./routes/Clients";
|
2023-02-03 11:56:20 +00:00
|
|
|
import { ClientRegistration } from "./registration/ClientRegistration";
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function ClientsSection() {
|
2020-09-10 18:04:03 +00:00
|
|
|
const { t } = useTranslation("clients");
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-03-05 10:18:47 +00:00
|
|
|
const { realm } = useRealm();
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2021-02-24 07:59:12 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
|
|
|
const [selectedClient, setSelectedClient] = useState<ClientRepresentation>();
|
|
|
|
|
2022-05-17 07:52:19 +00:00
|
|
|
const { hasAccess } = useAccess();
|
|
|
|
const isManager = hasAccess("manage-clients");
|
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
2021-10-20 14:26:05 +00:00
|
|
|
const params: ClientQuery = {
|
2020-12-07 19:23:18 +00:00
|
|
|
first: first!,
|
|
|
|
max: max!,
|
|
|
|
};
|
2020-10-05 18:13:23 +00:00
|
|
|
if (search) {
|
|
|
|
params.clientId = search;
|
2021-10-20 14:26:05 +00:00
|
|
|
params.search = true;
|
2020-10-05 18:13:23 +00:00
|
|
|
}
|
2020-12-07 19:23:18 +00:00
|
|
|
return await adminClient.clients.find({ ...params });
|
|
|
|
};
|
|
|
|
|
2023-01-04 10:33:43 +00:00
|
|
|
const useTab = (tab: ClientsTab) => useRoutableTab(toClients({ realm, tab }));
|
|
|
|
|
|
|
|
const listTab = useTab("list");
|
|
|
|
const initialAccessTokenTab = useTab("initial-access-token");
|
2023-02-03 11:56:20 +00:00
|
|
|
const clientRegistrationTab = useTab("client-registration");
|
2023-01-04 10:33:43 +00:00
|
|
|
|
2021-02-24 07:59:12 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("clientDelete", { clientId: selectedClient?.clientId }),
|
|
|
|
messageKey: "clients:clientDeleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.del({
|
|
|
|
id: selectedClient!.id!,
|
|
|
|
});
|
|
|
|
addAlert(t("clientDeletedSuccess"), AlertVariant.success);
|
2021-04-29 06:28:59 +00:00
|
|
|
refresh();
|
2021-02-24 07:59:12 +00:00
|
|
|
} catch (error) {
|
2021-09-27 14:54:01 +00:00
|
|
|
addError("clients:clientDeleteError", error);
|
2021-02-24 07:59:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const ClientDetailLink = (client: ClientRepresentation) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<Link
|
|
|
|
key={client.id}
|
|
|
|
to={toClient({ realm, clientId: client.id!, tab: "settings" })}
|
|
|
|
>
|
|
|
|
{client.clientId}
|
|
|
|
{!client.enabled && (
|
|
|
|
<Badge key={`${client.id}-disabled`} isRead className="pf-u-ml-sm">
|
|
|
|
{t("common:disabled")}
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</Link>
|
2020-12-07 19:23:18 +00:00
|
|
|
);
|
2021-02-25 09:10:28 +00:00
|
|
|
|
2022-12-22 08:31:09 +00:00
|
|
|
const ClientName = (client: ClientRepresentation) => (
|
|
|
|
<TableText wrapModifier="truncate">
|
|
|
|
{emptyFormatter()(client.name)}
|
|
|
|
</TableText>
|
|
|
|
);
|
|
|
|
|
2021-06-24 07:35:13 +00:00
|
|
|
const ClientDescription = (client: ClientRepresentation) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<TableText wrapModifier="truncate">
|
|
|
|
{emptyFormatter()(client.description)}
|
|
|
|
</TableText>
|
2021-06-24 07:35:13 +00:00
|
|
|
);
|
|
|
|
|
2022-05-17 07:52:19 +00:00
|
|
|
const ToolbarItems = () => {
|
|
|
|
if (!isManager) return <span />;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
component={(props) => (
|
|
|
|
<Link {...props} to={toAddClient({ realm })} />
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("createClient")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
component={(props) => (
|
|
|
|
<Link {...props} to={toImportClient({ realm })} />
|
|
|
|
)}
|
|
|
|
variant="link"
|
2022-10-03 11:00:26 +00:00
|
|
|
data-testid="importClient"
|
2022-05-17 07:52:19 +00:00
|
|
|
>
|
|
|
|
{t("importClient")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-03 19:25:05 +00:00
|
|
|
return (
|
2020-09-25 17:29:03 +00:00
|
|
|
<>
|
|
|
|
<ViewHeader
|
|
|
|
titleKey="clients:clientList"
|
|
|
|
subKey="clients:clientsExplain"
|
2021-12-21 15:32:53 +00:00
|
|
|
helpUrl={helpUrls.clientsUrl}
|
2021-03-03 07:02:40 +00:00
|
|
|
divider={false}
|
2020-09-25 17:29:03 +00:00
|
|
|
/>
|
2021-02-16 19:18:09 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2022-06-22 11:35:10 +00:00
|
|
|
<RoutableTabs
|
|
|
|
mountOnEnter
|
|
|
|
isBox
|
|
|
|
defaultLocation={toClients({
|
|
|
|
realm,
|
|
|
|
tab: "list",
|
|
|
|
})}
|
|
|
|
>
|
2021-03-03 07:02:40 +00:00
|
|
|
<Tab
|
|
|
|
data-testid="list"
|
|
|
|
title={<TabTitleText>{t("clientsList")}</TabTitleText>}
|
2023-01-04 10:33:43 +00:00
|
|
|
{...listTab}
|
2021-03-03 07:02:40 +00:00
|
|
|
>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="clients:clientList"
|
|
|
|
searchPlaceholderKey="clients:searchForClient"
|
2022-05-17 07:52:19 +00:00
|
|
|
toolbarItem={<ToolbarItems />}
|
2021-10-01 10:52:24 +00:00
|
|
|
actionResolver={(rowData: IRowData) => {
|
|
|
|
const client: ClientRepresentation = rowData.data;
|
|
|
|
const actions: Action<ClientRepresentation>[] = [
|
|
|
|
{
|
|
|
|
title: t("common:export"),
|
|
|
|
onClick() {
|
|
|
|
exportClient(client);
|
|
|
|
},
|
2021-03-03 07:02:40 +00:00
|
|
|
},
|
2021-10-01 10:52:24 +00:00
|
|
|
];
|
|
|
|
|
2022-05-30 09:23:24 +00:00
|
|
|
if (
|
|
|
|
!isRealmClient(client) &&
|
|
|
|
(isManager || client.access?.configure)
|
|
|
|
) {
|
2021-10-01 10:52:24 +00:00
|
|
|
actions.push({
|
|
|
|
title: t("common:delete"),
|
|
|
|
onClick() {
|
|
|
|
setSelectedClient(client);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}}
|
2021-03-03 07:02:40 +00:00
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "clientId",
|
2021-04-06 17:39:27 +00:00
|
|
|
displayKey: "common:clientId",
|
2022-12-22 08:31:09 +00:00
|
|
|
transforms: [cellWidth(20)],
|
2021-03-03 07:02:40 +00:00
|
|
|
cellRenderer: ClientDetailLink,
|
|
|
|
},
|
2022-12-22 08:31:09 +00:00
|
|
|
{
|
|
|
|
name: "clientName",
|
|
|
|
displayKey: "common:clientName",
|
|
|
|
transforms: [cellWidth(20)],
|
|
|
|
cellRenderer: ClientName,
|
|
|
|
},
|
2021-10-01 14:36:36 +00:00
|
|
|
{
|
|
|
|
name: "protocol",
|
|
|
|
displayKey: "common:type",
|
2022-12-22 08:31:09 +00:00
|
|
|
transforms: [cellWidth(10)],
|
2021-10-01 14:36:36 +00:00
|
|
|
cellRenderer: (client) =>
|
|
|
|
getProtocolName(t, client.protocol ?? "openid-connect"),
|
|
|
|
},
|
2021-03-03 07:02:40 +00:00
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: "common:description",
|
2022-12-22 08:31:09 +00:00
|
|
|
transforms: [cellWidth(30)],
|
2021-06-24 07:35:13 +00:00
|
|
|
cellRenderer: ClientDescription,
|
2021-03-03 07:02:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "baseUrl",
|
|
|
|
displayKey: "clients:homeURL",
|
2022-12-22 08:31:09 +00:00
|
|
|
transforms: [cellWidth(20)],
|
2021-03-03 07:02:40 +00:00
|
|
|
cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
|
2022-08-22 15:09:41 +00:00
|
|
|
cellRenderer: (c) =>
|
|
|
|
convertClientToUrl(c, adminClient.baseUrl),
|
2021-03-03 07:02:40 +00:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="initialAccessToken"
|
|
|
|
title={<TabTitleText>{t("initialAccessToken")}</TabTitleText>}
|
2023-01-04 10:33:43 +00:00
|
|
|
{...initialAccessTokenTab}
|
2021-03-03 07:02:40 +00:00
|
|
|
>
|
|
|
|
<InitialAccessTokenList />
|
|
|
|
</Tab>
|
2023-02-03 11:56:20 +00:00
|
|
|
<Tab
|
|
|
|
data-testid="registration"
|
|
|
|
title={<TabTitleText>{t("clientRegistration")}</TabTitleText>}
|
|
|
|
{...clientRegistrationTab}
|
|
|
|
>
|
|
|
|
<ClientRegistration />
|
|
|
|
</Tab>
|
2022-06-22 11:35:10 +00:00
|
|
|
</RoutableTabs>
|
2020-09-25 17:29:03 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-03 19:25:05 +00:00
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|