confirm with button

This commit is contained in:
Matthieu Jolimaitre 2024-02-05 02:11:59 +01:00
parent bfb53d5c64
commit c106b1f21d

View file

@ -53,11 +53,22 @@ export class WebVerifier {
private serve(request: Request): Response { private serve(request: Request): Response {
const url = new URL(request.url); const url = new URL(request.url);
if (!url.pathname.startsWith("/verify/")) return response_failure("Invalid path."); if (url.pathname.startsWith("/verify/")) return this.serve_verification_page(url);
if (url.pathname.startsWith("/confirm/")) return this.serve_confirmation(url);
return response_failure("Chemin invalide.");
}
private serve_verification_page(url: URL) {
const [uid] = url.pathname.slice("/verify/".length).split("/"); const [uid] = url.pathname.slice("/verify/".length).split("/");
const exists = this.awaiting.has(uid);
if (!exists) return response_failure("Lien de vérification invalide.");
return response_verification_page(this.create_confirm_link(uid));
}
private serve_confirmation(url: URL) {
const [uid] = url.pathname.slice("/confirm/".length).split("/");
const resolver = this.awaiting.get(uid); const resolver = this.awaiting.get(uid);
if (resolver === undefined) return response_failure("Invalid verification link."); if (resolver === undefined) return response_failure("Lien de vérification invalide.");
resolver(true); resolver(true);
this.awaiting.delete(uid); this.awaiting.delete(uid);
@ -65,12 +76,20 @@ export class WebVerifier {
return response_success(); return response_success();
} }
private async serve_result_page() {
//
}
private create_verif_link() { private create_verif_link() {
const uid = uuid.generate() as string; const uid = uuid.generate() as string;
const link = `http://${this.conf.url_prefix}/verify/${uid}`; const link = `http://${this.conf.url_prefix}/verify/${uid}`;
return { uid, link }; return { uid, link };
} }
private create_confirm_link(uid: string) {
return `http://${this.conf.url_prefix}/confirm/${uid}`;
}
private async start_peremption(uid: string) { private async start_peremption(uid: string) {
await wait(5 * 60 * 1000); // 5 mins await wait(5 * 60 * 1000); // 5 mins
const resolver = this.awaiting.get(uid); const resolver = this.awaiting.get(uid);
@ -82,34 +101,55 @@ export class WebVerifier {
} }
function response_failure(message: string) { function response_failure(message: string) {
const body = `<!DOCTYPE html> const body = html_pre(`
<head> EPITLS BOT
<meta charset="UTF-8"> ==========
<title>Epitls Verification</title>
</head> Échec
<body> ---------
<pre>
Failure :
${message} ${message}
</pre> `);
</body> return new Response(body, { headers: { "Content-Type": "text/html" } });
</html> }
`;
function response_verification_page(confirm_link: string) {
const body = html_pre(`
EPITLS BOT
==========
🟡 Vérification
----------------
Vous êtes sur le point de confirmer l'association.
<form action="${confirm_link}">
<input type="submit" value="Confirmer">
</form>
`);
return new Response(body, { headers: { "Content-Type": "text/html" } }); return new Response(body, { headers: { "Content-Type": "text/html" } });
} }
function response_success() { function response_success() {
const body = `<!DOCTYPE html> const body = html_pre(`
EPITLS BOT
==========
Vérifié avec succès
-----------------------
`);
return new Response(body, { headers: { "Content-Type": "text/html" } });
}
function html_pre(content: string) {
return `<!DOCTYPE html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Epitls Verification</title> <title>Epitls Verification</title>
</head> </head>
<body> <body>
<pre> <pre>
Verified successfully. ${content}
</pre> </pre>
</body> </body>
</html> </html>
`; `;
return new Response(body, { headers: { "Content-Type": "text/html" } });
} }