Skip to content

Show how to revoke OIDC tokens from security event listeners #47399

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/src/main/asciidoc/security-oidc-code-flow-authentication.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,64 @@ public class ServiceResource {
<2> Revoke the authorization code flow access token.
<3> Revoke the authorization code flow refresh token.

You can also revoke tokens in the security event listeners.

For example, when your application supports a standard <<user-initiated-logout>>, you can catch a logout event and revoke tokens:

[source,java]
----
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import io.quarkus.oidc.AccessTokenCredential;
import io.quarkus.oidc.OidcProviderClient;
import io.quarkus.oidc.RefreshToken;
import io.quarkus.oidc.SecurityEvent;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.ObservesAsync;

@ApplicationScoped
public class SecurityEventListener {

public CompletionStage<Void> processSecurityEvent(@ObservesAsync SecurityEvent event) {
if (SecurityEvent.Type.OIDC_LOGOUT_RP_INITIATED == event.getEventType()) { <1>
return revokeTokens(event.getSecurityIdentity()).subscribeAsCompletionStage();
}
return CompletableFuture.completedFuture(null);
}
private Uni<Void> revokeTokens(SecurityIdentity securityIdentity) {
return Uni.join().all(
revokeAccessToken(securityIdentity),
revokeRefreshToken(securityIdentity)
).andCollectFailures()
.replaceWithVoid()
.onFailure().recoverWithUni(t -> logFailure(t));
}

private static Uni<Boolean> revokeAccessToken(SecurityIdentity securityIdentity) { <2>
OidcProviderClient oidcProvider = securityIdentity.getAttribute(OidcProviderClient.class.getName());
String accessToken = securityIdentity.getCredential(AccessTokenCredential.class).getToken();
return oidcProvider.revokeAccessToken(accessToken);
}

private static Uni<Boolean> revokeRefreshToken(SecurityIdentity securityIdentity) { <3>
OidcProviderClient oidcProvider = securityIdentity.getAttribute(OidcProviderClient.class.getName());
String refreshToken = securityIdentity.getCredential(RefreshToken.class).getToken();
return oidcProvider.revokeRefreshToken(refreshToken);
}

private static Uni<Void> logFailure(Throwable t) {
// Log failure as required
return Uni.createFrom().voidItem();
}
}
----
<1> Revoke tokens if an RP-initiated logout event is observed.
<2> Revoke the authorization code flow access token.
<3> Revoke the authorization code flow refresh token.

=== Propagating tokens to downstream services

For information about Authorization Code Flow access token propagation to downstream services, see the xref:security-openid-connect-client-reference.adoc#token-propagation-rest[Token Propagation] section.
Expand Down
Loading