mirror of
https://github.com/ysoftdevs/oauth-playground-server.git
synced 2026-04-01 06:43:14 +02:00
DAG authorization pending - now correctly in JSON
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user