acton-service 0.23.0

Production-ready Rust backend framework with type-enforced API versioning
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! Account lifecycle management (NIST SP 800-53 AC-2)
//!
//! Provides account CRUD, lifecycle state management, email verification,
//! password management, and notification hooks for provisioning/deprovisioning.
//!
//! # Feature Dependencies
//!
//! Requires: `accounts` (which enables `auth`)
//!
//! # Architecture
//!
//! - **AccountService**: Central orchestrator for all account operations
//! - **AccountStorage**: Pluggable persistence (PostgreSQL, Turso, SurrealDB)
//! - **AccountNotification**: Event hooks for lifecycle changes
//! - **Audit integration**: Bridges account events to the audit log (when `audit` active)

pub mod config;
pub mod error;
#[cfg(feature = "account-handlers")]
pub mod handlers;
pub mod notification;
pub mod storage;
pub mod types;

pub use config::AccountsConfig;
pub use error::AccountError;
pub use notification::{AccountEvent, AccountNotification};
pub use storage::AccountStorage;
pub use types::{Account, AccountId, AccountIdError, AccountStatus, CreateAccount, UpdateAccount};

use chrono::Utc;
use std::sync::Arc;

use crate::auth::PasswordHasher;
use crate::error::Error;

/// Central service for account lifecycle management
///
/// Orchestrates storage, password hashing, validation, and notification
/// dispatch for all account operations.
#[derive(Clone)]
pub struct AccountService {
    storage: Arc<dyn AccountStorage>,
    hasher: PasswordHasher,
    config: AccountsConfig,
    notifications: Vec<Arc<dyn AccountNotification>>,
}

impl AccountService {
    /// Create a new account service
    pub fn new(
        storage: Arc<dyn AccountStorage>,
        hasher: PasswordHasher,
        config: AccountsConfig,
    ) -> Self {
        Self {
            storage,
            hasher,
            config,
            notifications: Vec::new(),
        }
    }

    /// Register a notification handler
    pub fn with_notification(mut self, handler: Arc<dyn AccountNotification>) -> Self {
        self.notifications.push(handler);
        self
    }

    /// Register the audit notification bridge (when `audit` feature is active)
    #[cfg(feature = "audit")]
    pub fn with_audit(self, audit_logger: crate::audit::AuditLogger) -> Self {
        let handler = Arc::new(AuditAccountNotification::new(audit_logger));
        self.with_notification(handler)
    }

    /// Create a new account
    ///
    /// - Validates email format and normalizes to lowercase
    /// - Checks for duplicate email
    /// - Hashes password (if provided)
    /// - Sets initial status per config
    pub async fn create_account(&self, data: CreateAccount) -> Result<Account, AccountError> {
        // Validate email format
        let email = data.email.trim().to_lowercase();
        if !is_valid_email(&email) {
            return Err(AccountError::Validation(format!(
                "invalid email format: {}",
                email
            )));
        }

        // Check for duplicate email
        if self
            .storage
            .get_by_email(&email)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?
            .is_some()
        {
            return Err(AccountError::AlreadyExists(email));
        }

        // Check for duplicate username if configured
        if self.config.unique_usernames {
            if let Some(ref username) = data.username {
                if self
                    .storage
                    .get_by_username(username)
                    .await
                    .map_err(|e| AccountError::Storage(e.to_string()))?
                    .is_some()
                {
                    return Err(AccountError::AlreadyExists(format!(
                        "username: {}",
                        username
                    )));
                }
            }
        }

        // Hash password
        let password_hash = if let Some(ref password) = data.password {
            Some(
                self.hasher
                    .hash(password)
                    .map_err(|e| AccountError::Validation(e.to_string()))?,
            )
        } else {
            None
        };

        // Determine initial status
        let require_verification = data
            .require_email_verification
            .unwrap_or(self.config.require_email_verification);

        let status = if require_verification {
            AccountStatus::PendingVerification
        } else {
            AccountStatus::Active
        };

        let now = Utc::now();
        let account = Account {
            id: AccountId::new(),
            email: email.clone(),
            username: data.username,
            password_hash,
            status,
            roles: data.roles,
            email_verified: !require_verification,
            email_verified_at: if !require_verification {
                Some(now)
            } else {
                None
            },
            last_login_at: None,
            locked_at: None,
            locked_reason: None,
            disabled_at: None,
            disabled_reason: None,
            expires_at: data.expires_at,
            password_changed_at: if data.password.is_some() {
                Some(now)
            } else {
                None
            },
            failed_login_count: 0,
            metadata: data.metadata,
            created_at: now,
            updated_at: now,
        };

        self.storage
            .create(&account)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Created {
            account_id: account.id.to_string(),
            email: account.email.clone(),
        });

