Skip to content

Commit

Permalink
add captcha type Digits
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Oct 11, 2024
1 parent 39a46d2 commit 48b9bee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/controller/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use axum::{
use axum_extra::{headers::Cookie, TypedHeader};
use axum_garde::WithValidation;
use bincode::config::standard;
use captcha::{CaptchaName, Difficulty};
use captcha::{
filters::{Cow, Noise, Wave},
Captcha, CaptchaName, Difficulty, Geometry,
};
use data_encoding::BASE64;
use garde::Validate;
use identicon::Identicon;
Expand Down Expand Up @@ -898,14 +901,14 @@ pub(crate) async fn signup() -> Result<impl IntoResponse, AppError> {
_ => unreachable!(),
};

let captcha_name = match site_config.captcha_name.as_str() {
"Amelia" => CaptchaName::Amelia,
"Lucy" => CaptchaName::Lucy,
"Mila" => CaptchaName::Mila,
let captcha = match site_config.captcha_name.as_str() {
"Amelia" => captcha::by_name(captcha_difficulty, CaptchaName::Amelia),
"Lucy" => captcha::by_name(captcha_difficulty, CaptchaName::Lucy),
"Mila" => captcha::by_name(captcha_difficulty, CaptchaName::Mila),
"Digits" => captcha_digits(),
_ => unreachable!(),
};

let captcha = captcha::by_name(captcha_difficulty, captcha_name);
let captcha_id = generate_nanoid_ttl(60);
DB.open_tree("captcha")?
.insert(&captcha_id, &*captcha.chars_as_string())?;
Expand All @@ -918,6 +921,26 @@ pub(crate) async fn signup() -> Result<impl IntoResponse, AppError> {
Ok(into_response(&page_signup))
}

/// Captcha with digits
///
/// From: https://github.com/daniel-e/captcha/blob/master/examples/captcha.rs
fn captcha_digits() -> Captcha {
let mut c = Captcha::new();
c.set_chars(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
c.add_chars(6)
.apply_filter(Noise::new(0.2))
.apply_filter(Wave::new(2.0, 20.0))
.view(220, 120)
.apply_filter(
Cow::new()
.min_radius(40)
.max_radius(50)
.circles(1)
.area(Geometry::new(40, 150, 50, 70)),
);
c
}

/// `POST /signup`
pub(crate) async fn signup_post(
WithValidation(input): WithValidation<Form<FormSignup>>,
Expand Down
1 change: 1 addition & 0 deletions templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<label class="radio"><input type="radio" name="captcha_name" value="Amelia" {% if site_config.captcha_name.as_str() == "Amelia" %} checked {% endif %} autocomplete="off" /> Amelia</label>
<label class="radio"><input type="radio" name="captcha_name" value="Lucy" {% if site_config.captcha_name.as_str() == "Lucy" %} checked {% endif %} autocomplete="off" /> Lucy</label>
<label class="radio"><input type="radio" name="captcha_name" value="Mila" {% if site_config.captcha_name.as_str() == "Mila" %} checked {% endif %} autocomplete="off" /> Mila</label>
<label class="radio"><input type="radio" name="captcha_name" value="Digits" {% if site_config.captcha_name.as_str() == "Digits" %} checked {% endif %} autocomplete="off" /> Digits</label>
</div>
</div>
</div>
Expand Down

0 comments on commit 48b9bee

Please sign in to comment.