tokens and more

This commit is contained in:
Mateusz Gruszczyński
2026-08-01 00:15:37 +02:00
parent 6c5232ccc5
commit 1401054c71
18 changed files with 1966 additions and 285 deletions
+34 -20
View File
@@ -444,7 +444,8 @@ pub async fn register(
"Account created. Check your e-mail and confirm the account before logging in."
.into(),
}),
).into_response());
)
.into_response());
}
let session = create_session(&state, &user).await?;
@@ -461,7 +462,8 @@ pub async fn register(
theme: session.theme,
message: "Account created.".into(),
}),
).into_response();
)
.into_response();
response.headers_mut().insert(header::SET_COOKIE, cookie);
Ok(response)
}
@@ -479,15 +481,19 @@ pub async fn login(
state
.check_rate_limit(client_limit_key.clone(), 30, window)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many login attempts. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many login attempts. Try again in {seconds} seconds."
))
})?;
state
.check_rate_limit(limit_key.clone(), 5, window)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many login attempts. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many login attempts. Try again in {seconds} seconds."
))
})?;
let session = if state.ldap.is_some() {
ldap::login(&state, &req.email, &req.password).await?
} else {
@@ -1816,9 +1822,11 @@ pub async fn request_reset(
state
.check_rate_limit(format!("password-reset-client:{client_key}"), 10, window)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many password reset requests. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many password reset requests. Try again in {seconds} seconds."
))
})?;
state
.check_rate_limit(
format!("password-reset:{client_key}:{}", normalize(&email)),
@@ -1826,9 +1834,11 @@ pub async fn request_reset(
window,
)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many password reset requests. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many password reset requests. Try again in {seconds} seconds."
))
})?;
info!(email_domain = %email_domain(&email), "password reset requested");
let smtp = state.smtp.as_ref().ok_or_else(|| {
AuthError::service_unavailable("Password reset is not configured on this server.")
@@ -1883,15 +1893,19 @@ pub async fn confirm_reset(
state
.check_rate_limit(client_limit_key.clone(), 20, window)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many reset attempts. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many reset attempts. Try again in {seconds} seconds."
))
})?;
state
.check_rate_limit(limit_key.clone(), 10, window)
.await
.map_err(|seconds| AuthError::rate_limited(&format!(
"Too many reset attempts. Try again in {seconds} seconds."
)))?;
.map_err(|seconds| {
AuthError::rate_limited(&format!(
"Too many reset attempts. Try again in {seconds} seconds."
))
})?;
info!("password reset confirmation requested");
let now_time = Utc::now();
let now = now_time.to_rfc3339();