        Ok(account)
    }

    /// Get an account by ID
    ///
    /// Checks expiration and auto-transitions to Expired if past `expires_at`.
    pub async fn get_account(&self, id: &str) -> Result<Option<Account>, AccountError> {
        let account = self
            .storage
            .get_by_id(id)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        if let Some(mut acct) = account {
            // Auto-expire if past expiration
            if acct.status != AccountStatus::Expired {
                if let Some(expires_at) = acct.expires_at {
                    if expires_at < Utc::now() {
                        let _ = self
                            .storage
                            .update_status(id, AccountStatus::Expired, None)
                            .await;
                        acct.status = AccountStatus::Expired;

                        self.notify(AccountEvent::Expired {
                            account_id: id.to_string(),
                        });
                    }
                }
            }
            Ok(Some(acct))
        } else {
            Ok(None)
        }
    }

    /// Get an account by email (for login flow)
    pub async fn get_account_by_email(&self, email: &str) -> Result<Option<Account>, AccountError> {
        self.storage
            .get_by_email(email)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))
    }

    /// Update account profile fields
    pub async fn update_account(
        &self,
        id: &str,
        data: UpdateAccount,
    ) -> Result<Account, AccountError> {
        let mut account = self
            .storage
            .get_by_id(id)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?
            .ok_or_else(|| AccountError::NotFound(id.to_string()))?;

        if let Some(email) = data.email {
            let email = email.trim().to_lowercase();
            if !is_valid_email(&email) {
                return Err(AccountError::Validation(format!(
                    "invalid email format: {}",
                    email
                )));
            }
            // Check for duplicate
            if let Some(existing) = self
                .storage
                .get_by_email(&email)
                .await
                .map_err(|e| AccountError::Storage(e.to_string()))?
            {
                if existing.id != account.id {
                    return Err(AccountError::AlreadyExists(email));
                }
            }
            account.email = email;
        }

        if let Some(username) = data.username {
            account.username = Some(username);
        }

        if let Some(roles) = data.roles {
            let old_roles = account.roles.clone();
            account.roles = roles.clone();
            if old_roles != roles {
                self.notify(AccountEvent::RolesUpdated {
                    account_id: id.to_string(),
                    roles,
                });
            }
        }

        if let Some(expires_at) = data.expires_at {
            account.expires_at = expires_at;
        }

        if let Some(metadata) = data.metadata {
            account.metadata = Some(metadata);
        }

        account.updated_at = Utc::now();

        self.storage
            .update(&account)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::ProfileUpdated {
            account_id: id.to_string(),
        });

        Ok(account)
    }

    /// Disable an account (admin action)
    pub async fn disable_account(&self, id: &str, reason: &str) -> Result<(), AccountError> {
        let account = self.require_account(id).await?;
        self.validate_transition(&account, AccountStatus::Disabled)?;

        self.storage
            .update_status(id, AccountStatus::Disabled, Some(reason))
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Disabled {
            account_id: id.to_string(),
            reason: reason.to_string(),
        });

        Ok(())
    }

    /// Enable (re-activate) an account
    pub async fn enable_account(&self, id: &str) -> Result<(), AccountError> {
        let account = self.require_account(id).await?;
        self.validate_transition(&account, AccountStatus::Active)?;

        self.storage
            .update_status(id, AccountStatus::Active, None)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Activated {
            account_id: id.to_string(),
        });

        Ok(())
    }

    /// Lock an account (security event)
    pub async fn lock_account(&self, id: &str, reason: &str) -> Result<(), AccountError> {
        let account = self.require_account(id).await?;
        self.validate_transition(&account, AccountStatus::Locked)?;

        self.storage
            .update_status(id, AccountStatus::Locked, Some(reason))
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Locked {
            account_id: id.to_string(),
            reason: reason.to_string(),
        });

        Ok(())
    }

    /// Unlock an account
    pub async fn unlock_account(&self, id: &str) -> Result<(), AccountError> {
        let account = self.require_account(id).await?;
        self.validate_transition(&account, AccountStatus::Active)?;

        self.storage
            .update_status(id, AccountStatus::Active, None)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Unlocked {
            account_id: id.to_string(),
        });

        Ok(())
    }

    /// Suspend an account
    pub async fn suspend_account(&self, id: &str, reason: &str) -> Result<(), AccountError> {
        let account = self.require_account(id).await?;
        self.validate_transition(&account, AccountStatus::Suspended)?;

        self.storage
            .update_status(id, AccountStatus::Suspended, Some(reason))
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::Suspended {
            account_id: id.to_string(),
            reason: reason.to_string(),
        });

        Ok(())
    }

    /// Verify an account's email address
    pub async fn verify_email(&self, id: &str) -> Result<(), AccountError> {
        let mut account = self.require_account(id).await?;

        if account.email_verified {
            return Ok(()); // Already verified, idempotent
        }

        account.email_verified = true;
        account.email_verified_at = Some(Utc::now());
        account.updated_at = Utc::now();

        // Transition from PendingVerification to Active
        if account.status == AccountStatus::PendingVerification {
            account.status = AccountStatus::Active;
        }

        self.storage
            .update(&account)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::EmailVerified {
            account_id: id.to_string(),
        });

        if account.status == AccountStatus::Active {
            self.notify(AccountEvent::Activated {
                account_id: id.to_string(),
            });
        }

        Ok(())
    }

    /// Change an account's password
    pub async fn change_password(&self, id: &str, new_password: &str) -> Result<(), AccountError> {
        let mut account = self.require_account(id).await?;

        let hash = self
            .hasher
            .hash(new_password)
            .map_err(|e| AccountError::Validation(e.to_string()))?;

        account.password_hash = Some(hash);
        account.password_changed_at = Some(Utc::now());
        account.updated_at = Utc::now();

        self.storage
            .update(&account)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        self.notify(AccountEvent::PasswordChanged {
            account_id: id.to_string(),
        });

        Ok(())
    }

    /// Hard delete an account (GDPR)
    pub async fn delete_account(&self, id: &str) -> Result<(), AccountError> {
        let deleted = self
            .storage
            .delete(id)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?;

        if !deleted {
            return Err(AccountError::NotFound(id.to_string()));
        }

        self.notify(AccountEvent::Deleted {
            account_id: id.to_string(),
        });

        Ok(())
    }

    /// List accounts with optional status filter and pagination
    pub async fn list_accounts(
        &self,
        status: Option<AccountStatus>,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<Account>, AccountError> {
        self.storage
            .list(status, limit, offset)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))
    }

    /// Count accounts with optional status filter
    pub async fn count_accounts(&self, status: Option<AccountStatus>) -> Result<u64, AccountError> {
        self.storage
            .count(status)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))
    }

    /// Authenticate with email and password
    ///
    /// Checks:
    /// 1. Account exists
    /// 2. Account is Active
    /// 3. Password matches
    /// 4. Updates `last_login_at`
    pub async fn authenticate(&self, email: &str, password: &str) -> Result<Account, AccountError> {
        let email = email.trim().to_lowercase();

        let account = self
            .storage
            .get_by_email(&email)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?
            .ok_or_else(|| AccountError::NotFound(email.clone()))?;

        // Check status
        if account.status != AccountStatus::Active {
            let reason = match account.status {
                AccountStatus::PendingVerification => "email not verified".to_string(),
                AccountStatus::Disabled => account
                    .disabled_reason
                    .clone()
                    .unwrap_or_else(|| "administratively disabled".to_string()),
                AccountStatus::Locked => account
                    .locked_reason
                    .clone()
                    .unwrap_or_else(|| "account locked".to_string()),
                AccountStatus::Expired => "account expired".to_string(),
                AccountStatus::Suspended => "account suspended".to_string(),
                _ => "account not active".to_string(),
            };
            return Err(AccountError::AccountInactive {
                status: account.status,
                reason,
            });
        }

        // Check password
        let password_hash = account
            .password_hash
            .as_ref()
            .ok_or(AccountError::InvalidCredentials)?;

        let valid = self
            .hasher
            .verify(password, password_hash)
            .map_err(|e| AccountError::Storage(e.to_string()))?;

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

        // Record login
        let _ = self.storage.record_login(account.id.as_str()).await;

        Ok(account)
    }

    // ========================================================================
    // Internal helpers
    // ========================================================================

    async fn require_account(&self, id: &str) -> Result<Account, AccountError> {
        self.storage
            .get_by_id(id)
            .await
            .map_err(|e| AccountError::Storage(e.to_string()))?
            .ok_or_else(|| AccountError::NotFound(id.to_string()))
    }

    fn validate_transition(
        &self,
        account: &Account,
        target: AccountStatus,
    ) -> Result<(), AccountError> {
        if !account.status.can_transition_to(target) {
            return Err(AccountError::InvalidTransition {
                from: account.status,
                to: target,
            });
        }
        Ok(())
    }

    fn notify(&self, event: AccountEvent) {
        for handler in &self.notifications {
            let handler = handler.clone();
            let event = event.clone();
            tokio::spawn(async move {
                handler.on_event(event).await;
            });
        }
    }
}

