acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
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
//! User model and authentication types
//!
//! Provides the core user model with email validation, password hashing,
//! and database integration via SQLx.
//!
//! # Example
//!
//! ```rust,no_run
//! use acton_htmx::auth::user::{User, CreateUser, EmailAddress};
//!
//! # async fn example() -> anyhow::Result<()> {
//! // Create a new user
//! let email = EmailAddress::parse("user@example.com")?;
//! let create_user = CreateUser {
//!     email,
//!     password: "secure-password".to_string(),
//! };
//!
//! // Hash password and save to database
//! // let user = User::create(create_user, &pool).await?;
//! # Ok(())
//! # }
//! ```

use crate::auth::password::{hash_password, verify_password, PasswordError};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Type};
use thiserror::Error;
use validator::Validate;

/// User authentication errors
#[derive(Debug, Error)]
pub enum UserError {
    /// Invalid email address format
    #[error("Invalid email address: {0}")]
    InvalidEmail(String),

    /// Password too weak
    #[error("Password does not meet requirements: {0}")]
    WeakPassword(String),

    /// Validation failed
    #[error("Validation error: {0}")]
    ValidationFailed(String),

    /// Password hashing failed
    #[error("Password hashing failed: {0}")]
    PasswordHashingFailed(#[from] PasswordError),

    /// Database operation failed
    #[error("Database error: {0}")]
    DatabaseError(#[from] sqlx::Error),

    /// User not found
    #[error("User not found")]
    NotFound,

    /// Invalid credentials
    #[error("Invalid email or password")]
    InvalidCredentials,
}

/// Email address newtype for validation
///
/// Ensures all email addresses in the system are valid.
///
/// # Example
///
/// ```rust
/// use acton_htmx::auth::user::EmailAddress;
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let email = EmailAddress::parse("user@example.com")?;
/// assert_eq!(email.as_str(), "user@example.com");
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Type)]
#[serde(transparent)]
#[sqlx(transparent)]
pub struct EmailAddress(String);

impl EmailAddress {
    /// Parse and validate an email address
    ///
    /// # Errors
    ///
    /// Returns error if email format is invalid
    ///
    /// # Example
    ///
    /// ```rust
    /// use acton_htmx::auth::user::EmailAddress;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let email = EmailAddress::parse("user@example.com")?;
    /// assert_eq!(email.as_str(), "user@example.com");
    ///
    /// let invalid = EmailAddress::parse("not-an-email");
    /// assert!(invalid.is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn parse(email: impl Into<String>) -> Result<Self, UserError> {
        // Validate with validator crate
        #[derive(Validate)]
        struct EmailValidator {
            #[validate(email)]
            email: String,
        }

        let email = email.into();

        // Basic email validation
        if !email.contains('@') || !email.contains('.') {
            return Err(UserError::InvalidEmail(
                "Email must contain @ and domain".to_string(),
            ));
        }

        let validator = EmailValidator {
            email: email.clone(),
        };

        validator.validate().map_err(|e| {
            UserError::ValidationFailed(format!("Invalid email format: {e}"))
        })?;

        Ok(Self(email.to_lowercase()))
    }

    /// Get the email as a string slice
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Convert into the inner string
    #[must_use]
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl std::fmt::Display for EmailAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for EmailAddress {
    type Err = UserError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

/// User model representing an authenticated user
///
/// This model is designed to be stored in a database and includes
/// all necessary fields for authentication, authorization, and session management.
///
/// # Security Considerations
///
/// - Password hash is stored, never the plaintext password
/// - Email addresses are normalized to lowercase
/// - Created/updated timestamps for audit trail
/// - Roles and permissions for authorization (Cedar policy integration)
///
/// # Database Schema
///
/// ```sql
/// CREATE TABLE users (
///     id BIGSERIAL PRIMARY KEY,
///     email TEXT NOT NULL UNIQUE,
///     password_hash TEXT NOT NULL,
///     roles TEXT[] NOT NULL DEFAULT '{"user"}',
///     permissions TEXT[] NOT NULL DEFAULT '{}',
///     email_verified BOOLEAN NOT NULL DEFAULT FALSE,
///     created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
///     updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
/// );
///
/// CREATE INDEX idx_users_email ON users(email);
/// CREATE INDEX idx_users_roles ON users USING GIN(roles);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct User {
    /// User ID (primary key)
    pub id: i64,

    /// Email address (unique, normalized to lowercase)
    #[serde(serialize_with = "serialize_email")]
    #[serde(deserialize_with = "deserialize_email")]
    pub email: EmailAddress,

    /// Argon2id password hash (never exposed in responses)
    #[serde(skip_serializing)]
    pub password_hash: String,

    /// User roles for authorization
    /// Common roles: "user", "admin", "moderator"
    /// Used by Cedar policy engine for role-based access control (RBAC)
    pub roles: Vec<String>,

    /// User permissions for fine-grained authorization
    /// Format: "resource:action" (e.g., "posts:create", "posts:delete")
    /// Used by Cedar policy engine for attribute-based access control (ABAC)
    pub permissions: Vec<String>,

    /// Email verification status
    /// Required for certain actions (e.g., posting content)
    pub email_verified: bool,

    /// Timestamp when user was created
    pub created_at: DateTime<Utc>,

    /// Timestamp when user was last updated
    pub updated_at: DateTime<Utc>,
}

