fastauth-core 0.4.0

Core of fastauth: the builder, plugin system, sessions, crypto and routing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Secrets, derived keys, signing, encryption, hashing and password hashing.
//! See `docs/09-security.md`.

use std::fmt;
use std::sync::Arc;

use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use chacha20poly1305::XChaCha20Poly1305;
use chacha20poly1305::aead::{Aead, KeyInit};
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;
use tokio::sync::Semaphore;
use zeroize::Zeroizing;

use crate::error::BuildError;

type HmacSha256 = Hmac<Sha256>;

/// A master secret. Zeroized on drop; never printed.
#[derive(Clone)]
pub struct Secret(Zeroizing<Vec<u8>>);

impl Secret {
    pub const MIN_LEN: usize = 32;

    /// Raw bytes. Prefer `from_env` or `from_base64` so the secret never sits in source.
    pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
        Secret(Zeroizing::new(bytes.into()))
    }

    /// Accepts base64url (what `fastauth secret` prints) or, failing that, the raw string.
    pub fn from_base64(value: &str) -> Self {
        match B64.decode(value.trim()) {
            Ok(bytes) if bytes.len() >= Self::MIN_LEN => Secret::from_bytes(bytes),
            _ => Secret::from_bytes(value.as_bytes().to_vec()),
        }
    }

    pub fn from_env(var: &str) -> Result<Self, BuildError> {
        let value = std::env::var(var)
            .map_err(|_| BuildError::new(format!("environment variable `{var}` is not set")))?;
        Ok(Secret::from_base64(&value))
    }

    /// 64 random bytes as base64url: a fresh secret.
    pub fn generate() -> String {
        B64.encode(random_bytes::<64>())
    }

    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }

    fn derive(&self, info: &str) -> Zeroizing<[u8; 32]> {
        let hk = hkdf::Hkdf::<Sha256>::new(Some(b"fastauth"), &self.0);
        let mut out = Zeroizing::new([0u8; 32]);
        hk.expand(info.as_bytes(), out.as_mut())
            .expect("32 bytes is a valid HKDF output length");
        out
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Secret(..)")
    }
}

pub fn random_bytes<const N: usize>() -> [u8; N] {
    let mut buf = [0u8; N];
    getrandom::fill(&mut buf).expect("the OS random number generator is available");
    buf
}

/// 32 random bytes as base64url (43 chars): session tokens, verification values, state.
pub fn random_token() -> String {
    B64.encode(random_bytes::<32>())
}

/// A random string from `alphabet`, without modulo bias.
pub fn random_string(len: usize, alphabet: &[u8]) -> String {
    assert!(!alphabet.is_empty() && alphabet.len() <= 256);
    let limit = 256 - (256 % alphabet.len());
    let mut out = String::with_capacity(len);
    while out.len() < len {
        for byte in random_bytes::<64>() {
            if (byte as usize) < limit && out.len() < len {
                out.push(alphabet[byte as usize % alphabet.len()] as char);
            }
        }
    }
    out
}

pub fn base64url(bytes: &[u8]) -> String {
    B64.encode(bytes)
}

pub fn base64url_decode(s: &str) -> Option<Vec<u8>> {
    B64.decode(s).ok()
}

/// SHA-256 digest. Stored instead of tokens.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Hash([u8; 32]);

impl Hash {
    pub fn sha256(data: impl AsRef<[u8]>) -> Self {
        Hash(Sha256::digest(data.as_ref()).into())
    }

    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    pub fn from_slice(bytes: &[u8]) -> Option<Self> {
        bytes.try_into().ok().map(Hash)
    }
}

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Hash({}..)", &base64url(&self.0)[..8])
    }
}

/// Constant-time string equality.
pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
    a.ct_eq(b).into()
}

/// Keys derived from the configured secrets. The first secret signs and encrypts; every
/// secret verifies and decrypts, so secrets rotate without signing anyone out.
pub struct Keys {
    cookie: Vec<Zeroizing<[u8; 32]>>,
    cache: Vec<Zeroizing<[u8; 32]>>,
    encrypt: Vec<Zeroizing<[u8; 32]>>,
    secrets: Vec<Secret>,
}

impl fmt::Debug for Keys {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Keys({} secrets)", self.secrets.len())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SignPurpose {
    Cookie,
    CookieCache,
}

impl Keys {
    pub(crate) fn new(secrets: Vec<Secret>) -> Self {
        Self {
            cookie: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/cookie"))
                .collect(),
            cache: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/cookie-cache"))
                .collect(),
            encrypt: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/encrypt"))
                .collect(),
            secrets,
        }
    }

    fn sign_keys(&self, purpose: SignPurpose) -> &[Zeroizing<[u8; 32]>] {
        match purpose {
            SignPurpose::Cookie => &self.cookie,
            SignPurpose::CookieCache => &self.cache,
        }
    }