/// Convert AccountError to the framework Error type
impl From<AccountError> for Error {
    fn from(err: AccountError) -> Self {
        match &err {
            AccountError::NotFound(_) => Error::NotFound(err.to_string()),
            AccountError::AlreadyExists(_) => Error::Conflict(err.to_string()),
            AccountError::InvalidTransition { .. } => Error::ValidationError(err.to_string()),
            AccountError::InvalidCredentials => Error::Unauthorized(err.to_string()),
            AccountError::AccountInactive { .. } => Error::Forbidden(err.to_string()),
            AccountError::Validation(_) => Error::BadRequest(err.to_string()),
            AccountError::Storage(_) => Error::Internal(err.to_string()),
            AccountError::InvalidId(_) => Error::BadRequest(err.to_string()),
        }
    }
}

/// Basic email format validation (lowercase, contains @, has domain)
fn is_valid_email(email: &str) -> bool {
    let parts: Vec<&str> = email.split('@').collect();
    if parts.len() != 2 {
        return false;
    }
    let local = parts[0];
    let domain = parts[1];
    !local.is_empty() && !domain.is_empty() && domain.contains('.')
}

// ============================================================================
// Audit integration bridge
// ============================================================================

#[cfg(feature = "audit")]
mod audit_integration {
    use async_trait::async_trait;

