aurelin 0.4.2

Aurelin is a Rust translation of Laravel
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
use std::{
    sync::Arc,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use aurelin_fortify::{
    CreatesNewUsers, EmailVerification, EmailVerificationError, EmailVerificationRateLimiter,
    EmailVerificationRequest, LoginRateLimiter, PasskeyRateLimiter, RegistrationInput,
    ResetPasswords, ResetPasswordsError, TwoFactorAuthentication, TwoFactorError,
    TwoFactorRateLimiter,
    passkeys::{PasskeyConfig, PasskeyError, Passkeys},
};
use aurelin_framework::auth::{
    BcryptPasswordHasher, FixedWindowRateLimiter, PasswordHashError, PasswordResetBroker,
    PasswordResetCredentials, PasswordResetError, PasswordResetRepository, PasswordResetRequest,
    RateLimitOutcome, StoredPasswordResetToken,
};
use aurelin_framework::events::EventDispatcher;
use aurelin_framework::mail::{MailError, Mailer};
use axum_login::{AuthUser, AuthnBackend, UserId};
use sea_orm::{
    ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, DbErr, EntityTrait,
    QueryFilter, sea_query::OnConflict,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{
    actions::{
        CreateNewUser, CreateNewUserError, CreateUser, ResetUserPassword, ResetUserPasswordError,
    },
    config::password_policy::{PasswordPolicy, PasswordPolicyViolation},
    entities::{password_reset_token, user},
};

pub type AuthSession = axum_login::AuthSession<AuthBackend>;
pub type User = user::Model;

mod fortify;
mod notifications;
mod passkeys;
mod settings;
mod two_factor;

pub use settings::ProfileUpdate;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PublicUser {
    pub id: i64,
    pub name: String,
    pub email: String,
    pub email_verified_at: Option<i64>,
}

impl From<&User> for PublicUser {
    fn from(user: &User) -> Self {
        Self {
            id: user.id,
            name: user.name.clone(),
            email: user.email.clone(),
            email_verified_at: user.email_verified_at,
        }
    }
}

impl AuthUser for User {
    type Id = i64;

    fn id(&self) -> Self::Id {
        self.id
    }

    fn session_auth_hash(&self) -> &[u8] {
        self.password_hash.as_bytes()
    }
}

pub use aurelin_fortify::axum::Credentials;

#[derive(Clone, Debug)]
pub struct AuthBackend {
    pub(super) database: DatabaseConnection,
    dummy_password_hash: Arc<String>,
    passwords: BcryptPasswordHasher,
    attempts: LoginRateLimiter,
    authentication_timebox_duration: Duration,
    authentication_events: Option<EventDispatcher>,
    password_reset_tokens: PasswordResetBroker<AuthRepository>,
    reset_passwords: ResetPasswords<AuthRepository, ResetUserPassword>,
    create_new_user: CreateNewUser,
    email_verification: EmailVerification,
    email_verification_attempts: EmailVerificationRateLimiter,
    password_policy: PasswordPolicy,
    password_update_attempts: FixedWindowRateLimiter,
    mailer: Mailer,
    application_url: Arc<String>,
    pub(super) two_factor: TwoFactorAuthentication,
    pub(super) two_factor_attempts: TwoFactorRateLimiter,
    pub(super) passkeys: Passkeys,
    passkey_attempts: PasskeyRateLimiter,
}

impl AuthBackend {
    pub async fn new(
        database: DatabaseConnection,
        application_key: &[u8],
        application_environment: &str,
        application_url: &str,
        mailer: Mailer,
    ) -> Result<Self, AuthError> {
        let passwords = BcryptPasswordHasher::default();
        let dummy_password_hash = passwords.hash("aurelin-invalid-credentials").await?;
        Ok(Self {
            password_reset_tokens: PasswordResetBroker::new(AuthRepository(database.clone())),
            reset_passwords: ResetPasswords::new(
                AuthRepository(database.clone()),
                ResetUserPassword::new(database.clone(), passwords),
            ),
            create_new_user: CreateNewUser::new(database.clone(), passwords),
            email_verification: EmailVerification::new(application_key)?,
            email_verification_attempts: EmailVerificationRateLimiter::default(),
            password_policy: PasswordPolicy::for_environment(application_environment),
            password_update_attempts: FixedWindowRateLimiter::new(6, Duration::from_secs(60)),
            mailer,
            application_url: Arc::new(application_url.trim_end_matches('/').to_owned()),
            two_factor: TwoFactorAuthentication::new(application_key, "Aurelin")?,
            two_factor_attempts: TwoFactorRateLimiter::default(),
            passkeys: Passkeys::new(PasskeyConfig::for_origin(
                "Aurelin",
                application_url,
                application_key,
            )?)?,
            passkey_attempts: PasskeyRateLimiter::default(),
            database,
            dummy_password_hash: Arc::new(dummy_password_hash),
            passwords,
            attempts: LoginRateLimiter::default(),
            authentication_timebox_duration:
                aurelin_fortify::axum::DEFAULT_AUTHENTICATION_TIMEBOX_DURATION,
            authentication_events: None,
        })
    }

    pub fn with_authentication_timebox_duration(mut self, duration: Duration) -> Self {
        self.authentication_timebox_duration = duration;
        self
    }

    /// Register application-owned observational authentication listeners.
    pub fn with_authentication_events(mut self, events: EventDispatcher) -> Self {
        self.authentication_events = Some(events);
        self
    }

    pub async fn create_user(
        &self,
        name: &str,
        email: &str,
        password: &str,
    ) -> Result<CreateUser, AuthError> {
        Ok(self
            .create_new_user
            .create(RegistrationInput {
                name: name.to_owned(),
                email: email.to_owned(),
                password: password.to_owned(),
            })
            .await?)
    }

    pub async fn login_retry_after(&self, key: &str) -> Option<Duration> {
        self.attempts.retry_after(key).await
    }

    pub async fn record_failed_login(&self, key: &str) -> RateLimitOutcome {
        self.attempts.attempt(key).await
    }

    pub async fn clear_failed_logins(&self, key: &str) {
        self.attempts.clear(key).await;
    }

    pub async fn validate_password(&self, password: &str) -> Result<(), PasswordPolicyViolation> {
        self.password_policy.validate(password).await
    }

    pub fn password_rules(&self) -> &'static str {
        self.password_policy.browser_rules()
    }

    pub fn application_url(&self) -> &str {
        &self.application_url
    }

    pub async fn create_password_reset_token(
        &self,
        email: &str,
    ) -> Result<PasswordResetRequest, AuthError> {
        let email = normalize_email(email);
        Ok(self
            .password_reset_tokens
            .create_token(&email, unix_timestamp()?)
            .await?)
    }

    pub async fn reset_password(
        &self,
        email: &str,
        token: &str,
        password: &str,
    ) -> Result<bool, AuthError> {
        let email = normalize_email(email);
        Ok(self
            .reset_passwords
            .reset(
                PasswordResetCredentials {
                    email: &email,
                    token,
                    password,
                },
                unix_timestamp()?,
            )
            .await?)
    }

    pub fn email_verification_url(&self, user: &User) -> Result<String, AuthError> {
        Ok(self
            .email_verification
            .url(user.id, &user.email, unix_timestamp()?)?)
    }

    pub async fn verify_email(
        &self,
        user: &User,
        email_hash: &str,
        expires: i64,
        signature: &str,
    ) -> Result<bool, AuthError> {
        if !self.email_verification.verify(
            EmailVerificationRequest {
                user_id: user.id,
                email: &user.email,
                email_hash,
                expires,
                signature,
            },
            unix_timestamp()?,
        )? {
            return Ok(false);
        }
        if user.email_verified_at.is_some() {
            return Ok(true);
        }
        let mut user: user::ActiveModel = user.clone().into();
        user.email_verified_at = Set(Some(unix_timestamp()?));
        user.updated_at = Set(unix_timestamp()?);
        user.update(&self.database).await?;
        Ok(true)
    }

    pub async fn email_verification_attempt(&self, user_id: i64) -> RateLimitOutcome {
        self.email_verification_attempts.attempt(user_id).await
    }

    pub async fn passkey_attempt(&self, key: &str) -> RateLimitOutcome {
        self.passkey_attempts.attempt(key).await
    }
}

impl AuthnBackend for AuthBackend {
    type User = User;
    type Credentials = Credentials;
    type Error = AuthError;

    async fn authenticate(
        &self,
        credentials: Self::Credentials,
    ) -> Result<Option<Self::User>, Self::Error> {
        let user = user::Entity::find()
            .filter(user::Column::Email.eq(normalize_email(&credentials.email)))
            .one(&self.database)
            .await?;
        let hash = user
            .as_ref()
            .map_or_else(
                || self.dummy_password_hash.as_str(),
                |user| user.password_hash.as_str(),
            )
            .to_owned();
        if !self.passwords.verify(&credentials.password, &hash).await? {
            return Ok(None);
        }
        let Some(user) = user else {
            return Ok(None);
        };
        if !self.passwords.needs_rehash(&user.password_hash)? {
            return Ok(Some(user));
        }
        let mut active: user::ActiveModel = user.into();
        active.password_hash = Set(self.passwords.hash(&credentials.password).await?);
        active.updated_at = Set(unix_timestamp()?);
        Ok(Some(active.update(&self.database).await?))
    }

    async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
        Ok(user::Entity::find_by_id(*user_id)
            .one(&self.database)
            .await?)
    }
}

