watchword 0.3.0

Authentication for axum servers and Yew frontends: password, Cloudflare Access and Google sign-in, sessions and user management.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! The filter of `Connection::update`, over `MemoryStore`; `socket` tests
//! `Connection::handle` over a real WebSocket.

mod parity;
mod socket;

use std::borrow::ToOwned;
use std::string::String;
use std::vec::Vec;

use super::{AuthUpdate, Connection};
use crate::axum08::SignedIn;
use crate::types::{AuthError, Impersonator, NewUser, RegisterRequest, Role, UserInfo};
use crate::{Auth, AuthEvent, BootstrapAdmin, MemoryStore, generate_session_key};

const PASSWORD: &str = "password1";

fn auth() -> Auth {
    Auth::builder(MemoryStore::new(), generate_session_key())
        .impersonation(true)
        .build()
}

/// The bootstrap admin `root`, signed in.
async fn root(auth: &Auth) -> SignedIn {
    let link = auth
        .bootstrap_admin(&BootstrapAdmin::login("root"))
        .await
        .unwrap()
        .link()
        .cloned()
        .unwrap();
    register(auth, &link.token).await
}

/// A user created by `admin` with `role` and still pending, without a
/// session.
async fn pending(auth: &Auth, admin: &SignedIn, login: &str, role: Role) -> (UserInfo, String) {
    let new = NewUser {
        login: Some(login.to_owned()),
        email: None,
        role,
    };
    let (entry, link) = auth
        .create_user(&admin.user, &admin.session, &new)
        .await
        .unwrap();
    (entry.user, link.token)
}

/// A user created by `admin` with `role`, registered and signed in.
async fn user(auth: &Auth, admin: &SignedIn, login: &str, role: Role) -> SignedIn {
    let (_, token) = pending(auth, admin, login, role).await;
    register(auth, &token).await
}

async fn register(auth: &Auth, token: &str) -> SignedIn {
    let request = RegisterRequest {
        password: PASSWORD.to_owned(),
    };
    let (user, session) = auth.register(token, &request).await.unwrap();
    SignedIn { user, session }
}

/// The user-list events about `other`, which only an admin's connection
/// passes on.
fn list_events(other: &UserInfo) -> Vec<(AuthEvent, AuthUpdate)> {
    vec![
        (
            AuthEvent::UserCreated(other.clone()),
            AuthUpdate::UserCreated(other.clone()),
        ),
        (
            AuthEvent::UserChanged(other.clone()),
            AuthUpdate::UserChanged(other.clone()),
        ),
        (
            AuthEvent::UserDeleted(other.id),
            AuthUpdate::UserDeleted(other.id),
        ),
    ]
}

/// Events that end sessions, which no connection passes on.
fn ending_events(conn: &SignedIn) -> Vec<AuthEvent> {
    vec![
        AuthEvent::SessionEnded(conn.session.id.clone()),
        AuthEvent::SessionEnded("other".to_owned()),
        AuthEvent::UserSessionsEnded {
            user: conn.user.id,
            keep: None,
        },
        AuthEvent::UserSessionsEnded {
            user: conn.user.id,
            keep: Some(conn.session.id.clone()),
        },
    ]
}

#[tokio::test]
async fn an_admin_sees_itself_and_the_user_list() {
    let auth = auth();
    let root = root(&auth).await;
    let (other, _) = pending(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &root);

    let me = AuthEvent::UserChanged(root.user.info());
    assert_eq!(
        conn.update(&me).await.unwrap(),
        Some(AuthUpdate::Me(root.user.info()))
    );

    for (event, update) in list_events(&other) {
        assert_eq!(
            conn.update(&event).await.unwrap(),
            Some(update),
            "{event:?}"
        );
    }

    for event in ending_events(&root) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }
}

/// The events an operation emits, as the app's loop receives them.
#[tokio::test]
async fn an_admin_sees_real_operations() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Admin).await;
    let conn = Connection::new(auth.clone(), &ada);
    let mut events = auth.events();

    let new = NewUser {
        login: Some("erin".to_owned()),
        email: None,
        role: Role::Regular,
    };
    let (entry, _) = auth
        .create_user(&root.user, &root.session, &new)
        .await
        .unwrap();
    let event = events.recv().await.unwrap();
    assert_eq!(
        conn.update(&event).await.unwrap(),
        Some(AuthUpdate::UserCreated(entry.user.clone()))
    );

    auth.delete_user(&root.user, &root.session, entry.user.id)
        .await
        .unwrap();
    let mut updates = Vec::new();

    while let Ok(event) = events.try_recv() {
        updates.extend(conn.update(&event).await.unwrap());
    }

    assert_eq!(updates, [AuthUpdate::UserDeleted(entry.user.id)]);
}