    use crate::audit::{AuditEvent, AuditEventKind, AuditLogger, AuditSeverity};

    use super::notification::{AccountEvent, AccountNotification};

    /// Notification handler that bridges account events to the audit log
    pub struct AuditAccountNotification {
        audit_logger: AuditLogger,
    }

    impl AuditAccountNotification {
        /// Create a new audit account notification handler
        pub fn new(audit_logger: AuditLogger) -> Self {
            Self { audit_logger }
        }
    }

    #[async_trait]
    impl AccountNotification for AuditAccountNotification {
        async fn on_event(&self, event: AccountEvent) {
            let (kind, severity, metadata) = match event {
                AccountEvent::Created {
                    ref account_id,
                    ref email,
                } => (
                    AuditEventKind::AccountCreated,
                    AuditSeverity::Informational,
                    serde_json::json!({
                        "account_id": account_id,
                        "email": email,
                    }),
                ),
                AccountEvent::Activated { ref account_id } => (
                    AuditEventKind::AccountEnabled,
                    AuditSeverity::Informational,
                    serde_json::json!({ "account_id": account_id }),
                ),
                AccountEvent::Disabled {
                    ref account_id,
                    ref reason,
                } => (
                    AuditEventKind::AccountDisabled,
                    AuditSeverity::Warning,
                    serde_json::json!({
                        "account_id": account_id,
                        "reason": reason,
                    }),
                ),
                AccountEvent::Locked {
                    ref account_id,
                    ref reason,
                } => (
                    AuditEventKind::AccountLocked,
                    AuditSeverity::Warning,
                    serde_json::json!({
                        "account_id": account_id,
                        "reason": reason,
                    }),
                ),
                AccountEvent::Unlocked { ref account_id } => (
                    AuditEventKind::AccountUnlocked,
                    AuditSeverity::Notice,
                    serde_json::json!({ "account_id": account_id }),
                ),
                AccountEvent::Suspended {
                    ref account_id,
                    ref reason,
                } => (
                    AuditEventKind::AccountDisabled,
                    AuditSeverity::Warning,
                    serde_json::json!({
                        "account_id": account_id,
                        "reason": reason,
                        "action": "suspended",
                    }),
                ),
                AccountEvent::Expired { ref account_id } => (
                    AuditEventKind::AccountExpired,
                    AuditSeverity::Notice,
                    serde_json::json!({ "account_id": account_id }),
                ),
                AccountEvent::Deleted { ref account_id } => (
                    AuditEventKind::AccountDeleted,
                    AuditSeverity::Warning,
                    serde_json::json!({ "account_id": account_id }),
                ),
                AccountEvent::EmailVerified { ref account_id } => (
                    AuditEventKind::AccountUpdated,
                    AuditSeverity::Informational,
                    serde_json::json!({
                        "account_id": account_id,
                        "action": "email_verified",
                    }),
                ),
                AccountEvent::PasswordChanged { ref account_id } => (
                    AuditEventKind::AccountUpdated,
                    AuditSeverity::Informational,
                    serde_json::json!({
                        "account_id": account_id,
                        "action": "password_changed",
                    }),
                ),
                AccountEvent::RolesUpdated {
                    ref account_id,
                    ref roles,
                } => (
                    AuditEventKind::AccountUpdated,
                    AuditSeverity::Notice,
                    serde_json::json!({
                        "account_id": account_id,
                        "action": "roles_updated",
                        "roles": roles,
                    }),
                ),
                AccountEvent::ProfileUpdated { ref account_id } => (
                    AuditEventKind::AccountUpdated,
                    AuditSeverity::Informational,
                    serde_json::json!({
                        "account_id": account_id,
                        "action": "profile_updated",
                    }),
                ),
            };

            let audit_event =
                AuditEvent::new(kind, severity, self.audit_logger.service_name().to_string())
                    .with_metadata(metadata);

            self.audit_logger.log(audit_event).await;
        }
    }
}

