rskit-auth 0.1.0-alpha.1

JWT, OIDC, password hashing, and request-context auth helpers
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
//! API key generation, peppered digest storage, and validation helpers.

use std::fmt;

use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use chrono::{DateTime, Utc};
use hmac::{Hmac, KeyInit, Mac};
use rskit_errors::{AppError, AppResult, ErrorCode};
use serde::{Deserialize, Serialize};
use sha2::Sha256;

type HmacSha256 = Hmac<Sha256>;

const DEFAULT_ENTROPY_BYTES: usize = 32;
const MIN_ENTROPY_BYTES: usize = 16;
const MIN_PEPPER_BYTES: usize = 32;

/// API key hashing configuration.
#[derive(Clone, Deserialize)]
pub struct HashingConfig {
    /// Secret pepper used for HMAC-SHA-256 digest storage.
    pub pepper: String,
    /// Random secret entropy in bytes.
    pub entropy_bytes: usize,
}

impl HashingConfig {
    /// Create a hashing configuration with the default entropy budget.
    #[must_use]
    pub fn new(pepper: impl Into<String>) -> Self {
        Self {
            pepper: pepper.into(),
            entropy_bytes: DEFAULT_ENTROPY_BYTES,
        }
    }

    /// Validate the configuration.
    pub fn validate(&self) -> AppResult<()> {
        if self.pepper.len() < MIN_PEPPER_BYTES {
            return Err(AppError::invalid_input(
                "pepper",
                format!("pepper must be at least {MIN_PEPPER_BYTES} bytes"),
            ));
        }
        if self.entropy_bytes < MIN_ENTROPY_BYTES {
            return Err(AppError::invalid_input(
                "entropy_bytes",
                format!("entropy_bytes must be at least {MIN_ENTROPY_BYTES}"),
            ));
        }
        Ok(())
    }
}

impl fmt::Debug for HashingConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("HashingConfig")
            .field("pepper", &"<redacted>")
            .field("entropy_bytes", &self.entropy_bytes)
            .finish()
    }
}

/// Persisted API key metadata.
#[derive(Clone, Serialize, Deserialize)]
pub struct Key {
    /// Unique identifier for this key.
    pub id: String,
    /// Identifier of the key owner.
    pub owner_id: String,
    /// Human-readable name for the key.
    pub name: String,
    /// Display-safe key prefix used for candidate lookup.
    pub key_prefix: String,
    /// Pepper-keyed HMAC-SHA-256 digest stored at rest.
    pub key_digest: String,
    /// Scopes granted to the key.
    pub scopes: Vec<String>,
    /// Whether the key is active.
    pub is_active: bool,
    /// Expiry time.
    pub expires_at: Option<DateTime<Utc>>,
    /// Grace period end after rotation.
    pub grace_ends_at: Option<DateTime<Utc>>,
    /// Replacement key ID after rotation.
    pub rotated_by_id: Option<String>,
    /// Last successful validation time.
    pub last_used_at: Option<DateTime<Utc>>,
    /// Creation timestamp.
    pub created_at: DateTime<Utc>,
}

impl fmt::Debug for Key {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Key")
            .field("id", &self.id)
            .field("owner_id", &self.owner_id)
            .field("name", &self.name)
            .field("key_prefix", &self.key_prefix)
            .field("key_digest", &"<redacted>")
            .field("scopes", &self.scopes)
            .field("is_active", &self.is_active)
            .field("expires_at", &self.expires_at)
            .field("grace_ends_at", &self.grace_ends_at)
            .field("rotated_by_id", &self.rotated_by_id)
            .field("last_used_at", &self.last_used_at)
            .field("created_at", &self.created_at)
            .finish()
    }
}

impl Key {
    /// Return `true` when the key is expired and beyond any grace period.
    #[must_use]
    pub fn is_expired_past_grace(&self) -> bool {
        let now = Utc::now();
        if let Some(grace_ends_at) = self.grace_ends_at
            && now > grace_ends_at
        {
            return true;
        }
        if let Some(expires_at) = self.expires_at
            && now > expires_at
            && self.grace_ends_at.is_none()
        {
            return true;
        }
        false
    }
}

