-
Notifications
You must be signed in to change notification settings - Fork 281
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
Ely 2534 OIDC logout support #2245
Closed
rsearls
wants to merge
19
commits into
wildfly-security:2.x
from
rsearls:ELY-2534-OIDC-logout-Farah-RLS-CHANGES-V2
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4698b60
OpenID Connect Logout support
pedroigor 298d503
Merge remote-tracking branch 'upstream/2.x' into ELY-2534
fjuma 4fe600b
[ELY-2534] Update copyright headers
fjuma d430574
[ELY-2534] Update the creation of the test HTTP server request for ba…
fjuma c16a6cb
[ELY-2534] Update back-channel logout handling so it doesn't rely on …
fjuma 0ba9eea
[ELY-2534] Farah's branch merged with 2.x
rsearls ad376b0
logout file with debug stmts
rsearls 3affa81
working code for rpinitiated logout. contains tmp debug stmts
rsearls 509a811
[ELY-2534] Restore the changes made so that back-channel logout handl…
fjuma 77d4911
[ELY-2534] Fix back-channel logout by moving the isSessionMarkedForIn…
fjuma 3aebc41
[ELY-2534] Use the client ID and sid from the ID token in the key for…
fjuma ebdeafe
[ELY-2534] OIDC logout support
rsearls 9a61d67
[ELY-2534] OIDC logout support
rsearls d20aa8a
Merge pull request #2 from fjuma/updated-oidc-logout
rsearls 48c15dd
[ELY-2534] OIDC logout support
rsearls 6974f0a
support post-logout-path as only an absolute URI
rsearls edd3d93
fixed unit test to support post_logout_path as a URI
rsearls 324e71c
working test fix
rsearls 8d1e837
remove obsolete ENUMs. Text change in a few msgs. Several constants m…
rsearls 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 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2021 Red Hat, Inc., and individual contributors | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
@@ -36,6 +36,7 @@ | |
* @author <a href="mailto:[email protected]">Farah Juma</a> | ||
*/ | ||
public class AuthenticatedActionsHandler { | ||
|
||
private OidcClientConfiguration deployment; | ||
private OidcHttpFacade facade; | ||
|
||
|
@@ -52,6 +53,7 @@ public boolean handledRequest() { | |
queryBearerToken(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
This file contains 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 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
270 changes: 270 additions & 0 deletions
270
http/oidc/src/main/java/org/wildfly/security/http/oidc/LogoutHandler.java
This file contains 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,270 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2024 Red Hat, Inc., and individual 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.wildfly.security.http.oidc; | ||
|
||
import static java.util.Collections.synchronizedMap; | ||
import static org.wildfly.security.http.oidc.ElytronMessages.log; | ||
|
||
import java.net.URISyntaxException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.jose4j.jwt.JwtClaims; | ||
import org.wildfly.security.http.HttpConstants; | ||
import org.wildfly.security.http.HttpScope; | ||
import org.wildfly.security.http.Scope; | ||
import org.wildfly.security.http.oidc.OidcHttpFacade.Request; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Pedro Igor</a> | ||
*/ | ||
final class LogoutHandler { | ||
|
||
private static final String POST_LOGOUT_REDIRECT_URI_PARAM = "post_logout_redirect_uri"; | ||
private static final String ID_TOKEN_HINT_PARAM = "id_token_hint"; | ||
private static final String LOGOUT_TOKEN_PARAM = "logout_token"; | ||
private static final String LOGOUT_TOKEN_TYPE = "Logout"; | ||
private static final String CLIENT_ID_SID_SEPARATOR = "-"; | ||
private static final String SID = "sid"; | ||
private static final String ISS = "iss"; | ||
|
||
/** | ||
* A bounded map to store sessions marked for invalidation after receiving logout requests through the back-channel | ||
*/ | ||
private Map<String, OidcClientConfiguration> sessionsMarkedForInvalidation = synchronizedMap(new LinkedHashMap<String, OidcClientConfiguration>(16, 0.75f, true) { | ||
@Override | ||
protected boolean removeEldestEntry(Map.Entry<String, OidcClientConfiguration> eldest) { | ||
boolean remove = sessionsMarkedForInvalidation.size() > eldest.getValue().getLogoutSessionWaitingLimit(); | ||
|
||
if (remove) { | ||
log.debugf("Limit [%s] reached for sessions waiting [%s] for logout", eldest.getValue().getLogoutSessionWaitingLimit(), sessionsMarkedForInvalidation.size()); | ||
} | ||
|
||
return remove; | ||
} | ||
}); | ||
|
||
boolean tryLogout(OidcHttpFacade facade) { | ||
log.trace("tryLogout entered"); | ||
RefreshableOidcSecurityContext securityContext = getSecurityContext(facade); | ||
if (securityContext == null) { | ||
// no active session | ||
log.trace("tryLogout securityContext == null"); | ||
return false; | ||
} | ||
|
||
if (isRpInitiatedLogoutPath(facade)) { | ||
log.trace("isRpInitiatedLogoutPath"); | ||
redirectEndSessionEndpoint(facade); | ||
return true; | ||
} | ||
|
||
if (isLogoutCallbackPath(facade)) { | ||
log.trace("isLogoutCallbackPath"); | ||
if (isFrontChannel(facade)) { | ||
log.trace("isFrontChannel"); | ||
handleFrontChannelLogoutRequest(facade); | ||
return true; | ||
} else { | ||
// we have an active session, should have received a GET logout request | ||
facade.getResponse().setStatus(HttpStatus.SC_METHOD_NOT_ALLOWED); | ||
facade.authenticationFailed(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
boolean isSessionMarkedForInvalidation(OidcHttpFacade facade) { | ||
HttpScope session = facade.getScope(Scope.SESSION); | ||
if (session == null || ! session.exists()) return false; | ||
RefreshableOidcSecurityContext securityContext = (RefreshableOidcSecurityContext) session.getAttachment(OidcSecurityContext.class.getName()); | ||
if (securityContext == null) { | ||
return false; | ||
} | ||
IDToken idToken = securityContext.getIDToken(); | ||
|
||
if (idToken == null) { | ||
return false; | ||
} | ||
return sessionsMarkedForInvalidation.remove(getSessionKey(facade, idToken.getSid())) != null; | ||
} | ||
|
||
private void redirectEndSessionEndpoint(OidcHttpFacade facade) { | ||
RefreshableOidcSecurityContext securityContext = getSecurityContext(facade); | ||
OidcClientConfiguration clientConfiguration = securityContext.getOidcClientConfiguration(); | ||
|
||
String logoutUri; | ||
|
||
try { | ||
URIBuilder redirectUriBuilder = new URIBuilder(clientConfiguration.getEndSessionEndpointUrl()) | ||
.addParameter(ID_TOKEN_HINT_PARAM, securityContext.getIDTokenString()); | ||
String postLogoutPath = clientConfiguration.getPostLogoutPath(); | ||
if (postLogoutPath != null) { | ||
log.trace("post_logout_redirect_uri: " + postLogoutPath); | ||
redirectUriBuilder.addParameter(POST_LOGOUT_REDIRECT_URI_PARAM, postLogoutPath); | ||
} | ||
|
||
logoutUri = redirectUriBuilder.build().toString(); | ||
log.trace("redirectEndSessionEndpoint path: " + redirectUriBuilder.toString()); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
log.debugf("Sending redirect to the end_session_endpoint: %s", logoutUri); | ||
facade.getResponse().setStatus(HttpStatus.SC_MOVED_TEMPORARILY); | ||
facade.getResponse().setHeader(HttpConstants.LOCATION, logoutUri); | ||
} | ||
|
||
boolean tryBackChannelLogout(OidcHttpFacade facade) { | ||
log.trace("tryBackChannelLogout entered"); | ||
if (isLogoutCallbackPath(facade)) { | ||
log.trace("isLogoutCallbackPath"); | ||
if (isBackChannel(facade)) { | ||
log.trace("isBackChannel"); | ||
handleBackChannelLogoutRequest(facade); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private void handleBackChannelLogoutRequest(OidcHttpFacade facade) { | ||
String logoutToken = facade.getRequest().getFirstParam(LOGOUT_TOKEN_PARAM); | ||
TokenValidator tokenValidator = TokenValidator.builder(facade.getOidcClientConfiguration()) | ||
.setSkipExpirationValidator() | ||
.setTokenType(LOGOUT_TOKEN_TYPE) | ||
.build(); | ||
JwtClaims claims; | ||
|
||
try { | ||
claims = tokenValidator.verify(logoutToken); | ||
} catch (Exception cause) { | ||
log.debug("Unexpected error when verifying logout token", cause); | ||
facade.getResponse().setStatus(HttpStatus.SC_BAD_REQUEST); | ||
facade.authenticationFailed(); | ||
return; | ||
} | ||
|
||
if (!isSessionRequiredOnLogout(facade)) { | ||
log.warn("Back-channel logout request received but can not infer sid from logout token to mark it for invalidation"); | ||
facade.getResponse().setStatus(HttpStatus.SC_BAD_REQUEST); | ||
facade.authenticationFailed(); | ||
return; | ||
} | ||
|
||
String sessionId = claims.getClaimValueAsString(SID); | ||
|
||
if (sessionId == null) { | ||
facade.getResponse().setStatus(HttpStatus.SC_BAD_REQUEST); | ||
facade.authenticationFailed(); | ||
return; | ||
} | ||
|
||
log.debug("Marking session for invalidation during back-channel logout"); | ||
sessionsMarkedForInvalidation.put(getSessionKey(facade, sessionId), facade.getOidcClientConfiguration()); | ||
} | ||
|
||
private String getSessionKey(OidcHttpFacade facade, String sessionId) { | ||
return facade.getOidcClientConfiguration().getClientId() + CLIENT_ID_SID_SEPARATOR + sessionId; | ||
} | ||
|
||
private void handleFrontChannelLogoutRequest(OidcHttpFacade facade) { | ||
if (isSessionRequiredOnLogout(facade)) { | ||
Request request = facade.getRequest(); | ||
String sessionId = request.getQueryParamValue(SID); | ||
|
||
if (sessionId == null) { | ||
facade.getResponse().setStatus(HttpStatus.SC_BAD_REQUEST); | ||
facade.authenticationFailed(); | ||
return; | ||
} | ||
|
||
RefreshableOidcSecurityContext context = getSecurityContext(facade); | ||
IDToken idToken = context.getIDToken(); | ||
String issuer = request.getQueryParamValue(ISS); | ||
|
||
if (idToken == null || !sessionId.equals(idToken.getSid()) || !idToken.getIssuer().equals(issuer)) { | ||
facade.getResponse().setStatus(HttpStatus.SC_BAD_REQUEST); | ||
facade.authenticationFailed(); | ||
return; | ||
} | ||
} | ||
|
||
log.debug("Invalidating session during front-channel logout"); | ||
facade.getTokenStore().logout(false); | ||
} | ||
|
||
private String getRedirectUri(OidcHttpFacade facade) { | ||
String uri = facade.getRequest().getURI(); | ||
|
||
if (uri.indexOf('?') != -1) { | ||
uri = uri.substring(0, uri.indexOf('?')); | ||
} | ||
int logoutPathIndex = uri.indexOf(getLogoutPath(facade)); | ||
|
||
if (logoutPathIndex != -1) { | ||
uri = uri.substring(0, logoutPathIndex); | ||
} | ||
|
||
return uri; | ||
} | ||
|
||
private boolean isLogoutCallbackPath(OidcHttpFacade facade) { | ||
String path = facade.getRequest().getRelativePath(); | ||
return path.endsWith(getLogoutCallbackPath(facade)); | ||
} | ||
|
||
private boolean isRpInitiatedLogoutPath(OidcHttpFacade facade) { | ||
String path = facade.getRequest().getRelativePath(); | ||
return path.endsWith(getLogoutPath(facade)); | ||
} | ||
|
||
private boolean isSessionRequiredOnLogout(OidcHttpFacade facade) { | ||
return facade.getOidcClientConfiguration().isSessionRequiredOnLogout(); | ||
} | ||
|
||
private RefreshableOidcSecurityContext getSecurityContext(OidcHttpFacade facade) { | ||
RefreshableOidcSecurityContext securityContext = (RefreshableOidcSecurityContext) facade.getSecurityContext(); | ||
|
||
if (securityContext == null) { | ||
facade.getResponse().setStatus(HttpStatus.SC_UNAUTHORIZED); | ||
facade.authenticationFailed(); | ||
return null; | ||
} | ||
|
||
return securityContext; | ||
} | ||
|
||
private String getLogoutPath(OidcHttpFacade facade) { | ||
return facade.getOidcClientConfiguration().getLogoutPath(); | ||
} | ||
private String getLogoutCallbackPath(OidcHttpFacade facade) { | ||
return facade.getOidcClientConfiguration().getLogoutCallbackPath(); | ||
} | ||
|
||
private boolean isBackChannel(OidcHttpFacade facade) { | ||
return "post".equalsIgnoreCase(facade.getRequest().getMethod()); | ||
} | ||
|
||
private boolean isFrontChannel(OidcHttpFacade facade) { | ||
return "get".equalsIgnoreCase(facade.getRequest().getMethod()); | ||
} | ||
} |
This file contains 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 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.
Might be good to mention the logout path is invalid, e.g., "Invalid logout output: %s is not a valid value for %s"
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.
done