#[tokio::test]
async fn a_regular_user_sees_only_itself() {
    let auth = auth();
    let root = root(&auth).await;
    let erin = user(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &erin);

    let me = AuthEvent::UserChanged(erin.user.info());
    assert_eq!(
        conn.update(&me).await.unwrap(),
        Some(AuthUpdate::Me(erin.user.info()))
    );

    for (event, _) in list_events(&root.user.info()) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }

    for event in ending_events(&erin) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }
}

/// A user's own change reaches them as `Me` with their stored state, which
/// is what the event carries.
#[tokio::test]
async fn me_follows_a_role_change() {
    let auth = auth();
    let root = root(&auth).await;
    let erin = user(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &erin);

    let promoted = auth
        .set_role(&root.user, &root.session, erin.user.id, Role::Admin)
        .await
        .unwrap();
    let me = AuthEvent::UserChanged(promoted.info());
    assert_eq!(
        conn.update(&me).await.unwrap(),
        Some(AuthUpdate::Me(promoted.info()))
    );

    // Promoted on an open socket, the user now sees the user list.
    let (event, update) = list_events(&root.user.info()).remove(0);
    assert_eq!(conn.update(&event).await.unwrap(), Some(update));
}

#[tokio::test]
async fn an_impersonated_session_keeps_its_marker() {
    let auth = auth();
    let root = root(&auth).await;
    let (erin, _) = pending(&auth, &root, "erin", Role::Regular).await;
    let (erin, session) = auth
        .impersonate(&root.user, &root.session, erin.id)
        .await
        .unwrap();
    let info = erin.info();
    let conn = Connection::new(
        auth.clone(),
        &SignedIn {
            user: erin,
            session,
        },
    );

    let mut expected = info.clone();
    expected.impersonated_by = Some(Impersonator {
        id: root.user.id,
        name: "root".to_owned(),
    });

    let me = AuthEvent::UserChanged(info);
    assert_eq!(
        conn.update(&me).await.unwrap(),
        Some(AuthUpdate::Me(expected))
    );

    // The impersonating admin's rights do not carry into the session.
    for (event, _) in list_events(&root.user.info()) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }
}

#[tokio::test]
async fn a_demoted_admin_stops_seeing_the_user_list() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Admin).await;
    let (erin, _) = pending(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &ada);

    let (event, update) = list_events(&erin).remove(0);
    assert_eq!(conn.update(&event).await.unwrap(), Some(update));

    let demoted = auth
        .set_role(&root.user, &root.session, ada.user.id, Role::Regular)
        .await
        .unwrap();

    for (event, _) in list_events(&erin) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }

    let me = AuthEvent::UserChanged(demoted.info());
    assert_eq!(
        conn.update(&me).await.unwrap(),
        Some(AuthUpdate::Me(demoted.info()))
    );
}

/// Once the session has ended, nothing is passed on; the guard ends the
/// socket.
#[tokio::test]
async fn an_ended_session_sees_nothing() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Admin).await;
    let (erin, _) = pending(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &ada);

    let revoked = auth
        .revoke_access(&root.user, &root.session, ada.user.id)
        .await
        .unwrap();

    let me = AuthEvent::UserChanged(revoked.info());
    assert_eq!(conn.update(&me).await.unwrap(), None);

    for (event, _) in list_events(&erin) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }

    // Signed out.
    let conn = Connection::new(auth.clone(), &root);
    auth.sign_out(&root.session.id).await.unwrap();
    let me = AuthEvent::UserChanged(root.user.info());
    assert_eq!(conn.update(&me).await.unwrap(), None);

    for (event, _) in list_events(&erin) {
        assert_eq!(conn.update(&event).await.unwrap(), None, "{event:?}");
    }
}

