DAG authorization pending - now correctly in JSON

This commit is contained in:
Dusan Jakub
2023-09-18 19:14:32 +02:00
parent 24a4235bf8
commit 9a7a437153
3 changed files with 29 additions and 15 deletions

View File

@@ -2,24 +2,37 @@ package com.ysoft.geecon.error;
import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance; import io.quarkus.qute.TemplateInstance;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Produces; import jakarta.ws.rs.Produces;
import jakarta.ws.rs.container.ResourceInfo;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import java.util.Arrays;
@ApplicationScoped
class ExceptionMappers { class ExceptionMappers {
@ServerExceptionMapper @Inject
@Produces(MediaType.APPLICATION_JSON) ResourceInfo resourceInfo;
public Response mapJson(OAuthException exception) {
return Response.status(Response.Status.BAD_REQUEST).entity(exception.getResponse()).build();
}
@ServerExceptionMapper @ServerExceptionMapper
@Produces(MediaType.APPLICATION_JSON) public Response exception(OAuthException exception) {
public Response mapHtml(OAuthException exception) { Object entity = producesJson() ? exception.getResponse() : Templates.error(exception.getResponse());
return Response.status(Response.Status.BAD_REQUEST).entity(Templates.error(exception.getResponse())).build(); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build();
} }
private boolean producesJson() {
Produces annotation = resourceInfo.getResourceMethod().getAnnotation(Produces.class);
if (annotation == null) {
return false;
}
String[] produces = annotation.value();
return Arrays.asList(produces).contains(MediaType.APPLICATION_JSON);
}
@CheckedTemplate @CheckedTemplate
public static class Templates { public static class Templates {
public static native TemplateInstance error(OAuthException.ErrorResponse response); public static native TemplateInstance error(OAuthException.ErrorResponse response);

View File

@@ -71,6 +71,6 @@ public class DeviceAuthGrantTest {
public void deviceAuthGrant_authorizationPending() throws IOException { public void deviceAuthGrant_authorizationPending() throws IOException {
DeviceAuthorizationGrantFlow flow = new DeviceAuthorizationGrantFlow(deviceUri, CLIENT); DeviceAuthorizationGrantFlow flow = new DeviceAuthorizationGrantFlow(deviceUri, CLIENT);
flow.start(); flow.start();
System.err.println(flow.exchangeDeviceCodeError()); flow.exchangeDeviceCodeError();
} }
} }

View File

@@ -3,6 +3,7 @@ package com.ysoft.geecon.helpers;
import com.ysoft.geecon.dto.AccessTokenResponse; import com.ysoft.geecon.dto.AccessTokenResponse;
import com.ysoft.geecon.dto.DeviceResponse; import com.ysoft.geecon.dto.DeviceResponse;
import com.ysoft.geecon.dto.OAuthClient; import com.ysoft.geecon.dto.OAuthClient;
import com.ysoft.geecon.error.OAuthException;
import java.io.IOException; import java.io.IOException;
@@ -55,7 +56,7 @@ public class DeviceAuthorizationGrantFlow {
.extract().as(AccessTokenResponse.class); .extract().as(AccessTokenResponse.class);
} }
public String exchangeDeviceCodeError() { public OAuthException.ErrorResponse exchangeDeviceCodeError() {
return given() return given()
.formParam("grant_type", "urn:ietf:params:oauth:grant-type:device_code") .formParam("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
.formParam("client_id", client.clientId()) .formParam("client_id", client.clientId())
@@ -63,10 +64,10 @@ public class DeviceAuthorizationGrantFlow {
.when() .when()
.post("/auth/token") .post("/auth/token")
.then() .then()
.statusCode(400).extract().asString(); .statusCode(400)
// .contentType(JSON) .contentType(JSON)
// .body("error", is(notNullValue())) .body("error", is(notNullValue()))
// .body("error_detail", is(notNullValue())) .body("error_description", is(notNullValue()))
// .extract().as(OAuthException.ErrorResponse.class); .extract().as(OAuthException.ErrorResponse.class);
} }
} }