codlet-core 0.15.2

Core authentication primitives for codlet: code policy, generation, normalization, keyed lookup derivation, lifecycle state machines, and storage traits.
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
//! Acceptance tests for RFC-013 (high-level orchestration API).
//!
//! Each test covers a scenario from the RFC checklist (§10.4):
//! - first-time join (issue → find → claim → issue session);
//! - callback-based flow;
//! - lost claim does not issue a session;
//! - host callback error does not leave a dangling session;
//! - public errors remain generic;
//! - returning login flow (validate session);
//! - logout (revoke session, clear cookie).

use std::time::Duration;

use codlet_core::CodePolicy;
use codlet_core::audit::{CollectingAuditSink, NoopAuditSink};
use codlet_core::auth::{
    CodeAuth, FormTokenManager, IssuedSession, NoRateLimit, RedeemError, SessionManager,
};
use codlet_core::clock::FixedClock;
use codlet_core::cookie::CookiePolicy;
use codlet_core::error::PublicRedemptionError;
use codlet_core::hashing::{SecretHasher, StaticKeyProvider};
use codlet_core::mem::{MemCodeStore, MemFormTokenStore, MemSessionStore};
use codlet_core::rng::SystemRandom;
use codlet_core::secret::{CodeId, SessionId, SubjectId};
use codlet_core::state::SessionValidationOutcome;
use codlet_core::store::token::TokenSubject;

// ── Shared fixtures ───────────────────────────────────────────────────────────

const NOW: u64 = 1_700_000_000;

fn hasher() -> SecretHasher<StaticKeyProvider> {
    SecretHasher::new(StaticKeyProvider::single("v1", b"test-key-32bytes".to_vec()).unwrap())
}

fn policy() -> CodePolicy {
    CodePolicy::default_human(Duration::from_secs(3600)).unwrap()
}

fn cookie() -> CookiePolicy {
    CookiePolicy::production_strict("sid", Duration::from_secs(30 * 86_400))
}

fn code_auth()
-> CodeAuth<MemCodeStore, NoRateLimit, StaticKeyProvider, FixedClock, CollectingAuditSink> {
    CodeAuth::without_rate_limit(
        MemCodeStore::new(),
        hasher(),
        FixedClock::at(NOW),
        CollectingAuditSink::new(),
        policy(),
    )
}

fn session_mgr() -> SessionManager<MemSessionStore, StaticKeyProvider, FixedClock, NoopAuditSink> {
    SessionManager::new(
        MemSessionStore::new(),
        hasher(),
        FixedClock::at(NOW),
        NoopAuditSink,
        cookie(),
    )
}

// ── RFC-013 acceptance: first-time join flow ──────────────────────────────────

#[tokio::test]
async fn two_step_issue_find_claim_session() {
    let ca = code_auth();
    let sm = session_mgr();
    let mut rng = SystemRandom::new();
    let mut sess_rng = SystemRandom::new();

    // Issue a code.
    let (code_id, plain) = ca
        .issue_code(
            &mut rng,
            CodeId::new("code-1".into()),
            None,
            None,
            Some("grant-A".into()),
        )
        .await
        .unwrap();

    // Find the code.
    let found = ca.find(plain.expose(), None).await.unwrap();
    assert_eq!(found.id, code_id);
    assert_eq!(found.grant.as_deref(), Some("grant-A"));

    // Claim it.
    let subject = SubjectId::new("user-1".into());
    let success = ca.claim(&found, subject.clone(), None).await.unwrap();
    assert_eq!(success.subject.as_str(), "user-1");
    assert_eq!(success.grant.as_deref(), Some("grant-A"));

    // Issue a session — requires the RedeemSuccess proof.
    let issued = sm
        .issue(&success, SessionId::new("sess-1".into()), &mut sess_rng)
        .await
        .unwrap();
    assert!(!issued.set_cookie.is_empty());
    assert!(issued.set_cookie.contains("sid="));
    assert!(issued.set_cookie.contains("HttpOnly"));
    assert!(issued.set_cookie.contains("Secure"));

    // Validate the session using the cookie value.
    let cookie_val = issued
        .set_cookie
        .split(';')
        .next()
        .unwrap()
        .trim_start_matches("sid=");
    let outcome = sm.validate(cookie_val).await.unwrap();
    assert!(outcome.is_authenticated());
    assert_eq!(outcome.subject().unwrap().as_str(), "user-1");
}

// ── Callback-based redemption flow ────────────────────────────────────────────

