2021-11-01 22:18:27 +00:00
|
|
|
import React, { useState } from "react";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-11-01 22:18:27 +00:00
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
2021-10-21 18:40:02 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { FormPanel } from "../components/scroll-form/FormPanel";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { camelCase } from "lodash-es";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useHistory, useParams } from "react-router";
|
|
|
|
import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-11-01 22:18:27 +00:00
|
|
|
import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation";
|
|
|
|
import type ClientPolicyConditionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyConditionRepresentation";
|
2021-11-09 18:27:25 +00:00
|
|
|
import {
|
|
|
|
EditClientPolicyParams,
|
|
|
|
toEditClientPolicy,
|
|
|
|
} from "./routes/EditClientPolicy";
|
|
|
|
import type { EditClientPolicyConditionParams } from "./routes/EditCondition";
|
2021-11-22 12:41:43 +00:00
|
|
|
import {
|
|
|
|
convertToMultiline,
|
|
|
|
toValue,
|
2021-12-08 15:08:42 +00:00
|
|
|
} from "../components/multi-line-input/multi-line-convert";
|
2021-11-22 12:41:43 +00:00
|
|
|
import {
|
|
|
|
COMPONENTS,
|
|
|
|
isValidComponentType,
|
|
|
|
} from "../components/dynamic/components";
|
2021-11-24 16:19:28 +00:00
|
|
|
import { MultivaluedChipsComponent } from "../components/dynamic/MultivaluedChipsComponent";
|
2021-11-23 14:59:04 +00:00
|
|
|
import { MultivaluedRoleComponent } from "../components/dynamic/MultivaluedRoleComponent";
|
2021-11-01 22:18:27 +00:00
|
|
|
export type ItemType = { value: string };
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
type ConfigProperty = ConfigPropertyRepresentation & {
|
|
|
|
config: any;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function NewClientPolicyCondition() {
|
2021-10-21 18:40:02 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const history = useHistory();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const [openConditionType, setOpenConditionType] = useState(false);
|
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>([]);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const [condition, setCondition] = useState<
|
|
|
|
ClientPolicyConditionRepresentation[]
|
|
|
|
>([]);
|
2021-11-09 18:27:25 +00:00
|
|
|
const [conditionData, setConditionData] =
|
|
|
|
useState<ClientPolicyConditionRepresentation>();
|
2021-10-21 18:40:02 +00:00
|
|
|
const [conditionType, setConditionType] = useState("");
|
2021-11-01 22:18:27 +00:00
|
|
|
const [conditionProperties, setConditionProperties] = useState<
|
|
|
|
ConfigPropertyRepresentation[]
|
|
|
|
>([]);
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const { policyName } = useParams<EditClientPolicyParams>();
|
2021-11-09 18:27:25 +00:00
|
|
|
const { conditionName } = useParams<EditClientPolicyConditionParams>();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
2021-11-22 12:41:43 +00:00
|
|
|
const form = useForm<ClientPolicyConditionRepresentation>({
|
|
|
|
shouldUnregister: false,
|
|
|
|
});
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
const setupForm = (
|
|
|
|
condition: ClientPolicyConditionRepresentation,
|
|
|
|
properties: ConfigPropertyRepresentation[]
|
|
|
|
) => {
|
2021-11-09 18:27:25 +00:00
|
|
|
form.reset();
|
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
Object.entries(condition.configuration!).map(([key, value]) => {
|
|
|
|
const formKey = `config.${key}`;
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
const property = properties.find((p) => p.name === key);
|
2021-11-23 14:59:04 +00:00
|
|
|
if (
|
|
|
|
property?.type === "MultivaluedString" &&
|
2021-11-24 16:19:28 +00:00
|
|
|
property.name !== "scopes" &&
|
|
|
|
property.name !== "groups"
|
2021-11-23 14:59:04 +00:00
|
|
|
) {
|
2021-11-22 12:41:43 +00:00
|
|
|
form.setValue(formKey, convertToMultiline(value));
|
2021-11-23 14:59:04 +00:00
|
|
|
} else if (property?.name === "client-scopes") {
|
|
|
|
form.setValue("config.scopes", value);
|
2021-11-22 12:41:43 +00:00
|
|
|
} else {
|
|
|
|
form.setValue(formKey, value);
|
2021-11-09 18:27:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
(policies) => {
|
|
|
|
setPolicies(policies.policies ?? []);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
if (conditionName) {
|
|
|
|
const currentPolicy = policies.policies?.find(
|
|
|
|
(item) => item.name === policyName
|
|
|
|
);
|
|
|
|
|
|
|
|
const typeAndConfigData = currentPolicy?.conditions?.find(
|
|
|
|
(item) => item.condition === conditionName
|
|
|
|
);
|
|
|
|
|
|
|
|
const currentCondition = conditionTypes?.find(
|
|
|
|
(condition) => condition.id === conditionName
|
|
|
|
);
|
|
|
|
|
|
|
|
setConditionData(typeAndConfigData!);
|
|
|
|
setConditionProperties(currentCondition?.properties!);
|
2021-11-22 12:41:43 +00:00
|
|
|
setupForm(typeAndConfigData!, currentCondition?.properties!);
|
2021-11-09 18:27:25 +00:00
|
|
|
}
|
2021-10-21 18:40:02 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
const save = async (configPolicy: ConfigProperty) => {
|
|
|
|
const configValues = configPolicy.config;
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-23 14:59:04 +00:00
|
|
|
const writeConfig = () => {
|
|
|
|
return conditionProperties.reduce((r: any, p) => {
|
2021-11-24 16:19:28 +00:00
|
|
|
p.type === "MultivaluedString" &&
|
|
|
|
p.name !== "scopes" &&
|
|
|
|
p.name !== "groups"
|
2021-11-22 12:41:43 +00:00
|
|
|
? (r[p.name!] = toValue(configValues[p.name!]))
|
|
|
|
: (r[p.name!] = configValues[p.name!]);
|
|
|
|
return r;
|
|
|
|
}, {});
|
2021-11-23 14:59:04 +00:00
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
const updatedPolicies = policies.map((policy) => {
|
|
|
|
if (policy.name !== policyName) {
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
let conditions = policy.conditions ?? [];
|
|
|
|
|
|
|
|
if (conditionName) {
|
|
|
|
const createdCondition = {
|
|
|
|
condition: conditionData?.condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const index = conditions.findIndex(
|
|
|
|
(condition) => conditionName === condition.condition
|
|
|
|
);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newConditions = [
|
|
|
|
...conditions.slice(0, index),
|
|
|
|
createdCondition,
|
|
|
|
...conditions.slice(index + 1),
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions: newConditions,
|
|
|
|
};
|
|
|
|
}
|
2021-11-01 22:18:27 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
conditions = conditions.concat({
|
2021-11-01 22:18:27 +00:00
|
|
|
condition: condition[0].condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
});
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions,
|
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
}) as ClientPolicyRepresentation[];
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
2021-11-01 22:18:27 +00:00
|
|
|
policies: updatedPolicies,
|
2021-10-21 18:40:02 +00:00
|
|
|
});
|
2021-11-01 22:18:27 +00:00
|
|
|
setPolicies(updatedPolicies);
|
2022-02-08 22:10:46 +00:00
|
|
|
history.push(toEditClientPolicy({ realm, policyName }));
|
2021-10-21 18:40:02 +00:00
|
|
|
addAlert(
|
2021-11-09 18:27:25 +00:00
|
|
|
conditionName
|
|
|
|
? t("realm-settings:updateClientConditionSuccess")
|
|
|
|
: t("realm-settings:createClientConditionSuccess"),
|
2021-10-21 18:40:02 +00:00
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:createClientConditionError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
2021-11-09 18:27:25 +00:00
|
|
|
<FormPanel
|
|
|
|
className="kc-login-screen"
|
|
|
|
title={conditionName ? t("editCondition") : t("addCondition")}
|
|
|
|
>
|
2021-10-21 18:40:02 +00:00
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
2021-11-22 12:41:43 +00:00
|
|
|
onSubmit={form.handleSubmit(save)}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("conditionType")}
|
|
|
|
fieldId="conditionType"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={
|
|
|
|
conditionType
|
2021-12-14 14:56:36 +00:00
|
|
|
? `realm-settings-help:${camelCase(
|
|
|
|
conditionType.replace(/-/g, " ")
|
|
|
|
)}`
|
|
|
|
: "realm-settings:anyClient"
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:conditionType"
|
2021-10-21 18:40:02 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="conditions"
|
|
|
|
defaultValue={"any-client"}
|
2021-11-22 12:41:43 +00:00
|
|
|
control={form.control}
|
2021-10-21 18:40:02 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
placeholderText={t("selectACondition")}
|
2021-11-22 12:41:43 +00:00
|
|
|
className="kc-conditionType-select"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="conditionType-select"
|
2021-10-21 18:40:02 +00:00
|
|
|
toggleId="provider"
|
2021-11-09 18:27:25 +00:00
|
|
|
isDisabled={!!conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
onToggle={(toggle) => setOpenConditionType(toggle)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value);
|
2021-11-01 22:18:27 +00:00
|
|
|
setConditionProperties(
|
|
|
|
(value as ComponentTypeRepresentation).properties
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
setConditionType((value as ComponentTypeRepresentation).id);
|
|
|
|
setCondition([
|
|
|
|
{
|
|
|
|
condition: (value as ComponentTypeRepresentation).id,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
setOpenConditionType(false);
|
|
|
|
}}
|
2021-11-09 18:27:25 +00:00
|
|
|
selections={conditionName ? conditionName : conditionType}
|
2021-10-21 18:40:02 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("conditionType")}
|
|
|
|
isOpen={openConditionType}
|
|
|
|
>
|
|
|
|
{conditionTypes?.map((condition) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={condition.id === value}
|
|
|
|
description={t(
|
2021-11-01 22:18:27 +00:00
|
|
|
`realm-settings-help:${camelCase(
|
|
|
|
condition.id.replace(/-/g, " ")
|
|
|
|
)}`
|
2021-10-21 18:40:02 +00:00
|
|
|
)}
|
|
|
|
key={condition.id}
|
|
|
|
value={condition}
|
|
|
|
>
|
|
|
|
{condition.id}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-11-22 12:41:43 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
<FormProvider {...form}>
|
2021-11-22 12:41:43 +00:00
|
|
|
{conditionProperties.map((property) => {
|
|
|
|
const componentType = property.type!;
|
2021-11-29 12:51:31 +00:00
|
|
|
if (property.name === "roles") {
|
2021-11-22 12:41:43 +00:00
|
|
|
return <MultivaluedRoleComponent {...property} />;
|
2021-11-29 12:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (property.name === "scopes" || property.name === "groups") {
|
2021-11-24 16:19:28 +00:00
|
|
|
return (
|
|
|
|
<MultivaluedChipsComponent
|
2021-11-29 12:51:31 +00:00
|
|
|
defaultValue={
|
|
|
|
property.name === "scopes" ? "offline_access" : "topgroup"
|
|
|
|
}
|
2021-11-24 16:19:28 +00:00
|
|
|
{...property}
|
|
|
|
/>
|
|
|
|
);
|
2021-11-29 12:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isValidComponentType(componentType)) {
|
2021-11-22 12:41:43 +00:00
|
|
|
const Component = COMPONENTS[componentType];
|
|
|
|
return <Component key={property.name} {...property} />;
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
`There is no editor registered for ${componentType}`
|
|
|
|
);
|
2021-11-09 18:27:25 +00:00
|
|
|
}
|
2021-11-22 12:41:43 +00:00
|
|
|
})}
|
2021-11-01 22:18:27 +00:00
|
|
|
</FormProvider>
|
2021-10-21 18:40:02 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-saveBtn"
|
|
|
|
isDisabled={conditionType === "" && !conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
2021-11-09 18:27:25 +00:00
|
|
|
{conditionName ? t("common:save") : t("common:add")}
|
2021-10-21 18:40:02 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-cancelBtn"
|
2021-10-21 18:40:02 +00:00
|
|
|
onClick={() =>
|
2021-11-09 18:27:25 +00:00
|
|
|
history.push(toEditClientPolicy({ realm, policyName }))
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|