use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use id_effect::{Effect, UtcDateTime, fail, succeed, suspend};
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;
use super::AccountError;
use crate::apikey::random_hex;
const ID_BYTES: usize = 8;
const SECRET_BYTES: usize = 32;
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn is_hex(s: &str, len: usize) -> bool {
s.len() == len
&& s
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
fn digest(secret: &str) -> [u8; 32] {
Sha256::digest(secret.as_bytes()).into()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TokenKind {
VerifyEmail,
ResetPassword,
}
impl TokenKind {
pub fn as_str(self) -> &'static str {
match self {
Self::VerifyEmail => "verify_email",
Self::ResetPassword => "reset_password",
}
}
pub fn parse(s: &str) -> Option<Self> {
match s {
"verify_email" => Some(Self::VerifyEmail),
"reset_password" => Some(Self::ResetPassword),
_ => None,
}
}
}
impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct AccountToken(String);
impl AccountToken {
pub fn generate(
kind: TokenKind,
account_id: impl Into<String>,
expires_at: UtcDateTime,
) -> Result<(Self, TokenRecord), AccountError> {
let random = |len| random_hex(len).map_err(|e| AccountError::Random(e.to_string()));
let id = random(ID_BYTES)?;
let secret = random(SECRET_BYTES)?;
let record = TokenRecord {
id: id.clone(),
kind,
account_id: account_id.into(),
digest: digest(&secret),
expires_at,
};
Ok((Self(format!("{id}_{secret}")), record))
}
#[inline]
pub fn expose(&self) -> &str {
&self.0
}
pub(crate) fn split(presented: &str) -> Option<(&str, &str)> {
let (id, secret) = presented.split_once('_')?;
(is_hex(id, ID_BYTES * 2) && is_hex(secret, SECRET_BYTES * 2)).then_some((id, secret))
}
}
impl fmt::Debug for AccountToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = self.0.split_once('_').map_or("", |(id, _)| id);
write!(f, "AccountToken({id}_…)")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TokenRecord {
id: String,
kind: TokenKind,
account_id: String,
digest: [u8; 32],
expires_at: UtcDateTime,
}
impl TokenRecord {
pub fn from_parts(
id: impl Into<String>,
kind: TokenKind,
account_id: impl Into<String>,
digest_hex: &str,
expires_at: UtcDateTime,
) -> Result<Self, AccountError> {
let id = id.into();
let malformed = || AccountError::Storage("malformed account token record".into());
if !is_hex(&id, ID_BYTES * 2) || !is_hex(digest_hex, 64) {
return Err(malformed());
}
let mut digest = [0_u8; 32];
for (i, byte) in digest.iter_mut().enumerate() {
*byte = u8::from_str_radix(&digest_hex[i * 2..i * 2 + 2], 16).map_err(|_| malformed())?;
}
Ok(Self {
id,
kind,
account_id: account_id.into(),
digest,
expires_at,
})
}
#[inline]
pub fn id(&self) -> &str {
&self.id
}
#[inline]
pub fn kind(&self) -> TokenKind {
self.kind
}
#[inline]
pub fn account_id(&self) -> &str {
&self.account_id
}
pub fn digest_hex(&self) -> String {
hex(&self.digest)
}
#[inline]
pub fn expires_at(&self) -> UtcDateTime {
self.expires_at
}
pub(crate) fn matches(&self, secret: &str) -> bool {
bool::from(digest(secret).ct_eq(&self.digest))
}
}
pub trait AccountTokenStore: Send + Sync + 'static {
fn insert(&self, record: TokenRecord) -> Effect<(), AccountError>;
fn find(&self, id: &str) -> Effect<Option<TokenRecord>, AccountError>;
fn consume(&self, id: &str) -> Effect<bool, AccountError>;
fn remove_account(&self, account_id: &str, kind: TokenKind) -> Effect<u64, AccountError>;
fn purge_expired(&self, now: UtcDateTime) -> Effect<u64, AccountError>;
}
#[derive(Default)]
struct MemoryTokensState {
records: HashMap<String, TokenRecord>,
fail: Option<String>,
}
#[derive(Clone, Default)]
pub struct MemoryAccountTokenStore {
inner: Arc<Mutex<MemoryTokensState>>,
}
impl fmt::Debug for MemoryAccountTokenStore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MemoryAccountTokenStore")
.field("len", &self.len())
.finish()
}
}
impl MemoryAccountTokenStore {
#[inline]
pub fn new() -> Self {
Self::default()
}
fn state(&self) -> MutexGuard<'_, MemoryTokensState> {
self.inner.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn len(&self) -> usize {
self.state().records.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn records(&self) -> Vec<TokenRecord> {
self.state().records.values().cloned().collect()
}
pub fn fail_with(&self, message: Option<&str>) {
self.state().fail = message.map(str::to_owned);
}
fn with<A: Send + 'static>(
&self,
f: impl FnOnce(&mut HashMap<String, TokenRecord>) -> A + Send + 'static,
) -> Effect<A, AccountError> {
let this = self.clone();
suspend(move || {
let mut st = this.state();
match st.fail.clone() {
Some(m) => fail(AccountError::Storage(m)),
None => succeed(f(&mut st.records)),
}
})
}
}
fn count(n: usize) -> u64 {
u64::try_from(n).unwrap_or(u64::MAX)
}
impl AccountTokenStore for MemoryAccountTokenStore {
fn insert(&self, record: TokenRecord) -> Effect<(), AccountError> {
self.with(move |records| {
records.insert(record.id.clone(), record);
})
}
fn find(&self, id: &str) -> Effect<Option<TokenRecord>, AccountError> {
let id = id.to_owned();
self.with(move |records| records.get(&id).cloned())
}
fn consume(&self, id: &str) -> Effect<bool, AccountError> {
let id = id.to_owned();
self.with(move |records| records.remove(&id).is_some())
}
fn remove_account(&self, account_id: &str, kind: TokenKind) -> Effect<u64, AccountError> {
let account_id = account_id.to_owned();
self.with(move |records| {
let before = records.len();
records.retain(|_, r| !(r.account_id == account_id && r.kind == kind));
count(before - records.len())
})
}
fn purge_expired(&self, now: UtcDateTime) -> Effect<u64, AccountError> {
self.with(move |records| {
let before = records.len();
records.retain(|_, r| r.expires_at > now);
count(before - records.len())
})
}
}
#[cfg(test)]
mod tests {
use id_effect::{Cause, Exit};
use super::*;
fn at(secs: i64) -> UtcDateTime {
UtcDateTime::unsafe_make(secs * 1000)
}
fn run<A: Send + 'static>(eff: Effect<A, AccountError>) -> Result<A, AccountError> {
match eff.block_on() {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(c) => panic!("unexpected cause: {c:?}"),
}
}
#[test]
fn generated_tokens_split_and_match_only_their_record() {
let (token, record) = AccountToken::generate(TokenKind::ResetPassword, "a1", at(9)).unwrap();
let (id, secret) = AccountToken::split(token.expose()).expect("well formed");
assert_eq!(id, record.id());
assert!(record.matches(secret));
assert!(!record.matches(&"0".repeat(64)));
assert_eq!(record.kind(), TokenKind::ResetPassword);
assert_eq!(record.account_id(), "a1");
assert_eq!(record.expires_at(), at(9));
assert!(!record.digest_hex().contains(secret));
assert_eq!(format!("{token:?}"), format!("AccountToken({id}_…)"));
let (other, _) = AccountToken::generate(TokenKind::ResetPassword, "a1", at(9)).unwrap();
assert_ne!(token, other);
}
#[test]
fn malformed_tokens_and_records_are_rejected() {
for bad in [
"",
"abc",
&format!("{}_{}", "0".repeat(16), "0".repeat(63)),
&format!("{}_{}", "G".repeat(16), "0".repeat(64)),
&format!("{}{}", "0".repeat(16), "0".repeat(64)),
] {
assert_eq!(AccountToken::split(bad), None, "{bad}");
}
let (_, record) = AccountToken::generate(TokenKind::VerifyEmail, "a", at(1)).unwrap();
let back =
TokenRecord::from_parts(record.id(), record.kind(), "a", &record.digest_hex(), at(1))
.unwrap();
assert_eq!(back, record);
assert!(TokenRecord::from_parts("x", TokenKind::VerifyEmail, "a", "00", at(1)).is_err());
}
#[test]
fn kinds_round_trip_their_names() {
for kind in [TokenKind::VerifyEmail, TokenKind::ResetPassword] {
assert_eq!(TokenKind::parse(kind.as_str()), Some(kind));
assert_eq!(kind.to_string(), kind.as_str());
}
assert_eq!(TokenKind::parse("other"), None);
}
#[test]
fn the_memory_store_consumes_once_and_purges() {
let store = MemoryAccountTokenStore::new();
let (_, a) = AccountToken::generate(TokenKind::VerifyEmail, "a", at(10)).unwrap();
let (_, b) = AccountToken::generate(TokenKind::ResetPassword, "a", at(10)).unwrap();
let (_, c) = AccountToken::generate(TokenKind::ResetPassword, "b", at(5)).unwrap();
for r in [&a, &b, &c] {
run(store.insert(r.clone())).unwrap();
}
assert_eq!(run(store.find(a.id())), Ok(Some(a.clone())));
assert_eq!(run(store.consume(a.id())), Ok(true));
assert_eq!(run(store.consume(a.id())), Ok(false));
assert_eq!(
run(store.remove_account("a", TokenKind::ResetPassword)),
Ok(1)
);
assert_eq!(run(store.purge_expired(at(5))), Ok(1));
assert!(store.is_empty());
store.fail_with(Some("down"));
assert!(matches!(run(store.find("x")), Err(AccountError::Storage(m)) if m == "down"));
assert!(format!("{store:?}").contains("len"));
}
}