#[tokio::test]
async fn callback_flow_issues_session_only_on_won() {
    let ca = code_auth();
    let sm = session_mgr();
    let mut rng = SystemRandom::new();
    let mut sess_rng = SystemRandom::new();

    let (_, plain) = ca
        .issue_code(&mut rng, CodeId::new("c2".into()), None, None, None)
        .await
        .unwrap();

    // Callback-based flow: on_won callback creates the subject.
    let success = ca
        .redeem_with_callback(plain.expose(), None, |_record| async {
            Ok::<_, std::convert::Infallible>(SubjectId::new("user-2".into()))
        })
        .await
        .unwrap();

    assert_eq!(success.subject.as_str(), "user-2");

    // Can issue a session from the proof.
    let issued = sm
        .issue(&success, SessionId::new("s2".into()), &mut sess_rng)
        .await
        .unwrap();
    assert!(issued.set_cookie.contains("sid="));
}

// ── Lost claim does not issue a session ───────────────────────────────────────

#[tokio::test]
async fn lost_claim_cannot_issue_session() {
    // RFC-013 §10.4: "Session issuance cannot occur before claim success."
    // We exercise this by ensuring find+claim returns Lost on second attempt
    // and that the caller has no proof to pass to SessionManager::issue.
    let ca = code_auth();
    let mut rng = SystemRandom::new();

    let (_, plain) = ca
        .issue_code(&mut rng, CodeId::new("c3".into()), None, None, None)
        .await
        .unwrap();

    let found = ca.find(plain.expose(), None).await.unwrap();
    let subj = SubjectId::new("winner".into());

    // First caller wins.
    let won = ca.claim(&found, subj, None).await.unwrap();
    assert_eq!(won.subject.as_str(), "winner");

    // Second caller loses.
    let lost = ca.claim(&found, SubjectId::new("loser".into()), None).await;
    assert!(matches!(lost, Err(RedeemError::ClaimLost { .. })));

    // Structural proof: the `RedeemSuccess` type cannot be constructed without
    // going through `claim`/`redeem_with_callback`.  We verified above that
    // `claim` returns `Err` on Lost, so there is no proof to pass to `issue`.
    let public = lost.unwrap_err();
    assert_eq!(*public.public(), PublicRedemptionError::InvalidOrExpired);
}

// ── Host callback error — claim is consumed, no session ───────────────────────

#[tokio::test]
async fn callback_error_leaves_claim_consumed_no_session() {
    // RFC-013 §5: "If claim wins but host hook fails, no session is issued."
    let ca = code_auth();
    let mut rng = SystemRandom::new();

    let (_, plain) = ca
        .issue_code(&mut rng, CodeId::new("c4".into()), None, None, None)
        .await
        .unwrap();

    // Callback always errors.
    let result = ca
        .redeem_with_callback(plain.expose(), None, |_| async {
            Err::<SubjectId, _>("host db unavailable")
        })
        .await;

    assert!(result.is_err());
    let err = result.unwrap_err();
    // The public error is generic (TemporarilyUnavailable for host failure).
    assert_eq!(*err.public(), PublicRedemptionError::TemporarilyUnavailable);

    // The code is now consumed; a second attempt with a new callback fails.
    let retry = ca
        .redeem_with_callback(plain.expose(), None, |_| async {
            Ok::<_, std::convert::Infallible>(SubjectId::new("late".into()))
        })
        .await;
    assert!(
        retry.is_err(),
        "code must not be redeemable after consumed-by-callback-error"
    );
}

// ── Public errors remain generic ──────────────────────────────────────────────

#[tokio::test]
async fn invalid_input_returns_generic_public_error() {
    let ca = code_auth();
    let err = ca.find("not-a-valid-code!!!", None).await.unwrap_err();
    // The public error must not reveal the internal reason.
    assert_eq!(*err.public(), PublicRedemptionError::InvalidOrExpired);
}

#[tokio::test]
async fn missing_code_returns_generic_public_error() {
    let ca = code_auth();
    // A structurally valid code that was never issued.
    let err = ca.find("ABCD2345", None).await.unwrap_err();
    assert_eq!(*err.public(), PublicRedemptionError::InvalidOrExpired);
}

// ── Returning login: validate ──────────────────────────────────────────────────

#[tokio::test]
async fn validate_expired_session_returns_unauthenticated() {
    use codlet_core::hashing::SecretDomain;
    use codlet_core::store::session::{SessionRecord, SessionStore};

    let store = MemSessionStore::new();
    let h = hasher();
    let (lk, kv) = h
        .lookup_key(SecretDomain::Session, "cookie-secret-xyz")
        .unwrap();

    // Insert an already-expired session.
    store
        .insert_session(SessionRecord {
            id: SessionId::new("s-old".into()),
            lookup_key: lk,
            key_version: kv,
            subject: SubjectId::new("user-3".into()),
            created_at: NOW - 100,
            expires_at: NOW - 1, // expired
        })
        .await
        .unwrap();

    let sm = SessionManager::new(
        store,
        hasher(),
        FixedClock::at(NOW),
        NoopAuditSink,
        cookie(),
    );

    let outcome = sm.validate("cookie-secret-xyz").await.unwrap();
    assert_eq!(outcome, SessionValidationOutcome::Unauthenticated);
}

// ── Logout: revoke + clear cookie ─────────────────────────────────────────────

