mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-16 19:05:38 +00:00
feat/web: Patchset 3 (#455)
https://apifox.com/apidoc/shared-ceda7a60-e817-4ea8-827b-de4e874dc45e implement all backend API
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
//! Axum & Tower_sessions 组合
|
||||
//!
|
||||
//! - Axum: [axum](https://docs.rs/axum)
|
||||
//! - Tower Sessions: [axum](https://docs.rs/tower-sessions)
|
||||
|
||||
use super::AbstractCaptcha;
|
||||
use super::CaptchaUtil;
|
||||
use async_trait::async_trait;
|
||||
use axum::response::Response;
|
||||
use std::fmt::Debug;
|
||||
use tower_sessions::Session;
|
||||
|
||||
const CAPTCHA_KEY: &'static str = "ez-captcha";
|
||||
|
||||
/// Axum & Tower_Sessions
|
||||
#[async_trait]
|
||||
pub trait CaptchaAxumTowerSessionExt {
|
||||
/// 错误类型
|
||||
type Error: Debug + Send + Sync + 'static;
|
||||
|
||||
/// 将验证码图片写入响应,并将用户的验证码信息保存至Session中
|
||||
///
|
||||
/// Write the Captcha Image into the response and save the Captcha information into the user's Session.
|
||||
async fn out(&mut self, session: &Session) -> Result<Response, Self::Error>;
|
||||
}
|
||||
|
||||
/// Axum & Tower_Sessions - 静态方法
|
||||
#[async_trait]
|
||||
pub trait CaptchaAxumTowerSessionStaticExt {
|
||||
/// 验证验证码,返回的布尔值代表验证码是否正确
|
||||
///
|
||||
/// Verify the Captcha code, and return whether user's code is correct.
|
||||
async fn ver(code: &str, session: &Session) -> bool {
|
||||
match session.get::<String>(CAPTCHA_KEY).await {
|
||||
Ok(Some(ans)) => ans.to_ascii_lowercase() == code.to_ascii_lowercase(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// 清除Session中的验证码
|
||||
///
|
||||
/// Clear the Captcha in the session.
|
||||
async fn clear(session: &Session) {
|
||||
if session.remove::<String>(CAPTCHA_KEY).await.is_err() {
|
||||
tracing::warn!("Exception occurs during clearing the session.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: AbstractCaptcha + Send> CaptchaAxumTowerSessionExt for CaptchaUtil<T> {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
async fn out(&mut self, session: &Session) -> Result<Response, Self::Error> {
|
||||
let mut data = vec![];
|
||||
self.captcha_instance.out(&mut data)?;
|
||||
|
||||
let ans: String = self.captcha_instance.get_chars().iter().collect();
|
||||
session.insert(CAPTCHA_KEY, ans).await?;
|
||||
|
||||
let resp = Response::builder()
|
||||
.header("Content-Type", self.captcha_instance.get_content_type())
|
||||
.body(data.into())?;
|
||||
Ok(resp)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CaptchaAxumTowerSessionStaticExt for CaptchaUtil {}
|
||||
@@ -0,0 +1,41 @@
|
||||
pub mod axum_tower_sessions;
|
||||
|
||||
use super::base::captcha::AbstractCaptcha;
|
||||
use super::captcha::spec::SpecCaptcha;
|
||||
use super::{CaptchaFont, NewCaptcha};
|
||||
|
||||
/// 验证码工具类 - Captcha Utils
|
||||
///
|
||||
/// 默认使用[SpecCaptcha](静态PNG字母验证码)作为验证码实现,用户也可以指定其他实现了[AbstractCaptcha]的类型。
|
||||
///
|
||||
/// Use [SpecCaptcha] (static PNG-format alphabetical Captcha) as the default implement of the Captcha service. Users may use other implementation of [AbstractCaptcha] they prefer.
|
||||
///
|
||||
pub struct CaptchaUtil<T: AbstractCaptcha = SpecCaptcha> {
|
||||
captcha_instance: T,
|
||||
}
|
||||
|
||||
impl<T: AbstractCaptcha> NewCaptcha for CaptchaUtil<T> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
captcha_instance: T::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_size(width: i32, height: i32) -> Self {
|
||||
Self {
|
||||
captcha_instance: T::with_size(width, height),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_size_and_len(width: i32, height: i32, len: usize) -> Self {
|
||||
Self {
|
||||
captcha_instance: T::with_size_and_len(width, height, len),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_all(width: i32, height: i32, len: usize, font: CaptchaFont, font_size: f32) -> Self {
|
||||
Self {
|
||||
captcha_instance: T::with_all(width, height, len, font, font_size),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user