    fn mac(key: &[u8; 32], value: &str) -> HmacSha256 {
        let mut mac =
            <HmacSha256 as KeyInit>::new_from_slice(key).expect("HMAC takes any key length");
        mac.update(value.as_bytes());
        mac
    }

    /// `value.signature`, signature base64url.
    pub fn sign(&self, purpose: SignPurpose, value: &str) -> String {
        let tag = Self::mac(&self.sign_keys(purpose)[0], value)
            .finalize()
            .into_bytes();
        format!("{value}.{}", B64.encode(tag))
    }

    /// The value, when the signature matches any configured secret.
    pub fn verify<'a>(&self, purpose: SignPurpose, signed: &'a str) -> Option<&'a str> {
        let (value, sig) = signed.rsplit_once('.')?;
        let sig = B64.decode(sig).ok()?;
        self.sign_keys(purpose)
            .iter()
            .any(|key| Self::mac(key, value).verify_slice(&sig).is_ok())
            .then_some(value)
    }

    /// `[key id][24-byte nonce][ciphertext]`.
    pub fn encrypt(&self, plaintext: &[u8]) -> Vec<u8> {
        let cipher =
            XChaCha20Poly1305::new_from_slice(self.encrypt[0].as_ref()).expect("32-byte key");
        let nonce_bytes = random_bytes::<24>();
        let nonce = chacha20poly1305::XNonce::from(nonce_bytes);
        let ciphertext = cipher
            .encrypt(&nonce, plaintext)
            .expect("XChaCha20-Poly1305 encryption cannot fail");
        let mut out = Vec::with_capacity(1 + 24 + ciphertext.len());
        out.push(0);
        out.extend_from_slice(&nonce_bytes);
        out.extend_from_slice(&ciphertext);
        out
    }

    pub fn decrypt(&self, data: &[u8]) -> Option<Vec<u8>> {
        if data.len() < 25 {
            return None;
        }
        let (nonce, ciphertext) = data[1..].split_at(24);
        let nonce = chacha20poly1305::XNonce::try_from(nonce).ok()?;
        self.encrypt.iter().find_map(|key| {
            let cipher = XChaCha20Poly1305::new_from_slice(key.as_ref()).ok()?;
            cipher.decrypt(&nonce, ciphertext).ok()
        })
    }

    /// A per-plugin key, so plugins can't reach core's keys.
    pub fn derive_key(&self, plugin_id: &str) -> Zeroizing<[u8; 32]> {
        self.secrets[0].derive(&format!("fastauth/v1/plugin/{plugin_id}"))
    }
}

/// Password hashing. Runs on a blocking thread; see `Passwords`.
pub trait PasswordHasher: Send + Sync + 'static {
    fn hash(&self, password: &str) -> Result<String, String>;
    fn verify(&self, password: &str, hash: &str) -> Result<bool, String>;
    /// Whether a stored hash should be replaced on the next successful sign-in.
    fn needs_rehash(&self, hash: &str) -> bool {
        let _ = hash;
        false
    }
}

/// argon2id with OWASP parameters: 19 MiB, t=2, p=1.
#[derive(Debug, Clone)]
pub struct Argon2id {
    params: argon2::Params,
}

impl Argon2id {
    pub fn owasp() -> Self {
        Self::with_params(19 * 1024, 2, 1)
    }

    pub fn with_params(m_cost_kib: u32, t_cost: u32, p_cost: u32) -> Self {
        let params =
            argon2::Params::new(m_cost_kib, t_cost, p_cost, None).expect("valid argon2 parameters");
        Self { params }
    }

    fn engine(&self) -> argon2::Argon2<'static> {
        argon2::Argon2::new(
            argon2::Algorithm::Argon2id,
            argon2::Version::V0x13,
            self.params.clone(),
        )
    }
}

impl Default for Argon2id {
    fn default() -> Self {
        Self::owasp()
    }
}

impl PasswordHasher for Argon2id {
    fn hash(&self, password: &str) -> Result<String, String> {
        use argon2::password_hash::PasswordHasher as _;
        self.engine()
            .hash_password(password.as_bytes())
            .map(|h| h.to_string())
            .map_err(|e| e.to_string())
    }

    fn verify(&self, password: &str, hash: &str) -> Result<bool, String> {
        use argon2::password_hash::PasswordVerifier as _;
        let parsed =
            argon2::password_hash::phc::PasswordHash::new(hash).map_err(|e| e.to_string())?;
        Ok(self
            .engine()
            .verify_password(password.as_bytes(), &parsed)
            .is_ok())
    }

