From 110a8728e1d41c46c6e9f2d3f889a08bc2ffb4bf Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sun, 11 Aug 2024 12:09:28 +0000
Subject: [PATCH] feat: Add query finder
Signed-off-by: Awiteb
---
src/finder/mod.rs | 2 +
src/finder/query_finder.rs | 151 +++++++++++++++++++++++++++++++++++++
2 files changed, 153 insertions(+)
create mode 100644 src/finder/query_finder.rs
diff --git a/src/finder/mod.rs b/src/finder/mod.rs
index e0568c7..b75c5b4 100644
--- a/src/finder/mod.rs
+++ b/src/finder/mod.rs
@@ -13,9 +13,11 @@ use salvo_core::http::Request;
mod form_finder;
mod header_finder;
+mod query_finder;
pub use form_finder::*;
pub use header_finder::*;
+pub use query_finder::*;
/// Trait to find the captcha token and answer from the request.
pub trait CaptchaFinder: Send + Sync {
diff --git a/src/finder/query_finder.rs b/src/finder/query_finder.rs
new file mode 100644
index 0000000..7f98629
--- /dev/null
+++ b/src/finder/query_finder.rs
@@ -0,0 +1,151 @@
+// 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 salvo_core::http::Request;
+
+use crate::CaptchaFinder;
+
+/// Find the captcha token and answer from the url query
+#[derive(Debug)]
+pub struct CaptchaQueryFinder {
+ /// The query name of the captcha token
+ ///
+ /// Default: "c_t"
+ pub token_name: String,
+
+ /// The query name of the captcha answer
+ ///
+ /// Default: "c_a"
+ pub answer_name: String,
+}
+
+impl CaptchaQueryFinder {
+ /// Create a new [`CaptchaQueryFinder`]
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Set the token query name
+ pub fn token_name(mut self, token_name: String) -> Self {
+ self.token_name = token_name;
+ self
+ }
+
+ /// Set the answer query name
+ pub fn answer_name(mut self, answer_name: String) -> Self {
+ self.answer_name = answer_name;
+ self
+ }
+}
+
+impl Default for CaptchaQueryFinder {
+ /// Create a default [`CaptchaQueryFinder`] with:
+ /// - token_name: "c_t"
+ /// - answer_name: "c_a"
+ fn default() -> Self {
+ Self {
+ token_name: "c_t".to_string(),
+ answer_name: "c_a".to_string(),
+ }
+ }
+}
+
+impl CaptchaFinder for CaptchaQueryFinder {
+ async fn find_token(&self, req: &mut Request) -> Option