io.quarkus.oidc.runtime.CodeAuthenticationMechanism Maven / Gradle / Ivy
package io.quarkus.oidc.runtime;
import java.net.URI;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import javax.enterprise.context.ApplicationScoped;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.quarkus.oidc.AccessTokenCredential;
import io.quarkus.oidc.IdTokenCredential;
import io.quarkus.oidc.RefreshToken;
import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.identity.IdentityProviderManager;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
import io.quarkus.vertx.http.runtime.security.ChallengeData;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.oauth2.AccessToken;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.impl.CookieImpl;
@ApplicationScoped
public class CodeAuthenticationMechanism extends AbstractOidcAuthenticationMechanism {
private static final String STATE_COOKIE_NAME = "q_auth";
private static final String SESSION_COOKIE_NAME = "q_session";
private static final String SESSION_COOKIE_DELIM = "___";
private static QuarkusSecurityIdentity augmentIdentity(SecurityIdentity securityIdentity,
String accessToken,
String refreshToken) {
final RefreshToken refreshTokenCredential = new RefreshToken(refreshToken);
return QuarkusSecurityIdentity.builder()
.setPrincipal(securityIdentity.getPrincipal())
.addCredentials(securityIdentity.getCredentials())
.addCredential(new AccessTokenCredential(accessToken, refreshTokenCredential))
.addCredential(refreshTokenCredential)
.addRoles(securityIdentity.getRoles())
.addAttributes(securityIdentity.getAttributes())
.addPermissionChecker(new Function>() {
@Override
public CompletionStage apply(Permission permission) {
return securityIdentity.checkPermission(permission);
}
})
.build();
}
@Override
public CompletionStage authenticate(RoutingContext context,
IdentityProviderManager identityProviderManager) {
Cookie sessionCookie = context.request().getCookie(SESSION_COOKIE_NAME);
// if session already established, try to re-authenticate
if (sessionCookie != null) {
String[] tokens = sessionCookie.getValue().split(SESSION_COOKIE_DELIM);
return authenticate(identityProviderManager, new IdTokenCredential(tokens[0]))
.thenCompose(new Function>() {
@Override
public CompletionStage apply(SecurityIdentity securityIdentity) {
return CompletableFuture.completedFuture(augmentIdentity(securityIdentity, tokens[1], tokens[2]));
}
});
}
// start a new session by starting the code flow dance
return performCodeFlow(identityProviderManager, context);
}
@Override
public CompletionStage getChallenge(RoutingContext context) {
removeSessionCookie(context);
ChallengeData challenge;
JsonObject params = new JsonObject();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy