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 crate::structures::{CaptchaAnswer, Status};
|
||||||
use rocket::{form::Form, http::Cookie, http::CookieJar, serde::json::Json};
|
use rocket::{form::Form, http::Cookie, http::CookieJar, serde::json::Json};
|
||||||
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
// Create a new captcha image
|
// Create a new captcha image
|
||||||
fn create_captcha() -> Captcha {
|
fn create_captcha() -> Captcha {
|
||||||
let mut captcha = Captcha::new(); // create empty 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
|
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
|
info!("created new captcha {}", captcha_text); // Print the captcha text
|
||||||
cookies.add(Cookie::new(
|
cookies.add_private(Cookie::new( // Set the token as a private cookie
|
||||||
"token", // Add a cookie to store the captcha answer
|
"token", // Add a cookie to store the captcha answer
|
||||||
Uuid::new_v5(&Uuid::NAMESPACE_X500, &captcha_text.as_bytes())
|
captcha_text
|
||||||
.to_simple()
|
|
||||||
.to_string(),
|
|
||||||
));
|
));
|
||||||
|
|
||||||
return file.unwrap(); // Return the captcha image
|
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
|
// Check if the provided captcha answer is correct
|
||||||
#[post("/api/captcha", data = "<answer>")]
|
#[post("/api/captcha", data = "<answer>")]
|
||||||
pub fn check_captcha(answer: Form<CaptchaAnswer>) -> Json<Status> {
|
pub fn check_captcha(answer: Form<CaptchaAnswer>, cookies: &CookieJar<'_>) -> Json<Status> {
|
||||||
return Json(Status {
|
let user_token = cookies.get_private("token").unwrap();
|
||||||
status: "fail".to_string(),
|
if user_token.value() == answer.text {
|
||||||
reason: "meow".to_string(),
|
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