Acq communication

This commit is contained in:
Cédric
2025-12-07 20:52:41 +01:00
parent d32e892095
commit 2621ffd618
6 changed files with 140 additions and 60 deletions

View File

@@ -1,18 +1,19 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MASI 3DSecure</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header, footer {
header,
footer {
background: #f4f4f4;
padding: 15px;
text-align: center;
@@ -22,65 +23,88 @@
padding: 20px;
}
#payment
{
#payment {
display: none;
}
</style>
</head>
<body>
<main>
<div id="login">
<h2>Login</h2>
<input id="username-field" placeholder="Username">
<br>
<input id="password-field" placeholder="Password">
<br>
<button id="connect-button">Connect</button>
</div>
<div id="payment">
<h2>Payment</h2>
<input id="token-input" placeholder="Token">
<br>
<button>Pay</button>
</div>
</main>
<main>
<div id="login">
<h2>Login</h2>
<input id="username-field" placeholder="Username">
<br>
<input id="password-field" placeholder="Password">
<br>
<button id="connect-button">Connect</button>
</div>
<div id="payment">
<h2>Payment</h2>
<input id="token-input" placeholder="Token">
<br>
<button id="connect-button-pay">Pay</button>
</div>
</main>
<script>
<script>
let btnConnect = document.getElementById("connect-button");
let btnPay = document.getElementById("connect-button-pay");
let btnConnect = document.getElementById("connect-button");
let btnPay = document.getElementById("connect-button");
btnConnect.addEventListener("click", async() => {
let username = document.getElementById("username-field").value;
let pwd = document.getElementById("password-field").value;
btnConnect.addEventListener("click", async () => {
let username = document.getElementById("username-field").value;
let pwd = document.getElementById("password-field").value;
let data = {
username: username,
password: pwd
};
let data = {
username: username,
password: pwd
};
await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => {
if (response.ok) {
localStorage.setItem("creds", JSON.stringify(data));
document.getElementById("payment").style.display = 'block';
} else {
throw new Error("Authentication request failed")
}
})
.catch(error => {
console.error(error);
});
});
await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)})
.then(response => {
if(response.ok) {
localStorage.setItem("creds", JSON.stringify(data));
document.getElementById("payment").style.display = 'block';
} else {
throw new Error("Authentication request failed")
}
})
.catch(error => {
console.error(error);
});
});
btnPay.addEventListener("click", async() => {
</script>
let token = document.getElementById("token-input").value;
await fetch("/payement", {
method: "POST",
headers: {
"Content-Type": "application/json"
//"Authorization" : "username:password"
},
body: JSON.stringify(token)
})
.then(response => {
if (response.ok) {
//Logique à executer si serveur répond avec 200 HTTP
} else {
throw new Error("Payment request failed")
}
})
.catch(error => {
console.error(error);
});
})
</script>
</body>
</html>

View File

@@ -8,6 +8,8 @@ import javax.net.ssl.*;
import static common.common.src.ports.Ports.*;
import java.io.IOException;
public class Main {
private static final String HOST = "127.0.0.1";
@@ -51,10 +53,19 @@ public class Main {
}
}
public static void handleRequest(SSLSocket serverSocket) {
Logger.displayInfo("Request handled");
try{
String request = SocketManager.readResponse(serverSocket);
Logger.displayReceived(request);
}catch (IOException ioe){
throw new RuntimeException(ioe);
}
}
}

View File

@@ -0,0 +1,10 @@
package acq.acq.src.json;
import com.fasterxml.jackson.annotation.JsonProperty;
public record Token(
@JsonProperty("token")
String token,
@JsonProperty("signature")
String signature) {
}

View File

@@ -48,6 +48,10 @@ public class Main {
authThread.start();
}
/**
* Gére les requets de vérification de token
* @param clientSocket SSLsocket to listen to
*/
private static void handleRequest(SSLSocket clientSocket) {
try{
String response = SocketManager.readResponse(clientSocket);

View File

@@ -1,5 +1,7 @@
package httpServer.httpServer.src;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;

View File

@@ -1,9 +1,11 @@
package httpServer.httpServer.src.handlers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import common.common.src.html.HtmlManager;
import common.common.src.logger.Logger;
import common.common.src.json.JsonManager;
import httpServer.httpServer.src.annotations.AllowedVerb;
import httpServer.httpServer.src.annotations.OnlyAuthorizedClients;
import httpServer.httpServer.src.authorization.AuthorizedClients;
@@ -11,6 +13,7 @@ import httpServer.httpServer.src.authorization.Client;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class RequestHandler implements IRequestHandler {
@@ -25,12 +28,38 @@ public class RequestHandler implements IRequestHandler {
}
}
@AllowedVerb(name = "GET")
@OnlyAuthorizedClients
@AllowedVerb(name = "POST")
//@OnlyAuthorizedClients
public void handlePayment(HttpExchange exchange) {
Logger.displayReceived("/payment request");
try{
respondToGet(exchange, "./assets/pages/payment.html");
InputStream is = exchange.getRequestBody();
String body = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
.lines()
.reduce("", (acc, line) -> acc + line + "\n");
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(body);
String token = node.get("token").asText();
System.out.println("Received Payement POST data:\n" + token);
// Build json payload
Map<String, Object> map = Map.of("token", token);
String jsonString = JsonManager.serialize(map);
// Send request to ACQ
// Wait ACQ ACK or NAK
// Return result
} catch(Exception e){
e.printStackTrace();
}