fastauth-core 0.5.0

Core of fastauth: the builder, plugin system, sessions, crypto and routing.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Core operations shared by core endpoints and plugins: users, accounts, sessions,
//! sign-in with gates, verification values, email.

use std::borrow::Cow;

use http::StatusCode;
use time::{Duration, OffsetDateTime};

use crate::adapter::{Adapter, DbError, Transaction};
use crate::crypto::{Hash, random_token};
use crate::endpoint::{Ctx, Gate, SignInAttempt};
use crate::error::{AuthError, codes};
use crate::events::{
    AccountLinked, EmailVerified, SessionCreated, SessionRevoked, UserClaimed, UserCreated,
};
use crate::mail::{AuthEmail, VerifyEmail};
use crate::model::Model;
use crate::models::{Account, Session, User, Verification};
use crate::session::{
    SESSION_DATA, SessionView, UserView, clear_session_cookies, set_session_cookies,
};
use crate::value::{ColumnValue, Id, Value};

/// A user to create.
#[derive(Clone, Debug, Default)]
pub struct NewUser {
    pub email: String,
    pub name: String,
    pub image: Option<String>,
    pub email_verified: bool,
    /// App and plugin columns, already validated.
    pub extra: Vec<(&'static str, Value)>,
}

/// An account to create with a user or link to one.
#[derive(Clone, Debug, Default)]
pub struct NewAccount {
    pub provider_id: String,
    /// Defaults to the user id (what the credential account uses).
    pub account_id: Option<String>,
    pub password_hash: Option<String>,
    /// Plaintext; encrypted before it is stored.
    pub access_token: Option<String>,
    /// Plaintext; encrypted before it is stored.
    pub refresh_token: Option<String>,
    pub id_token: Option<String>,
    pub access_token_expires_at: Option<OffsetDateTime>,
    pub refresh_token_expires_at: Option<OffsetDateTime>,
    pub scope: Option<String>,
}

impl NewAccount {
    pub fn credential(password_hash: String) -> Self {
        Self {
            provider_id: Account::CREDENTIAL.into(),
            password_hash: Some(password_hash),
            ..Self::default()
        }
    }

    pub fn oauth(provider_id: impl Into<String>, account_id: impl Into<String>) -> Self {
        Self {
            provider_id: provider_id.into(),
            account_id: Some(account_id.into()),
            ..Self::default()
        }
    }
}

#[derive(Clone, Debug)]
pub enum SignInResult {
    SignedIn(Box<SessionView>),
    /// A gate wants more (e.g. a TOTP code). Respond with this JSON; no session exists.
    Challenge(serde_json::Value),
}

fn user_exists(err: DbError) -> AuthError {
    match err {
        DbError::UniqueViolation { ref table, .. } if table == User::TABLE => AuthError::new(
            StatusCode::UNPROCESSABLE_ENTITY,
            codes::USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL,
            "user already exists, use another email",
        ),
        other => other.into(),
    }
}

pub(crate) fn url_encode(value: &str) -> String {
    url::form_urlencoded::byte_serialize(value.as_bytes()).collect()
}

pub struct Internal<'a, D, A> {
    ctx: &'a Ctx<D, A>,
}

impl<'a, D: Adapter, A> Internal<'a, D, A> {
    pub(crate) fn new(ctx: &'a Ctx<D, A>) -> Self {
        Self { ctx }
    }

    pub async fn find_user_by_email(&self, email: &str) -> Result<Option<UserView>, AuthError> {
        let row = self
            .ctx
            .db()
            .find::<User>(User::EMAIL.eq(email.trim().to_lowercase()))
            .one_row()
            .await?;
        row.map(UserView::from_row)
            .transpose()
            .map_err(AuthError::internal)
    }

    pub async fn find_user_by_id(&self, id: &Id) -> Result<Option<UserView>, AuthError> {
        let row = self
            .ctx
            .db()
            .find::<User>(User::ID.eq(id))
            .one_row()
            .await?;
        row.map(UserView::from_row)
            .transpose()
            .map_err(AuthError::internal)
    }