// Custom serialization for EmailAddress in User struct
fn serialize_email<S>(email: &EmailAddress, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_str(email.as_str())
}

fn deserialize_email<'de, D>(deserializer: D) -> Result<EmailAddress, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    EmailAddress::parse(s).map_err(serde::de::Error::custom)
}

impl User {
    /// Verify a password against this user's hash
    ///
    /// Uses constant-time comparison to prevent timing attacks.
    ///
    /// # Errors
    ///
    /// Returns error if verification fails
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use acton_htmx::auth::user::User;
    /// # async fn example(user: User) -> anyhow::Result<()> {
    /// if user.verify_password("user-password")? {
    ///     println!("Password correct!");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn verify_password(&self, password: &str) -> Result<bool, PasswordError> {
        verify_password(password, &self.password_hash)
    }

    /// Create a new user with hashed password
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Password hashing fails
    /// - Database operation fails
    /// - Email already exists (unique constraint)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use acton_htmx::auth::user::{User, CreateUser, EmailAddress};
    /// use sqlx::PgPool;
    ///
    /// # async fn example(pool: &PgPool) -> anyhow::Result<()> {
    /// let email = EmailAddress::parse("new@example.com")?;
    /// let create = CreateUser {
    ///     email,
    ///     password: "secure-password".to_string(),
    /// };
    ///
    /// let user = User::create(create, pool).await?;
    /// println!("Created user with ID: {}", user.id);
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "postgres")]
    pub async fn create(
        data: CreateUser,
        pool: &sqlx::PgPool,
    ) -> Result<Self, UserError> {
        // Validate password strength
        validate_password_strength(&data.password)?;

        // Hash password
        let password_hash = hash_password(&data.password)?;

        // Insert into database with default role "user"
        let user = sqlx::query_as::<_, Self>(
            r"
            INSERT INTO users (email, password_hash, roles, permissions, email_verified)
            VALUES ($1, $2, $3, $4, $5)
            RETURNING id, email, password_hash, roles, permissions, email_verified, created_at, updated_at
            ",
        )
        .bind(data.email.as_str())
        .bind(&password_hash)
        .bind(vec!["user".to_string()]) // Default role
        .bind(Vec::<String>::new()) // Empty permissions
        .bind(false) // Email not verified
        .fetch_one(pool)
        .await?;

        Ok(user)
    }

    /// Find a user by email
    ///
    /// # Errors
    ///
    /// Returns error if database operation fails or user not found
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use acton_htmx::auth::user::{User, EmailAddress};
    /// use sqlx::PgPool;
    ///
    /// # async fn example(pool: &PgPool) -> anyhow::Result<()> {
    /// let email = EmailAddress::parse("user@example.com")?;
    /// let user = User::find_by_email(&email, pool).await?;
    /// println!("Found user: {}", user.email);
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "postgres")]
    pub async fn find_by_email(
        email: &EmailAddress,
        pool: &sqlx::PgPool,
    ) -> Result<Self, UserError> {
        let user = sqlx::query_as::<_, Self>(
            r"
            SELECT id, email, password_hash, roles, permissions, email_verified, created_at, updated_at
            FROM users
            WHERE email = $1
            ",
        )
        .bind(email.as_str())
        .fetch_optional(pool)
        .await?
        .ok_or(UserError::NotFound)?;

        Ok(user)
    }

    /// Find a user by ID
    ///
    /// # Errors
    ///
    /// Returns error if database operation fails or user not found
    #[cfg(feature = "postgres")]
    pub async fn find_by_id(id: i64, pool: &sqlx::PgPool) -> Result<Self, UserError> {
        let user = sqlx::query_as::<_, Self>(
            r"
            SELECT id, email, password_hash, roles, permissions, email_verified, created_at, updated_at
            FROM users
            WHERE id = $1
            ",
        )
        .bind(id)
        .fetch_optional(pool)
        .await?
        .ok_or(UserError::NotFound)?;

        Ok(user)
    }

    /// Authenticate a user with email and password
    ///
    /// # Errors
    ///
    /// Returns `UserError::InvalidCredentials` if:
    /// - Email not found
    /// - Password incorrect
    ///
    /// Returns other errors for database or verification failures
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use acton_htmx::auth::user::{User, EmailAddress};
    /// use sqlx::PgPool;
    ///
    /// # async fn example(pool: &PgPool) -> anyhow::Result<()> {
    /// let email = EmailAddress::parse("user@example.com")?;
    /// match User::authenticate(&email, "password", pool).await {
    ///     Ok(user) => println!("Authenticated: {}", user.email),
    ///     Err(_) => println!("Invalid credentials"),
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "postgres")]
    pub async fn authenticate(
        email: &EmailAddress,
        password: &str,
        pool: &sqlx::PgPool,
    ) -> Result<Self, UserError> {
        // Find user by email
        let user = Self::find_by_email(email, pool)
            .await
            .map_err(|_| UserError::InvalidCredentials)?;

        // Verify password
        let valid = user
            .verify_password(password)
            .map_err(|_| UserError::InvalidCredentials)?;

        if !valid {
            return Err(UserError::InvalidCredentials);
        }

        Ok(user)
    }
}

