id_effect_auth 0.13.0

Authentication for id_effect services: password hashing, sessions, JWT, API-key and OIDC credentials
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! API keys: random secrets stored as SHA-256 digests, found by a public id and compared in
//! constant time.
//!
//! A key reads `<prefix>_<id>_<secret>`: a configurable prefix (so leaked keys are easy to
//! scan for), 16 hex digits of public id that the store is searched by, and 64 hex digits
//! (256 bits) of secret. [`ApiKey::generate`] mints one and the [`ApiKeyRecord`] to store; the
//! plaintext is shown to its owner once and never stored. A 256-bit random secret needs no slow
//! password hash: its SHA-256 digest is stored, and [`ApiKeys::verify`] compares digests with
//! [`subtle`]'s constant-time equality.
//!
//! ```
//! use id_effect::Exit;
//! use id_effect_auth::{ApiKey, ApiKeys, AuthPrincipal, MemoryApiKeyStore};
//!
//! let store = MemoryApiKeyStore::new();
//! let (key, record) = ApiKey::generate("ledger", AuthPrincipal::new("svc-billing")).unwrap();
//! store.insert(record);
//!
//! let keys = ApiKeys::new(store, "ledger");
//! let Exit::Success(p) = keys.verify(key.expose()).block_on() else { panic!() };
//! assert_eq!(id_effect_authz::Principal::id(&p), "svc-billing");
//! ```

use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use id_effect::{Effect, fail, succeed, suspend};
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;

use crate::error::AuthError;
use crate::principal::{AuthPrincipal, AuthScheme};

/// Bytes of public id in a key (16 hex digits).
const ID_BYTES: usize = 8;
/// Bytes of secret in a key (256 bits, 64 hex digits).
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))
}

/// `len` bytes from the operating system's CSPRNG, as lowercase hex.
pub(crate) fn random_hex(len: usize) -> Result<String, AuthError> {
  let mut bytes = vec![0_u8; len];
  getrandom::fill(&mut bytes).map_err(|e| AuthError::Key(format!("random source: {e}")))?;
  Ok(hex(&bytes))
}

fn digest(secret: &str) -> [u8; 32] {
  Sha256::digest(secret.as_bytes()).into()
}

/// A plaintext API key. `Debug` shows only its prefix and id.
#[derive(Clone, PartialEq, Eq)]
pub struct ApiKey(String);

impl ApiKey {
  /// A fresh key `<prefix>_<id>_<secret>` for `principal`, and the record to store.
  ///
  /// # Errors
  ///
  /// [`AuthError::Key`] when `prefix` is empty or not lower-case ASCII letters and digits, or
  /// when the operating system's random source fails.
  pub fn generate(
    prefix: &str,
    principal: AuthPrincipal,
  ) -> Result<(Self, ApiKeyRecord), AuthError> {
    check_prefix(prefix)?;
    let id = random_hex(ID_BYTES)?;
    let secret = random_hex(SECRET_BYTES)?;
    let record = ApiKeyRecord {
      id: id.clone(),
      digest: digest(&secret),
      principal,
    };
    Ok((Self(format!("{prefix}_{id}_{secret}")), record))
  }

  /// The key, to hand to its owner once.
  #[inline]
  pub fn expose(&self) -> &str {
    &self.0
  }
}

impl fmt::Debug for ApiKey {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let shown = self.0.rsplit_once('_').map_or("", |(head, _)| head);
    write!(f, "ApiKey({shown}_…)")
  }
}

fn check_prefix(prefix: &str) -> Result<(), AuthError> {
  let ok = !prefix.is_empty()
    && prefix
      .bytes()
      .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit());
  if ok {
    Ok(())
  } else {
    Err(AuthError::Key(format!(
      "API key prefix {prefix:?} must be lower-case letters and digits"
    )))
  }
}

/// What a store keeps for one API key: the public id, the SHA-256 digest of the secret and the
/// principal the key authenticates as. Never the plaintext.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ApiKeyRecord {
  id: String,
  digest: [u8; 32],
  principal: AuthPrincipal,
}

