From aa6dc765701741f79aaaa0cc09450409bcf67d84 Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Sun, 21 Nov 2021 21:46:01 -0500 Subject: [PATCH] Set captcha answer as private cookie --- src/captcha.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/captcha.rs b/src/captcha.rs index 0943cef..48535c0 100644 --- a/src/captcha.rs +++ b/src/captcha.rs @@ -10,8 +10,6 @@ use std::path::Path; use crate::structures::{CaptchaAnswer, Status}; use rocket::{form::Form, http::Cookie, http::CookieJar, serde::json::Json}; -use uuid::Uuid; - // Create a new captcha image fn create_captcha() -> Captcha { let mut captcha = Captcha::new(); // create empty captcha @@ -43,12 +41,10 @@ pub fn return_captcha(cookies: &CookieJar<'_>) -> File { let file = File::open(&("/tmp/captcha".to_owned() + &captcha_text + ".png")); // Open the captcha image file - println!("created new captcha {}", captcha_text); // Print the captcha text - cookies.add(Cookie::new( + info!("created new captcha {}", captcha_text); // Print the captcha text + cookies.add_private(Cookie::new( // Set the token as a private cookie "token", // Add a cookie to store the captcha answer - Uuid::new_v5(&Uuid::NAMESPACE_X500, &captcha_text.as_bytes()) - .to_simple() - .to_string(), + captcha_text )); return file.unwrap(); // Return the captcha image @@ -56,9 +52,17 @@ pub fn return_captcha(cookies: &CookieJar<'_>) -> File { // Check if the provided captcha answer is correct #[post("/api/captcha", data = "")] -pub fn check_captcha(answer: Form) -> Json { - return Json(Status { - status: "fail".to_string(), - reason: "meow".to_string(), - }); +pub fn check_captcha(answer: Form, cookies: &CookieJar<'_>) -> Json { + let user_token = cookies.get_private("token").unwrap(); + if user_token.value() == answer.text { + return Json(Status { + status: "success".to_string(), + reason: "correct token".to_string(), + }); + } else { + return Json(Status { + status: "fail".to_string(), + reason: "incorrect token".to_string(), + }); + } }