/// Data for creating a new user
///
/// # Example
///
/// ```rust
/// use acton_htmx::auth::user::{CreateUser, EmailAddress};
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let create = CreateUser {
///     email: EmailAddress::parse("new@example.com")?,
///     password: "secure-password".to_string(),
/// };
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Validate)]
pub struct CreateUser {
    /// User's email address
    pub email: EmailAddress,

    /// Plaintext password (will be hashed before storage)
    #[validate(length(min = 8, message = "Password must be at least 8 characters"))]
    pub password: String,
}

/// Validate password strength
///
/// # Requirements
///
/// - At least 8 characters
/// - At least one uppercase letter
/// - At least one lowercase letter
/// - At least one digit
///
/// # Errors
///
/// Returns error if password does not meet requirements
fn validate_password_strength(password: &str) -> Result<(), UserError> {
    if password.len() < 8 {
        return Err(UserError::WeakPassword(
            "Password must be at least 8 characters".to_string(),
        ));
    }

    let has_uppercase = password.chars().any(char::is_uppercase);
    let has_lowercase = password.chars().any(char::is_lowercase);
    let has_digit = password.chars().any(|c| c.is_ascii_digit());

    if !has_uppercase {
        return Err(UserError::WeakPassword(
            "Password must contain at least one uppercase letter".to_string(),
        ));
    }

    if !has_lowercase {
        return Err(UserError::WeakPassword(
            "Password must contain at least one lowercase letter".to_string(),
        ));
    }

    if !has_digit {
        return Err(UserError::WeakPassword(
            "Password must contain at least one digit".to_string(),
        ));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_email_address_parsing() {
        // Valid emails
        assert!(EmailAddress::parse("user@example.com").is_ok());
        assert!(EmailAddress::parse("user.name@example.co.uk").is_ok());
        assert!(EmailAddress::parse("user+tag@example.com").is_ok());

        // Invalid emails
        assert!(EmailAddress::parse("not-an-email").is_err());
        assert!(EmailAddress::parse("@example.com").is_err());
        assert!(EmailAddress::parse("user@").is_err());
        assert!(EmailAddress::parse("user").is_err());
    }

    #[test]
    fn test_email_normalization() {
        let email1 = EmailAddress::parse("User@Example.COM").unwrap();
        let email2 = EmailAddress::parse("user@example.com").unwrap();

        assert_eq!(email1, email2);
        assert_eq!(email1.as_str(), "user@example.com");
    }

    #[test]
    fn test_password_strength_validation() {
        // Valid passwords
        assert!(validate_password_strength("SecurePass123").is_ok());
        assert!(validate_password_strength("MyP@ssw0rd").is_ok());

        // Too short
        assert!(validate_password_strength("Pass1").is_err());

        // Missing uppercase
        assert!(matches!(
            validate_password_strength("password123"),
            Err(UserError::WeakPassword(_))
        ));

        // Missing lowercase
        assert!(matches!(
            validate_password_strength("PASSWORD123"),
            Err(UserError::WeakPassword(_))
        ));

        // Missing digit
        assert!(matches!(
            validate_password_strength("PasswordOnly"),
            Err(UserError::WeakPassword(_))
        ));
    }

    #[test]
    fn test_user_password_verification() {
        let password = "TestPassword123";
        let hash = hash_password(password).expect("Failed to hash password");

        let user = User {
            id: 1,
            email: EmailAddress::parse("test@example.com").unwrap(),
            password_hash: hash,
            roles: vec!["user".to_string()],
            permissions: vec![],
            email_verified: false,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert!(user.verify_password(password).expect("Verification failed"));
        assert!(!user
            .verify_password("wrong-password")
            .expect("Verification failed"));
    }

    #[test]
    fn test_email_serialization() {
        let email = EmailAddress::parse("test@example.com").unwrap();
        let json = serde_json::to_string(&email).expect("Failed to serialize");
        assert_eq!(json, r#""test@example.com""#);

        let deserialized: EmailAddress =
            serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(deserialized, email);
    }

    #[test]
    fn test_user_serialization_skips_password() {
        let user = User {
            id: 1,
            email: EmailAddress::parse("test@example.com").unwrap(),
            password_hash: "hash".to_string(),
            roles: vec!["user".to_string()],
            permissions: vec![],
            email_verified: false,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let json = serde_json::to_string(&user).expect("Failed to serialize");
        assert!(!json.contains("password_hash"));
        assert!(json.contains("test@example.com"));
    }
}