-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Implement a new impersonation flow that uses action tokens #40767
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
twobiers
wants to merge
6
commits into
keycloak:main
Choose a base branch
from
twobiers:impersonation-action-token
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.
+535
−219
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f060d4c
Implement a new impersonation flow that uses action tokens
twobiers 1545f57
Add redirection to SAMLServletAdapterTest for successful impersonation
twobiers 4d22240
Add impersonator realm to token and event
twobiers eda8bc7
Remove unnecessary assertion in SAMLServletAdapterTest
twobiers 2e74209
Ensure Impersonation token is only used a single time
twobiers a8b6c03
Fix second request invocation
twobiers 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
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
83 changes: 83 additions & 0 deletions
83
...main/java/org/keycloak/authentication/actiontoken/impersonate/ImpersonateActionToken.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,83 @@ | ||
/* | ||
* Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.authentication.actiontoken.impersonate; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.keycloak.authentication.actiontoken.DefaultActionToken; | ||
|
||
public class ImpersonateActionToken extends DefaultActionToken { | ||
|
||
public static final String TOKEN_TYPE = "impersonate"; | ||
|
||
@JsonProperty("impersonator") | ||
private String impersonatorUsername; | ||
|
||
@JsonProperty("impersonatorId") | ||
private String impersonatorId; | ||
|
||
@JsonProperty("impersonatorRealm") | ||
private String impersonatorRealm; | ||
|
||
@JsonProperty("reduri") | ||
private String redirectUri; | ||
|
||
public ImpersonateActionToken(String userId, String impersonatorUsername, String impersonatorId, | ||
String impersonatorRealm, String redirectUri, | ||
int absoluteExpirationInSecs) { | ||
super(userId, TOKEN_TYPE, absoluteExpirationInSecs, null); | ||
this.impersonatorUsername = impersonatorUsername; | ||
this.impersonatorId = impersonatorId; | ||
this.impersonatorRealm = impersonatorRealm; | ||
this.redirectUri = redirectUri; | ||
} | ||
|
||
private ImpersonateActionToken() { | ||
} | ||
|
||
public String getImpersonatorId() { | ||
return impersonatorId; | ||
} | ||
|
||
public void setImpersonatorId(String impersonatorId) { | ||
this.impersonatorId = impersonatorId; | ||
} | ||
|
||
public String getImpersonatorUsername() { | ||
return impersonatorUsername; | ||
} | ||
|
||
public void setImpersonatorUsername(String impersonatorUsername) { | ||
this.impersonatorUsername = impersonatorUsername; | ||
} | ||
|
||
public void setImpersonatorRealm(String impersonatorRealm) { | ||
this.impersonatorRealm = impersonatorRealm; | ||
} | ||
|
||
public String getImpersonatorRealm() { | ||
return impersonatorRealm; | ||
} | ||
|
||
public String getRedirectUri() { | ||
return redirectUri; | ||
} | ||
|
||
public void setRedirectUri(String redirectUri) { | ||
this.redirectUri = redirectUri; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...va/org/keycloak/authentication/actiontoken/impersonate/ImpersonateActionTokenHandler.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,138 @@ | ||
/* | ||
* Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.authentication.actiontoken.impersonate; | ||
|
||
import static org.keycloak.models.ImpersonationSessionNote.IMPERSONATOR_ID; | ||
import static org.keycloak.models.ImpersonationSessionNote.IMPERSONATOR_USERNAME; | ||
|
||
import java.net.URI; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.Response.Status; | ||
|
||
import org.keycloak.TokenVerifier; | ||
import org.keycloak.authentication.actiontoken.AbstractActionTokenHandler; | ||
import org.keycloak.authentication.actiontoken.ActionTokenContext; | ||
import org.keycloak.authentication.actiontoken.TokenUtils; | ||
import org.keycloak.common.ClientConnection; | ||
import org.keycloak.common.util.Time; | ||
import org.keycloak.events.Details; | ||
import org.keycloak.events.Errors; | ||
import org.keycloak.events.EventBuilder; | ||
import org.keycloak.events.EventType; | ||
import org.keycloak.models.DefaultActionTokenKey; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.SingleUseObjectKeyModel; | ||
import org.keycloak.models.SingleUseObjectProvider; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.models.UserSessionModel; | ||
import org.keycloak.services.ErrorPage; | ||
import org.keycloak.services.managers.AuthenticationManager; | ||
import org.keycloak.services.managers.AuthenticationSessionManager; | ||
import org.keycloak.services.managers.UserSessionManager; | ||
import org.keycloak.services.messages.Messages; | ||
|
||
public class ImpersonateActionTokenHandler extends AbstractActionTokenHandler<ImpersonateActionToken> { | ||
|
||
public ImpersonateActionTokenHandler() { | ||
super(ImpersonateActionToken.TOKEN_TYPE, ImpersonateActionToken.class, Messages.IMPERSONATE_ERROR, | ||
EventType.IMPERSONATE, Errors.INVALID_TOKEN); | ||
} | ||
|
||
@Override | ||
public TokenVerifier.Predicate<? super ImpersonateActionToken>[] getVerifiers( | ||
ActionTokenContext<ImpersonateActionToken> tokenContext) { | ||
return TokenUtils.predicates(); | ||
} | ||
|
||
@Override | ||
public Response handleToken(ImpersonateActionToken token, ActionTokenContext<ImpersonateActionToken> tokenContext) { | ||
KeycloakSession session = tokenContext.getSession(); | ||
RealmModel realm = tokenContext.getRealm(); | ||
UserModel user = session.users().getUserById(realm, token.getUserId()); | ||
ClientConnection clientConnection = tokenContext.getClientConnection(); | ||
EventBuilder event = new EventBuilder(realm, session, clientConnection); | ||
|
||
if (user == null) { | ||
return handleImpersonationError(tokenContext, "User not found", Status.NOT_FOUND); | ||
} | ||
if (!user.isEnabled()) { | ||
return handleImpersonationError(tokenContext, "User is disabled", Status.BAD_REQUEST); | ||
} | ||
if (user.getServiceAccountClientLink() != null) { | ||
return handleImpersonationError(tokenContext, "Service accounts cannot be impersonated", Status.BAD_REQUEST); | ||
} | ||
|
||
// If the current user is already impersonating another user, we expire the existing session to prevent | ||
// multiple impersonations at the same time. | ||
UserSessionModel activeUserSession = session.getContext().getUserSession(); | ||
if (activeUserSession != null && !activeUserSession.getUser().getId().equals(user.getId())) { | ||
AuthenticationManager.expireIdentityCookie(session); | ||
AuthenticationManager.expireRememberMeCookie(session); | ||
AuthenticationManager.expireAuthSessionCookie(session); | ||
AuthenticationManager.backchannelLogout(session, realm, activeUserSession, session.getContext().getUri(), clientConnection, session.getContext().getRequestHeaders(), true); | ||
} | ||
|
||
UserSessionModel userSession = new UserSessionManager(session).createUserSession(realm, user, user.getUsername(), clientConnection.getRemoteHost(), "impersonate", false, null, null); | ||
userSession.setNote(IMPERSONATOR_ID.toString(), token.getImpersonatorId()); | ||
userSession.setNote(IMPERSONATOR_USERNAME.toString(), token.getImpersonatorUsername()); | ||
|
||
AuthenticationManager.createLoginCookie(session, realm, userSession.getUser(), userSession, session.getContext().getUri(), clientConnection); | ||
URI redirect = URI.create(token.getRedirectUri()); | ||
|
||
event.event(EventType.IMPERSONATE) | ||
.session(userSession) | ||
.user(user) | ||
.detail(Details.IMPERSONATOR_REALM, token.getImpersonatorRealm()) | ||
.detail(Details.IMPERSONATOR, token.getImpersonatorUsername()) | ||
.success(); | ||
|
||
SingleUseObjectKeyModel actionTokenKey = DefaultActionTokenKey.from(token.serializeKey()); | ||
if (actionTokenKey != null) { | ||
SingleUseObjectProvider singleUseObjectProvider = session.singleUseObjects(); | ||
singleUseObjectProvider.put(actionTokenKey.serializeKey(), actionTokenKey.getExp() - Time.currentTime(), null); | ||
} | ||
|
||
return Response.status(Response.Status.FOUND) | ||
.location(redirect) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public boolean canUseTokenRepeatedly(ImpersonateActionToken token, | ||
ActionTokenContext<ImpersonateActionToken> tokenContext) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean checkIsUserValid(ImpersonateActionToken token, | ||
ActionTokenContext<ImpersonateActionToken> tokenContext) { | ||
// Impersonations are actually performed as part of a different user session, so we don't | ||
// want to check the validity of the user here. | ||
return false; | ||
} | ||
|
||
private Response handleImpersonationError(ActionTokenContext<?> tokenContext, String errorMessage, Status status) { | ||
if (tokenContext != null && tokenContext.getAuthenticationSession() != null) { | ||
new AuthenticationSessionManager(tokenContext.getSession()) | ||
.removeAuthenticationSession(tokenContext.getRealm(), tokenContext.getAuthenticationSession(), true); | ||
} | ||
|
||
return ErrorPage.error(tokenContext.getSession(), null, status, errorMessage); | ||
} | ||
} |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"beformed" -> "performed"