    fn account_values(
        &self,
        user_id: &Id,
        account: NewAccount,
    ) -> Result<Vec<(&'static str, Value)>, AuthError> {
        let keys = self.ctx.keys();
        let seal = |t: Option<String>| t.map(|t| keys.encrypt(t.as_bytes()));
        Ok(vec![
            ("id", self.ctx.new_id()?.into_value()),
            ("user_id", user_id.clone().into_value()),
            ("provider_id", account.provider_id.into_value()),
            (
                "account_id",
                account
                    .account_id
                    .unwrap_or_else(|| user_id.to_string())
                    .into_value(),
            ),
            ("password_hash", account.password_hash.into_value()),
            ("access_token", seal(account.access_token).into_value()),
            ("refresh_token", seal(account.refresh_token).into_value()),
            ("id_token", account.id_token.into_value()),
            (
                "access_token_expires_at",
                account.access_token_expires_at.into_value(),
            ),
            (
                "refresh_token_expires_at",
                account.refresh_token_expires_at.into_value(),
            ),
            ("scope", account.scope.into_value()),
        ])
    }

    /// Creates a user (and optionally its first account) in one transaction.
    /// Answers `USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL` (422) when the email is taken.
    pub async fn create_user(
        &self,
        user: NewUser,
        account: Option<NewAccount>,
    ) -> Result<UserView, AuthError> {
        let id = self.ctx.new_id()?;
        let mut values = vec![
            ("id", id.clone().into_value()),
            ("email", user.email.trim().to_lowercase().into_value()),
            ("email_verified", user.email_verified.into_value()),
            ("name", user.name.into_value()),
            ("image", user.image.into_value()),
        ];
        values.extend(user.extra);
        let account = account
            .map(|account| self.account_values(&id, account))
            .transpose()?;
        let tx = self.ctx.adapter().begin().await?;
        let db = self.ctx.db_on(&tx);
        let row = db.insert_row::<User>(values).await.map_err(user_exists)?;
        if let Some(account) = account {
            db.insert_row::<Account>(account).await?;
        }
        tx.commit().await?;
        let view = UserView::from_row(row).map_err(AuthError::internal)?;
        self.ctx.emit(UserCreated {
            user_id: view.id.clone(),
            email: view.email.clone(),
        });
        Ok(view)
    }

    pub async fn link_account(
        &self,
        user_id: &Id,
        account: NewAccount,
    ) -> Result<Account, AuthError> {
        let provider_id = account.provider_id.clone();
        let row = self
            .ctx
            .db()
            .insert_row::<Account>(self.account_values(user_id, account)?)
            .await?;
        self.ctx.emit(AccountLinked {
            user_id: user_id.clone(),
            provider_id,
        });
        Account::decode(&row).map_err(AuthError::internal)
    }

    pub async fn find_account(
        &self,
        provider_id: &str,
        account_id: &str,
    ) -> Result<Option<Account>, AuthError> {
        let filter = Account::PROVIDER_ID
            .eq(provider_id)
            .and(Account::ACCOUNT_ID.eq(account_id));
        Ok(self.ctx.db().find::<Account>(filter).one().await?)
    }

    pub async fn credential_account(&self, user_id: &Id) -> Result<Option<Account>, AuthError> {
        let filter = Account::USER_ID
            .eq(user_id)
            .and(Account::PROVIDER_ID.eq(Account::CREDENTIAL));
        Ok(self.ctx.db().find::<Account>(filter).one().await?)
    }

    /// Checks a new password against the configured length limits and policy.
    pub fn validate_new_password(&self, password: &str) -> Result<(), AuthError> {
        let Some(config) = &self.ctx.config().email_password else {
            return Ok(());
        };
        let length = password.chars().count();
        if length < config.min_password_length {
            return Err(AuthError::bad_request(
                codes::PASSWORD_TOO_SHORT,
                "password is too short",
            ));
        }
        if length > config.max_password_length {
            return Err(AuthError::bad_request(
                codes::PASSWORD_TOO_LONG,
                "password is too long",
            ));
        }
        if config
            .password_policy
            .as_ref()
            .is_some_and(|policy| !policy(password))
        {
            return Err(AuthError::bad_request(
                codes::PASSWORD_TOO_WEAK,
                "password is too weak",
            ));
        }
        Ok(())
    }

    /// Validates, hashes and stores a password, creating the credential account if needed.
    pub async fn set_password(&self, user_id: &Id, password: &str) -> Result<(), AuthError> {
        self.validate_new_password(password)?;
        let hash = self
            .ctx
            .passwords()
            .hash(password)
            .await
            .map_err(AuthError::internal)?;
        let updated = self
            .ctx
            .db()
            .update::<Account>(
                Account::USER_ID
                    .eq(user_id)
                    .and(Account::PROVIDER_ID.eq(Account::CREDENTIAL)),
            )
            .set(Account::PASSWORD_HASH, Some(hash.clone()))
            .exec()
            .await?;
        if updated == 0 {
            self.link_account(user_id, NewAccount::credential(hash))
                .await?;
        }
        Ok(())
    }

