Set captcha answer as private cookie
This commit is contained in:
parent
a100bcb552
commit
aa6dc76570
|
@ -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 = "<answer>")]
|
||||
pub fn check_captcha(answer: Form<CaptchaAnswer>) -> Json<Status> {
|
||||
return Json(Status {
|
||||
status: "fail".to_string(),
|
||||
reason: "meow".to_string(),
|
||||
});
|
||||
pub fn check_captcha(answer: Form<CaptchaAnswer>, cookies: &CookieJar<'_>) -> Json<Status> {
|
||||
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(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue