use std::time::Duration;
use super::tokens::sha256_hex;
use crate::crypto::{constant_time_eq, random_token};
use crate::db::{Db, sql};
use crate::webhook::hmac_sha256_hex;
use crate::{AppState, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CodeCheck {
Valid,
Wrong {
tries_left: u32,
},
Missing,
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct OneTimeCode;
const MAX_DIGITS: u32 = 12;
impl OneTimeCode {
pub async fn issue(
state: &AppState,
purpose: &str,
subject: &str,
ttl: Duration,
max_tries: u32,
) -> Result<String> {
Self::issue_digits(state, purpose, subject, 6, ttl, max_tries).await
}
pub async fn issue_digits(
state: &AppState,
purpose: &str,
subject: &str,
digits: u32,
ttl: Duration,
max_tries: u32,
) -> Result<String> {
let code = random_digits(digits);
store(state, purpose, subject, &code, None, ttl, max_tries).await?;
Ok(code)
}
pub async fn verify(
state: &AppState,
purpose: &str,
subject: &str,
code: &str,
) -> Result<CodeCheck> {
check(state, purpose, subject, code, None).await
}
pub async fn discard(state: &AppState, purpose: &str, subject: &str) -> Result<bool> {
let done = sql("DELETE FROM one_time_codes WHERE purpose = ? AND subject = ?")
.bind(purpose)
.bind(subject)
.execute(&state.db)
.await?;
Ok(done > 0)
}
}
pub async fn prune_expired_codes(db: &Db) -> Result<u64> {
Ok(sql("DELETE FROM one_time_codes WHERE expires_at < ?")
.bind(crate::clock::unix_secs())
.execute(db)
.await?)
}
pub(crate) fn random_digits(digits: u32) -> String {
let digits = digits.clamp(4, MAX_DIGITS);
let mut out = String::with_capacity(digits as usize);
let mut bytes = [0u8; 16];
rand::fill(&mut bytes);
let mut pool = bytes.to_vec();
while out.len() < digits as usize {
let Some(byte) = pool.pop() else {
rand::fill(&mut bytes);
pool = bytes.to_vec();
continue;
};
if byte < 250 {
out.push(char::from(b'0' + byte % 10));
}
}
out
}
fn clean(typed: &str) -> String {
typed
.chars()
.filter(|c| !c.is_whitespace() && *c != '-')
.collect()
}
fn keyed(state: &AppState, purpose: &str, subject: &str, secret: &str) -> String {
hmac_sha256_hex(
state.key.signing(),
format!("renox.one-time\0{purpose}\0{subject}\0{secret}"),
)
}
pub(crate) fn bind_hash(nonce: &str) -> String {
sha256_hex(nonce)
}
pub(crate) async fn store(
state: &AppState,
purpose: &str,
subject: &str,
secret: &str,
binding: Option<&str>,
ttl: Duration,
max_tries: u32,
) -> Result<i64> {
let now = crate::clock::unix_secs();
sql("DELETE FROM one_time_codes WHERE expires_at < ?")
.bind(now)
.execute(&state.db)
.await?;
let row = sql("INSERT INTO one_time_codes \
(purpose, subject, code, binding, attempts, max_attempts, expires_at, created_at) \
VALUES (?, ?, ?, ?, 0, ?, ?, ?) \
ON CONFLICT (purpose, subject) DO UPDATE SET code = excluded.code, \
binding = excluded.binding, attempts = 0, max_attempts = excluded.max_attempts, \
expires_at = excluded.expires_at, created_at = excluded.created_at \
RETURNING id")
.bind(purpose)
.bind(subject)
.bind(keyed(state, purpose, subject, secret))
.bind(binding.map(bind_hash))
.bind(i64::from(max_tries.max(1)))
.bind(now + i64::try_from(ttl.as_secs()).unwrap_or(i64::MAX / 2).max(1))
.bind(now)
.fetch_one(&state.db)
.await?;
Ok(row.try_get("id")?)
}
pub(crate) async fn check(
state: &AppState,
purpose: &str,
subject: &str,
typed: &str,
binding: Option<&str>,
) -> Result<CodeCheck> {
let now = crate::clock::unix_secs();
let Some(row) = sql("UPDATE one_time_codes SET attempts = attempts + 1 \
WHERE purpose = ? AND subject = ? AND expires_at > ? AND attempts < max_attempts \
AND (binding IS NULL OR binding = ?) \
RETURNING id, code, attempts, max_attempts")
.bind(purpose)
.bind(subject)
.bind(now)
.bind(binding.map(bind_hash))
.fetch_optional(&state.db)
.await?
else {
return Ok(CodeCheck::Missing);
};
let id: i64 = row.try_get("id")?;
let stored: String = row.try_get("code")?;
let attempts: i64 = row.try_get("attempts")?;
let max: i64 = row.try_get("max_attempts")?;
if constant_time_eq(&stored, &keyed(state, purpose, subject, &clean(typed))) {
let done = sql("DELETE FROM one_time_codes WHERE id = ? AND code = ?")
.bind(id)
.bind(&stored)
.execute(&state.db)
.await?;
return Ok(if done == 1 {
CodeCheck::Valid
} else {
CodeCheck::Missing
});
}
let left = u32::try_from((max - attempts).max(0)).unwrap_or(0);
if left == 0 {
sql("DELETE FROM one_time_codes WHERE id = ?")
.bind(id)
.execute(&state.db)
.await?;
}
Ok(CodeCheck::Wrong { tries_left: left })
}
pub(crate) async fn issue_token(
state: &AppState,
purpose: &str,
subject: &str,
binding: Option<&str>,
ttl: Duration,
) -> Result<String> {
let secret = random_token();
let id = store(state, purpose, subject, &secret, binding, ttl, 1).await?;
Ok(format!("{id}.{secret}"))
}
pub(crate) async fn peek_token(
state: &AppState,
purpose: &str,
token: &str,
binding: Option<&str>,
) -> Result<Option<(i64, String, String)>> {
let Some((id, secret)) = token.split_once('.') else {
return Ok(None);
};
let Ok(id) = id.parse::<i64>() else {
return Ok(None);
};
let Some(row) = sql("SELECT subject, code, binding FROM one_time_codes \
WHERE id = ? AND purpose = ? AND expires_at > ?")
.bind(id)
.bind(purpose)
.bind(crate::clock::unix_secs())
.fetch_optional(&state.db)
.await?
else {
return Ok(None);
};
let subject: String = row.try_get("subject")?;
let stored: String = row.try_get("code")?;
let bound: Option<String> = row.try_get("binding")?;
let right_secret = constant_time_eq(&stored, &keyed(state, purpose, &subject, secret));
let right_browser = match (&bound, binding) {
(None, _) => true,
(Some(bound), Some(nonce)) => constant_time_eq(bound, &bind_hash(nonce)),
(Some(_), None) => false,
};
Ok((right_secret && right_browser).then_some((id, subject, stored)))
}
pub(crate) async fn consume_token(
state: &AppState,
purpose: &str,
token: &str,
binding: Option<&str>,
) -> Result<Option<String>> {
let Some((id, subject, stored)) = peek_token(state, purpose, token, binding).await? else {
return Ok(None);
};
let done = sql("DELETE FROM one_time_codes WHERE id = ? AND code = ?")
.bind(id)
.bind(&stored)
.execute(&state.db)
.await?;
Ok((done == 1).then_some(subject))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn digits_are_digits_of_the_right_length() {
for digits in [4, 6, 8, 12] {
let code = random_digits(digits);
assert_eq!(code.len(), digits as usize);
assert!(code.bytes().all(|b| b.is_ascii_digit()));
}
assert_eq!(random_digits(1).len(), 4);
assert_eq!(random_digits(40).len(), 12);
}
#[test]
fn typed_codes_lose_spaces_and_dashes() {
assert_eq!(clean(" 123 456\n"), "123456");
assert_eq!(clean("123-456"), "123456");
}
}