    /// Creates a session and sets its cookies, running no sign-in gate at all. This is the
    /// low-level primitive: sign a user in with [`sign_in`](Self::sign_in), and finish a
    /// challenged sign-in with [`complete_sign_in`](Self::complete_sign_in). Finishing one
    /// with this would skip every other plugin's gates, so a banned user would get a session.
    pub async fn create_session(
        &self,
        user_id: &Id,
        remember_me: bool,
    ) -> Result<SessionView, AuthError> {
        let now = self.ctx.now();
        let token = random_token();
        let session = Session {
            id: self.ctx.new_id()?,
            token_hash: Hash::sha256(&token),
            user_id: user_id.clone(),
            expires_at: now + self.ctx.config().session.expires_in,
            ip_address: self.ctx.ip().map(str::to_owned),
            user_agent: self
                .ctx
                .user_agent()
                .map(|ua| ua.chars().take(512).collect()),
            created_at: now,
            updated_at: now,
        };
        let row = self
            .ctx
            .db()
            .insert_row::<Session>(session.encode())
            .await?;
        let user = self
            .find_user_by_id(user_id)
            .await?
            .ok_or_else(|| AuthError::internal("session for a missing user"))?;
        let config = self.ctx.config();
        set_session_cookies(
            config,
            self.ctx.keys(),
            &self.ctx.effects,
            &token,
            remember_me,
        );
        self.ctx.set_cookie(crate::cookies::SetCookie::delete(
            config.cookie_name(SESSION_DATA),
        ));
        self.ctx.emit(SessionCreated {
            session_id: session.id.clone(),
            user_id: user_id.clone(),
        });
        let view = SessionView {
            session,
            row,
            user,
            token: Some(token),
            ephemeral: false,
        };
        for hook in &self.ctx.shared.session_hooks {
            hook(self.ctx.retype(), view.clone()).await?;
        }
        Ok(view)
    }

    /// The unexpired session `token` belongs to, with its user, without refreshing it.
    /// For plugins that hold session tokens of their own, such as one per signed-in account.
    pub async fn find_session_by_token(
        &self,
        token: &str,
    ) -> Result<Option<SessionView>, AuthError> {
        crate::session::by_token(&self.ctx.shared, self.ctx.adapter(), token).await
    }

    /// The request-only session a resolver's [`Resolved::User`](crate::endpoint::Resolved)
    /// gets for this user, after the session gates. `None` when the user doesn't exist; the
    /// gate's error when one refuses the user (a banned user, with the admin plugin). For
    /// plugins that act for a user without a session, such as an API key's owner.
    pub async fn ephemeral_session(&self, user_id: &Id) -> Result<Option<SessionView>, AuthError> {
        crate::session::for_user(
            &self.ctx.shared,
            self.ctx.within.as_ref(),
            &self.ctx.request,
            &self.ctx.effects,
            user_id,
        )
        .await
    }

    /// Makes `token` the request's session: sets the session cookie (for the configured
    /// lifetime, or for the browser session when `remember_me` is false) and drops the
    /// cookie cache, which belonged to the previous session.
    pub fn set_session_cookies(&self, token: &str, remember_me: bool) {
        let config = self.ctx.config();
        set_session_cookies(
            config,
            self.ctx.keys(),
            &self.ctx.effects,
            token,
            remember_me,
        );
        self.ctx.set_cookie(crate::cookies::SetCookie::delete(
            config.cookie_name(SESSION_DATA),
        ));
    }

    /// Runs every sign-in gate in registration order, then creates the session. The first
    /// gate that denies or challenges ends the sign-in without a session.
    pub async fn sign_in(
        &self,
        user: UserView,
        method: impl Into<Cow<'static, str>>,
        remember_me: bool,
    ) -> Result<SignInResult, AuthError> {
        self.run_gates(user, method.into(), remember_me, None).await
    }

