use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use axum::extract::{Extension, Path, State};
use axum::response::Response;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::events::{LockedOut, LoginFailed, announce};
use super::module::{Settings, go, message, texts};
use super::one_time::{self, CodeCheck};
use super::tokens::sha256_hex;
use super::{Recipient, User};
use crate::db::Model;
use crate::i18n::Lang;
use crate::queue::{Job, JobContext};
use crate::validation::{Errors, Valid, Validate, ValidationError, Validator};
use crate::{AppState, ClientIp, Error, Htmx, Result, Session, View, context, view};
const LINK: &str = "login-link";
const CODE: &str = "login-code";
const NONCE: &str = "_login_nonce";
const PENDING: &str = "_login_code";
const PER_IDENTIFIER: u32 = 5;
const PER_IP: u32 = 20;
const SEND_WINDOW: Duration = Duration::from_secs(15 * 60);
type IdentifyFn = Arc<
dyn Fn(AppState, String) -> Pin<Box<dyn Future<Output = Result<Option<User>>> + Send>>
+ Send
+ Sync,
>;
type DeliverFn = Arc<
dyn Fn(AppState, LoginCodeDelivery) -> Pin<Box<dyn Future<Output = Result> + Send>>
+ Send
+ Sync,
>;
#[derive(Clone)]
pub struct LoginCodes {
pub(super) channels: Vec<String>,
pub(super) digits: u32,
pub(super) ttl: Duration,
pub(super) max_attempts: u32,
identify: Option<IdentifyFn>,
deliver: Option<DeliverFn>,
}
impl LoginCodes {
pub fn new() -> Self {
Self {
channels: Vec::new(),
digits: 6,
ttl: Duration::from_secs(5 * 60),
max_attempts: 5,
identify: None,
deliver: None,
}
}
pub fn channel(mut self, name: &str) -> Self {
if !self.channels.iter().any(|c| c == name) {
self.channels.push(name.to_owned());
}
self
}
pub fn channels<I, S>(mut self, names: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.channels.clear();
for name in names {
self = self.channel(name.as_ref());
}
self
}
pub fn digits(mut self, digits: u32) -> Self {
self.digits = digits.clamp(4, 12);
self
}
pub fn ttl(mut self, ttl: Duration) -> Self {
self.ttl = ttl;
self
}
pub fn max_attempts(mut self, attempts: u32) -> Self {
self.max_attempts = attempts.max(1);
self
}
pub fn identify<F, Fut>(mut self, find: F) -> Self
where
F: Fn(AppState, String) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Option<User>>> + Send + 'static,
{
self.identify = Some(Arc::new(move |state, input| Box::pin(find(state, input))));
self
}
pub fn deliver<F, Fut>(mut self, deliver: F) -> Self
where
F: Fn(AppState, LoginCodeDelivery) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result> + Send + 'static,
{
self.deliver = Some(Arc::new(move |state, delivery| {
Box::pin(deliver(state, delivery))
}));
self
}
}
impl Default for LoginCodes {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LoginCodeDelivery {
pub user: Option<User>,
pub channel: String,
pub address: String,
pub code: String,
pub expires_in: Duration,
pub locale: String,
pub text: String,
}
#[derive(Clone)]
pub(crate) struct MagicLink {
pub(crate) ttl: Duration,
}
#[derive(Debug, Clone)]
pub(crate) struct Sent {
pub(crate) channel: String,
pub(crate) code: Option<String>,
pub(crate) link: Option<String>,
}
#[derive(Clone)]
pub(crate) struct Passwordless {
pub(crate) magic_link: Option<MagicLink>,
pub(crate) same_browser: bool,
pub(crate) codes: Option<LoginCodes>,
pub(crate) register_on_first_use: bool,
pub(crate) sent: Arc<Mutex<Vec<Sent>>>,
}
#[derive(Serialize, Deserialize)]
pub(crate) struct SendLoginSecret {
link: Option<String>,
code: Option<String>,
user_id: Option<i64>,
channel: String,
address: String,
ttl_secs: u64,
locale: String,
}
impl Job for SendLoginSecret {
const NAME: &'static str = "renox.send-login-secret";
const MAX_ATTEMPTS: u32 = 3;
const ENCRYPTED: bool = true;
async fn handle(self, ctx: JobContext) -> Result {
let state = ctx.state;
let user = match self.user_id {
Some(id) => User::find(&state.db, id).await?,
None => None,
};
let lang = Lang::of(&state, &self.locale);
let text = texts(&lang);
let minutes = (self.ttl_secs / 60).max(1).to_string();
let say = |key: &str, params: &[(&str, String)]| {
crate::i18n::format(text[key].as_str().unwrap_or_default(), params, None)
};
if state.config.debug
&& let Some(settings) = &state.auth
{
let mut sent = settings
.passwordless
.sent
.lock()
.unwrap_or_else(|e| e.into_inner());
sent.push(Sent {
channel: self.channel.clone(),
code: self.code.clone(),
link: self.link.clone(),
});
let extra = sent.len().saturating_sub(50);
sent.drain(..extra);
}
if let Some(link) = &self.link {
let to = user
.as_ref()
.map_or_else(|| self.address.clone(), |u| u.email.clone());
let outro = say("mail_link_outro", &[("minutes", minutes)]);
let mail = state.mail_view_in(
&self.locale,
to,
say("mail_link_subject", &[]),
"renox/mail/auth/login-link",
context! { link, outro, text => text.clone() },
)?;
return state.mailer.send(mail).await;
}
let Some(code) = self.code else {
return Ok(());
};
let message = say(
"login_code_message",
&[
("app", state.config.name.clone()),
("code", code.clone()),
("minutes", minutes.clone()),
],
);
let delivery = LoginCodeDelivery {
user: user.clone(),
channel: self.channel.clone(),
address: self.address.clone(),
code: code.clone(),
expires_in: Duration::from_secs(self.ttl_secs),
locale: self.locale.clone(),
text: message.clone(),
};
let hook = state
.auth
.as_ref()
.and_then(|settings| settings.passwordless.codes.as_ref())
.and_then(|codes| codes.deliver.clone());
if let Some(hook) = hook {
return hook(state.clone(), delivery).await;
}
if self.channel == "mail" {
let to = user
.as_ref()
.map_or_else(|| self.address.clone(), |u| u.email.clone());
let outro = say("mail_code_outro", &[("minutes", minutes)]);
let mail = state.mail_view_in(
&self.locale,
to,
say("mail_code_subject", &[]),
"renox/mail/auth/login-code",
context! { code, outro, text => text.clone() },
)?;
return state.mailer.send(mail).await;
}
let recipient = match &user {
Some(user) => Recipient::for_user(user).and(&self.channel, &self.address),
None => Recipient::to(&self.channel, &self.address),
}
.in_locale(&self.locale);
let send = state.channel(&self.channel)?;
let payload = json!({
"kind": "login_code",
"code": code,
"text": message,
"expires_in": self.ttl_secs,
});
send(state.clone(), recipient, payload).await
}
}
fn nonce(session: &Session) -> Result<String> {
if let Some(nonce) = session.get::<String>(NONCE) {
return Ok(nonce);
}
let nonce = crate::crypto::random_token();
session.put(NONCE, &nonce)?;
Ok(nonce)
}
async fn within_send_limits(
state: &AppState,
kind: &str,
identifier: &str,
ip: Option<IpAddr>,
) -> Result<bool> {
if let Some(ip) = ip {
let (count, _) =
crate::counters::increment(&state.db, &format!("passwordless:ip:{ip}"), SEND_WINDOW)
.await?;
if count > PER_IP {
return Err(Error::TooManyRequests);
}
}
let key = format!("passwordless:{kind}:{identifier}");
let (count, _) = crate::counters::increment(&state.db, &key, SEND_WINDOW).await?;
Ok(count <= PER_IDENTIFIER)
}
async fn ensure_not_locked(
state: &AppState,
lang: &Lang,
identifier: &str,
ip: Option<IpAddr>,
field: &str,
) -> Result<()> {
let Some(seconds) = state.throttle.blocked_for(identifier, ip).await else {
return Ok(());
};
announce(
state,
LockedOut {
email: identifier.to_owned(),
ip: ip.map(|ip| ip.to_string()),
seconds,
},
)
.await;
let mut errors = Errors::new();
errors.add(
field,
message(lang, "auth.throttle", &[("seconds", seconds.to_string())]),
);
Err(ValidationError::new(errors).into())
}
async fn failed(state: &AppState, identifier: &str, ip: Option<IpAddr>) {
state.throttle.fail(identifier, ip).await;
let event = LoginFailed {
email: identifier.to_owned(),
ip: ip.map(|ip| ip.to_string()),
};
announce(state, event).await;
}
fn field_error(field: &str, text: impl Into<String>) -> Error {
let mut errors = Errors::new();
errors.add(field, text.into());
ValidationError::new(errors).into()
}
async fn mark_verified(state: &AppState, user: &User) -> Result {
if user.email_verified_at.is_none() {
crate::db::sql(
"UPDATE users SET email_verified_at = ? WHERE id = ? AND email_verified_at IS NULL",
)
.bind(crate::db::now())
.bind(user.id)
.execute(&state.db)
.await?;
}
Ok(())
}
fn looks_like_email(input: &str) -> bool {
input.contains('@') && !input.contains(char::is_whitespace)
}
async fn first_use_account(
state: &AppState,
settings: &Settings,
email: &str,
via: &str,
) -> Result<Option<User>> {
if !settings.passwordless.register_on_first_use || !settings.registration {
return Ok(None);
}
match super::register_verified(state, "", email, &[("via", via)]).await {
Ok(user) => Ok(Some(user)),
Err(err) if err.is_unique_violation() => User::find_by_email(&state.db, email).await,
Err(err) => Err(err),
}
}
pub(super) async fn show_link_request(lang: Lang) -> View {
view(
"renox/auth/login-link.html",
context! { text => texts(&lang) },
)
}
#[derive(Deserialize)]
pub(super) struct LinkForm {
email: String,
}
impl Validate for LinkForm {
fn rules(&self, v: &mut Validator) {
v.field("email", &super::user::normalize_email(&self.email))
.required()
.email()
.max(255);
}
}
pub(super) async fn send_link(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
ClientIp(ip): ClientIp,
lang: Lang,
Valid(form): Valid<LinkForm>,
) -> Result<Response> {
let Some(link) = settings.passwordless.magic_link.clone() else {
return Err(Error::NotFound);
};
let email = super::user::normalize_email(&form.email);
if within_send_limits(&state, "link", &email, ip).await? {
let user = User::find_by_email(&state.db, &email).await?;
let first_use =
user.is_none() && settings.passwordless.register_on_first_use && settings.registration;
let binding = if settings.passwordless.same_browser {
Some(nonce(&session)?)
} else {
None
};
let token =
one_time::issue_token(&state, LINK, &email, binding.as_deref(), link.ttl).await?;
if user.is_some() || first_use {
let job = SendLoginSecret {
link: Some(state.absolute_url("login.link.show", &[&token])?),
code: None,
user_id: user.as_ref().map(|u| u.id),
channel: "mail".into(),
address: email,
ttl_secs: link.ttl.as_secs(),
locale: crate::i18n::current_locale(&state),
};
state.dispatch(job).await?;
}
}
session.flash("status", texts(&lang)["login_link_sent"].clone())?;
Ok(go(&htmx, state.url("login.link.request", &[])?))
}
#[derive(Serialize)]
struct LinkPage {
token: String,
email: String,
valid: bool,
}
pub(super) async fn show_link(
State(state): State<AppState>,
session: Session,
lang: Lang,
Path(token): Path<String>,
) -> Result<View> {
let seen = one_time::peek_token(
&state,
LINK,
&token,
session.get::<String>(NONCE).as_deref(),
)
.await?;
let page = LinkPage {
valid: seen.is_some(),
email: seen.map(|(_, subject, _)| subject).unwrap_or_default(),
token,
};
Ok(view(
"renox/auth/login-link-confirm.html",
context! { text => texts(&lang), page },
))
}
pub(super) async fn consume_link(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
ClientIp(ip): ClientIp,
lang: Lang,
Path(token): Path<String>,
) -> Result<Response> {
let text = texts(&lang);
let back = |session: &Session| -> Result<Response> {
session.flash("error", text["login_link_invalid"].clone())?;
Ok(go(&htmx, state.url("login.link.request", &[])?))
};
let nonce_now = session.get::<String>(NONCE);
let Some((_, subject, _)) =
one_time::peek_token(&state, LINK, &token, nonce_now.as_deref()).await?
else {
return back(&session);
};
ensure_not_locked(&state, &lang, &subject, ip, "email").await?;
let Some(email) = one_time::consume_token(&state, LINK, &token, nonce_now.as_deref()).await?
else {
return back(&session);
};
let user = match User::find_by_email(&state.db, &email).await? {
Some(user) => user,
None => match first_use_account(&state, &settings, &email, "magic_link").await? {
Some(user) => user,
None => return back(&session),
},
};
mark_verified(&state, &user).await?;
let to = super::external::sign_in_via(&state, &session, &user, false, ip, "magic_link").await?;
session.remove(NONCE);
Ok(go(&htmx, to))
}
#[derive(Serialize, Deserialize)]
struct PendingCode {
identifier: String,
channel: String,
}
#[derive(Serialize)]
struct ChannelChoice {
name: String,
label: String,
}
fn choices(codes: &LoginCodes, text: &Value) -> Vec<ChannelChoice> {
codes
.channels
.iter()
.map(|name| ChannelChoice {
label: channel_label(name, text),
name: name.clone(),
})
.collect()
}
fn channel_label(name: &str, text: &Value) -> String {
match text.get(format!("channel_{name}")).and_then(Value::as_str) {
Some(label) => label.to_owned(),
None => {
let mut chars = name.chars();
chars
.next()
.map(|first| first.to_uppercase().chain(chars).collect())
.unwrap_or_default()
}
}
}
fn codes_of(settings: &Settings) -> Result<&LoginCodes> {
settings.passwordless.codes.as_ref().ok_or(Error::NotFound)
}
pub(super) async fn show_code_request(
Extension(settings): Extension<Arc<Settings>>,
lang: Lang,
) -> Result<View> {
let codes = codes_of(&settings)?;
let text = texts(&lang);
let channels = choices(codes, &text);
Ok(view(
"renox/auth/login-code.html",
context! { multiple => channels.len() > 1, channels, text },
))
}
#[derive(Deserialize)]
pub(super) struct CodeRequestForm {
identifier: String,
channel: Option<String>,
}
impl Validate for CodeRequestForm {
fn rules(&self, v: &mut Validator) {
v.field("identifier", &self.identifier).required().max(255);
}
}
async fn who(
state: &AppState,
codes: &LoginCodes,
identifier: &str,
) -> Result<(Option<User>, String)> {
let user = match &codes.identify {
Some(find) => find(state.clone(), identifier.to_owned()).await?,
None => User::find_by_email(&state.db, identifier).await?,
};
let subject = match &user {
Some(user) => format!("user:{}", user.id),
None => format!("unknown:{}", sha256_hex(&identifier.to_lowercase())),
};
Ok((user, subject))
}
async fn issue_code(
settings: &Settings,
state: &AppState,
session: &Session,
ip: Option<IpAddr>,
identifier: &str,
channel: &str,
) -> Result<()> {
let codes = codes_of(settings)?;
let key = identifier.to_lowercase();
let allowed = within_send_limits(state, "code", &key, ip).await?;
let (user, subject) = who(state, codes, identifier).await?;
let nonce = nonce(session)?;
session.put(
PENDING,
PendingCode {
identifier: identifier.to_owned(),
channel: channel.to_owned(),
},
)?;
if !allowed {
return Ok(());
}
let first_use = user.is_none()
&& settings.passwordless.register_on_first_use
&& settings.registration
&& channel == "mail"
&& looks_like_email(identifier);
let code = one_time::random_digits(codes.digits);
one_time::store(
state,
CODE,
&subject,
&code,
Some(&nonce),
codes.ttl,
codes.max_attempts,
)
.await?;
if user.is_some() || first_use {
let job = SendLoginSecret {
link: None,
code: Some(code),
user_id: user.as_ref().map(|u| u.id),
channel: channel.to_owned(),
address: if channel == "mail" {
super::user::normalize_email(identifier)
} else {
identifier.to_owned()
},
ttl_secs: codes.ttl.as_secs(),
locale: crate::i18n::current_locale(state),
};
state.dispatch(job).await?;
}
Ok(())
}
fn chosen(codes: &LoginCodes, wanted: Option<&str>) -> String {
wanted
.filter(|name| codes.channels.iter().any(|c| c == name))
.or(codes.channels.first().map(String::as_str))
.unwrap_or("mail")
.to_owned()
}
pub(super) async fn send_code(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
ClientIp(ip): ClientIp,
lang: Lang,
Valid(form): Valid<CodeRequestForm>,
) -> Result<Response> {
let codes = codes_of(&settings)?;
let identifier = form.identifier.trim();
ensure_not_locked(&state, &lang, &identifier.to_lowercase(), ip, "identifier").await?;
let channel = chosen(codes, form.channel.as_deref());
issue_code(&settings, &state, &session, ip, identifier, &channel).await?;
session.flash("status", texts(&lang)["login_code_sent"].clone())?;
Ok(go(&htmx, state.url("login.code.verify", &[])?))
}
#[derive(Deserialize)]
pub(super) struct ResendForm {
channel: Option<String>,
}
impl Validate for ResendForm {
fn rules(&self, _: &mut Validator) {}
}
pub(super) async fn resend_code(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
ClientIp(ip): ClientIp,
lang: Lang,
Valid(form): Valid<ResendForm>,
) -> Result<Response> {
let codes = codes_of(&settings)?;
let Some(pending) = session.get::<PendingCode>(PENDING) else {
return Ok(go(&htmx, state.url("login.code.request", &[])?));
};
ensure_not_locked(
&state,
&lang,
&pending.identifier.to_lowercase(),
ip,
"code",
)
.await?;
let channel = chosen(codes, form.channel.as_deref().or(Some(&pending.channel)));
issue_code(
&settings,
&state,
&session,
ip,
&pending.identifier,
&channel,
)
.await?;
session.flash("status", texts(&lang)["login_code_sent"].clone())?;
Ok(go(&htmx, state.url("login.code.verify", &[])?))
}
#[derive(Serialize)]
struct VerifyPage {
channel: String,
minutes: u64,
others: Vec<ChannelChoice>,
}
pub(super) async fn show_code_verify(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
lang: Lang,
) -> Result<Response> {
use axum::response::IntoResponse;
let codes = codes_of(&settings)?;
let Some(pending) = session.get::<PendingCode>(PENDING) else {
return Ok(go(&htmx, state.url("login.code.request", &[])?));
};
let text = texts(&lang);
let page = VerifyPage {
channel: channel_label(&pending.channel, &text),
minutes: (codes.ttl.as_secs() / 60).max(1),
others: choices(codes, &text)
.into_iter()
.filter(|choice| choice.name != pending.channel)
.collect(),
};
let intro = crate::i18n::format(
text["login_code_verify_intro"].as_str().unwrap_or_default(),
&[
("channel", page.channel.clone()),
("minutes", page.minutes.to_string()),
],
None,
);
Ok(view(
"renox/auth/login-code-verify.html",
context! { text, page, intro },
)
.into_response())
}
#[derive(Deserialize)]
pub(super) struct CodeForm {
code: String,
}
impl Validate for CodeForm {
fn rules(&self, v: &mut Validator) {
v.field("code", &self.code).required().max(32);
}
}
pub(super) async fn check_code(
Extension(settings): Extension<Arc<Settings>>,
State(state): State<AppState>,
session: Session,
htmx: Htmx,
ClientIp(ip): ClientIp,
lang: Lang,
Valid(form): Valid<CodeForm>,
) -> Result<Response> {
let codes = codes_of(&settings)?;
let text = texts(&lang);
let to_request = |session: &Session, key: &str| -> Result<Response> {
session.remove(PENDING);
session.flash("error", text[key].clone())?;
Ok(go(&htmx, state.url("login.code.request", &[])?))
};
let (Some(pending), Some(nonce)) = (
session.get::<PendingCode>(PENDING),
session.get::<String>(NONCE),
) else {
return Ok(go(&htmx, state.url("login.code.request", &[])?));
};
let key = pending.identifier.to_lowercase();
ensure_not_locked(&state, &lang, &key, ip, "code").await?;
let (user, subject) = who(&state, codes, &pending.identifier).await?;
let result = one_time::check(&state, CODE, &subject, &form.code, Some(&nonce)).await?;
match result {
CodeCheck::Valid => {}
CodeCheck::Wrong { tries_left } => {
failed(&state, &key, ip).await;
if tries_left == 0 {
return to_request(&session, "login_code_burned");
}
return Err(field_error(
"code",
text["login_code_wrong"].as_str().unwrap_or_default(),
));
}
CodeCheck::Missing => return to_request(&session, "login_code_expired"),
}
let user = match user {
Some(user) => user,
None if pending.channel == "mail" && looks_like_email(&pending.identifier) => {
let email = super::user::normalize_email(&pending.identifier);
match first_use_account(&state, &settings, &email, "code").await? {
Some(user) => user,
None => return to_request(&session, "login_code_expired"),
}
}
None => return to_request(&session, "login_code_expired"),
};
if pending.channel == "mail" {
mark_verified(&state, &user).await?;
}
let to = super::external::sign_in_via(&state, &session, &user, false, ip, "code").await?;
session.remove(PENDING);
session.remove(NONCE);
Ok(go(&htmx, to))
}
pub(crate) fn all_sent(state: &AppState) -> Vec<Sent> {
state.auth.as_ref().map_or_else(Vec::new, |settings| {
settings
.passwordless
.sent
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
})
}