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>
let btn = document.getElementById("connect-button");
btn.addEventListener("click", () => {
btn.addEventListener("click", async () => {
let username = document.getElementById("username-field").value;
let pwd = document.getElementById("password-field").value;
@@ -42,20 +42,22 @@
password: pwd
};
fetch("/login", {
await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.text())
.then(result => {
console.log("Server response:", result);
})
.catch(error => {
console.error("Error:", error);
});
body: JSON.stringify(data)})
.then(response => {
if(response.ok) {
window.location.href = "/payment";
} else {
throw new Error("Authentication request failed")
}
})
.catch(error => {
console.error("Error:", error);
});
});
</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;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
public class HtmlManager {
public String serveFile(String path){
public String serveFile(String path) throws IOException {
return readFile(path);
}
private String readFile(String path){
private String readFile(String path) throws IOException {
File file = new File(path);
try(BufferedReader fileReader = new BufferedReader(new FileReader(file))){
@@ -21,8 +18,6 @@ public class HtmlManager {
responseBody.append(line);
}
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 {
final int port = 8043;
final HtmlManager htmlManager = new HtmlManager();
final AuthorizedClients authorizedClients = new AuthorizedClients();
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
server.createContext("/", exchange -> {
if(isUnauthorizedVerb(exchange, "GET")){
exchange.sendResponseHeaders(405, -1);
return;
}
Logger.displayReceived("/ request");
respondToGet(exchange, "./assets/pages/index.html");
});
String content = htmlManager.serveFile("./assets/pages/index.html");
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);
server.createContext("/payment", exchange -> {
Logger.displayReceived("/payment request");
respondToGet(exchange, "./assets/pages/payment.html");
});
server.createContext("/login", exchange -> {
@@ -58,12 +53,37 @@ public class Main {
exchange.sendResponseHeaders(200, response.getBytes().length);
send(exchange, response.getBytes());
exchange.getResponseBody().close();
});
server.start();
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 {
return !verb.equalsIgnoreCase(exchange.getRequestMethod());
}

View File

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