    /// Finishes a sign-in that a gate of plugin `challenger` answered with
    /// [`Gate::Challenge`], once the user has met the challenge (e.g. a correct TOTP code).
    /// Pass the plugin's own id (`TwoFactor::ID`, or `"app"` for a gate the app registered)
    /// and the method the sign-in started with.
    ///
    /// Runs every gate except `challenger`'s, in registration order, whether they come before
    /// or after it, then creates the session. A remaining gate can still deny, which returns
    /// its error with no session. It can also challenge, which returns
    /// [`SignInResult::Challenge`] with no session; answer with its body. That gate's plugin
    /// finishes the sign-in with its own `complete_sign_in`, which runs `challenger`'s gates
    /// again, so two gates that both challenge the same user keep challenging each other.
    ///
    /// A 500 when no gate belongs to `challenger`: a wrong id would run the challenger's
    /// own gate again and challenge forever.
    pub async fn complete_sign_in(
        &self,
        challenger: &str,
        user: UserView,
        method: impl Into<Cow<'static, str>>,
        remember_me: bool,
    ) -> Result<SignInResult, AuthError> {
        if !self
            .ctx
            .shared
            .gates
            .iter()
            .any(|gate| gate.owner == challenger)
        {
            return Err(AuthError::internal(format!(
                "complete_sign_in: no sign-in gate belongs to `{challenger}`"
            )));
        }
        self.run_gates(user, method.into(), remember_me, Some(challenger))
            .await
    }

    async fn run_gates(
        &self,
        user: UserView,
        method: Cow<'static, str>,
        remember_me: bool,
        skip: Option<&str>,
    ) -> Result<SignInResult, AuthError> {
        let attempt = SignInAttempt {
            user: user.clone(),
            method,
            remember_me,
        };
        for owned in &self.ctx.shared.gates {
            if skip == Some(owned.owner) {
                continue;
            }
            match (owned.gate)(self.ctx.retype(), attempt.clone()).await? {
                Gate::Allow => {}
                Gate::Deny(err) => return Err(err),
                Gate::Challenge(body) => return Ok(SignInResult::Challenge(body)),
            }
        }
        Ok(SignInResult::SignedIn(Box::new(
            self.create_session(&user.id, remember_me).await?,
        )))
    }

    /// Makes the person who just proved control of an email the only owner of its unverified
    /// user: deletes every account (password and OAuth links) and session the user collected
    /// before the proof, then marks the email verified and emits [`UserClaimed`], on which
    /// plugins drop credentials of their own (passkeys). Email-primary sign-ins (magic links,
    /// email codes) call this when they land on a user whose email isn't verified, so whoever
    /// registered that address first keeps no access. A verified user comes back unchanged.
    pub async fn claim_unverified_user(&self, user: UserView) -> Result<UserView, AuthError> {
        if user.email_verified {
            return Ok(user);
        }
        self.ctx
            .db()
            .delete::<Account>(Account::USER_ID.eq(&user.id))
            .await?;
        self.revoke_user_sessions(&user.id, None).await?;
        self.ctx
            .db()
            .update::<User>(User::ID.eq(&user.id))
            .set(User::EMAIL_VERIFIED, true)
            .exec()
            .await?;
        self.ctx.emit(EmailVerified {
            user_id: user.id.clone(),
        });
        self.ctx.emit(UserClaimed {
            user_id: user.id.clone(),
        });
        self.find_user_by_id(&user.id)
            .await?
            .ok_or_else(|| AuthError::internal("user deleted while claiming it"))
    }

    pub async fn revoke_session(&self, user_id: &Id, session_id: &Id) -> Result<bool, AuthError> {
        let deleted = self
            .ctx
            .db()
            .delete::<Session>(Session::ID.eq(session_id).and(Session::USER_ID.eq(user_id)))
            .await?;
        if deleted > 0 {
            self.ctx.emit(SessionRevoked {
                user_id: user_id.clone(),
                session_id: Some(session_id.clone()),
            });
        }
        Ok(deleted > 0)
    }

    /// Deletes every session of a user, except `keep` when given.
    pub async fn revoke_user_sessions(
        &self,
        user_id: &Id,
        keep: Option<&Id>,
    ) -> Result<u64, AuthError> {
        let mut filter = Session::USER_ID.eq(user_id);
        if let Some(keep) = keep {
            filter = filter.and(Session::ID.ne(keep));
        }
        let deleted = self.ctx.db().delete::<Session>(filter).await?;
        if deleted > 0 {
            self.ctx.emit(SessionRevoked {
                user_id: user_id.clone(),
                session_id: None,
            });
        }
        Ok(deleted)
    }

    pub fn clear_session_cookies(&self) {
        clear_session_cookies(self.ctx.config(), &self.ctx.effects);
    }

    /// Stores a single-use value and returns it. Only its hash is kept.
    pub async fn create_verification(
        &self,
        identifier: &str,
        ttl: Duration,
        data: Option<String>,
    ) -> Result<String, AuthError> {
        let token = random_token();
        let now = self.ctx.now();
        let verification = Verification {
            id: self.ctx.new_id()?,
            identifier: identifier.to_owned(),
            value_hash: Hash::sha256(&token),
            data,
            expires_at: now + ttl,
            created_at: now,
            updated_at: now,
        };
        self.ctx.db().insert(&verification).await?;
        Ok(token)
    }

