io.quarkus.resteasy.runtime.UnauthorizedExceptionMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-resteasy Show documentation
Show all versions of quarkus-resteasy Show documentation
REST endpoint framework implementing JAX-RS and more
package io.quarkus.resteasy.runtime;
import jakarta.annotation.Priority;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.jboss.logging.Logger;
import io.quarkus.security.UnauthorizedException;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.security.ChallengeData;
import io.quarkus.vertx.http.runtime.security.HttpAuthenticator;
import io.vertx.ext.web.RoutingContext;
/**
* @author Michal Szynkiewicz, [email protected]
*
* Date: 05/10/2019
*/
@Provider
@Priority(Priorities.USER + 1)
public class UnauthorizedExceptionMapper implements ExceptionMapper {
private static final Logger log = Logger.getLogger(UnauthorizedExceptionMapper.class.getName());
private volatile CurrentVertxRequest currentVertxRequest;
CurrentVertxRequest currentVertxRequest() {
if (currentVertxRequest == null) {
currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get();
}
return currentVertxRequest;
}
@Override
public Response toResponse(UnauthorizedException exception) {
RoutingContext context = currentVertxRequest().getCurrent();
if (context != null) {
HttpAuthenticator authenticator = context.get(HttpAuthenticator.class.getName());
if (authenticator != null) {
ChallengeData challengeData = authenticator.getChallenge(context)
.await().indefinitely();
if (challengeData != null) {
Response.ResponseBuilder status = Response.status(challengeData.status);
if (challengeData.headerName != null) {
status.header(challengeData.headerName.toString(), challengeData.headerContent);
}
log.debugf("Returning an authentication challenge, status code: %d", challengeData.status);
return status.build();
} else {
log.debug("ChallengeData is null, returning HTTP status 401");
return Response.status(401).build();
}
} else {
log.error("HttpAuthenticator is not found, returning HTTP status 401");
}
} else {
log.error("RoutingContext is not found, returning HTTP status 401");
}
return Response.status(401).entity("Not authorized").build();
}
}