// Generate & verify CAPTCHA's extern crate captcha; use std::fs::File; use captcha::filters::{Grid, Noise, Wave}; use captcha::Captcha; use std::path::Path; use crate::structures::CaptchaAnswer; use rocket::{form::Form, http::Cookie, http::CookieJar}; // Create a new captcha image fn create_captcha() -> Captcha { let mut captcha = Captcha::new(); // create empty captcha captcha .add_chars(7) .apply_filter(Noise::new(0.2)) .apply_filter(Grid::new(41, 67)) .add_text_area() .apply_filter(Wave::new(1.6373, 5.1363)) .view(220, 120); // create image let captcha_text = captcha.chars_as_string(); captcha .save(Path::new( &("/tmp/captcha".to_owned() + &captcha_text + ".png"), )) .expect("save failed"); // save captcha return captcha; } // API to get a new captcha #[get("/captcha")] pub fn return_captcha(cookies: &CookieJar<'_>) -> File { let captcha = create_captcha(); // Create a new captcha let captcha_text = captcha.chars_as_string(); // Store the captcha answer text let file = File::open(&("/tmp/captcha".to_owned() + &captcha_text + ".png")); // Open the captcha image file 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 captcha_text, )); return file.unwrap(); // Return the captcha image } // Check if the provided captcha answer is correct #[post("/captcha", data = "")] pub fn check_captcha(answer: Form, cookies: &CookieJar<'_>) -> String { let user_token = cookies.get_private("token").unwrap(); if user_token.value() == answer.text { return "correct".to_string(); } else { return "incorrect".to_string(); } }