    /// Stores a caller-chosen value (e.g. a 6-digit OTP) under `identifier`, replacing older ones.
    pub async fn store_verification_value(
        &self,
        identifier: &str,
        value: &str,
        ttl: Duration,
        data: Option<String>,
    ) -> Result<(), AuthError> {
        self.ctx
            .db()
            .delete::<Verification>(Verification::IDENTIFIER.eq(identifier))
            .await?;
        let now = self.ctx.now();
        let verification = Verification {
            id: self.ctx.new_id()?,
            identifier: identifier.to_owned(),
            value_hash: Hash::sha256(format!("{identifier}:{value}")),
            data,
            expires_at: now + ttl,
            created_at: now,
            updated_at: now,
        };
        self.ctx.db().insert(&verification).await?;
        Ok(())
    }

    /// Consumes a value stored with `store_verification_value`.
    pub async fn consume_verification_value(
        &self,
        identifier: &str,
        value: &str,
    ) -> Result<Option<Verification>, AuthError> {
        self.consume_hash(Hash::sha256(format!("{identifier}:{value}")))
            .await
    }

    /// The unexpired verification for `token`, without consuming it.
    pub async fn find_verification(&self, token: &str) -> Result<Option<Verification>, AuthError> {
        let found = self
            .ctx
            .db()
            .find::<Verification>(Verification::VALUE_HASH.eq(Hash::sha256(token)))
            .one()
            .await?;
        Ok(found.filter(|v| v.expires_at > self.ctx.now()))
    }

    /// Finds and deletes the verification for `token`. `None` if unknown or expired.
    pub async fn consume_verification(
        &self,
        token: &str,
    ) -> Result<Option<Verification>, AuthError> {
        self.consume_hash(Hash::sha256(token)).await
    }

    async fn consume_hash(&self, hash: Hash) -> Result<Option<Verification>, AuthError> {
        let Some(found) = self
            .ctx
            .db()
            .find::<Verification>(Verification::VALUE_HASH.eq(hash))
            .one()
            .await?
        else {
            return Ok(None);
        };
        let deleted = self
            .ctx
            .db()
            .delete::<Verification>(Verification::ID.eq(&found.id))
            .await?;
        Ok((deleted == 1 && found.expires_at > self.ctx.now()).then_some(found))
    }

    /// Whether a mailer is configured, so `send_email` can succeed.
    pub fn can_send_email(&self) -> bool {
        self.ctx.mailer().is_some()
    }

    pub async fn send_email(&self, email: AuthEmail) -> Result<(), AuthError> {
        let Some(mailer) = self.ctx.mailer() else {
            return Err(AuthError::internal(
                "an email needs sending but no mailer is configured",
            ));
        };
        mailer.send(email).await.map_err(AuthError::internal)
    }

    /// Sends `email` without waiting for the mailer, so the route answers in the same time
    /// whether or not it sent anything. Use it where sending depends on whether an account
    /// exists. A failed send is logged (`tracing::error!`), not returned.
    pub async fn send_email_in_background(&self, email: AuthEmail) -> Result<(), AuthError> {
        let Some(mailer) = self.ctx.mailer() else {
            return Err(AuthError::internal(
                "an email needs sending but no mailer is configured",
            ));
        };
        crate::mail::send_in_background(mailer.clone(), email).await;
        Ok(())
    }

    /// The database work of creating and looking up a verification token, for the branch
    /// of a route that sends nothing, so both branches take about as long.
    pub async fn simulate_verification(&self) -> Result<(), AuthError> {
        let token = random_token();
        self.find_verification(&token).await?;
        Ok(())
    }

    /// Creates a verification token and emails the link in the background (see
    /// `send_email_in_background`).
    pub async fn send_verification_email(
        &self,
        user: &UserView,
        callback_url: Option<&str>,
    ) -> Result<(), AuthError> {
        let ttl = self.ctx.config().email_verification.ttl;
        let token = self
            .create_verification(&format!("email-verification:{}", user.id), ttl, None)
            .await?;
        let mut url = format!(
            "{}/verify-email?token={token}",
            self.ctx.config().auth_url()
        );
        if let Some(callback) = callback_url {
            url.push_str(&format!("&callbackURL={}", url_encode(callback)));
        }
        self.send_email_in_background(AuthEmail::VerifyEmail(VerifyEmail {
            to: user.email.clone(),
            name: user.name.clone(),
            url,
            token,
        }))
        .await
    }
}