/// One-time API key material returned to callers.
#[derive(Clone, zeroize::Zeroize, zeroize::ZeroizeOnDrop)]
pub struct GenerateResult {
    /// Plaintext key shown once.
    pub plain_key: String,
    /// Prefix stored alongside metadata.
    pub key_prefix: String,
    /// Pepper-keyed digest stored at rest.
    pub key_digest: String,
}

impl fmt::Debug for GenerateResult {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("GenerateResult")
            .field("plain_key", &"<redacted>")
            .field("key_prefix", &self.key_prefix)
            .field("key_digest", &"<redacted>")
            .finish()
    }
}

/// API key hasher and issuer.
///
/// The HMAC key is validated and pre-initialized at construction time so that
/// `digest` and `compare` are fully infallible at call time.
#[derive(Clone)]
pub struct Hasher {
    config: HashingConfig,
    /// Pre-initialized HMAC state; cloned cheaply for each digest call.
    hmac_prototype: HmacSha256,
}

impl fmt::Debug for Hasher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Hasher")
            .field("config", &self.config)
            .field("hmac_prototype", &"<redacted>")
            .finish()
    }
}

impl Hasher {
    /// Construct a new hasher, validating config and pre-initializing the HMAC key.
    pub fn new(mut config: HashingConfig) -> AppResult<Self> {
        if config.entropy_bytes == 0 {
            config.entropy_bytes = DEFAULT_ENTROPY_BYTES;
        }
        config.validate()?;
        let hmac_prototype =
            HmacSha256::new_from_slice(config.pepper.as_bytes()).map_err(|error| {
                AppError::new(
                    ErrorCode::InvalidInput,
                    format!("invalid API key pepper: {error}"),
                )
            })?;
        Ok(Self {
            config,
            hmac_prototype,
        })
    }

    /// Return the active configuration.
    #[must_use]
    pub const fn config(&self) -> &HashingConfig {
        &self.config
    }

    /// Generate a new API key with the supplied prefix.
    pub fn generate_key(&self, prefix: &str) -> AppResult<GenerateResult> {
        validate_prefix(prefix)?;

        let mut random_bytes = vec![0_u8; self.config.entropy_bytes];
        rand::fill(random_bytes.as_mut_slice());
        let secret = URL_SAFE_NO_PAD.encode(random_bytes);
        let plain_key = format!("{prefix}.{secret}");

        Ok(GenerateResult {
            key_prefix: prefix.to_string(),
            key_digest: self.digest(&plain_key),
            plain_key,
        })
    }

    /// Compute the peppered HMAC-SHA-256 digest for a plaintext key.
    ///
    /// Infallible — the HMAC key was validated at construction.
    #[must_use]
    pub fn digest(&self, plain_key: &str) -> String {
        let mut mac = self.hmac_prototype.clone();
        mac.update(plain_key.as_bytes());
        hex::encode(mac.finalize().into_bytes())
    }

    /// Constant-time digest comparison.
    #[must_use]
    pub fn compare(&self, plain_key: &str, stored_digest: &str) -> bool {
        let computed = self.digest(plain_key);
        subtle::ConstantTimeEq::ct_eq(computed.as_bytes(), stored_digest.as_bytes()).into()
    }
}

/// Split a plaintext key into `(prefix, secret)`.
pub fn split_key(plain_key: &str) -> AppResult<(String, String)> {
    let Some((prefix, secret)) = plain_key.split_once('.') else {
        return Err(AppError::invalid_input("api_key", "invalid API key format"));
    };
    if prefix.is_empty() || secret.is_empty() {
        return Err(AppError::invalid_input("api_key", "invalid API key format"));
    }
    Ok((prefix.to_string(), secret.to_string()))
}

/// Error returned when a key is not usable.
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum KeyValidationError {
    /// The key has been revoked.
    #[error("key is revoked")]
    Revoked,
    /// The key is expired past its grace period.
    #[error("key is expired")]
    Expired,
}

/// Validate key usability.
pub fn validate(key: &Key) -> Result<(), KeyValidationError> {
    if !key.is_active {
        return Err(KeyValidationError::Revoked);
    }
    if key.is_expired_past_grace() {
        return Err(KeyValidationError::Expired);
    }
    Ok(())
}

