Skip to content

Commit

Permalink
Add useful information to the authentication error (#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Nov 10, 2023
1 parent 0efc6df commit f10fafd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@

package io.apicurio.registry.rest;

import io.quarkus.security.AuthenticationFailedException;

import io.apicurio.registry.services.http.ErrorHttpResponse;
import io.apicurio.registry.services.http.RegistryExceptionMapperService;
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
RegistryExceptionMapperService exceptionMapperService;

public class AuthenticationFailedExceptionMapper implements ExceptionMapper<AuthenticationFailedException> {
@Override
public Response toResponse(AuthenticationFailedException exception) {
return Response.status(401).build();
public Response toResponse(UnauthorizedException exception) {
ErrorHttpResponse errorHttpResponse = exceptionMapperService.mapException(exception);
return Response.status(401).entity(errorHttpResponse).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class RegistryExceptionMapper implements ExceptionMapper<Throwable> {

@Inject
RegistryExceptionMapperService exceptionMapper;

@Context
HttpServletRequest request;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.apicurio.rest.client.auth.exception.ForbiddenException;
import io.apicurio.rest.client.auth.exception.NotAuthorizedException;
import io.apicurio.tenantmanager.client.exception.TenantManagerClientException;
import io.quarkus.security.UnauthorizedException;
import io.smallrye.mutiny.TimeoutException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand Down Expand Up @@ -136,6 +137,8 @@ public class RegistryExceptionMapperService {
map.put(ParametersConflictException.class, HTTP_CONFLICT);
map.put(DownloadNotFoundException.class, HTTP_NOT_FOUND);
map.put(ConfigPropertyNotFoundException.class, HTTP_NOT_FOUND);
map.put(UnauthorizedException.class, HTTP_UNAUTHORIZED);
map.put(io.quarkus.security.ForbiddenException.class, HTTP_FORBIDDEN);
// From io.apicurio.common.apps.multitenancy.TenantManagerService:
map.put(NotAuthorizedException.class, HTTP_FORBIDDEN);
map.put(ForbiddenException.class, HTTP_FORBIDDEN);
Expand Down
19 changes: 15 additions & 4 deletions app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
import io.apicurio.registry.rest.client.AdminClient;
import io.apicurio.registry.rest.client.RegistryClient;
import io.apicurio.registry.rest.client.exception.ArtifactNotFoundException;
import io.apicurio.registry.rest.v2.beans.*;
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
import io.apicurio.registry.rest.v2.beans.ArtifactOwner;
import io.apicurio.registry.rest.v2.beans.EditableMetaData;
import io.apicurio.registry.rest.v2.beans.IfExists;
import io.apicurio.registry.rest.v2.beans.Rule;
import io.apicurio.registry.rest.v2.beans.UserInfo;
import io.apicurio.registry.rules.compatibility.CompatibilityLevel;
import io.apicurio.registry.rules.validity.ValidityLevel;
import io.apicurio.registry.types.ArtifactType;
Expand Down Expand Up @@ -51,9 +56,7 @@
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Fabian Martinez
Expand Down Expand Up @@ -99,6 +102,14 @@ public void testWrongCreds() throws Exception {
});
}

@Test
public void testNoCreds() throws Exception {
RegistryClient clientNoAuth = createClient(null);
Assertions.assertThrows(NotAuthorizedException.class, () -> {
clientNoAuth.listArtifactsInGroup(groupId);
});
}

@Test
public void testReadOnly() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.READONLY_CLIENT_ID, "test1");
Expand Down
2 changes: 1 addition & 1 deletion storage/mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-registry-storage</artifactId>
<version>2.4.13-SNAPSHOT</version>
<version>2.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down

0 comments on commit f10fafd

Please sign in to comment.