impl ApiKeyRecord {
  /// A record as a store reads it back: id, the digest as 64 hex digits
  /// ([`digest_hex`](Self::digest_hex)) and the principal.
  ///
  /// # Errors
  ///
  /// [`AuthError::Malformed`] when `id` or `digest_hex` has the wrong shape.
  pub fn from_parts(
    id: impl Into<String>,
    digest_hex: &str,
    principal: AuthPrincipal,
  ) -> Result<Self, AuthError> {
    let id = id.into();
    if !is_hex(&id, ID_BYTES * 2) || !is_hex(digest_hex, 64) {
      return Err(AuthError::Malformed("API key record".into()));
    }
    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(|_| AuthError::Malformed("API key record".into()))?;
    }
    Ok(Self {
      id,
      digest,
      principal,
    })
  }

  /// The public id (the store's lookup key).
  #[inline]
  pub fn id(&self) -> &str {
    &self.id
  }

  /// The digest of the secret, as 64 hex digits (to store).
  pub fn digest_hex(&self) -> String {
    hex(&self.digest)
  }

  /// The principal the key authenticates as.
  #[inline]
  pub fn principal(&self) -> &AuthPrincipal {
    &self.principal
  }
}

/// Where API-key records live, looked up by public id. Implement it over a table.
pub trait ApiKeyStore: Send + Sync + 'static {
  /// The record with public id `id`, if any (a revoked key has none).
  fn find(&self, id: &str) -> Effect<Option<ApiKeyRecord>, AuthError>;

  /// [`Self::find`] without an effect, for a store that looks keys up without I/O:
  /// `Some(outcome)` of the lookup now, or `None` (the default) to run [`Self::find`]. The
  /// `Authenticate` middleware (feature `axum`) tries it first, so an in-process store skips the
  /// effect runtime.
  fn find_now(&self, id: &str) -> Option<Result<Option<ApiKeyRecord>, AuthError>> {
    let _ = id;
    None
  }
}

#[derive(Default)]
struct MemoryKeysState {
  records: HashMap<String, ApiKeyRecord>,
  fail: Option<String>,
}

/// In-memory [`ApiKeyStore`] for tests and examples. Clones share the records.
#[derive(Clone, Default)]
pub struct MemoryApiKeyStore {
  inner: Arc<Mutex<MemoryKeysState>>,
}

impl fmt::Debug for MemoryApiKeyStore {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("MemoryApiKeyStore")
      .field("len", &self.state().records.len())
      .finish()
  }
}

impl MemoryApiKeyStore {
  /// An empty store.
  #[inline]
  pub fn new() -> Self {
    Self::default()
  }

  fn state(&self) -> MutexGuard<'_, MemoryKeysState> {
    self.inner.lock().unwrap_or_else(PoisonError::into_inner)
  }

  /// Store `record` (replacing one with the same id).
  pub fn insert(&self, record: ApiKeyRecord) {
    self.state().records.insert(record.id.clone(), record);
  }

  /// Revoke the key with public id `id`; `true` if it existed.
  pub fn revoke(&self, id: &str) -> bool {
    self.state().records.remove(id).is_some()
  }

  /// Make every later lookup fail with [`AuthError::Unavailable`]`(message)`, or recover with
  /// `None`.
  pub fn fail_with(&self, message: Option<&str>) {
    self.state().fail = message.map(str::to_owned);
  }

  fn lookup(&self, id: &str) -> Result<Option<ApiKeyRecord>, AuthError> {
    let st = self.state();
    match &st.fail {
      Some(m) => Err(AuthError::Unavailable(m.clone())),
      None => Ok(st.records.get(id).cloned()),
    }
  }
}

impl ApiKeyStore for MemoryApiKeyStore {
  fn find(&self, id: &str) -> Effect<Option<ApiKeyRecord>, AuthError> {
    let (this, id) = (self.clone(), id.to_owned());
    suspend(move || match this.lookup(&id) {
      Ok(found) => succeed(found),
      Err(e) => fail(e),
    })
  }

  fn find_now(&self, id: &str) -> Option<Result<Option<ApiKeyRecord>, AuthError>> {
    Some(self.lookup(id))
  }
}