#[tokio::test]
async fn revoke_session_and_clear_cookie() {
    let ca = code_auth();
    let sm = session_mgr();
    let mut rng = SystemRandom::new();
    let mut sess_rng = SystemRandom::new();

    let (_, plain) = ca
        .issue_code(&mut rng, CodeId::new("c5".into()), None, None, None)
        .await
        .unwrap();
    let found = ca.find(plain.expose(), None).await.unwrap();
    let success = ca
        .claim(&found, SubjectId::new("user-4".into()), None)
        .await
        .unwrap();
    let IssuedSession {
        session_id,
        set_cookie,
    } = sm
        .issue(&success, SessionId::new("sess-4".into()), &mut sess_rng)
        .await
        .unwrap();

    // Extract cookie value.
    let cookie_val: String = set_cookie
        .split(';')
        .next()
        .unwrap()
        .trim_start_matches("sid=")
        .to_string();

    // Confirm active before revocation.
    assert!(sm.validate(&cookie_val).await.unwrap().is_authenticated());

    // Revoke.
    let clear_cookie = sm.revoke(&session_id).await.unwrap();
    assert!(
        clear_cookie.contains("Max-Age=0"),
        "clear cookie must use Max-Age=0"
    );
    assert!(
        clear_cookie.contains("sid="),
        "clear cookie must have correct name"
    );

    // Now invalid.
    assert_eq!(
        sm.validate(&cookie_val).await.unwrap(),
        SessionValidationOutcome::Unauthenticated
    );
}

// ── FormTokenManager integration ──────────────────────────────────────────────

#[tokio::test]
async fn form_token_issue_and_consume() {
    let ft = FormTokenManager::new(
        MemFormTokenStore::new(),
        hasher(),
        FixedClock::at(NOW),
        NoopAuditSink,
        Duration::from_secs(3600),
    );
    let mut rng = SystemRandom::new();

    let secret = ft
        .issue(
            &mut rng,
            TokenSubject::Authenticated(SubjectId::new("u1".into())),
            "logout",
            None,
        )
        .await
        .unwrap();

    let subj = TokenSubject::Authenticated(SubjectId::new("u1".into()));
    // First consume: Proceed.
    let r1 = ft
        .consume(secret.expose(), &subj, "logout", None)
        .await
        .unwrap();
    assert!(r1.is_none(), "first consume must return Ok(None)");

    // Second consume: Replay (no result_ref stored yet).
    let r2 = ft
        .consume(secret.expose(), &subj, "logout", None)
        .await
        .unwrap();
    assert!(r2.is_none(), "replay with no result_ref returns None");
}

#[tokio::test]
async fn form_token_wrong_subject_is_invalid() {
    let ft = FormTokenManager::new(
        MemFormTokenStore::new(),
        hasher(),
        FixedClock::at(NOW),
        NoopAuditSink,
        Duration::from_secs(3600),
    );
    let mut rng = SystemRandom::new();
    let secret = ft
        .issue(
            &mut rng,
            TokenSubject::Authenticated(SubjectId::new("alice".into())),
            "save",
            None,
        )
        .await
        .unwrap();

    // Bob tries to use Alice's token.
    let bob = TokenSubject::Authenticated(SubjectId::new("bob".into()));
    let err = ft
        .consume(secret.expose(), &bob, "save", None)
        .await
        .unwrap_err();
    assert!(matches!(err, codlet_core::FormTokenError::Invalid { .. }));
}

// ── Audit events emitted ──────────────────────────────────────────────────────

#[tokio::test]
async fn audit_events_emitted_through_complete_flow() {
    // Verify the audit sink receives CodeIssued, CodeRedeemed, SessionIssued.
    // Use two separate sinks because CodeAuth and SessionManager are separate.
    let _code_sink = CollectingAuditSink::new();
    let _sess_sink = CollectingAuditSink::new();

    let _ca = CodeAuth::without_rate_limit(
        MemCodeStore::new(),
        hasher(),
        FixedClock::at(NOW),
        CollectingAuditSink::new(), // fresh inner sink (audit not inspected here)
        policy(),
    );
    // We can't share the sink easily without Arc; test the types separately.
    // Verify issue emits CodeIssued:
    let sink = CollectingAuditSink::new();
    let ca2 = CodeAuth::without_rate_limit(
        MemCodeStore::new(),
        hasher(),
        FixedClock::at(NOW),
        sink,
        policy(),
    );
    let mut rng = SystemRandom::new();
    let _ = ca2
        .issue_code(
            &mut rng,
            CodeId::new("cx".into()),
            Some("test".into()),
            None,
            None,
        )
        .await
        .unwrap();
    // The sink is moved into ca2; we test via compile/run success that
    // events are recorded — direct drain would require refactoring the API
    // to accept &A instead of A. That's a future ergonomics improvement.
    // For now, verify the flow completes without panicking.
}