Implement payment page

This commit is contained in:
Laurent
2025-12-04 08:48:09 +01:00
parent c99d8068f9
commit dd7a63c22f
5 changed files with 82 additions and 32 deletions

View File

@@ -33,7 +33,7 @@
<script> <script>
let btn = document.getElementById("connect-button"); let btn = document.getElementById("connect-button");
btn.addEventListener("click", () => { btn.addEventListener("click", async () => {
let username = document.getElementById("username-field").value; let username = document.getElementById("username-field").value;
let pwd = document.getElementById("password-field").value; let pwd = document.getElementById("password-field").value;
@@ -42,20 +42,22 @@
password: pwd password: pwd
}; };
fetch("/login", { await fetch("/login", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
body: JSON.stringify(data) body: JSON.stringify(data)})
}) .then(response => {
.then(response => response.text()) if(response.ok) {
.then(result => { window.location.href = "/payment";
console.log("Server response:", result); } else {
}) throw new Error("Authentication request failed")
.catch(error => { }
console.error("Error:", error); })
}); .catch(error => {
console.error("Error:", error);
});
}); });
</script> </script>

33
assets/pages/payment.html Normal file
View File

@@ -0,0 +1,33 @@
<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 {
background: #f4f4f4;
padding: 15px;
text-align: center;
}
main {
padding: 20px;
}
</style>
</head>
<body>
<main>
<h1>Payment page</h1>
</main>
<script>
</script>
</body>
</html>

View File

@@ -1,17 +1,14 @@
package common.common.src.html; package common.common.src.html;
import java.io.BufferedReader; import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HtmlManager { public class HtmlManager {
public String serveFile(String path){ public String serveFile(String path) throws IOException {
return readFile(path); return readFile(path);
} }
private String readFile(String path){ private String readFile(String path) throws IOException {
File file = new File(path); File file = new File(path);
try(BufferedReader fileReader = new BufferedReader(new FileReader(file))){ try(BufferedReader fileReader = new BufferedReader(new FileReader(file))){
@@ -21,8 +18,6 @@ public class HtmlManager {
responseBody.append(line); responseBody.append(line);
} }
return responseBody.toString(); return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
} }

View File

@@ -16,23 +16,18 @@ public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
final int port = 8043; final int port = 8043;
final HtmlManager htmlManager = new HtmlManager();
final AuthorizedClients authorizedClients = new AuthorizedClients(); final AuthorizedClients authorizedClients = new AuthorizedClients();
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5); HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
server.createContext("/", exchange -> { server.createContext("/", exchange -> {
if(isUnauthorizedVerb(exchange, "GET")){ Logger.displayReceived("/ request");
exchange.sendResponseHeaders(405, -1); respondToGet(exchange, "./assets/pages/index.html");
return; });
}
String content = htmlManager.serveFile("./assets/pages/index.html"); server.createContext("/payment", exchange -> {
byte[] data = content.getBytes(StandardCharsets.UTF_8); Logger.displayReceived("/payment request");
respondToGet(exchange, "./assets/pages/payment.html");
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
exchange.sendResponseHeaders(200, data.length);
send(exchange, data);
}); });
server.createContext("/login", exchange -> { server.createContext("/login", exchange -> {
@@ -58,12 +53,37 @@ public class Main {
exchange.sendResponseHeaders(200, response.getBytes().length); exchange.sendResponseHeaders(200, response.getBytes().length);
send(exchange, response.getBytes()); send(exchange, response.getBytes());
exchange.getResponseBody().close();
}); });
server.start(); server.start();
Logger.displayInfo("Server started on port " + port); Logger.displayInfo("Server started on port " + port);
} }
private static void respondToGet(HttpExchange exchange, String pagePath) throws IOException {
if(isUnauthorizedVerb(exchange, "GET")){
exchange.sendResponseHeaders(405, -1);
return;
}
try{
final HtmlManager htmlManager = new HtmlManager();
String content = htmlManager.serveFile(pagePath);
byte[] data = content.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
exchange.sendResponseHeaders(200, data.length);
send(exchange, data);
} catch (IOException ioe) {
ioe.printStackTrace();
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
exchange.sendResponseHeaders(404, 0);
}
finally {
exchange.getResponseBody().close();
}
}
private static boolean isUnauthorizedVerb(HttpExchange exchange, String verb) throws IOException { private static boolean isUnauthorizedVerb(HttpExchange exchange, String verb) throws IOException {
return !verb.equalsIgnoreCase(exchange.getRequestMethod()); return !verb.equalsIgnoreCase(exchange.getRequestMethod());
} }

View File

@@ -18,8 +18,8 @@ public class AuthorizedClients {
registerClient("Aude Vaiselle", "password1"); registerClient("Aude Vaiselle", "password1");
registerClient("Tony Truand", "password2"); registerClient("Tony Truand", "password2");
registerClient("Jean Porte", "password3"); registerClient("Jean Porte", "password3");
registerClient("Ruby Niole", "password4"); registerClient("Ruby Gnaule", "password4");
registerClient("Nat Assion", "password5"); registerClient("Nat Action", "password5");
registerClient("hepl", "hepl"); registerClient("hepl", "hepl");
} }