8000 Grant roles when an organization invitation is accepted by SferaDev · Pull Request #52788 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Grant roles when an organization invitation is accepted - #52788

Open
SferaDev wants to merge 1 commit into
keycloak:mainfrom
SferaDev:org-invitation-roles
Open

SferaDev wants to merge 1 commit into
keycloak:mainfrom
SferaDev:org-invitation-roles

Conversation

@SferaDev
@SferaDev SferaDev commented Sep 15, 2026 •
Copy link
Copy Markdown
Contributor

Closes #45238

This lets an organization invitation carry realm and client roles, granted when the invitee joins. Right now the only way to do this is an INVITE_ORG event listener that looks the invitation up again and maps the roles outside the transaction that adds the member, which is what we run today.

invite-user and invite-existing-user take an optional, repeated roles form parameter with role ids:

POST /admin/realms/{realm}/organizations/{org-id}/members/invite-user

email=jdoe@example.org&roles=<realm-role-id>&roles=<client-role-id>

Invitation representations now include roles. Without the parameter nothing changes. The admin client gets matching inviteUser/inviteExistingUser overloads, and the JS admin client type gets roles.

Attaching roles needs the same permissions as mapping them directly: auth.roles().requireMapRole(role) for each role and auth.users().requireMapRoles(user) for the invitee, checked before the invitation is created or the email is sent. So an admin can't attach a role they couldn't grant (e.g. manage-orgs-admin inviting someone as realm-admin gets a 403), or grant roles to a user they can't map roles to. Someone invited to register has no user yet, so that check falls back to map-roles on users in general. Unknown role ids return 400.

The ids are stored in a new ORG_INVITATION_ROLE table, mapped like SCOPE_MAPPING on clients, with the FK to ORG_INVITATION cascading on delete. There's no FK to KEYCLOAK_ROLE; deleting a role removes its rows in JpaRealmProvider.preRemove, next to the scope mapping cleanup. The roles are granted right after the membership is added in InviteOrgActionTokenHandler, RegistrationUserCreation and IdpAddOrganizationMemberAuthenticator, so both commit together. Resending an invitation keeps its roles.