/// API-key checking over an [`ApiKeyStore`] for keys with one prefix. Cheap to clone.
#[derive(Clone)]
pub struct ApiKeys {
  store: Arc<dyn ApiKeyStore>,
  prefix: Arc<str>,
}

impl fmt::Debug for ApiKeys {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("ApiKeys")
      .field("prefix", &self.prefix)
      .finish_non_exhaustive()
  }
}

impl ApiKeys {
  /// Check keys `<prefix>_…` against `store`.
  pub fn new(store: impl ApiKeyStore, prefix: impl Into<String>) -> Self {
    Self {
      store: Arc::new(store),
      prefix: Arc::from(prefix.into()),
    }
  }

  /// The key prefix.
  #[inline]
  pub fn prefix(&self) -> &str {
    &self.prefix
  }

  /// The principal `presented` authenticates as (scheme [`AuthScheme::ApiKey`]).
  ///
  /// A key of the wrong shape is [`AuthError::Malformed`]; an unknown id or a wrong secret is
  /// [`AuthError::InvalidCredentials`]; a store failure is [`AuthError::Unavailable`].
  pub fn verify(&self, presented: &str) -> Effect<AuthPrincipal, AuthError> {
    let Some((id, secret)) = self.split(presented) else {
      return fail(not_a_key());
    };
    let presented_digest = digest(secret);
    self
      .store
      .find(id)
      .flat_map(move |found| match accept(found, &presented_digest) {
        Ok(principal) => succeed(principal),
        Err(e) => fail(e),
      })
  }

  /// [`Self::verify`] answered now, when the store looks keys up without I/O
  /// ([`ApiKeyStore::find_now`]); `None` to run [`Self::verify`].
  #[cfg(feature = "axum")]
  pub(crate) fn verify_now(&self, presented: &str) -> Option<Result<AuthPrincipal, AuthError>> {
    let Some((id, secret)) = self.split(presented) else {
      return Some(Err(not_a_key()));
    };
    let found = self.store.find_now(id)?;
    let presented_digest = digest(secret);
    Some(found.and_then(|found| accept(found, &presented_digest)))
  }

  /// `(id, secret)` of a well-formed key with this prefix.
  fn split<'k>(&self, key: &'k str) -> Option<(&'k str, &'k str)> {
    let rest = key.strip_prefix(&*self.prefix)?.strip_prefix('_')?;
    let (id, secret) = rest.split_once('_')?;
    (is_hex(id, ID_BYTES * 2) && is_hex(secret, SECRET_BYTES * 2)).then_some((id, secret))
  }
}

fn not_a_key() -> AuthError {
  AuthError::Malformed("not an API key of this service".into())
}

/// The principal of the record `found` for a key whose secret digests to `presented_digest`
/// (constant-time compare); an unknown id or a wrong secret is [`AuthError::InvalidCredentials`].
fn accept(
  found: Option<ApiKeyRecord>,
  presented_digest: &[u8; 32],
) -> Result<AuthPrincipal, AuthError> {
  match found {
    Some(record) if bool::from(record.digest.ct_eq(presented_digest)) => {
      Ok(record.principal.with_scheme(AuthScheme::ApiKey))
    }
    _ => Err(AuthError::InvalidCredentials),
  }
}

#[cfg(test)]
mod tests {
  use id_effect::{Cause, Exit};

  use super::*;

  fn run<A: Send + 'static>(eff: Effect<A, AuthError>) -> Result<A, AuthError> {
    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_keys_verify_and_records_never_hold_the_secret() {
    let store = MemoryApiKeyStore::new();
    let (key, record) = ApiKey::generate("ide", AuthPrincipal::new("svc").with_role("r")).unwrap();
    assert!(key.expose().starts_with("ide_"));
    assert_eq!(key.expose().len(), 4 + 16 + 1 + 64);
    let secret = key.expose().rsplit_once('_').unwrap().1;
    assert!(!record.digest_hex().contains(secret));
    assert!(!format!("{record:?}").contains(secret));
    assert!(!format!("{key:?}").contains(secret));
    assert!(format!("{key:?}").contains(record.id()));
    store.insert(record.clone());
    let keys = ApiKeys::new(store.clone(), "ide");
    assert_eq!(keys.prefix(), "ide");
    let p = run(keys.verify(key.expose())).unwrap();
    assert_eq!(
      p,
      record.principal().clone().with_scheme(AuthScheme::ApiKey)
    );
    assert!(format!("{keys:?}").contains("ide"));
    assert!(format!("{store:?}").contains("len: 1"));
  }

