Skip to content

Commit

Permalink
Add useful information to the authentication error (#5316)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Oct 8, 2024
1 parent 4b5e37d commit f7a50d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package io.apicurio.registry.rest;

import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.UnauthorizedException;
import jakarta.annotation.Priority;
import jakarta.inject.Inject;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFailedExceptionMapper implements ExceptionMapper<UnauthorizedException> {

@Inject
RegistryExceptionMapper exceptionMapperService;

public class AuthenticationFailedExceptionMapper implements ExceptionMapper<AuthenticationFailedException> {
@Override
public Response toResponse(AuthenticationFailedException exception) {
return Response.status(401).build();
public Response toResponse(UnauthorizedException exception) {
Response errorHttpResponse = exceptionMapperService.toResponse(exception);
return Response.status(401).entity(errorHttpResponse).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.slf4j.Logger;

/**
* TODO use v1 beans when appropriate (when handling REST API v1 calls)
Expand All @@ -19,9 +18,6 @@
@Provider
public class RegistryExceptionMapper implements ExceptionMapper<Throwable> {

@Inject
Logger log;

@Inject
CoreRegistryExceptionMapperService coreMapper;

Expand Down
11 changes: 11 additions & 0 deletions app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.kiota.http.vertx.VertXRequestAdapter;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.vertx.core.Vertx;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -90,6 +91,16 @@ public void testWrongCreds() throws Exception {
assertTrue(exception.getMessage().contains("Unauthorized"));
}

@Test
public void testNoCreds() throws Exception {
var adapter = new VertXRequestAdapter(Vertx.vertx());
adapter.setBaseUrl(registryV3ApiUrl);
RegistryClient client = new RegistryClient(adapter);
Assertions.assertThrows(Exception.class, () -> {
client.groups().byGroupId(groupId).artifacts().get();
});
}

@Test
public void testReadOnly() throws Exception {
var adapter = new VertXRequestAdapter(
Expand Down

0 comments on commit f7a50d3

Please sign in to comment.