DAG authorization pending - test mistakenly accepting HTML instead of JSON

This commit is contained in:
Dusan Jakub
2023-09-18 19:04:56 +02:00
parent 2be05a2573
commit 24a4235bf8
5 changed files with 142 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
package com.ysoft.geecon.error;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
class ExceptionMappers {
@ServerExceptionMapper
@Produces(MediaType.APPLICATION_JSON)
public Response mapJson(OAuthException exception) {
return Response.status(Response.Status.BAD_REQUEST).entity(exception.getResponse()).build();
}
@ServerExceptionMapper
@Produces(MediaType.APPLICATION_JSON)
public Response mapHtml(OAuthException exception) {
return Response.status(Response.Status.BAD_REQUEST).entity(Templates.error(exception.getResponse())).build();
}
@CheckedTemplate
public static class Templates {
public static native TemplateInstance error(OAuthException.ErrorResponse response);
}
}

View File

@@ -1,8 +1,30 @@
package com.ysoft.geecon.error;
import com.fasterxml.jackson.annotation.JsonProperty;
public class OAuthException extends RuntimeException {
private final ErrorResponse response;
// https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-09.html#name-error-response-2
public OAuthException(ErrorResponse response) {
super("OAuth error: " + response.error() + " " + response.description());
this.response = response;
}
public OAuthException(String error, String description) {
this(new ErrorResponse(error, description));
}
@Deprecated
public OAuthException(String message) {
super(message);
this(message, message);
}
public ErrorResponse getResponse() {
return response;
}
public record ErrorResponse(@JsonProperty("error") String error,
@JsonProperty("error_description") String description) {
}
}

View File

@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.login-container h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
font-weight: bold;
display: block;
}
.form-group input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.login-button {
width: 100%;
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
.login-button:hover {
background-color: #0056b3;
}
.error-popup {
background-color: #ff6b6b;
color: #fff;
padding: 10px;
text-align: center;
border-radius: 5px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Error</h2>
<div class="error-popup" id="error-popup">{response.error}</div>
</div>
</body>
</html>

View File

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

View File

@@ -54,4 +54,19 @@ public class DeviceAuthorizationGrantFlow {
.body("refresh_token", is(notNullValue()))
.extract().as(AccessTokenResponse.class);
}
public String exchangeDeviceCodeError() {
return given()
.formParam("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
.formParam("client_id", client.clientId())
.formParam("device_code", deviceResponse.deviceCode())
.when()
.post("/auth/token")
.then()
.statusCode(400).extract().asString();
// .contentType(JSON)
// .body("error", is(notNullValue()))
// .body("error_detail", is(notNullValue()))
// .extract().as(OAuthException.ErrorResponse.class);
}
}