From 843dd2612bae00ac671791b8e6b4395ed2d62d33 Mon Sep 17 00:00:00 2001 From: Carles Arnal Date: Fri, 10 Nov 2023 17:06:54 +0100 Subject: [PATCH] Add useful information to the authentication error --- .../AuthenticationFailedExceptionMapper.java | 19 +++++++++++++++---- .../rest/RegistryExceptionMapper.java | 4 ---- .../registry/auth/SimpleAuthTest.java | 11 +++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/apicurio/registry/rest/AuthenticationFailedExceptionMapper.java b/app/src/main/java/io/apicurio/registry/rest/AuthenticationFailedExceptionMapper.java index 1effb9f912..299ceab38a 100644 --- a/app/src/main/java/io/apicurio/registry/rest/AuthenticationFailedExceptionMapper.java +++ b/app/src/main/java/io/apicurio/registry/rest/AuthenticationFailedExceptionMapper.java @@ -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 { + + @Inject + RegistryExceptionMapper exceptionMapperService; -public class AuthenticationFailedExceptionMapper implements ExceptionMapper { @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(); } } diff --git a/app/src/main/java/io/apicurio/registry/rest/RegistryExceptionMapper.java b/app/src/main/java/io/apicurio/registry/rest/RegistryExceptionMapper.java index fe0b1a44af..6ed1806868 100644 --- a/app/src/main/java/io/apicurio/registry/rest/RegistryExceptionMapper.java +++ b/app/src/main/java/io/apicurio/registry/rest/RegistryExceptionMapper.java @@ -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) @@ -19,9 +18,6 @@ @Provider public class RegistryExceptionMapper implements ExceptionMapper { - @Inject - Logger log; - @Inject CoreRegistryExceptionMapperService coreMapper; diff --git a/app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java b/app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java index 109a3b89aa..e0f1d77589 100644 --- a/app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java +++ b/app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java @@ -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; @@ -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(