#[derive(Debug, Error)]
pub enum AuthError {
    #[error("database operation failed")]
    Database(#[from] DbErr),
    #[error(transparent)]
    Password(#[from] PasswordHashError),
    #[error(transparent)]
    PasswordReset(#[from] PasswordResetError<DbErr>),
    #[error(transparent)]
    ResetPasswords(#[from] ResetPasswordsError<DbErr, ResetUserPasswordError>),
    #[error(transparent)]
    CreateNewUser(#[from] CreateNewUserError),
    #[error(transparent)]
    EmailVerification(#[from] EmailVerificationError),
    #[error(transparent)]
    TwoFactor(#[from] TwoFactorError),
    #[error(transparent)]
    Passkey(#[from] PasskeyError),
    #[error(transparent)]
    Mail(#[from] MailError),
    #[error("this passkey is already registered")]
    PasskeyAlreadyRegistered,
    #[error("system clock produced an invalid timestamp")]
    InvalidTimestamp,
    #[error("system clock is before the Unix epoch")]
    Clock(#[from] std::time::SystemTimeError),
}

#[derive(Clone, Debug)]
struct AuthRepository(DatabaseConnection);

impl PasswordResetRepository for AuthRepository {
    type Error = DbErr;

    async fn user_exists(&self, email: &str) -> Result<bool, Self::Error> {
        Ok(user::Entity::find()
            .filter(user::Column::Email.eq(email))
            .one(&self.0)
            .await?
            .is_some())
    }

    async fn token(&self, email: &str) -> Result<Option<StoredPasswordResetToken>, Self::Error> {
        Ok(password_reset_token::Entity::find_by_id(email)
            .one(&self.0)
            .await?
            .map(|token| StoredPasswordResetToken {
                digest: token.token_hash,
                created_at: token.created_at,
            }))
    }

    async fn replace_token(
        &self,
        email: &str,
        token: StoredPasswordResetToken,
    ) -> Result<(), Self::Error> {
        password_reset_token::Entity::insert(password_reset_token::ActiveModel {
            email: Set(email.to_owned()),
            token_hash: Set(token.digest),
            created_at: Set(token.created_at),
        })
        .on_conflict(
            OnConflict::column(password_reset_token::Column::Email)
                .update_columns([
                    password_reset_token::Column::TokenHash,
                    password_reset_token::Column::CreatedAt,
                ])
                .to_owned(),
        )
        .exec(&self.0)
        .await?;
        Ok(())
    }

    async fn delete_token(&self, email: &str, expected_digest: &str) -> Result<bool, Self::Error> {
        let result = password_reset_token::Entity::delete_many()
            .filter(password_reset_token::Column::Email.eq(email))
            .filter(password_reset_token::Column::TokenHash.eq(expected_digest))
            .exec(&self.0)
            .await?;
        Ok(result.rows_affected == 1)
    }
}

fn normalize_email(email: &str) -> String {
    email.to_lowercase()
}

pub(super) fn unix_timestamp() -> Result<i64, std::time::SystemTimeError> {
    Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64)
}