Not included: organization roles (waiting on #50606), groups and invitation attributes (#48842), and the admin console, which I'd rather do in a follow-up.

Tests: new OrganizationInvitationRoleTest (link, registration, unknown role, deleted role, resend), a permission case in OrganizationAdminRolesPermissionsTest, and OrganizationIn 8000 vitationRoleFgapTest for the user-level check with FGAP v2. Those pass locally, and so do the existing OrganizationInvitationLinkTest and OrganizationInvitationManagementTest.

A few things I wasn't sure about:

I used an AI coding assistant while working on this, and I've reviewed the change and will handle the review myself.

Copilot AI balanced review requested due to automatic review settings September 15, 2026 14:52
@SferaDev
SferaDev requested review from a team as code owners September 15, 2026 14:52
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Adds support for attaching realm/client roles to organization invitations so roles are granted atomically when an invite is accepted, removing the need for post-acceptance event listeners.

Changes:

  • Extend invite endpoints/admin clients/representations to accept and expose invitation roles (by role id).
  • Persist invitation role ids via new ORG_INVITATION_ROLE table and JPA mapping.
  • Grant recorded roles during invite acceptance flows (action token, registration, IdP join) and add integration tests.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/base/src/test/java/org/keycloak/tests/organization/authz/OrganizationAdminRolesPermissionsTest.java Adds coverage to ensure attaching roles to invitations requires map-role permission.
tests/base/src/test/java/org/keycloak/tests/organization/admin/OrganizationInvitationRoleTest.java New end-to-end integration tests covering role grant, unknown/deleted roles, and resend behavior.
services/src/main/java/org/keycloak/organization/utils/Organizations.java Adds helper to grant roles from an invitation during acceptance.
services/src/main/java/org/keycloak/organization/authentication/authenticators/broker/IdpAddOrganizationMemberAuthenticator.java Grants invitation roles when accepting via broker flow.
services/src/main/java/org/keycloak/organization/admin/resource/OrganizationMemberResource.java Extends invite endpoints to accept repeated roles form param.
services/src/main/java/org/keycloak/organization/admin/resource/OrganizationInvitationResource.java Resolves/authorizes role ids, persists them on invitations, returns roles in representations, and preserves roles on resend.
services/src/main/java/org/keycloak/authentication/forms/RegistrationUserCreation.java Grants invitation roles when invite is accepted via registration flow.
services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java Grants invitation roles when invite is accepted via action token.
server-spi/src/main/java/org/keycloak/models/OrganizationInvitationModel.java Extends invitation model contract with role id getters/setters.
model/jpa/src/main/resources/META-INF/jpa-changelog-26.8.0.xml Adds Liquibase changelog for ORG_INVITATION_ROLE table.
model/jpa/src/main/java/org/keycloak/models/jpa/entities/OrganizationInvitationEntity.java Implements role id persistence via @ElementCollection.
js/libs/keycloak-admin-client/src/defs/organizationInvitationRepresentation.ts Adds roles field to JS admin client types.
integration/admin-client-core/src/main/java/org/keycloak/admin/client/resource/OrganizationMembersResource.java Adds admin-client overloads that accept role ids.
docs/documentation/server_admin/topics/organizations/managing-members.adoc Documents roles form parameter behavior and permission requirements.
docs/documentation/release_notes/topics/26_8_0.adoc Adds release note entry for invitation role grants.
core/src/main/java/org/keycloak/representations/idm/OrganizationInvitationRepresentation.java Adds roles to invitation representation.

Note

Copilot is running an experiment and ran this review at Lite.


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI review requested due to automatic review settings September 15, 2026 15:13
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Suppressed comments (1)

services/src/main/java/org/keycloak/organization/authentication/authenticators/broker/IdpAddOrganizationMemberAuthenticator.java:1

  • This introduces an extra getById(...) lookup during IdP login flows. If invitation is already an OrganizationInvitationModel instance (not just an ID holder), you can pass it directly to grantInvitationRoles(...) to avoid a redundant DB read per acceptance.
/*

Note

Copilot is running an experiment and ran this review at Lite.

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest#crudWithoutFailover

Keycloak CI - Clustering IT

org.opentest4j.AssertionFailedError: expected: <false> but was: <true>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertFalse.failNotFalse(AssertFalse.java:63)
	at org.junit.jupiter.api.AssertFalse.assertFalse(AssertFalse.java:36)
...

Report flaky test

org.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest#crudWithFailover

Keycloak CI - Clustering IT

jakarta.ws.rs.WebApplicationException: Create method returned status Forbidden (Code: 403); expected status: Created (201). Response body: {"error":"HTTP 403 Forbidden"}
	at org.keycloak.testsuite.admin.ApiUtil.getCreatedId(ApiUtil.java:38)
	at org.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest.createEntity(ClientScopeInvalidationClusterTest.java:56)
	at org.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest.createEntity(ClientScopeInvalidationClusterTest.java:32)
	at org.keycloak.testsuite.cluster.AbstractInvalidationClusterTest.createEntityOnCurrentFailNode(AbstractInvalidationClusterTest.java:111)
...

Report flaky test

org.keycloak.testsuite.adapter.servlet.SAMLServletAdapterTest#salesPostEmptyConsumerPostURL

Keycloak CI - Adapter IT Strict Cookies

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

Copilot AI review requested due to automatic review settings September 15, 2026 20:21
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reaso 8000 n will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.

Note

Copilot is running an experiment and ran this review at Lite.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new SPI methods break minor-release compatibility, and the database column rejects valid federated role IDs.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 19/19 changed files
  • Comments generated: 2
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.

Copilot AI review requested due to automatic review settings September 16, 2026 05:57
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The IdP acceptance path can grant roles to a different same-email account without the required user-specific authorization.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 18/18 changed files
  • Comments generated: 1
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.

Copilot AI review requested due to automatic review settings September 16, 2026 07:15
@SferaDev
SferaDev force-pushed the org-invitation-roles branch from cd15c59 to 88d73b5 Compare September 16, 2026 07:15
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings September 16, 2026 08:13
@SferaDev
SferaDev force-pushed the org-invitation-roles branch from 88d73b5 to cd637e2 Compare September 16, 2026 08:13
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vramik
vramik requested a review from pedroigor October 9, 2026 08:05
@SferaDev
SferaDev force-pushed the org-invitation-roles branch from cd637e2 to b0a9d1c Compare November 30, 2026 11:26
Copilot AI balanced review requested due to automatic review settings November 30, 2026 11:26
@SferaDev
SferaDev force-pushed the org-invitation-roles branch from b0a9d1c to d04f09b Compare November 30, 2026 11:26
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical invitation-acceptance flaws can grant roles to the wrong user and leave membership and earlier role grants committed when a later grant fails.

2 open issues
2 resolved since last review

Review effort: Balanced

@SferaDev
SferaDev force-pushed the org-invitation-roles branch from d04f09b to 93c3d3c Compare November 30, 2026 12:02
Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:02
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

LDAP-backed role grants can survive failed invitation acceptance, breaking atomicity, while invitation responses expose role metadata without role-view permission checks.

2 open issues
2 resolved since last review

Review effort: Balanced

Organization invitations can now carry realm and client roles. The invite-user
and invite-existing-user endpoints take an optional repeated roles parameter
with role ids, and invitation representations expose the roles they grant.

Attaching roles needs the same permissions as mapping them directly: each role
goes through the map-role check and the invitee through the map-roles check, so
an administrator cannot attach a role they could not grant, nor grant roles to
a user they are not allowed to map roles to. Someone invited to register has no
account yet, so that check falls back to users in general. Unknown and blank
role ids are rejected.

The ids are stored in a new ORG_INVITATION_ROLE table, mapped like the scope
mappings of a client, with the foreign key to ORG_INVITATION cascading on
delete. Deleting a role removes its rows next to the other role references, and
the role id column is as wide as the one used for user role mappings so that
federated ids fit. The roles are granted right after the membership is added,
from the invitation link, the registration flow and an identity provider, so
both commit together. Resending an invitation keeps its roles and checks the
permissions again, against the administrator resending it.

Closes keycloak#45238

Signed-off-by: Alexis Rico <sferadev@gmail.com>
@SferaDev
SferaDev force-pushed the org-invitation-roles branch from 93c3d3c to 3f775db Compare November 30, 2026 12:23
Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:23
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Failed role grants can leave LDAP roles assigned after membership rollback and irreversibly consume invitation tokens, preventing acceptance retries.

1 open issue
2 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support attaching roles to user invitations (organization / realm level)

2 participants

0