Repository navigation
Add support for more update attribute user events #51017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ksushant881
wants to merge
8
commits into
keycloak:main
Choose a base branch
from
ksushant881:add-support-for-user-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4e2e91
Add support for more update attribute user events
ksushant881 27f2d96
Fix spotless check issues
ksushant881 57666c6
Improve workflow event handling for admin operations
ksushant881 0d11326
Fix spotless check issues
ksushant881 cfa1d6e
Fix workflow user event triggers for admin user updates
ksushant881 6fa1058
Trigger workflows on credential updates and attribute removals by admins
ksushant881 8023b76
Read credential types after setting credentials in admin user updates
ksushant881 f7f046a
Match email changes in profile updates and require profile changes
ksushant881 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...in/java/org/keycloak/models/workflow/events/UserRemoveCredentialWorkflowEventFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.WorkflowEventProvider; | ||
| import org.keycloak.models.workflow.WorkflowEventProviderFactory; | ||
|
|
||
| public class UserRemoveCredentialWorkflowEventFactory implements WorkflowEventProviderFactory<WorkflowEventProvider> { | ||
|
|
||
| public static final String ID = "user-remove-credential"; | ||
|
|
||
| @Override | ||
| public WorkflowEventProvider create(KeycloakSession session, String configParameter) { | ||
| return new UserRemoveCredentialWorkflowEventProvider(session, configParameter, this.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return ID; | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...n/java/org/keycloak/models/workflow/events/UserRemoveCredentialWorkflowEventProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.keycloak.events.Details; | ||
| import org.keycloak.events.Event; | ||
| import org.keycloak.events.EventType; | ||
| import org.keycloak.events.admin.AdminEvent; | ||
| import org.keycloak.events.admin.OperationType; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.AbstractWorkflowEventProvider; | ||
| import org.keycloak.models.workflow.ResourceType; | ||
| import org.keycloak.models.workflow.WorkflowEvent; | ||
| import org.keycloak.models.workflow.WorkflowExecutionContext; | ||
|
|
||
| public class UserRemoveCredentialWorkflowEventProvider extends AbstractWorkflowEventProvider { | ||
|
|
||
| private static final Pattern CREDENTIALS_PATH = Pattern.compile("^users/([^/]+)/credentials/[^/]+$"); | ||
|
|
||
| public UserRemoveCredentialWorkflowEventProvider(KeycloakSession session, String configParameter, String providerId) { | ||
| super(session, configParameter, providerId); | ||
| } | ||
|
|
||
| @Override | ||
| public ResourceType getSupportedResourceType() { | ||
| return ResourceType.USERS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(Event event) { | ||
| return EventType.REMOVE_CREDENTIAL.equals(event.getType()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(AdminEvent adminEvent) { | ||
| // removing a credential through the admin API is reported as an ACTION on the credential path | ||
| return org.keycloak.events.admin.ResourceType.USER.equals(adminEvent.getResourceType()) | ||
| && OperationType.ACTION.equals(adminEvent.getOperationType()) | ||
| && adminEvent.getResourcePath() != null | ||
| && CREDENTIALS_PATH.matcher(adminEvent.getResourcePath()).matches(); | ||
| } | ||
|
|
||
| @Override | ||
| public WorkflowEvent create(AdminEvent adminEvent) { | ||
| if (supports(adminEvent)) { | ||
| // the user id is not the last segment of the resource path, so it can't be resolved by the default implementation | ||
| Matcher matcher = CREDENTIALS_PATH.matcher(adminEvent.getResourcePath()); | ||
| return matcher.matches() ? new WorkflowEvent(getSupportedResourceType(), matcher.group(1), adminEvent, providerId) : null; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean evaluate(WorkflowExecutionContext context) { | ||
| if (!super.evaluate(context)) { | ||
| return false; | ||
| } | ||
| if (configParameter == null || configParameter.isBlank()) { | ||
| // nothing else to check | ||
| return true; | ||
| } | ||
| // this is the case when the credential type is passed as a parameter to the event provider - like user-remove-credential(otp) | ||
| Object originalEvent = context.getEvent().getEvent(); | ||
| Map<String, String> details = null; | ||
|
|
||
| if (originalEvent instanceof Event event) { | ||
| details = event.getDetails(); | ||
| } else if (originalEvent instanceof AdminEvent adminEvent) { | ||
| details = adminEvent.getDetails(); | ||
| } | ||
|
|
||
| return details != null && configParameter.equalsIgnoreCase(details.get(Details.CREDENTIAL_TYPE)); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
.../main/java/org/keycloak/models/workflow/events/UserUpdateConsentWorkflowEventFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.WorkflowEventProvider; | ||
| import org.keycloak.models.workflow.WorkflowEventProviderFactory; | ||
|
|
||
| public class UserUpdateConsentWorkflowEventFactory implements WorkflowEventProviderFactory<WorkflowEventProvider> { | ||
|
|
||
| public static final String ID = "user-update-consent"; | ||
|
|
||
| @Override | ||
| public WorkflowEventProvider create(KeycloakSession session, String configParameter) { | ||
| return new UserUpdateConsentWorkflowEventProvider(session, configParameter, this.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return ID; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...main/java/org/keycloak/models/workflow/events/UserUpdateConsentWorkflowEventProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.events.Event; | ||
| import org.keycloak.events.EventType; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.AbstractWorkflowEventProvider; | ||
| import org.keycloak.models.workflow.ResourceType; | ||
|
|
||
| public class UserUpdateConsentWorkflowEventProvider extends AbstractWorkflowEventProvider { | ||
|
|
||
| public UserUpdateConsentWorkflowEventProvider(KeycloakSession session, String configParameter, String providerId) { | ||
| super(session, configParameter, providerId); | ||
| } | ||
|
|
||
| @Override | ||
| public ResourceType getSupportedResourceType() { | ||
| return ResourceType.USERS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(Event event) { | ||
| // admin events are not supported: the only consent operation in the admin API revokes the consent, which is the | ||
| // equivalent of a REVOKE_GRANT user event rather than a consent update | ||
| return EventType.UPDATE_CONSENT.equals(event.getType()); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...in/java/org/keycloak/models/workflow/events/UserUpdateCredentialWorkflowEventFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.WorkflowEventProvider; | ||
| import org.keycloak.models.workflow.WorkflowEventProviderFactory; | ||
|
|
||
| public class UserUpdateCredentialWorkflowEventFactory implements WorkflowEventProviderFactory<WorkflowEventProvider> { | ||
|
|
||
| public static final String ID = "user-update-credential"; | ||
|
|
||
| @Override | ||
| public WorkflowEventProvider create(KeycloakSession session, String configParameter) { | ||
| return new UserUpdateCredentialWorkflowEventProvider(session, configParameter, this.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return ID; | ||
| } | ||
| } |
96 changes: 96 additions & 0 deletions
96
...n/java/org/keycloak/models/workflow/events/UserUpdateCredentialWorkflowEventProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.keycloak.events.Details; | ||
| import org.keycloak.events.Event; | ||
| import org.keycloak.events.EventType; | ||
| import org.keycloak.events.admin.AdminEvent; | ||
| import org.keycloak.events.admin.OperationType; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.credential.PasswordCredentialModel; | ||
| import org.keycloak.models.workflow.AbstractWorkflowEventProvider; | ||
| import org.keycloak.models.workflow.ResourceType; | ||
| import org.keycloak.models.workflow.WorkflowEvent; | ||
| import org.keycloak.models.workflow.WorkflowExecutionContext; | ||
|
|
||
| public class UserUpdateCredentialWorkflowEventProvider extends AbstractWorkflowEventProvider { | ||
|
|
||
| private static final Pattern RESET_PASSWORD_PATH = Pattern.compile("^users/([^/]+)/reset-password$"); | ||
| private static final Pattern USER_PATH = Pattern.compile("^users/[^/]+$"); | ||
|
|
||
| public UserUpdateCredentialWorkflowEventProvider(KeycloakSession session, String configParameter, String providerId) { | ||
| super(session, configParameter, providerId); | ||
| } | ||
|
|
||
| @Override | ||
| public ResourceType getSupportedResourceType() { | ||
| return ResourceType.USERS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(Event event) { | ||
| return EventType.UPDATE_CREDENTIAL.equals(event.getType()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(AdminEvent adminEvent) { | ||
| if (!org.keycloak.events.admin.ResourceType.USER.equals(adminEvent.getResourceType()) || adminEvent.getResourcePath() == null) { | ||
| return false; | ||
| } | ||
| if (OperationType.ACTION.equals(adminEvent.getOperationType())) { | ||
| return RESET_PASSWORD_PATH.matcher(adminEvent.getResourcePath()).matches(); | ||
| } | ||
| // admin user updates only carry the credential type detail when credentials were set from the user representation | ||
| return OperationType.UPDATE.equals(adminEvent.getOperationType()) | ||
| && USER_PATH.matcher(adminEvent.getResourcePath()).matches() | ||
| && adminEvent.getDetails() != null | ||
| && adminEvent.getDetails().containsKey(Details.CREDENTIAL_TYPE); | ||
| } | ||
|
|
||
| @Override | ||
| public WorkflowEvent create(AdminEvent adminEvent) { | ||
| if (!supports(adminEvent)) { | ||
| return null; | ||
| } | ||
| Matcher matcher = RESET_PASSWORD_PATH.matcher(adminEvent.getResourcePath()); | ||
| if (matcher.matches()) { | ||
| // the user id is not the last segment of the reset password path, so it can't be resolved by the default implementation | ||
| return new WorkflowEvent(getSupportedResourceType(), matcher.group(1), adminEvent, providerId); | ||
| } | ||
| return super.create(adminEvent); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean evaluate(WorkflowExecutionContext context) { | ||
| if (!super.evaluate(context)) { | ||
| return false; | ||
| } | ||
| if (configParameter == null || configParameter.isBlank()) { | ||
| // nothing else to check | ||
| return true; | ||
| } | ||
| // this is the case when the credential type is passed as a parameter to the event provider - like user-update-credential(password) | ||
| Object originalEvent = context.getEvent().getEvent(); | ||
| Map<String, String> details = null; | ||
|
|
||
| if (originalEvent instanceof Event event) { | ||
| details = event.getDetails(); | ||
| } else if (originalEvent instanceof AdminEvent adminEvent) { | ||
| details = adminEvent.getDetails(); | ||
| } | ||
|
|
||
| String credentialType = details != null ? details.get(Details.CREDENTIAL_TYPE) : null; | ||
|
|
||
| if (credentialType == null && originalEvent instanceof AdminEvent) { | ||
| // resetting the password is the only admin operation that doesn't carry the credential type | ||
| credentialType = PasswordCredentialModel.TYPE; | ||
| } | ||
|
|
||
| // admin user updates can set several credentials at once, in which case their types are joined | ||
| return credentialType != null && Arrays.stream(credentialType.split("::")).anyMatch(configParameter::equalsIgnoreCase); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...rc/main/java/org/keycloak/models/workflow/events/UserUpdateEmailWorkflowEventFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.WorkflowEventProvider; | ||
| import org.keycloak.models.workflow.WorkflowEventProviderFactory; | ||
|
|
||
| public class UserUpdateEmailWorkflowEventFactory implements WorkflowEventProviderFactory<WorkflowEventProvider> { | ||
|
|
||
| public static final String ID = "user-update-email"; | ||
|
|
||
| @Override | ||
| public WorkflowEventProvider create(KeycloakSession session, String configParameter) { | ||
| return new UserUpdateEmailWorkflowEventProvider(session, configParameter, this.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return ID; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
...c/main/java/org/keycloak/models/workflow/events/UserUpdateEmailWorkflowEventProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.keycloak.events.Details; | ||
| import org.keycloak.events.Event; | ||
| import org.keycloak.events.EventType; | ||
| import org.keycloak.events.admin.AdminEvent; | ||
| import org.keycloak.events.admin.OperationType; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.AbstractWorkflowEventProvider; | ||
| import org.keycloak.models.workflow.ResourceType; | ||
|
|
||
| public class UserUpdateEmailWorkflowEventProvider extends AbstractWorkflowEventProvider { | ||
|
|
||
| private static final Pattern USER_PATH = Pattern.compile("^users/[^/]+$"); | ||
|
|
||
| public UserUpdateEmailWorkflowEventProvider(KeycloakSession session, String configParameter, String providerId) { | ||
| super(session, configParameter, providerId); | ||
| } | ||
|
|
||
| @Override | ||
| public ResourceType getSupportedResourceType() { | ||
| return ResourceType.USERS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(Event event) { | ||
| // the email is also updated with the profile when the update email action is disabled | ||
| return EventType.UPDATE_EMAIL.equals(event.getType()) | ||
| || (EventType.UPDATE_PROFILE.equals(event.getType()) && hasEmailChangeDetails(event.getDetails())); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(AdminEvent adminEvent) { | ||
| return org.keycloak.events.admin.ResourceType.USER.equals(adminEvent.getResourceType()) | ||
| && OperationType.UPDATE.equals(adminEvent.getOperationType()) | ||
| && adminEvent.getResourcePath() != null | ||
| && USER_PATH.matcher(adminEvent.getResourcePath()).matches() | ||
| && hasEmailChangeDetails(adminEvent.getDetails()); | ||
| } | ||
|
|
||
| /** | ||
| * Profile updates only carry the email details when the email has actually changed, and only the previous email when it | ||
| * has been cleared. | ||
| */ | ||
| private static boolean hasEmailChangeDetails(Map<String, String> details) { | ||
| return details != null && (details.containsKey(Details.UPDATED_EMAIL) || details.containsKey(Details.PREVIOUS_EMAIL)); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
.../main/java/org/keycloak/models/workflow/events/UserUpdateProfileWorkflowEventFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.keycloak.models.workflow.events; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.workflow.WorkflowEventProvider; | ||
| import org.keycloak.models.workflow.WorkflowEventProviderFactory; | ||
|
|
||
| public class UserUpdateProfileWorkflowEventFactory implements WorkflowEventProviderFactory<WorkflowEventProvider> { | ||
|
|
||
| public static final String ID = "user-update-profile"; | ||
|
|
||
| @Override | ||
| public WorkflowEventProvider create(KeycloakSession session, String configParameter) { | ||
| return new UserUpdateProfileWorkflowEventProvider(session, configParameter, this.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return ID; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.