  #[test]
  fn wrong_secret_unknown_id_revoked_and_malformed() {
    let store = MemoryApiKeyStore::new();
    let (key, record) = ApiKey::generate("ide", AuthPrincipal::new("svc")).unwrap();
    store.insert(record.clone());
    let keys = ApiKeys::new(store.clone(), "ide");
    let wrong = format!("ide_{}_{}", record.id(), "0".repeat(64));
    assert_eq!(run(keys.verify(&wrong)), Err(AuthError::InvalidCredentials));
    let unknown = format!("ide_{}_{}", "1".repeat(16), "0".repeat(64));
    assert_eq!(
      run(keys.verify(&unknown)),
      Err(AuthError::InvalidCredentials)
    );
    for bad in [
      "",
      "ide",
      "other_0000000000000000_00",
      "ide_zz_00",
      &format!("ide_{}", record.id()),
      &format!("ide_{}_{}", record.id(), "A".repeat(64)),
    ] {
      assert!(
        matches!(run(keys.verify(bad)), Err(AuthError::Malformed(_))),
        "{bad}"
      );
    }
    assert!(store.revoke(record.id()));
    assert!(!store.revoke(record.id()));
    assert_eq!(
      run(keys.verify(key.expose())),
      Err(AuthError::InvalidCredentials)
    );
    store.insert(record);
    store.fail_with(Some("db"));
    assert_eq!(
      run(keys.verify(key.expose())),
      Err(AuthError::Unavailable("db".into()))
    );
    store.fail_with(None);
    assert!(run(keys.verify(key.expose())).is_ok());
  }

  #[cfg(feature = "axum")]
  #[test]
  fn verify_now_answers_like_verify() {
    let store = MemoryApiKeyStore::new();
    let (key, record) = ApiKey::generate("ide", AuthPrincipal::new("svc")).unwrap();
    store.insert(record.clone());
    let keys = ApiKeys::new(store.clone(), "ide");
    let check = |presented: &str| {
      assert_eq!(
        keys.verify_now(presented),
        Some(run(keys.verify(presented))),
        "{presented}"
      );
    };
    let wrong = format!("ide_{}_{}", record.id(), "0".repeat(64));
    let unknown = format!("ide_{}_{}", "1".repeat(16), "0".repeat(64));
    for presented in [
      key.expose(),
      &wrong,
      &unknown,
      "",
      "other_0000000000000000_00",
    ] {
      check(presented);
    }
    store.fail_with(Some("db"));
    check(key.expose());
    // A store without an inline lookup leaves the key to the effect.
    struct Remote(MemoryApiKeyStore);
    impl ApiKeyStore for Remote {
      fn find(&self, id: &str) -> Effect<Option<ApiKeyRecord>, AuthError> {
        self.0.find(id)
      }
    }
    store.fail_with(None);
    let remote = ApiKeys::new(Remote(store), "ide");
    assert_eq!(remote.verify_now(key.expose()), None);
    assert!(run(remote.verify(key.expose())).is_ok());
  }

  #[test]
  fn records_round_trip_through_hex_and_prefixes_are_checked() {
    let (_, record) = ApiKey::generate("k1", AuthPrincipal::new("svc")).unwrap();
    let back = ApiKeyRecord::from_parts(
      record.id(),
      &record.digest_hex(),
      record.principal().clone(),
    )
    .unwrap();
    assert_eq!(back, record);
    let p = || AuthPrincipal::new("x");
    assert!(ApiKeyRecord::from_parts("short", &record.digest_hex(), p()).is_err());
    assert!(ApiKeyRecord::from_parts(record.id(), "00", p()).is_err());
    for bad in ["", "Ab", "a_b", "a-b"] {
      assert!(matches!(ApiKey::generate(bad, p()), Err(AuthError::Key(_))));
    }
  }
}