-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add support for OIDC BackChannel Logout #24611
Merged
sberyozkin
merged 1 commit into
quarkusio:main
from
sberyozkin:oidc_back_channel_logout
Apr 13, 2022
Merged
Changes from all commits
Commits
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
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
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
145 changes: 145 additions & 0 deletions
145
extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/BackChannelLogoutHandler.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,145 @@ | ||
package io.quarkus.oidc.runtime; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.jwt.Claims; | ||
import org.jboss.logging.Logger; | ||
import org.jose4j.jwt.consumer.InvalidJwtException; | ||
|
||
import io.quarkus.oidc.OidcTenantConfig; | ||
import io.quarkus.oidc.common.runtime.OidcConstants; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class BackChannelLogoutHandler { | ||
private static final Logger LOG = Logger.getLogger(BackChannelLogoutHandler.class); | ||
|
||
@Inject | ||
DefaultTenantConfigResolver resolver; | ||
|
||
private final OidcConfig oidcConfig; | ||
|
||
public BackChannelLogoutHandler(OidcConfig oidcConfig) { | ||
this.oidcConfig = oidcConfig; | ||
} | ||
|
||
public void setup(@Observes Router router) { | ||
addRoute(router, oidcConfig.defaultTenant); | ||
|
||
for (OidcTenantConfig oidcTenantConfig : oidcConfig.namedTenants.values()) { | ||
addRoute(router, oidcTenantConfig); | ||
} | ||
} | ||
|
||
private void addRoute(Router router, OidcTenantConfig oidcTenantConfig) { | ||
if (oidcTenantConfig.isTenantEnabled() && oidcTenantConfig.logout.backchannel.path.isPresent()) { | ||
router.route(oidcTenantConfig.logout.backchannel.path.get()).handler(new RouteHandler(oidcTenantConfig)); | ||
} | ||
} | ||
|
||
class RouteHandler implements Handler<RoutingContext> { | ||
private final OidcTenantConfig oidcTenantConfig; | ||
|
||
RouteHandler(OidcTenantConfig oidcTenantConfig) { | ||
this.oidcTenantConfig = oidcTenantConfig; | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
LOG.debugf("Back channel logout request for the tenant %s received", oidcTenantConfig.getTenantId().get()); | ||
final TenantConfigContext tenantContext = getTenantConfigContext(context); | ||
if (tenantContext == null) { | ||
LOG.debugf( | ||
"Tenant configuration for the tenant %s is not available or does not match the backchannel logout path", | ||
oidcTenantConfig.getTenantId().get()); | ||
} | ||
|
||
if (OidcUtils.isFormUrlEncodedRequest(context)) { | ||
OidcUtils.getFormUrlEncodedData(context) | ||
.subscribe().with(new Consumer<MultiMap>() { | ||
@Override | ||
public void accept(MultiMap form) { | ||
|
||
String encodedLogoutToken = form.get(OidcConstants.BACK_CHANNEL_LOGOUT_TOKEN); | ||
if (encodedLogoutToken == null) { | ||
LOG.debug("Back channel logout token is missing"); | ||
context.response().setStatusCode(400); | ||
} else { | ||
try { | ||
// Do the general validation of the logout token now, compare with the IDToken later | ||
// Check the signature, as well the issuer and audience if it is configured | ||
TokenVerificationResult result = tenantContext.provider | ||
.verifyLogoutJwtToken(encodedLogoutToken); | ||
|
||
if (verifyLogoutTokenClaims(result)) { | ||
resolver.getBackChannelLogoutTokens().put(oidcTenantConfig.tenantId.get(), | ||
result); | ||
context.response().setStatusCode(200); | ||
} else { | ||
context.response().setStatusCode(400); | ||
} | ||
} catch (InvalidJwtException e) { | ||
LOG.debug("Back channel logout token is invalid"); | ||
context.response().setStatusCode(400); | ||
|
||
} | ||
} | ||
context.response().end(); | ||
} | ||
|
||
}); | ||
|
||
} else { | ||
LOG.debug("HTTP POST and " + HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED.toString() | ||
+ " content type must be used with the Back channel logout request"); | ||
context.response().setStatusCode(400); | ||
context.response().end(); | ||
} | ||
} | ||
|
||
private boolean verifyLogoutTokenClaims(TokenVerificationResult result) { | ||
// events | ||
JsonObject events = result.localVerificationResult.getJsonObject(OidcConstants.BACK_CHANNEL_EVENTS_CLAIM); | ||
if (events == null || events.getJsonObject(OidcConstants.BACK_CHANNEL_EVENT_NAME) == null) { | ||
LOG.debug("Back channel logout token does not have a valid 'events' claim"); | ||
return false; | ||
} | ||
if (!result.localVerificationResult.containsKey(Claims.sub.name()) | ||
&& !result.localVerificationResult.containsKey(OidcConstants.BACK_CHANNEL_LOGOUT_SID_CLAIM)) { | ||
LOG.debug("Back channel logout token does not have 'sub' or 'sid' claim"); | ||
return false; | ||
} | ||
if (result.localVerificationResult.containsKey(Claims.nonce.name())) { | ||
LOG.debug("Back channel logout token must not contain 'nonce' claim"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private TenantConfigContext getTenantConfigContext(RoutingContext context) { | ||
String requestPath = context.request().path(); | ||
if (isMatchingTenant(requestPath, resolver.getTenantConfigBean().getDefaultTenant())) { | ||
return resolver.getTenantConfigBean().getDefaultTenant(); | ||
} | ||
for (TenantConfigContext tenant : resolver.getTenantConfigBean().getStaticTenantsConfig().values()) { | ||
if (isMatchingTenant(requestPath, tenant)) { | ||
return tenant; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private boolean isMatchingTenant(String requestPath, TenantConfigContext tenant) { | ||
return tenant.oidcConfig.isTenantEnabled() | ||
&& tenant.oidcConfig.getTenantId().get().equals(oidcTenantConfig.getTenantId().get()) | ||
&& requestPath.equals(tenant.oidcConfig.logout.backchannel.path.orElse(null)); | ||
} | ||
} | ||
} |
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.
Wouldn't make more sense to have this property available only for the backchannel logout config category?
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.
I thought about for a moment but then decided it could of common interest, the same extra restriction (that this token was not created 6 months ago :-) ) can be applied to bearer or even code flow tokens (smallrye-jwt has it and MP-JWT 2.1 will have it too).