Skip to main content

doido_auth/
user.rs

1//! Generic authenticated subject contract — apps implement this on their User model.
2
3use doido_core::Result;
4use doido_model::password::HasSecurePassword;
5use doido_model::sea_orm::DatabaseConnection;
6use serde::de::DeserializeOwned;
7use serde::Serialize;
8use std::future::Future;
9
10/// The contract your User SeaORM model implements (manually or via `#[auth_user]`).
11pub trait AuthUser: Clone + Send + Sync + 'static {
12    /// Primary key type (`i64`, `Uuid`, …).
13    type Id: Clone
14        + Send
15        + Sync
16        + std::fmt::Debug
17        + Serialize
18        + DeserializeOwned
19        + Into<serde_json::Value>
20        + 'static;
21
22    fn id(&self) -> Self::Id;
23    fn email(&self) -> &str;
24    fn password_digest(&self) -> Option<&str>;
25
26    fn find_by_email(
27        db: &DatabaseConnection,
28        email: &str,
29    ) -> impl Future<Output = Result<Option<Self>>> + Send;
30
31    fn find_by_id(
32        db: &DatabaseConnection,
33        id: Self::Id,
34    ) -> impl Future<Output = Result<Option<Self>>> + Send;
35}
36
37/// Verify `password` against `user`'s stored digest when present.
38pub fn authenticate_password<U: AuthUser + HasSecurePassword>(user: &U, password: &str) -> bool {
39    user.authenticate(password)
40}