    fn needs_rehash(&self, hash: &str) -> bool {
        let Ok(parsed) = argon2::password_hash::phc::PasswordHash::new(hash) else {
            return true;
        };
        let Ok(params) = argon2::Params::try_from(&parsed) else {
            return true;
        };
        parsed.algorithm.as_str() != "argon2id"
            || params.m_cost() < self.params.m_cost()
            || params.t_cost() < self.params.t_cost()
    }
}

/// Runs a `PasswordHasher` on the blocking pool behind a semaphore, so a burst of sign-ins
/// can't allocate unbounded argon2 memory.
#[derive(Clone)]
pub struct Passwords {
    hasher: Arc<dyn PasswordHasher>,
    permits: Arc<Semaphore>,
    dummy_hash: Arc<std::sync::OnceLock<String>>,
}

impl fmt::Debug for Passwords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Passwords(..)")
    }
}

impl Passwords {
    pub fn new(hasher: Arc<dyn PasswordHasher>) -> Self {
        let cpus = std::thread::available_parallelism().map_or(2, |n| n.get());
        Self {
            hasher,
            permits: Arc::new(Semaphore::new(cpus)),
            dummy_hash: Arc::default(),
        }
    }

    async fn run<T: Send + 'static>(
        &self,
        f: impl FnOnce(&dyn PasswordHasher) -> T + Send + 'static,
    ) -> T {
        let _permit = self
            .permits
            .acquire()
            .await
            .expect("semaphore is never closed");
        let hasher = self.hasher.clone();
        match tokio::runtime::Handle::try_current() {
            Ok(handle) => handle
                .spawn_blocking(move || f(hasher.as_ref()))
                .await
                .expect("password hashing task panicked"),
            Err(_) => f(hasher.as_ref()),
        }
    }

    pub async fn hash(&self, password: &str) -> Result<String, String> {
        let password = Zeroizing::new(password.to_owned());
        self.run(move |h| h.hash(&password)).await
    }

    pub async fn verify(&self, password: &str, hash: &str) -> Result<bool, String> {
        let password = Zeroizing::new(password.to_owned());
        let hash = hash.to_owned();
        self.run(move |h| h.verify(&password, &hash)).await
    }

    pub fn needs_rehash(&self, hash: &str) -> bool {
        self.hasher.needs_rehash(hash)
    }

    /// Spends the same time as a real verification, for unknown emails.
    pub async fn verify_dummy(&self, password: &str) {
        let dummy = match self.dummy_hash.get() {
            Some(h) => h.clone(),
            None => {
                let h = self
                    .hash("fastauth-dummy-password")
                    .await
                    .unwrap_or_default();
                self.dummy_hash.get_or_init(|| h).clone()
            }
        };
        let _ = self.verify(password, &dummy).await;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn keys() -> Keys {
        Keys::new(vec![Secret::from_bytes(vec![7u8; 32])])
    }

    #[test]
    fn sign_round_trips_and_rejects_tampering() {
        let keys = keys();
        let signed = keys.sign(SignPurpose::Cookie, "token");
        assert_eq!(keys.verify(SignPurpose::Cookie, &signed), Some("token"));
        assert_eq!(keys.verify(SignPurpose::CookieCache, &signed), None);
        assert_eq!(
            keys.verify(SignPurpose::Cookie, &signed.replace("token", "tokem")),
            None
        );
    }

    #[test]
    fn rotation_verifies_old_signatures() {
        let old = keys();
        let signed = old.sign(SignPurpose::Cookie, "v");
        let rotated = Keys::new(vec![
            Secret::from_bytes(vec![9u8; 32]),
            Secret::from_bytes(vec![7u8; 32]),
        ]);
        assert_eq!(rotated.verify(SignPurpose::Cookie, &signed), Some("v"));
        assert_eq!(
            rotated.decrypt(&old.encrypt(b"hi")).as_deref(),
            Some(&b"hi"[..])
        );
    }

    #[test]
    fn encryption_round_trips() {
        let keys = keys();
        let sealed = keys.encrypt(b"secret");
        assert_ne!(&sealed[25..], b"secret");
        assert_eq!(keys.decrypt(&sealed).unwrap(), b"secret");
    }

    #[test]
    fn argon2_hashes_and_verifies() {
        let hasher = Argon2id::with_params(1024, 1, 1);
        let hash = hasher.hash("correct horse").unwrap();
        assert!(hash.starts_with("$argon2id$"));
        assert!(hasher.verify("correct horse", &hash).unwrap());
        assert!(!hasher.verify("wrong", &hash).unwrap());
        assert!(Argon2id::owasp().needs_rehash(&hash));
    }

    #[test]
    fn random_string_uses_alphabet() {
        let s = random_string(40, b"ab");
        assert_eq!(s.len(), 40);
        assert!(s.bytes().all(|b| b == b'a' || b == b'b'));
    }
}