From b39ba84a10450ad411674e113abeeaa985004dcc Mon Sep 17 00:00:00 2001 From: Etienne Coutaud Date: Wed, 18 Nov 2026 10:43:38 +0100 Subject: [PATCH] fix(authorization): answer a repeated role assignment with the one already held --- docs/behavior.md | 5 ++-- .../routes/authorization-checks.spec.ts | 25 +++++++++++++++++++ src/workos/routes/authorization-checks.ts | 6 +++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/docs/behavior.md b/docs/behavior.md index f20df42..3d965fc 100644 --- a/docs/behavior.md +++ b/docs/behavior.md @@ -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 diff --git a/src/workos/routes/authorization-checks.spec.ts b/src/workos/routes/authorization-checks.spec.ts index 06b00ea..8e4a914 100644 --- a/src/workos/routes/authorization-checks.spec.ts +++ b/src/workos/routes/authorization-checks.spec.ts @@ -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) => + 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(); diff --git a/src/workos/routes/authorization-checks.ts b/src/workos/routes/authorization-checks.ts index 44c2e2a..8088a62 100644 --- a/src/workos/routes/authorization-checks.ts +++ b/src/workos/routes/authorization-checks.ts @@ -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,