From c106b1f21d6b599d4bb82b9d59434acfab9ae21d Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Mon, 5 Feb 2024 02:11:59 +0100 Subject: [PATCH] confirm with button --- src/verifier.ts | 76 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/src/verifier.ts b/src/verifier.ts index 0898175..55069cb 100644 --- a/src/verifier.ts +++ b/src/verifier.ts @@ -53,11 +53,22 @@ export class WebVerifier { private serve(request: Request): Response { 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 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); - if (resolver === undefined) return response_failure("Invalid verification link."); + if (resolver === undefined) return response_failure("Lien de vérification invalide."); resolver(true); this.awaiting.delete(uid); @@ -65,12 +76,20 @@ export class WebVerifier { return response_success(); } + private async serve_result_page() { + // + } + private create_verif_link() { const uid = uuid.generate() as string; const link = `http://${this.conf.url_prefix}/verify/${uid}`; return { uid, link }; } + private create_confirm_link(uid: string) { + return `http://${this.conf.url_prefix}/confirm/${uid}`; + } + private async start_peremption(uid: string) { await wait(5 * 60 * 1000); // 5 mins const resolver = this.awaiting.get(uid); @@ -82,34 +101,55 @@ export class WebVerifier { } function response_failure(message: string) { - const body = ` - - - Epitls • Verification - - -
-❌ Failure :
-${message}    
-
- - -`; + const body = html_pre(` +EPITLS BOT +========== + +❌ Échec +--------- +${message} +`); + return new Response(body, { headers: { "Content-Type": "text/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. +
+ +
+`); return new Response(body, { headers: { "Content-Type": "text/html" } }); } function response_success() { - const body = ` + 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 ` Epitls • Verification
-✅ Verified successfully.    
+${content}
 
`; - return new Response(body, { headers: { "Content-Type": "text/html" } }); }