sova-auth 0.1.8

Fortify-style authentication for Sova (register, 2FA, reset, roles)
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
//! Offline Fortify JSON API coverage (`/api/auth/*`).

use sova_auth::{assign_role, AuthMigrator, Feature, Fortify};
use sova_core::{ClientRequest, ResponseAssert, TestClient};
use sova_mail::{FakeMail, Mail};
use sova_session::memory_sessions;
use sova_testing::{SqliteTestDb, TestApp};
use serde_json::{json, Value};
use totp_rs::{Algorithm, Secret, TOTP};

const SECRET: &str = "test-fortify-secret-fortify-api!!";

async fn build() -> (SqliteTestDb, TestClient, FakeMail) {
    let mail = Mail::fake().from("Test <noreply@test.local>");
    let fake = mail.recorder().expect("fake recorder").clone();
    let (_db, app) = TestApp::builder()
        .migrator::<AuthMigrator>()
        .env("FORTIFY_SECRET", SECRET)
        .install(memory_sessions())
        .install(mail)
        .install(
            Fortify::new()
                .features(Feature::all().iter().copied())
                .web_forms(false)
                .api_mount("/api/auth")
                .public_url("http://127.0.0.1")
                .app_name("Test")
                .home("/")
                .login_redirect("/login")
                .secret(SECRET),
        )
        .build()
        .await;
    let c = TestClient::tracked(app).await.expect("test client");
    (_db, c, fake)
}

fn json_headers(req: ClientRequest<'_>) -> ClientRequest<'_> {
    req.header("accept", "application/json")
}

async fn register(c: &TestClient, name: &str, email: &str, password: &str) -> Value {
    let res = json_headers(c.post("/api/auth/register"))
        .json(&json!({
            "name": name,
            "email": email,
            "password": password,
            "password_confirmation": password,
        }))
        .await;
    assert!(
        (200..300).contains(&res.status_code().as_u16()),
        "register {}: {}",
        email,
        res.status_code()
    );
    res.json_value()
}

async fn login(c: &TestClient, email: &str, password: &str) -> sova_core::Response {
    json_headers(c.post("/api/auth/login"))
        .json(&json!({ "email": email, "password": password }))
        .await
}

fn query_param(haystack: &str, key: &str) -> String {
    let url = haystack
        .split_whitespace()
        .find(|w| w.contains("http://") || w.contains("https://"))
        .unwrap_or(haystack);
    let Some(q) = url.split('?').nth(1) else {
        panic!("no query in mail body: {haystack}");
    };
    for pair in q.split('&') {
        let mut it = pair.splitn(2, '=');
        if it.next() == Some(key) {
            let raw = it.next().unwrap_or("");
            return raw
                .replace("%40", "@")
                .replace("%2E", ".")
                .replace("%2e", ".");
        }
    }
    panic!("missing query key `{key}` in: {haystack}");
}

fn totp_now(secret_b32: &str) -> String {
    let secret = Secret::Encoded(secret_b32.to_string())
        .to_bytes()
        .expect("secret");
    let totp = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret,
        Some("Sova".into()),
        "user".into(),
    )
    .expect("totp");
    totp.generate_current().expect("code")
}

#[tokio::test]
async fn register_success_and_duplicate_email() {
    let (_db, c, _fake) = build().await;

    let body = register(&c, "Ada", "ada@example.com", "secret123").await;
    assert_eq!(body["email"], "ada@example.com");
    assert_eq!(body["name"], "Ada");
    assert!(body["id"].as_i64().is_some());

    let dup = json_headers(c.post("/api/auth/register"))
        .json(&json!({
            "name": "Other",
            "email": "ada@example.com",
            "password": "secret123",
            "password_confirmation": "secret123",
        }))
        .await;
    assert_eq!(dup.status_code().as_u16(), 409);
}

#[tokio::test]
async fn login_success_wrong_password_and_logout() {
    let (_db, c, _fake) = build().await;
    register(&c, "Bob", "bob@example.com", "secret123").await;

    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);

    let bad = login(&c, "bob@example.com", "wrong-password").await;
    assert_eq!(bad.status_code().as_u16(), 401);

    let ok = login(&c, "bob@example.com", "secret123").await;
    ok.assert_status(200);
    assert_eq!(ok.json_value()["email"], "bob@example.com");

    let me = json_headers(c.get("/api/auth/me")).await;
    me.assert_status(200);
    assert_eq!(me.json_value()["email"], "bob@example.com");

    let out = json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await;
    out.assert_status(200);
    assert_eq!(out.json_value()["ok"], true);

    let me2 = json_headers(c.get("/api/auth/me")).await;
    assert_eq!(me2.status_code().as_u16(), 401);
}

#[tokio::test]
async fn profile_get_and_update() {
    let (_db, c, _fake) = build().await;
    register(&c, "Cara", "cara@example.com", "secret123").await;

    let me = json_headers(c.get("/api/auth/profile")).await;
    me.assert_status(200);
    assert_eq!(me.json_value()["name"], "Cara");

    let upd = json_headers(c.post("/api/auth/profile"))
        .json(&json!({
            "name": "Cara Updated",
            "email": "cara@example.com",
        }))
        .await;
    upd.assert_status(200);
    assert_eq!(upd.json_value()["ok"], true);

    let me2 = json_headers(c.get("/api/auth/me")).await;
    me2.assert_status(200);
    assert_eq!(me2.json_value()["name"], "Cara Updated");
}

#[tokio::test]
async fn password_update() {
    let (_db, c, _fake) = build().await;
    register(&c, "Dan", "dan@example.com", "secret123").await;

    let bad = json_headers(c.post("/api/auth/password"))
        .json(&json!({
            "current_password": "nope",
            "password": "newsecret1",
            "password_confirmation": "newsecret1",
        }))
        .await;
    assert_eq!(bad.status_code().as_u16(), 400);

    let ok = json_headers(c.post("/api/auth/password"))
        .json(&json!({
            "current_password": "secret123",
            "password": "newsecret1",
            "password_confirmation": "newsecret1",
        }))
        .await;
    ok.assert_status(200);
    assert_eq!(ok.json_value()["ok"], true);

    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);

    login(&c, "dan@example.com", "secret123")
        .await
        .assert_status(401);
    login(&c, "dan@example.com", "newsecret1")
        .await
        .assert_status(200);
}

#[tokio::test]
async fn forgot_and_reset_password_via_fake_mail() {
    let (_db, c, fake) = build().await;
    register(&c, "Eve", "eve@example.com", "secret123").await;
    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);
    fake.clear();

    let forgot = json_headers(c.post("/api/auth/forgot-password"))
        .json(&json!({ "email": "eve@example.com" }))
        .await;
    forgot.assert_status(200);
    assert_eq!(forgot.json_value()["ok"], true);

    let sent = fake.sent();
    assert_eq!(sent.len(), 1, "expected reset mail");
    let body = sent[0]
        .text
        .as_deref()
        .or(sent[0].html.as_deref())
        .expect("mail body");
    let token = query_param(body, "token");
    let email = query_param(body, "email");
    assert_eq!(email, "eve@example.com");

    let reset = json_headers(c.post("/api/auth/reset-password"))
        .json(&json!({
            "email": "eve@example.com",
            "token": token,
            "password": "resetpass1",
            "password_confirmation": "resetpass1",
        }))
        .await;
    reset.assert_status(200);
    assert_eq!(reset.json_value()["ok"], true);

    login(&c, "eve@example.com", "secret123")
        .await
        .assert_status(401);
    login(&c, "eve@example.com", "resetpass1")
        .await
        .assert_status(200);
}

#[tokio::test]
async fn email_verification_via_fake_mail() {
    let (_db, c, fake) = build().await;
    fake.clear();

    register(&c, "Fay", "fay@example.com", "secret123").await;

    let sent = fake.sent();
    assert!(
        !sent.is_empty(),
        "expected verify mail on register with EmailVerification"
    );
    let body = sent[0]
        .text
        .as_deref()
        .or(sent[0].html.as_deref())
        .expect("mail body");
    let token = query_param(body, "token");

    let me = json_headers(c.get("/api/auth/me")).await;
    me.assert_status(200);
    assert_eq!(me.json_value()["email_verified"], false);

    let verify = json_headers(c.post("/api/auth/email/verify"))
        .json(&json!({ "token": token }))
        .await;
    verify.assert_status(200);
    assert_eq!(verify.json_value()["ok"], true);

    // Session CurrentUser may still show old flag until re-login.
    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);
    login(&c, "fay@example.com", "secret123")
        .await
        .assert_status(200);
    let me2 = json_headers(c.get("/api/auth/me")).await;
    me2.assert_status(200);
    assert_eq!(me2.json_value()["email_verified"], true);
}

#[tokio::test]
async fn two_factor_enable_confirm_challenge_disable() {
    let (_db, c, _fake) = build().await;
    register(&c, "Gus", "gus@example.com", "secret123").await;

    let en = json_headers(c.post("/api/auth/two-factor"))
        .json(&json!({}))
        .await;
    en.assert_status(200);
    let en_body = en.json_value();
    let secret = en_body["secret"].as_str().expect("secret");
    assert!(en_body["recovery_codes"].as_array().is_some());

    let code = totp_now(secret);
    let confirm = json_headers(c.post("/api/auth/two-factor/confirm"))
        .json(&json!({ "code": code }))
        .await;
    confirm.assert_status(200);
    assert_eq!(confirm.json_value()["ok"], true);

    let me = json_headers(c.get("/api/auth/me")).await;
    me.assert_status(200);
    assert_eq!(me.json_value()["two_factor_enabled"], true);

    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);

    let challenge_login = login(&c, "gus@example.com", "secret123").await;
    challenge_login.assert_status(200);
    assert_eq!(challenge_login.json_value()["two_factor"], true);

    let me_pending = json_headers(c.get("/api/auth/me")).await;
    assert_eq!(me_pending.status_code().as_u16(), 401);

    let challenge = json_headers(c.post("/api/auth/two-factor/challenge"))
        .json(&json!({ "code": totp_now(secret) }))
        .await;
    challenge.assert_status(200);
    assert_eq!(challenge.json_value()["email"], "gus@example.com");

    let disable = json_headers(c.post("/api/auth/two-factor/disable"))
        .json(&json!({ "password": "secret123" }))
        .await;
    disable.assert_status(200);
    assert_eq!(disable.json_value()["ok"], true);

    let me2 = json_headers(c.get("/api/auth/me")).await;
    me2.assert_status(200);
    assert_eq!(me2.json_value()["two_factor_enabled"], false);
}

#[tokio::test]
async fn roles_crud_as_admin() {
    let (tdb, c, _fake) = build().await;
    let body = register(&c, "Admin", "admin@example.com", "secret123").await;
    let uid = body["id"].as_i64().unwrap();

    let denied = json_headers(c.get("/api/auth/roles")).await;
    assert_eq!(denied.status_code().as_u16(), 403);

    let db = tdb.handle().await;
    assign_role(&db, uid, "admin").await.expect("assign admin");
    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);
    login(&c, "admin@example.com", "secret123")
        .await
        .assert_status(200);

    let list = json_headers(c.get("/api/auth/roles")).await;
    list.assert_status(200);
    assert!(list.json_value()["roles"].as_array().unwrap().len() >= 2);

    let created = json_headers(c.post("/api/auth/roles"))
        .json(&json!({ "name": "Editor", "slug": "editor" }))
        .await;
    assert_eq!(created.status_code().as_u16(), 201);
    let role_id = created.json_value()["role"]["id"].as_i64().unwrap();

    let show = json_headers(c.get(format!("/api/auth/roles/{role_id}"))).await;
    show.assert_status(200);
    assert_eq!(show.json_value()["role"]["slug"], "editor");

    let perms = json_headers(c.get("/api/auth/permissions")).await;
    perms.assert_status(200);
    let perm_id = perms.json_value()["permissions"][0]["id"]
        .as_i64()
        .expect("perm id");

    let sync = json_headers(c.put(format!("/api/auth/roles/{role_id}/permissions")))
        .json(&json!({ "permission_ids": [perm_id] }))
        .await;
    sync.assert_status(200);

    // Assign editor to a *different* user so the admin session keeps users.manage.
    let other = register(&c, "Other", "other@example.com", "secret123").await;
    let other_id = other["id"].as_i64().unwrap();
    // register logs in as other — switch back to admin
    json_headers(c.post("/api/auth/logout"))
        .json(&json!({}))
        .await
        .assert_status(200);
    login(&c, "admin@example.com", "secret123")
        .await
        .assert_status(200);

    let user_roles = json_headers(c.get(format!("/api/auth/users/{other_id}/roles"))).await;
    user_roles.assert_status(200);

    let set_roles = json_headers(c.put(format!("/api/auth/users/{other_id}/roles")))
        .json(&json!({ "role_ids": [role_id] }))
        .await;
    set_roles.assert_status(200);

    let del = json_headers(c.delete(format!("/api/auth/roles/{role_id}"))).await;
    del.assert_status(200);
}

#[tokio::test]
async fn confirm_password_status() {
    let (_db, c, _fake) = build().await;
    register(&c, "Hal", "hal@example.com", "secret123").await;

    let st = json_headers(c.get("/api/auth/confirmed-password-status")).await;
    st.assert_status(200);
    assert_eq!(st.json_value()["confirmed"], false);

    let conf = json_headers(c.post("/api/auth/confirm-password"))
        .json(&json!({ "password": "secret123" }))
        .await;
    conf.assert_status(200);
    assert_eq!(conf.json_value()["confirmed"], true);

    let st2 = json_headers(c.get("/api/auth/confirmed-password-status")).await;
    st2.assert_status(200);
    assert_eq!(st2.json_value()["confirmed"], true);
}