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";
|
|
|
|
import { camelCase } from "lodash";
|
|
|
|
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";
|
|
|
|
import type { EditClientPolicyParams } from "./routes/EditClientPolicy";
|
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-08 09:46:03 +00:00
|
|
|
import { DynamicComponents } from "../components/dynamic/DynamicComponents";
|
2021-11-01 22:18:27 +00:00
|
|
|
|
|
|
|
export type ItemType = { value: string };
|
2021-10-21 18:40:02 +00:00
|
|
|
|
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 { handleSubmit, control } = useForm<ClientPolicyRepresentation>({
|
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const [openConditionType, setOpenConditionType] = useState(false);
|
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>([]);
|
|
|
|
const [condition, setCondition] = useState<
|
|
|
|
ClientPolicyConditionRepresentation[]
|
|
|
|
>([]);
|
|
|
|
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>();
|
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
2021-11-01 22:18:27 +00:00
|
|
|
const form = useForm();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
|
|
|
(policies) => {
|
|
|
|
setPolicies(policies.policies ?? []);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async () => {
|
2021-11-01 22:18:27 +00:00
|
|
|
const updatedPolicies = policies.map((policy) => {
|
|
|
|
if (policy.name !== policyName) {
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formValues = form.getValues();
|
|
|
|
const configValues = formValues.config;
|
|
|
|
|
|
|
|
const writeConfig = () => {
|
|
|
|
if (condition[0]?.condition === "any-client") {
|
|
|
|
return {};
|
|
|
|
} else if (condition[0]?.condition === "client-access-type") {
|
|
|
|
return { type: [formValues["client-accesstype"].label] };
|
|
|
|
} else if (condition[0]?.condition === "client-updater-context") {
|
|
|
|
return {
|
|
|
|
"update-client-source": [Object.values(formValues)[0]],
|
|
|
|
};
|
|
|
|
} else if (condition[0]?.condition === "client-scopes") {
|
|
|
|
return {
|
|
|
|
type: Object.values(formValues)[0].type,
|
|
|
|
scopes: (Object.values(formValues)[0].scopes as ItemType[]).map(
|
|
|
|
(item) => (item as ItemType).value
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} else
|
|
|
|
return {
|
|
|
|
[Object.keys(configValues)[0]]: Object.values(
|
|
|
|
configValues?.[Object.keys(configValues)[0]]
|
|
|
|
).map((item) => (item as ItemType).value),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const conditions = (policy.conditions ?? []).concat({
|
|
|
|
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-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);
|
2021-10-21 18:40:02 +00:00
|
|
|
history.push(
|
|
|
|
`/${realm}/realm-settings/clientPolicies/${policyName}/edit-policy`
|
|
|
|
);
|
|
|
|
addAlert(
|
|
|
|
t("realm-settings:createClientConditionSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:createClientConditionError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
|
|
|
<FormPanel className="kc-login-screen" title={t("addCondition")}>
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("conditionType")}
|
|
|
|
fieldId="conditionType"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={
|
|
|
|
conditionType
|
2021-11-01 22:18:27 +00:00
|
|
|
? t(
|
|
|
|
`realm-settings-help:${camelCase(
|
|
|
|
conditionType.replace(/-/g, " ")
|
|
|
|
)}`
|
|
|
|
)
|
2021-10-21 18:40:02 +00:00
|
|
|
: t("anyClient")
|
|
|
|
}
|
|
|
|
forLabel={t("conditionType")}
|
|
|
|
forID="conditionType"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="conditions"
|
|
|
|
defaultValue={"any-client"}
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
placeholderText={t("selectACondition")}
|
|
|
|
toggleId="provider"
|
|
|
|
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);
|
|
|
|
}}
|
|
|
|
selections={conditionType}
|
|
|
|
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-01 22:18:27 +00:00
|
|
|
<FormProvider {...form}>
|
2021-11-08 09:46:03 +00:00
|
|
|
<DynamicComponents properties={conditionProperties} />
|
2021-11-01 22:18:27 +00:00
|
|
|
</FormProvider>
|
2021-10-21 18:40:02 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="edit-policy-tab-save"
|
|
|
|
isDisabled={conditionType === ""}
|
|
|
|
>
|
|
|
|
{t("common:add")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
onClick={() =>
|
|
|
|
history.push(
|
|
|
|
`/${realm}/realm-settings/clientPolicies/${policyName}/edit-policy`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|