#[cfg(feature = "audit")]
pub use audit_integration::AuditAccountNotification;

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

    #[test]
    fn test_email_validation() {
        assert!(is_valid_email("user@example.com"));
        assert!(is_valid_email("user+tag@example.co.uk"));
        assert!(!is_valid_email("user@"));
        assert!(!is_valid_email("@example.com"));
        assert!(!is_valid_email("userexample.com"));
        assert!(!is_valid_email("user@example"));
        assert!(!is_valid_email(""));
    }

    #[test]
    fn test_account_error_to_framework_error() {
        let err: Error = AccountError::NotFound("test".into()).into();
        assert!(matches!(err, Error::NotFound(_)));

        let err: Error = AccountError::AlreadyExists("test@test.com".into()).into();
        assert!(matches!(err, Error::Conflict(_)));

        let err: Error = AccountError::InvalidCredentials.into();
        assert!(matches!(err, Error::Unauthorized(_)));

        let err: Error = AccountError::AccountInactive {
            status: AccountStatus::Disabled,
            reason: "test".into(),
        }
        .into();
        assert!(matches!(err, Error::Forbidden(_)));

        let err: Error = AccountError::Validation("bad".into()).into();
        assert!(matches!(err, Error::BadRequest(_)));

        let err: Error = AccountError::Storage("fail".into()).into();
        assert!(matches!(err, Error::Internal(_)));
    }
}