fn validate_prefix(prefix: &str) -> AppResult<()> {
    if prefix.is_empty() {
        return Err(AppError::invalid_input(
            "prefix",
            "prefix must be non-empty",
        ));
    }
    if prefix
        .chars()
        .any(|char| !char.is_ascii_alphanumeric() && char != '-' && char != '_')
    {
        return Err(AppError::invalid_input(
            "prefix",
            "prefix must contain only [A-Za-z0-9_-]",
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{GenerateResult, Hasher, HashingConfig, Key, split_key};
    use chrono::Utc;

    #[test]
    fn hashing_config_debug_redacts_pepper() {
        let formatted = format!("{:?}", HashingConfig::new("very-secret-pepper-material"));
        assert!(formatted.contains("<redacted>"));
        assert!(!formatted.contains("very-secret-pepper-material"));
    }

    #[test]
    fn generate_result_debug_redacts_plain_key() {
        let formatted = format!(
            "{:?}",
            GenerateResult {
                plain_key: "sk_live.secret".into(),
                key_prefix: "sk_live".into(),
                key_digest: "digest".into(),
            }
        );
        assert!(formatted.contains("<redacted>"));
        assert!(!formatted.contains("sk_live.secret"));
    }

    #[test]
    fn key_and_hasher_debug_redact_secret_material() {
        let hasher =
            Hasher::new(HashingConfig::new("very-secret-pepper-material-32-bytes")).unwrap();
        let key = Key {
            id: "key-1".into(),
            owner_id: "owner-1".into(),
            name: "primary".into(),
            key_prefix: "pk".into(),
            key_digest: "stored-digest".into(),
            scopes: vec!["read".into()],
            is_active: true,
            expires_at: None,
            grace_ends_at: None,
            rotated_by_id: None,
            last_used_at: None,
            created_at: Utc::now(),
        };

        let hasher_debug = format!("{hasher:?}");
        assert!(hasher_debug.contains("<redacted>"));
        assert!(!hasher_debug.contains("very-secret-pepper-material-32-bytes"));

        let key_debug = format!("{key:?}");
        assert!(key_debug.contains("<redacted>"));
        assert!(!key_debug.contains("stored-digest"));
    }

    #[test]
    fn empty_api_key_prefix_is_rejected() {
        let hasher =
            Hasher::new(HashingConfig::new("very-secret-pepper-material-32-bytes")).unwrap();

        let error = hasher.generate_key("").unwrap_err();

        assert_eq!(error.code(), rskit_errors::ErrorCode::InvalidInput);
        assert!(error.message().contains("prefix"));
    }

    #[test]
    fn generate_and_compare_roundtrip() {
        let hasher = Hasher::new(HashingConfig {
            pepper: "p".repeat(32),
            entropy_bytes: 32,
        })
        .unwrap();
        let issued = hasher.generate_key("pk").unwrap();
        assert!(issued.plain_key.starts_with("pk."));
        assert!(hasher.compare(&issued.plain_key, &issued.key_digest));
    }

    #[test]
    fn split_key_rejects_malformed_values() {
        assert!(split_key("pk.secret").is_ok());
        assert!(split_key("malformed").is_err());
    }

    #[test]
    fn hashing_config_rejects_short_pepper_and_entropy() {
        let short_pepper = HashingConfig {
            pepper: "short".into(),
            entropy_bytes: 32,
        };
        assert!(short_pepper.validate().is_err());

        let short_entropy = HashingConfig {
            pepper: "p".repeat(32),
            entropy_bytes: 1,
        };
        assert!(short_entropy.validate().is_err());
    }

    #[test]
    fn key_debug_redacts_digest() {
        let key = crate::apikey::Key {
            id: "key-1".into(),
            owner_id: "user-1".into(),
            name: "primary".into(),
            key_prefix: "pk".into(),
            key_digest: "digest-secret".into(),
            scopes: Vec::new(),
            is_active: true,
            expires_at: None,
            grace_ends_at: None,
            rotated_by_id: None,
            last_used_at: None,
            created_at: chrono::Utc::now(),
        };

        let formatted = format!("{key:?}");

        assert!(formatted.contains("<redacted>"));
        assert!(!formatted.contains("digest-secret"));
    }
}