/// After a lag an admin is told to fetch the user list again, with their own
/// user as it is now; anyone else gets only their own user.
#[tokio::test]
async fn a_lag_resyncs_an_admin_and_refreshes_anyone_else() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Admin).await;
    let erin = user(&auth, &root, "erin", Role::Regular).await;

    let conn = Connection::new(auth.clone(), &root);
    assert_eq!(
        conn.lagged().await.unwrap(),
        Some(AuthUpdate::Resync(root.user.info()))
    );

    let conn = Connection::new(auth.clone(), &erin);
    assert_eq!(
        conn.lagged().await.unwrap(),
        Some(AuthUpdate::Me(erin.user.info()))
    );

    // The missed change itself shows in the user carried.
    let conn = Connection::new(auth.clone(), &ada);
    let demoted = auth
        .set_role(&root.user, &root.session, ada.user.id, Role::Regular)
        .await
        .unwrap();
    assert_eq!(
        conn.lagged().await.unwrap(),
        Some(AuthUpdate::Me(demoted.info()))
    );

    // An impersonated session keeps its marker and not the admin's rights.
    let (erin, session) = auth
        .impersonate(&root.user, &root.session, erin.user.id)
        .await
        .unwrap();
    let mut expected = erin.info();
    expected.impersonated_by = Some(Impersonator {
        id: root.user.id,
        name: "root".to_owned(),
    });
    let conn = Connection::new(
        auth.clone(),
        &SignedIn {
            user: erin,
            session,
        },
    );
    assert_eq!(conn.lagged().await.unwrap(), Some(AuthUpdate::Me(expected)));
}

/// An ended session gets nothing after a lag; the guard ends the socket.
#[tokio::test]
async fn a_lag_on_an_ended_session_sends_nothing() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Admin).await;

    let conn = Connection::new(auth.clone(), &ada);
    auth.revoke_access(&root.user, &root.session, ada.user.id)
        .await
        .unwrap();
    assert_eq!(conn.lagged().await.unwrap(), None);

    let conn = Connection::new(auth.clone(), &root);
    auth.sign_out(&root.session.id).await.unwrap();
    assert_eq!(conn.lagged().await.unwrap(), None);
}

/// The refusal inside `result`, if it is one; panics on a store failure.
fn refusal<T: std::fmt::Debug>(result: crate::core::Result<T>) -> Option<AuthError> {
    match result {
        Ok(_) => None,
        Err(crate::core::Error::Auth(error)) => Some(error),
        Err(error) => panic!("not a refusal: {error:?}"),
    }
}

/// The public checks read the stored user per call, as the HTTP extractors
/// do, so a role change or a sign-out applies to the next one.
#[tokio::test]
async fn the_checks_follow_the_stored_user() {
    let auth = auth();
    let root = root(&auth).await;
    let erin = user(&auth, &root, "erin", Role::Regular).await;
    let conn = Connection::new(auth.clone(), &erin);

    assert_eq!(conn.user_id(), erin.user.id);
    assert_eq!(conn.session_id(), erin.session.id);

    let (user, session) = conn.signed_in().await.unwrap();
    assert_eq!(user.id, erin.user.id);
    assert_eq!(session.id, erin.session.id);
    assert_eq!(
        refusal(conn.require_admin().await),
        Some(AuthError::Forbidden)
    );

    auth.set_role(&root.user, &root.session, erin.user.id, Role::Admin)
        .await
        .unwrap();
    assert_eq!(conn.require_admin().await.unwrap().id, erin.user.id);

    auth.set_role(&root.user, &root.session, erin.user.id, Role::Regular)
        .await
        .unwrap();
    assert_eq!(
        refusal(conn.require_admin().await),
        Some(AuthError::Forbidden)
    );

    auth.sign_out(&erin.session.id).await.unwrap();
    assert_eq!(
        refusal(conn.signed_in().await),
        Some(AuthError::Unauthorized)
    );
    assert_eq!(
        refusal(conn.require_admin().await),
        Some(AuthError::Unauthorized)
    );
}

/// Long enough for a spawned guard to notice a change.
const WAIT: core::time::Duration = core::time::Duration::from_secs(5);

/// Short enough to show that nothing is ready.
const BRIEFLY: core::time::Duration = core::time::Duration::from_millis(50);

