From 2bfa4de8aad9c45819b5cafeb9c41d7ef4e52c32 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Fri, 9 Aug 2024 20:14:39 +0000
Subject: [PATCH] refactor: Refactor the `CaptchaFinder` and `CaptchaStorage`
to work with token and answer as string
Signed-off-by: Awiteb
---
Cargo.toml | 4 +
examples/simple_login.rs | 19 +---
src/captcha_gen.rs | 15 ++-
src/finder.rs | 219 +++++++++++++++------------------------
src/lib.rs | 53 +++++-----
src/storage.rs | 44 ++++----
6 files changed, 149 insertions(+), 205 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
index 63221dd..88d6e02 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,3 +28,7 @@ tempfile = "3.9"
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }
base64 = "0.21"
salvo = { version = ">= 0.65, < 0.69", default-features = false, features = ["server", "http1","http2", "affix"] }
+
+[[example]]
+name = "simple_login"
+required-features = ["cacache-storage"]
diff --git a/examples/simple_login.rs b/examples/simple_login.rs
index ee1f7d7..9a0fe39 100644
--- a/examples/simple_login.rs
+++ b/examples/simple_login.rs
@@ -3,15 +3,6 @@
// You can see a video of this example here:
//
// Run the example with `cargo run --example simple_login --features cacache-storage`
-//
-// Or set up a crate for it, with the following `Cargo.toml`:
-// ```toml
-// [dependencies]
-// base64 = ">= 0.21"
-// salvo = { version = ">= 0.65", features = ["affix"] }
-// salvo-captcha = { version = ">= 0.1", features = ["cacache-storage"] }
-// tokio = { version = ">= 1.35", features = ["macros", "rt-multi-thread", "time"] }
-// ```
use std::{sync::Arc, time::Duration};
@@ -19,9 +10,6 @@ use base64::{engine::GeneralPurpose, Engine};
use salvo::prelude::*;
use salvo_captcha::*;
-// The type of the captcha
-type MyCaptcha = Captcha>;
-
// To convert the image to base64, to show it in the browser
const BASE_64_ENGINE: GeneralPurpose = GeneralPurpose::new(
&base64::alphabet::STANDARD,
@@ -29,7 +17,7 @@ const BASE_64_ENGINE: GeneralPurpose = GeneralPurpose::new(
);
#[handler]
-async fn index(_req: &mut Request, res: &mut Response, depot: &mut Depot) {
+async fn index(res: &mut Response, depot: &mut Depot) {
// Get the captcha from the depot
let captcha_storage = depot.obtain::>().unwrap();
@@ -51,7 +39,7 @@ async fn index(_req: &mut Request, res: &mut Response, depot: &mut Depot) {
#[handler]
async fn auth(req: &mut Request, res: &mut Response, depot: &mut Depot) {
// Get the captcha state from the depot, where we can know if the captcha is passed
- let captcha_state = depot.get_captcha_state().unwrap();
+ let captcha_state = depot.get_captcha_state();
// Not important, just for demo
let Some(username) = req.form::("username").await else {
res.status_code(StatusCode::BAD_REQUEST);
@@ -76,7 +64,7 @@ async fn auth(req: &mut Request, res: &mut Response, depot: &mut Depot) {
#[tokio::main]
async fn main() {
- let captcha_middleware = MyCaptcha::new(
+ let captcha_middleware = Captcha::new(
CacacheStorage::new("./captcha-cache"),
CaptchaFormFinder::new(),
)
@@ -115,6 +103,7 @@ async fn main() {
);
let acceptor = TcpListener::new(("127.0.0.1", 5800)).bind().await;
+ println!("Starting server on http://127.0.0.1:5800");
Server::new(acceptor).serve(router).await;
captcha_cleaner.await.ok();
}
diff --git a/src/captcha_gen.rs b/src/captcha_gen.rs
index 7c6ba4f..e19a361 100644
--- a/src/captcha_gen.rs
+++ b/src/captcha_gen.rs
@@ -1,3 +1,14 @@
+// Copyright (c) 2024, Awiteb
+// A captcha middleware for Salvo framework.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
use crate::{CaptchaDifficulty, CaptchaName, CaptchaStorage};
/// Captcha generator, used to generate a new captcha image. This trait are implemented for all [`CaptchaStorage`].
@@ -11,7 +22,7 @@ pub trait CaptchaGenerator: CaptchaStorage {
&self,
name: CaptchaName,
difficulty: CaptchaDifficulty,
- ) -> impl std::future::Future