Cloudflare Turnstile on e-mail sign-up and password reset, and a limit per address
- The sign-up and "forgot password" forms carry a token from Cloudflare Turnstile's widget (managed, shown only when Cloudflare asks for a click); the backend checks it before an e-mail goes out. Off without TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY (development); the site key comes to the page through /v1/config. - At most ten such e-mail requests an hour per visitor address, so a script cannot make the site mail thousands of strangers. - The privacy notice (HU, EN) mentions Turnstile with Cloudflare. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -227,6 +227,19 @@ pub struct RegisterRequest {
|
||||
invite_token: Option<String>,
|
||||
/// The slug of the shared link the visitor came from.
|
||||
referral: Option<String>,
|
||||
/// Cloudflare Turnstile's token (see [`crate::turnstile`]).
|
||||
#[serde(default)]
|
||||
turnstile_token: Option<String>,
|
||||
}
|
||||
|
||||
/// Before a sign-up or reset e-mail goes out: at most a few per visitor
|
||||
/// address an hour, and Cloudflare Turnstile's check when it is set up.
|
||||
async fn not_a_robot(state: &AppState, headers: &axum::http::HeaderMap, token: Option<&str>) -> Result<(), ApiError> {
|
||||
let ip = super::client_ip(headers);
|
||||
if let Some(ip) = &ip {
|
||||
state.signup_limiter.hit(ip)?;
|
||||
}
|
||||
crate::turnstile::verify(state.config.turnstile.as_ref(), token, ip.as_deref()).await
|
||||
}
|
||||
|
||||
/// Password endpoints answer 403 while `PASSWORD_LOGIN` is off.
|
||||
@@ -244,10 +257,12 @@ pub fn password_login_enabled(state: &crate::AppState) -> Result<(), ApiError> {
|
||||
/// password instead), so the form does not reveal who is registered.
|
||||
pub async fn register(
|
||||
State(state): St,
|
||||
headers: axum::http::HeaderMap,
|
||||
WithRejection(Json(req), _): Body<RegisterRequest>,
|
||||
) -> ApiResult<impl IntoResponse> {
|
||||
password_login_enabled(&state)?;
|
||||
let email = normalize_email(&req.email)?;
|
||||
not_a_robot(&state, &headers, req.turnstile_token.as_deref()).await?;
|
||||
let locale = validate_locale(&state, req.locale.as_deref())?;
|
||||
|
||||
let mut tx = state.db.begin().await?;
|
||||
@@ -553,6 +568,9 @@ pub async fn logout_all(State(state): St, auth: AuthUser, jar: CookieJar) -> Api
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ResetRequest {
|
||||
email: String,
|
||||
/// Cloudflare Turnstile's token (see [`crate::turnstile`]).
|
||||
#[serde(default)]
|
||||
turnstile_token: Option<String>,
|
||||
}
|
||||
|
||||
/// Forgotten password: a link to set a new one (also adds a password to a
|
||||
@@ -560,9 +578,11 @@ pub struct ResetRequest {
|
||||
/// whether the address is registered.
|
||||
pub async fn request_password_reset(
|
||||
State(state): St,
|
||||
headers: axum::http::HeaderMap,
|
||||
WithRejection(Json(req), _): Body<ResetRequest>,
|
||||
) -> ApiResult<StatusCode> {
|
||||
password_login_enabled(&state)?;
|
||||
not_a_robot(&state, &headers, req.turnstile_token.as_deref()).await?;
|
||||
let Ok(email) = normalize_email(&req.email) else { return Ok(StatusCode::ACCEPTED) };
|
||||
let mut tx = state.db.begin().await?;
|
||||
let user: Option<User> = sqlx::query_as(&format!("SELECT {USER_COLUMNS} FROM users WHERE email = $1 FOR UPDATE"))
|
||||
|
||||
@@ -233,5 +233,7 @@ async fn public_config(State(state): St) -> Json<Value> {
|
||||
"referral_rewards": c.referral_rewards,
|
||||
"signup_mode": c.signup_mode.as_str(),
|
||||
"password_min_length": crate::auth::MIN_PASSWORD_LEN,
|
||||
// Cloudflare Turnstile on the e-mail sign-up and reset forms (null: none).
|
||||
"turnstile_site_key": c.turnstile.as_ref().map(|t| t.site_key.clone()),
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user