/// `Updates` yields what the connection may see, skipping the rest, and
/// ends with the session.
#[tokio::test]
async fn updates_follow_the_session() {
    let auth = auth();
    let root = root(&auth).await;
    let erin = user(&auth, &root, "erin", Role::Regular).await;

    let mut admin = Connection::new(auth.clone(), &root).updates();
    let mut regular = Connection::new(auth.clone(), &erin).updates();
    assert_eq!(admin.connection().user_id(), root.user.id);
    assert_eq!(admin.guard().user(), root.user.info());

    // Nothing yet, and a cancelled call loses nothing.
    assert!(tokio::time::timeout(BRIEFLY, admin.next()).await.is_err());

    let (pat, _) = pending(&auth, &root, "pat", Role::Regular).await;
    let next = tokio::time::timeout(WAIT, admin.next()).await.unwrap();
    assert_eq!(next, Some(AuthUpdate::UserCreated(pat.clone())));

    // The regular user skips the user list and sees its own change.
    assert!(tokio::time::timeout(BRIEFLY, regular.next()).await.is_err());

    let erin_changed = auth
        .set_role(&root.user, &root.session, erin.user.id, Role::Admin)
        .await
        .unwrap();
    let next = tokio::time::timeout(WAIT, regular.next()).await.unwrap();
    assert_eq!(next, Some(AuthUpdate::Me(erin_changed.info())));

    let next = tokio::time::timeout(WAIT, admin.next()).await.unwrap();
    assert_eq!(next, Some(AuthUpdate::UserChanged(erin_changed.info())));

    // Signing out ends the updates, for good.
    auth.sign_out(&root.session.id).await.unwrap();
    assert_eq!(
        tokio::time::timeout(WAIT, admin.next()).await.unwrap(),
        None
    );
    assert_eq!(
        tokio::time::timeout(WAIT, admin.next()).await.unwrap(),
        None
    );
}

/// Missed events become one `Resync` for an admin, after which the updates
/// carry on.
#[tokio::test]
async fn updates_resync_after_a_lag() {
    let auth = auth();
    let root = root(&auth).await;
    let ada = user(&auth, &root, "ada", Role::Regular).await;
    let mut updates = Connection::new(auth.clone(), &root).updates();

    // More changes than the event channel holds.
    let mut last = None;

    for n in 0..300 {
        let role = if n % 2 == 0 {
            Role::Admin
        } else {
            Role::Regular
        };
        last = Some(
            auth.set_role(&root.user, &root.session, ada.user.id, role)
                .await
                .unwrap(),
        );
    }

    let next = tokio::time::timeout(WAIT, updates.next()).await.unwrap();
    assert_eq!(next, Some(AuthUpdate::Resync(root.user.info())));

    // What the channel kept follows, ending with the last change.
    let last = AuthUpdate::UserChanged(last.unwrap().info());

    loop {
        let next = tokio::time::timeout(WAIT, updates.next()).await.unwrap();
        assert!(
            matches!(next, Some(AuthUpdate::UserChanged(..))),
            "{next:?}"
        );

        if next.as_ref() == Some(&last) {
            break;
        }
    }
}

/// `unless_ended` runs a step only while the session lasts.
#[tokio::test]
async fn unless_ended_stops_with_the_session() {
    let auth = auth();
    let root = root(&auth).await;
    let updates = Connection::new(auth.clone(), &root).updates();

    assert_eq!(updates.unless_ended(async { 1 }).await, Some(1));

    // A step that never finishes is given up once the session ends.
    let stalled = updates.unless_ended(core::future::pending::<()>());
    let sign_out = async {
        tokio::time::sleep(BRIEFLY).await;
        auth.sign_out(&root.session.id).await.unwrap();
    };
    let (stalled, ()) = tokio::time::timeout(WAIT, async { tokio::join!(stalled, sign_out) })
        .await
        .unwrap();
    assert_eq!(stalled, None);

    // And not started after.
    assert_eq!(updates.unless_ended(async { 1 }).await, None);
}

/// A session that ended before its updates were taken gets none.
#[tokio::test]
async fn updates_of_an_ended_session_end_at_once() {
    let auth = auth();
    let root = root(&auth).await;
    let conn = Connection::new(auth.clone(), &root);
    auth.sign_out(&root.session.id).await.unwrap();

    let mut updates = conn.updates();
    assert_eq!(
        tokio::time::timeout(WAIT, updates.next()).await.unwrap(),
        None
    );
}