8000 Use the selected client when evaluating authorization policies by JBoogieman · Pull Request #55494 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
import { UserSelect } from "../../components/users/UserSelect";
import { useAccess } from "../../context/access/Access";
import { useRealm } from "../../context/realm-context/RealmContext";
import { FormFields } from "../ClientDetails";
import { defaultContextAttributes } from "../utils";
import { KeyBasedAttributeInput } from "./KeyBasedAttributeInput";
import { Results } from "./evaluate/Results";
Expand All @@ -51,7 +50,7 @@ interface EvaluateFormInputs
attributes: Record<string, string>[];
};
resources?: Record<string, string>[];
client: FormFields;
client: string;
user: string[];
}

Expand Down Expand Up @@ -144,7 +143,7 @@ const AuthorizationEvaluateContent = ({ client }: Props) => {
const keys = keyValueToArray(formValues.resources as KeyValueType[]);
const resEval: ResourceEvaluation = {
roleIds: formValues.roleIds ?? [],
clientId: formValues.client.id!,
clientId: formValues.client || undefined,
userId: formValues.user![0],
resources: resources
.filter((resource) => Object.keys(keys).includes(resource.name!))
Expand Down Expand Up @@ -201,7 +200,8 @@ const AuthorizationEvaluateContent = ({ client }: Props) => {
name="client"
label="client"
helpText={"clientHelp"}
defaultValue={client.clientId}
clientKey="id"
defaultValue={client.id}
/>
<UserSelect
name="user"
Expand Down
88 changes: 88 additions & 0 deletions js/apps/admin-ui/test/clients/authorization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect } from "@playwright/test";
import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation.js";
import { Logic } from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation.js";
import adminClient from "../utils/AdminClient.ts";
import {
Expand Down Expand Up @@ -644,3 +645,90 @@ test.describe.serial("Client authorization evaluate permission scopes", () => {
});
});
});

test.describe.serial("Client authorization evaluate selected client", () => {
const clientId = `client-authz-evaluate-client-${crypto.randomUUID()}`;
const otherClientId = `client-authz-evaluate-other-${crypto.randomUUID()}`;
const username = `evaluate-client-user-${crypto.randomUUID()}`;

test.beforeAll(async () => {
await adminClient.createClient({
protocol: "openid-connect",
clientId,
publicClient: false,
authorizationServicesEnabled: true,
serviceAccountsEnabled: true,
standardFlowEnabled: true,
});
const otherClient = await adminClient.createClient({
protocol: "openid-connect",
clientId: otherClientId,
});
await adminClient.createUser({ username, enabled: true });

const scope = await adminClient.createAuthorizationScope(clientId, {
name: "doc:read",
});
const documents = await adminClient.createResource(clientId, {
name: "Documents",
scopes: [scope],
});
// Grants only when the evaluated token's azp is the other client.
const otherClientPolicy = await adminClient.createAuthzPolicy(clientId, {
name: "permit-other-client",
type: "client",
logic: Logic.POSITIVE,
clients: [otherClient.id],
} as PolicyRepresentation);
await adminClient.createAuthzPermission(clientId, {
name: "Read documents",
type: "scope",
resources: [documents._id!],
scopes: [scope.id!],
policies: [otherClientPolicy.id!],
});
});

test.afterAll(async () => {
await adminClient.deleteClient(clientId);
await adminClient.deleteClient(otherClientId);
await adminClient.deleteUser(username);
});

test.beforeEach(async ({ page }) => {
await login(page);
await goToClients(page);
await searchItem(page, "Search for client", clientId);
await clickTableRowItem(page, clientId);
await goToAuthorizationTab(page);
await goToEvaluateSubTab(page);
await selectItem(page, page.getByTestId("user"), username);
});

test("Should evaluate as the resource server client by default", async ({
page,
}) => {
await page.getByTestId("authorization-eval").click();

await expandEvaluateResource(page, "Documents");
await assertEvaluatePermissionScopes(page, "Read documents", {
status: "Deny",
granted: "-",
denied: "doc:read",
});
});

test("Should evaluate as the selected client", async ({ page }) => {
// The selected client used to be dropped from the request, so the
// evaluation always ran as the resource server client.
await inputClient(page, otherClientId);
await page.getByTestId("authorization-eval").click();

await expandEvaluateResource(page, "Documents");
await assertEvaluatePermissionScopes(page, "Read documents", {
status: "Permit",
granted: "doc:read",
denied: "-",
});
});
});
Loading
0