refactor: Move the CacacheStorage
to another module
All checks were successful
Rust CI / Rust CI (push) Successful in 1m51s
All checks were successful
Rust CI / Rust CI (push) Successful in 1m51s
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
dfa35daf0f
commit
4be7a66d03
2 changed files with 92 additions and 79 deletions
|
@ -9,62 +9,22 @@
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
use std::{sync::Arc, time::Duration};
|
|
||||||
|
|
||||||
#[cfg(feature = "cacache-storage")]
|
|
||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
time::SystemTime,
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Trait to store the captcha token and answer. is also clear the expired captcha.
|
use crate::CaptchaStorage;
|
||||||
///
|
|
||||||
/// The trait will be implemented for `Arc<T>` if `T` implements the trait.
|
|
||||||
///
|
|
||||||
/// The trait is thread safe, so the storage can be shared between threads.
|
|
||||||
pub trait CaptchaStorage: Send + Sync + 'static
|
|
||||||
where
|
|
||||||
Self: Clone + std::fmt::Debug,
|
|
||||||
{
|
|
||||||
/// The error type of the storage.
|
|
||||||
type Error: std::fmt::Display + std::fmt::Debug + Send;
|
|
||||||
|
|
||||||
/// Store the captcha token and answer.
|
|
||||||
fn store_answer(
|
|
||||||
&self,
|
|
||||||
answer: String,
|
|
||||||
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send;
|
|
||||||
|
|
||||||
/// Returns the answer of the captcha token. This method will return None if the token is not exist.
|
|
||||||
fn get_answer(
|
|
||||||
&self,
|
|
||||||
token: &str,
|
|
||||||
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send;
|
|
||||||
|
|
||||||
/// Clear the expired captcha.
|
|
||||||
fn clear_expired(
|
|
||||||
&self,
|
|
||||||
expired_after: Duration,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
|
|
||||||
|
|
||||||
/// Clear the captcha by token.
|
|
||||||
fn clear_by_token(
|
|
||||||
&self,
|
|
||||||
token: &str,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The [`cacache`] storage.
|
/// The [`cacache`] storage.
|
||||||
///
|
///
|
||||||
/// [`cacache`]: https://github.com/zkat/cacache-rs
|
/// [`cacache`]: https://github.com/zkat/cacache-rs
|
||||||
#[cfg(feature = "cacache-storage")]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CacacheStorage {
|
pub struct CacacheStorage {
|
||||||
/// The cacache cache directory.
|
/// The cacache cache directory.
|
||||||
cache_dir: PathBuf,
|
cache_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cacache-storage")]
|
|
||||||
impl CacacheStorage {
|
impl CacacheStorage {
|
||||||
/// Create a new CacacheStorage
|
/// Create a new CacacheStorage
|
||||||
pub fn new(cache_dir: impl Into<PathBuf>) -> Self {
|
pub fn new(cache_dir: impl Into<PathBuf>) -> Self {
|
||||||
|
@ -79,7 +39,6 @@ impl CacacheStorage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cacache-storage")]
|
|
||||||
impl CaptchaStorage for CacacheStorage {
|
impl CaptchaStorage for CacacheStorage {
|
||||||
type Error = cacache::Error;
|
type Error = cacache::Error;
|
||||||
|
|
||||||
|
@ -144,43 +103,7 @@ impl CaptchaStorage for CacacheStorage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> CaptchaStorage for Arc<T>
|
|
||||||
where
|
|
||||||
T: CaptchaStorage,
|
|
||||||
{
|
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
fn store_answer(
|
|
||||||
&self,
|
|
||||||
answer: String,
|
|
||||||
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
|
|
||||||
self.as_ref().store_answer(answer)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_answer(
|
|
||||||
&self,
|
|
||||||
token: &str,
|
|
||||||
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
|
|
||||||
self.as_ref().get_answer(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clear_expired(
|
|
||||||
&self,
|
|
||||||
expired_after: Duration,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
|
|
||||||
self.as_ref().clear_expired(expired_after)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clear_by_token(
|
|
||||||
&self,
|
|
||||||
token: &str,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
|
|
||||||
self.as_ref().clear_by_token(token)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[cfg(feature = "cacache-storage")]
|
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
90
src/storage/mod.rs
Normal file
90
src/storage/mod.rs
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
// Copyright (c) 2024, Awiteb <a@4rs.nl>
|
||||||
|
// 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 std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
#[cfg(feature = "cacache-storage")]
|
||||||
|
mod cacache_storage;
|
||||||
|
|
||||||
|
#[cfg(feature = "cacache-storage")]
|
||||||
|
pub use cacache_storage::*;
|
||||||
|
|
||||||
|
/// Trait to store the captcha token and answer. is also clear the expired captcha.
|
||||||
|
///
|
||||||
|
/// The trait will be implemented for `Arc<T>` if `T` implements the trait.
|
||||||
|
///
|
||||||
|
/// The trait is thread safe, so the storage can be shared between threads.
|
||||||
|
pub trait CaptchaStorage: Send + Sync + 'static
|
||||||
|
where
|
||||||
|
Self: Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
/// The error type of the storage.
|
||||||
|
type Error: std::fmt::Display + std::fmt::Debug + Send;
|
||||||
|
|
||||||
|
/// Store the captcha token and answer.
|
||||||
|
fn store_answer(
|
||||||
|
&self,
|
||||||
|
answer: String,
|
||||||
|
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send;
|
||||||
|
|
||||||
|
/// Returns the answer of the captcha token. This method will return None if the token is not exist.
|
||||||
|
fn get_answer(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send;
|
||||||
|
|
||||||
|
/// Clear the expired captcha.
|
||||||
|
fn clear_expired(
|
||||||
|
&self,
|
||||||
|
expired_after: Duration,
|
||||||
|
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
|
||||||
|
|
||||||
|
/// Clear the captcha by token.
|
||||||
|
fn clear_by_token(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> CaptchaStorage for Arc<T>
|
||||||
|
where
|
||||||
|
T: CaptchaStorage,
|
||||||
|
{
|
||||||
|
type Error = T::Error;
|
||||||
|
|
||||||
|
fn store_answer(
|
||||||
|
&self,
|
||||||
|
answer: String,
|
||||||
|
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
|
||||||
|
self.as_ref().store_answer(answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_answer(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
|
||||||
|
self.as_ref().get_answer(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_expired(
|
||||||
|
&self,
|
||||||
|
expired_after: Duration,
|
||||||
|
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
|
||||||
|
self.as_ref().clear_expired(expired_after)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_by_token(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
|
||||||
|
self.as_ref().clear_by_token(token)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue