2024-03-22 10:27:09 +00:00
|
|
|
import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation";
|
2021-11-17 08:27:56 +00:00
|
|
|
import {
|
2021-12-16 16:31:17 +00:00
|
|
|
AlertVariant,
|
2021-11-17 08:27:56 +00:00
|
|
|
Button,
|
|
|
|
Divider,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Radio,
|
|
|
|
} from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { HelpItem } from "ui-shared";
|
|
|
|
import { adminClient } from "../../admin-client";
|
2024-03-22 10:27:09 +00:00
|
|
|
import { DefaultSwitchControl } from "../../components/SwitchControl";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2023-05-24 12:11:06 +00:00
|
|
|
import { FixedButtonsGroup } from "../../components/form/FixedButtonGroup";
|
|
|
|
import { FormAccess } from "../../components/form/FormAccess";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
2024-03-22 10:27:09 +00:00
|
|
|
import { useAccess } from "../../context/access/Access";
|
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2021-12-01 11:05:24 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
2023-01-16 11:34:18 +00:00
|
|
|
import { DecisionStrategySelect } from "./DecisionStrategySelect";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { ImportDialog } from "./ImportDialog";
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
const POLICY_ENFORCEMENT_MODES = [
|
|
|
|
"ENFORCING",
|
|
|
|
"PERMISSIVE",
|
|
|
|
"DISABLED",
|
|
|
|
] as const;
|
|
|
|
|
2023-01-16 11:34:18 +00:00
|
|
|
export type FormFields = Omit<
|
|
|
|
ResourceServerRepresentation,
|
|
|
|
"scopes" | "resources"
|
|
|
|
>;
|
|
|
|
|
2021-11-17 08:27:56 +00:00
|
|
|
export const AuthorizationSettings = ({ clientId }: { clientId: string }) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-11-17 08:27:56 +00:00
|
|
|
const [resource, setResource] = useState<ResourceServerRepresentation>();
|
2021-12-01 11:05:24 +00:00
|
|
|
const [importDialog, toggleImportDialog] = useToggle();
|
|
|
|
|
2023-01-16 11:34:18 +00:00
|
|
|
const form = useForm<FormFields>({});
|
2022-05-04 17:51:17 +00:00
|
|
|
const { control, reset, handleSubmit } = form;
|
2021-11-17 08:27:56 +00:00
|
|
|
|
2021-12-16 16:31:17 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2023-11-28 13:07:11 +00:00
|
|
|
const { hasAccess } = useAccess();
|
|
|
|
|
|
|
|
const isDisabled = !hasAccess("manage-authorization");
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.clients.getResourceServer({ id: clientId }),
|
|
|
|
(resource) => {
|
|
|
|
setResource(resource);
|
|
|
|
reset(resource);
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[],
|
2021-11-17 08:27:56 +00:00
|
|
|
);
|
|
|
|
|
2021-12-16 16:31:17 +00:00
|
|
|
const importResource = async (value: ResourceServerRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.importResource({ id: clientId }, value);
|
|
|
|
addAlert(t("importResourceSuccess"), AlertVariant.success);
|
|
|
|
reset({ ...value });
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("importResourceError", error);
|
2021-12-16 16:31:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-16 11:34:18 +00:00
|
|
|
const onSubmit = async (resource: ResourceServerRepresentation) => {
|
2021-12-16 16:31:17 +00:00
|
|
|
try {
|
|
|
|
await adminClient.clients.updateResourceServer(
|
|
|
|
{ id: clientId },
|
2023-07-11 14:03:21 +00:00
|
|
|
resource,
|
2021-12-16 16:31:17 +00:00
|
|
|
);
|
|
|
|
addAlert(t("updateResourceSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("resourceSaveError", error);
|
2021-12-16 16:31:17 +00:00
|
|
|
}
|
2021-12-01 11:05:24 +00:00
|
|
|
};
|
|
|
|
|
2021-11-17 08:27:56 +00:00
|
|
|
if (!resource) {
|
2021-12-01 07:58:25 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-11-17 08:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
2021-12-01 11:05:24 +00:00
|
|
|
{importDialog && (
|
|
|
|
<ImportDialog
|
|
|
|
onConfirm={importResource}
|
|
|
|
closeDialog={toggleImportDialog}
|
|
|
|
/>
|
|
|
|
)}
|
2023-01-16 11:34:18 +00:00
|
|
|
<FormAccess
|
2023-11-28 13:07:11 +00:00
|
|
|
role="manage-authorization"
|
2023-01-16 11:34:18 +00:00
|
|
|
isHorizontal
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
2021-11-17 08:27:56 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("import")}
|
|
|
|
fieldId="import"
|
|
|
|
labelIcon={
|
2023-09-13 14:05:17 +00:00
|
|
|
<HelpItem helpText={t("importHelp")} fieldLabelId="import" />
|
2021-11-17 08:27:56 +00:00
|
|
|
}
|
|
|
|
>
|
2021-12-01 11:05:24 +00:00
|
|
|
<Button variant="secondary" onClick={toggleImportDialog}>
|
|
|
|
{t("import")}
|
|
|
|
</Button>
|
2021-11-17 08:27:56 +00:00
|
|
|
</FormGroup>
|
|
|
|
<Divider />
|
|
|
|
<FormGroup
|
|
|
|
label={t("policyEnforcementMode")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2023-09-08 13:17:17 +00:00
|
|
|
helpText={t("policyEnforcementModeHelp")}
|
2023-09-13 14:05:17 +00:00
|
|
|
fieldLabelId="policyEnforcementMode"
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="policyEnforcementMode"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="policyEnforcementMode"
|
|
|
|
data-testid="policyEnforcementMode"
|
2022-01-12 16:01:54 +00:00
|
|
|
defaultValue={POLICY_ENFORCEMENT_MODES[0]}
|
2021-11-17 08:27:56 +00:00
|
|
|
control={control}
|
2023-01-16 11:34:18 +00:00
|
|
|
render={({ field }) => (
|
2021-11-17 08:27:56 +00:00
|
|
|
<>
|
|
|
|
{POLICY_ENFORCEMENT_MODES.map((mode) => (
|
|
|
|
<Radio
|
|
|
|
id={mode}
|
|
|
|
key={mode}
|
|
|
|
data-testid={mode}
|
2023-01-16 11:34:18 +00:00
|
|
|
isChecked={field.value === mode}
|
2023-11-28 13:07:11 +00:00
|
|
|
isDisabled={isDisabled}
|
2021-11-17 08:27:56 +00:00
|
|
|
name="policyEnforcementMode"
|
2023-01-16 11:34:18 +00:00
|
|
|
onChange={() => field.onChange(mode)}
|
2021-11-17 08:27:56 +00:00
|
|
|
label={t(`policyEnforcementModes.${mode}`)}
|
|
|
|
className="pf-u-mb-md"
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-05-04 17:51:17 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<DecisionStrategySelect isLimited />
|
2024-03-22 10:27:09 +00:00
|
|
|
<DefaultSwitchControl
|
2021-11-17 08:27:56 +00:00
|
|
|
name="allowRemoteResourceManagement"
|
2024-03-22 10:27:09 +00:00
|
|
|
label={t("allowRemoteResourceManagement")}
|
|
|
|
labelIcon={t("allowRemoteResourceManagementHelp")}
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
2024-03-22 10:27:09 +00:00
|
|
|
</FormProvider>
|
2023-05-24 12:11:06 +00:00
|
|
|
<FixedButtonsGroup
|
2021-12-16 16:31:17 +00:00
|
|
|
name="authenticationSettings"
|
2021-11-17 08:27:56 +00:00
|
|
|
reset={() => reset(resource)}
|
2022-05-17 07:52:19 +00:00
|
|
|
isActive
|
2023-05-24 12:11:06 +00:00
|
|
|
isSubmit
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|