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
5 changes: 3 additions & 2 deletions docs/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ Native AuthKit groups can receive scoped roles through
one by ID or remove by role and resource criteria. Omitting the resource targets the
organization's own resource. Direct role assignments also support criteria removal through
`DELETE /authorization/organization_memberships/{id}/role_assignments`. Duplicate group
assignments return `409` with code `duplicate_group_role_assignment`; an unknown role slug is a
`404`, as is group criteria removal that matches nothing; roles and resources from another
assignments return `409` with code `duplicate_group_role_assignment`, while a repeated direct
assignment (same membership, role and resource) answers `201` with the assignment already held, as
production does; an unknown role slug is a `404`, as is group criteria removal that matches nothing; roles and resources from another
organization are rejected. Permission checks, effective-permission lists, and accessible-resource discovery resolve
current group membership and include grants on resource ancestors. Removing a member or group
revokes its derived access immediately. Membership and resource assignment lists expose inherited
Expand Down
25 changes: 25 additions & 0 deletions src/workos/routes/authorization-checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ describe('Authorization check + role assignment routes', () => {
expect(body.resource).toEqual({ id: resource.id, external_id: 'doc-1', resource_type_slug: 'doc' });
});

it('answers a repeated role assignment with the one already held', async () => {
const { membership, org } = await setup();
await req('/authorization/resources', {
method: 'POST',
body: JSON.stringify({ resource_type_slug: 'doc', external_id: 'doc-1', organization_id: org.id, name: 'doc-1' }),
});
const assign = (body: Record<string, unknown>) =>
req(`/authorization/organization_memberships/${membership.id}/role_assignments`, {
method: 'POST',
body: JSON.stringify({ role_slug: 'admin-role', ...body }),
});
const onDoc = { resource_external_id: 'doc-1', resource_type_slug: 'doc' };

const first = await assign(onDoc);
const second = await assign(onDoc);
expect(second.status).toBe(201);
expect((await json(second)).id).toBe((await json(first)).id);

const onOrg = await json(await assign({}));
expect(onOrg.id).not.toBe((await json(await assign(onDoc))).id);

const list = await json(await req(`/authorization/organization_memberships/${membership.id}/role_assignments`));
expect(list.data.length).toBe(2);
});

it('creates a role assignment scoped to a resource by id', async () => {
const { membership, org } = await setup();

Expand Down
6 changes: 6 additions & 0 deletions src/workos/routes/authorization-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ export function authorizationCheckRoutes(ctx: RouteContext): void {

const resource = resolveResourceTarget(ws, body, membership.organization_id);

// Production answers a repeated assignment with the one already held, still as a 201.
const existing = ws.roleAssignments
.findBy('organization_membership_id', membershipId)
.find((assignment) => assignment.role_id === role.id && assignment.resource_id === (resource?.id ?? null));
if (existing) return c.json(formatRoleAssignment(existing), 201);

const assignment = ws.roleAssignments.insert({
object: 'role_assignment',
organization_membership_id: membershipId,
Expand Down
Loading