backbone-auth 3.1.1

Backbone Framework Auth - Authentication and authorization system
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
//! Database integration example for backbone-auth
//! Demonstrates PostgreSQL and MongoDB integration patterns

use backbone_auth::*;
use uuid::Uuid;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use async_trait::async_trait;

// PostgreSQL integration example
#[cfg(feature = "postgres")]
mod postgres_integration {
    use super::*;
    use sqlx::{PgPool, Row};
    use chrono::{DateTime, Utc};

    /// PostgreSQL user database implementation
    pub struct PostgresUserRepository {
        pool: PgPool,
    }

    impl PostgresUserRepository {
        pub async fn new(database_url: &str) -> Result<Self> {
            let pool = PgPool::connect(database_url).await
                .map_err(|e| anyhow::anyhow!("Failed to connect to PostgreSQL: {}", e))?;

            // Run migrations
            sqlx::migrate!("./migrations").run(&pool).await
                .map_err(|e| anyhow::anyhow!("Failed to run migrations: {}", e))?;

            Ok(Self { pool })
        }

        pub async fn create_sample_data(&self) -> Result<()> {
            let user_id = Uuid::new_v4();
            let password_hash = "$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG";

            sqlx::query!(
                r#"
                INSERT INTO users (id, email, password_hash, roles, is_active, is_locked,
                                  two_factor_enabled, two_factor_methods, created_at, updated_at)
                VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
                ON CONFLICT (email) DO NOTHING
                "#,
                user_id,
                "admin@startapp.id",
                password_hash,
                &["admin".to_string()],
                true,
                false,
                true,
                &["totp".to_string()],
                Utc::now(),
                Utc::now()
            )
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to create sample data: {}", e))?;

            println!("✅ Sample PostgreSQL user data created");
            Ok(())
        }
    }

    #[async_trait]
    impl UserRepository for PostgresUserRepository {
        async fn find_by_email(&self, email: &str) -> Result<Option<User>> {
            let row = sqlx::query!(
                r#"
                SELECT id, email, password_hash, roles, is_active, is_locked,
                       two_factor_enabled, two_factor_methods, account_expires_at,
                       requires_password_change, created_at, updated_at, deleted_at
                FROM users
                WHERE email = $1 AND deleted_at IS NULL
                "#,
                email
            )
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Database error: {}", e))?;

            if let Some(row) = row {
                Ok(Some(User {
                    id: row.id,
                    email: row.email,
                    password_hash: row.password_hash,
                    roles: row.roles.unwrap_or_default(),
                    is_active: row.is_active,
                    is_locked: row.is_locked,
                    two_factor_enabled: row.two_factor_enabled,
                    two_factor_methods: row.two_factor_methods.unwrap_or_default(),
                    account_expires_at: row.account_expires_at,
                    requires_password_change: row.requires_password_change,
                }))
            } else {
                Ok(None)
            }
        }

        async fn save(&self, user: &User) -> Result<()> {
            sqlx::query!(
                r#"
                INSERT INTO users (id, email, password_hash, roles, is_active, is_locked,
                                  two_factor_enabled, two_factor_methods, account_expires_at,
                                  requires_password_change, created_at, updated_at)
                VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
                "#,
                user.id,
                user.email,
                user.password_hash,
                &user.roles,
                user.is_active,
                user.is_locked,
                user.two_factor_enabled,
                &user.two_factor_methods,
                user.account_expires_at,
                user.requires_password_change,
                Utc::now(),
                Utc::now()
            )
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to save user: {}", e))?;

            Ok(())
        }

        async fn update(&self, user: &User) -> Result<()> {
            sqlx::query!(
                r#"
                UPDATE users
                SET email = $2, password_hash = $3, roles = $4, is_active = $5,
                    is_locked = $6, two_factor_enabled = $7, two_factor_methods = $8,
                    account_expires_at = $9, requires_password_change = $10, updated_at = $11
                WHERE id = $1
                "#,
                user.id,
                user.email,
                user.password_hash,
                &user.roles,
                user.is_active,
                user.is_locked,
                user.two_factor_enabled,
                &user.two_factor_methods,
                user.account_expires_at,
                user.requires_password_change,
                Utc::now()
            )
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to update user: {}", e))?;

            Ok(())
        }

        async fn delete(&self, user_id: &Uuid) -> Result<()> {
            sqlx::query!(
                "UPDATE users SET deleted_at = $1 WHERE id = $2",
                Utc::now(),
                user_id
            )
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to delete user: {}", e))?;

            Ok(())
        }

        async fn find_by_id(&self, user_id: &Uuid) -> Result<Option<User>> {
            let row = sqlx::query!(
                r#"
                SELECT id, email, password_hash, roles, is_active, is_locked,
                       two_factor_enabled, two_factor_methods, account_expires_at,
                       requires_password_change, created_at, updated_at, deleted_at
                FROM users
                WHERE id = $1 AND deleted_at IS NULL
                "#,
                user_id
            )
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("Database error: {}", e))?;

            if let Some(row) = row {
                Ok(Some(User {
                    id: row.id,
                    email: row.email,
                    password_hash: row.password_hash,
                    roles: row.roles.unwrap_or_default(),
                    is_active: row.is_active,
                    is_locked: row.is_locked,
                    two_factor_enabled: row.two_factor_enabled,
                    two_factor_methods: row.two_factor_methods.unwrap_or_default(),
                    account_expires_at: row.account_expires_at,
                    requires_password_change: row.requires_password_change,
                }))
            } else {
                Ok(None)
            }
        }
    }

    /// Session management with PostgreSQL
    pub struct PostgresSessionRepository {
        pool: PgPool,
    }

    impl PostgresSessionRepository {
        pub async fn new(pool: PgPool) -> Result<Self> {
            // Create sessions table
            sqlx::query!(
                r#"
                CREATE TABLE IF NOT EXISTS sessions (
                    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                    user_id UUID NOT NULL REFERENCES users(id),
                    token_hash VARCHAR(255) NOT NULL,
                    device_info JSONB,
                    ip_address INET,
                    user_agent TEXT,
                    expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
                    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                    last_accessed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                    is_active BOOLEAN DEFAULT true
                )
                "#
            )
            .execute(&pool)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to create sessions table: {}", e))?;

            Ok(Self { pool })
        }
    }
}

// MongoDB integration example
#[cfg(feature = "mongodb")]
mod mongodb_integration {
    use super::*;
    use mongodb::{Client, Database, Collection};
    use mongodb::bson::{doc, Document};
    use serde_json::Value;
    use chrono::{DateTime, Utc};

    /// MongoDB user database implementation
    pub struct MongoUserRepository {
        collection: Collection<Document>,
    }

    impl MongoUserRepository {
        pub async fn new(database_url: &str, database_name: &str) -> Result<Self> {
            let client = Client::with_uri_str(database_url).await
                .map_err(|e| anyhow::anyhow!("Failed to connect to MongoDB: {}", e))?;

            let database = client.database(database_name);
            let collection = database.collection("users");

            // Create indexes
            collection.create_index(
                doc! { "email": 1 },
                None
            ).await
            .map_err(|e| anyhow::anyhow!("Failed to create email index: {}", e))?;

            collection.create_index(
                doc! { "deleted_at": 1 },
                None
            ).await
            .map_err(|e| anyhow::anyhow!("Failed to create deleted_at index: {}", e))?;

            Ok(Self { collection })
        }

        pub async fn create_sample_data(&self) -> Result<()> {
            let user_id = Uuid::new_v4();
            let user_doc = doc! {
                "_id": user_id,
                "email": "admin@startapp.id",
                "password_hash": "$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG",
                "roles": ["admin"],
                "is_active": true,
                "is_locked": false,
                "two_factor_enabled": true,
                "two_factor_methods": ["totp"],
                "created_at": Utc::now(),
                "updated_at": Utc::now()
            };

            self.collection.insert_one(user_doc, None).await
                .map_err(|e| anyhow::anyhow!("Failed to create sample data: {}", e))?;

            println!("✅ Sample MongoDB user data created");
            Ok(())
        }

        fn bson_to_user(doc: &Document) -> Result<User> {
            Ok(User {
                id: doc.get_uuid("_id")
                    .map_err(|e| anyhow::anyhow!("Failed to get user ID: {}", e))?,
                email: doc.get_str("email")
                    .map_err(|e| anyhow::anyhow!("Failed to get email: {}", e))?
                    .to_string(),
                password_hash: doc.get_str("password_hash")
                    .map_err(|e| anyhow::anyhow!("Failed to get password hash: {}", e))?
                    .to_string(),
                roles: doc.get_array("roles")
                    .unwrap_or(&mongodb::bson::Array::new())
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect(),
                is_active: doc.get_bool("is_active").unwrap_or(true),
                is_locked: doc.get_bool("is_locked").unwrap_or(false),
                two_factor_enabled: doc.get_bool("two_factor_enabled").unwrap_or(false),
                two_factor_methods: doc.get_array("two_factor_methods")
                    .unwrap_or(&mongodb::bson::Array::new())
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect(),
                account_expires_at: doc.get_datetime("account_expires_at").ok().map(|dt| dt.to_chrono()),
                requires_password_change: doc.get_bool("requires_password_change").unwrap_or(false),
            })
        }
    }

    #[async_trait]
    impl UserRepository for MongoUserRepository {
        async fn find_by_email(&self, email: &str) -> Result<Option<User>> {
            let filter = doc! {
                "email": email,
                "deleted_at": { "$exists": false }
            };

            if let Some(doc) = self.collection.find_one(filter, None).await
                .map_err(|e| anyhow::anyhow!("Database error: {}", e))? {
                Ok(Some(Self::bson_to_user(&doc)?))
            } else {
                Ok(None)
            }
        }

        async fn save(&self, user: &User) -> Result<()> {
            let doc = doc! {
                "_id": user.id,
                "email": &user.email,
                "password_hash": &user.password_hash,
                "roles": &user.roles,
                "is_active": user.is_active,
                "is_locked": user.is_locked,
                "two_factor_enabled": user.two_factor_enabled,
                "two_factor_methods": &user.two_factor_methods,
                "account_expires_at": user.account_expires_at,
                "requires_password_change": user.requires_password_change,
                "created_at": Utc::now(),
                "updated_at": Utc::now()
            };

            self.collection.insert_one(doc, None).await
                .map_err(|e| anyhow::anyhow!("Failed to save user: {}", e))?;

            Ok(())
        }

        async fn update(&self, user: &User) -> Result<()> {
            let filter = doc! { "_id": user.id };
            let update = doc! {
                "$set": {
                    "email": &user.email,
                    "password_hash": &user.password_hash,
                    "roles": &user.roles,
                    "is_active": user.is_active,
                    "is_locked": user.is_locked,
                    "two_factor_enabled": user.two_factor_enabled,
                    "two_factor_methods": &user.two_factor_methods,
                    "account_expires_at": user.account_expires_at,
                    "requires_password_change": user.requires_password_change,
                    "updated_at": Utc::now()
                }
            };

            self.collection.update_one(filter, update, None).await
                .map_err(|e| anyhow::anyhow!("Failed to update user: {}", e))?;

            Ok(())
        }

        async fn delete(&self, user_id: &Uuid) -> Result<()> {
            let filter = doc! { "_id": user_id };
            let update = doc! {
                "$set": {
                    "deleted_at": Utc::now(),
                    "updated_at": Utc::now()
                }
            };

            self.collection.update_one(filter, update, None).await
                .map_err(|e| anyhow::anyhow!("Failed to delete user: {}", e))?;

            Ok(())
        }

        async fn find_by_id(&self, user_id: &Uuid) -> Result<Option<User>> {
            let filter = doc! {
                "_id": user_id,
                "deleted_at": { "$exists": false }
            };

            if let Some(doc) = self.collection.find_one(filter, None).await
                .map_err(|e| anyhow::anyhow!("Database error: {}", e))? {
                Ok(Some(Self::bson_to_user(&doc)?))
            } else {
                Ok(None)
            }
        }
    }
}

// Mock database implementations for demonstration
struct MockPostgresUserRepository {
    users: HashMap<String, User>,
}

impl MockPostgresUserRepository {
    fn new() -> Self {
        let mut users = HashMap::new();
        let user_id = Uuid::new_v4();

        users.insert("admin@startapp.id".to_string(), User {
            id: user_id,
            email: "admin@startapp.id".to_string(),
            password_hash: "$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG".to_string(),
            roles: vec!["admin".to_string()],
            is_active: true,
            is_locked: false,
            two_factor_enabled: true,
            two_factor_methods: vec!["totp".to_string()],
            account_expires_at: None,
            requires_password_change: false,
        });

        Self { users }
    }
}

#[async_trait]
impl UserRepository for MockPostgresUserRepository {
    async fn find_by_email(&self, email: &str) -> Result<Option<User>> {
        println!("🐘 PostgreSQL: Querying user by email: {}", email);
        Ok(self.users.get(email).cloned())
    }

    async fn save(&self, user: &User) -> Result<()> {
        println!("🐘 PostgreSQL: INSERT INTO users VALUES ({}, {}, ...)", user.id, user.email);
        Ok(())
    }

    async fn update(&self, user: &User) -> Result<()> {
        println!("🐘 PostgreSQL: UPDATE users SET ... WHERE id = {}", user.id);
        Ok(())
    }

    async fn delete(&self, user_id: &Uuid) -> Result<()> {
        println!("🐘 PostgreSQL: UPDATE users SET deleted_at = NOW() WHERE id = {}", user_id);
        Ok(())
    }

    async fn find_by_id(&self, user_id: &Uuid) -> Result<Option<User>> {
        println!("🐘 PostgreSQL: SELECT * FROM users WHERE id = {}", user_id);
        Ok(None)
    }
}

struct MockMongoUserRepository {
    users: HashMap<String, User>,
}

impl MockMongoUserRepository {
    fn new() -> Self {
        let mut users = HashMap::new();
        let user_id = Uuid::new_v4();

        users.insert("admin@startapp.id".to_string(), User {
            id: user_id,
            email: "admin@startapp.id".to_string(),
            password_hash: "$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG".to_string(),
            roles: vec!["admin".to_string()],
            is_active: true,
            is_locked: false,
            two_factor_enabled: true,
            two_factor_methods: vec!["totp".to_string()],
            account_expires_at: None,
            requires_password_change: false,
        });

        Self { users }
    }
}

#[async_trait]
impl UserRepository for MockMongoUserRepository {
    async fn find_by_email(&self, email: &str) -> Result<Option<User>> {
        println!("🍃 MongoDB: db.users.findOne({{ email: '{}' }})", email);
        Ok(self.users.get(email).cloned())
    }

    async fn save(&self, user: &User) -> Result<()> {
        println!("🍃 MongoDB: db.users.insertOne({{ email: '{}', _id: {} }})", user.email, user.id);
        Ok(())
    }

    async fn update(&self, user: &User) -> Result<()> {
        println!("🍃 MongoDB: db.users.updateOne({{ _id: {} }}, {{ $set: {{ email: '{}' }} }})", user.id, user.email);
        Ok(())
    }

    async fn delete(&self, user_id: &Uuid) -> Result<()> {
        println!("🍃 MongoDB: db.users.updateOne({{ _id: {} }}, {{ $set: {{ deleted_at: new Date() }} }})", user_id);
        Ok(())
    }

    async fn find_by_id(&self, user_id: &Uuid) -> Result<Option<User>> {
        println!("🍃 MongoDB: db.users.findOne({{ _id: {} }})", user_id);
        Ok(None)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Backbone Auth Database Integration Examples ===\n");

    // Initialize auth service
    let auth_service = AuthService::new(AuthServiceConfig {
        jwt_secret: "database_integration_secret_change_in_production".to_string(),
        token_expiry_hours: 24,
        ..Default::default()
    })?;

    // Initialize mock security service
    struct MockSecurityService;
    #[async_trait::async_trait]
    impl SecurityService for MockSecurityService {
        async fn check_rate_limit(&self, _email: &str, _ip_address: Option<&str>) -> Result<()> { Ok(()) }
        async fn analyze_login_attempt(&self, _user_id: &Uuid, _device_info: &DeviceInfo, _ip_address: Option<&str>) -> Result<SecurityFlags> {
            Ok(SecurityFlags {
                new_device: false,
                suspicious_location: false,
                brute_force_detected: false,
                anomaly_detected: false,
            })
        }
        async fn log_failed_auth_attempt(&self, _user_id: &Uuid, _ip_address: Option<&str>) -> Result<()> { Ok(()) }
        async fn log_successful_auth(&self, _user_id: &Uuid, _ip_address: Option<&str>) -> Result<()> { Ok(()) }
    }

    let security_service = MockSecurityService;

    // 1. PostgreSQL Integration Example
    println!("🐘 PostgreSQL Integration Example");
    println!("================================");
    let postgres_repo = MockPostgresUserRepository::new();

    let auth_request = AuthRequest {
        email: "admin@startapp.id".to_string(),
        password: "SecureAdminPass123".to_string(),
        ip_address: Some("192.168.1.100".to_string()),
        device_info: DeviceInfo {
            device_id: "postgres_client_123".to_string(),
            user_agent: "PostgreSQL-Client/1.0".to_string(),
            ip_address: Some("192.168.1.100".to_string()),
            fingerprint: Some("fp_postgres_client".to_string()),
        },
        remember_me: Some(true),
    };

    match auth_service.authenticate_enhanced(
        auth_request,
        &postgres_repo,
        &security_service
    ).await {
        Ok(result) => {
            println!("✅ PostgreSQL authentication successful!");
            println!("   User ID: {}", result.user_id);
            println!("   Token generated: {}", result.token.is_some());
        }
        Err(e) => println!("❌ PostgreSQL authentication failed: {}", e),
    }
    println!();

    // 2. MongoDB Integration Example
    println!("🍃 MongoDB Integration Example");
    println!("==============================");
    let mongo_repo = MockMongoUserRepository::new();

    let auth_request = AuthRequest {
        email: "admin@startapp.id".to_string(),
        password: "SecureAdminPass123".to_string(),
        ip_address: Some("192.168.1.101".to_string()),
        device_info: DeviceInfo {
            device_id: "mongo_client_456".to_string(),
            user_agent: "MongoDB-Client/1.0".to_string(),
            ip_address: Some("192.168.1.101".to_string()),
            fingerprint: Some("fp_mongo_client".to_string()),
        },
        remember_me: Some(true),
    };

    match auth_service.authenticate_enhanced(
        auth_request,
        &mongo_repo,
        &security_service
    ).await {
        Ok(result) => {
            println!("✅ MongoDB authentication successful!");
            println!("   User ID: {}", result.user_id);
            println!("   Token generated: {}", result.token.is_some());
        }
        Err(e) => println!("❌ MongoDB authentication failed: {}", e),
    }
    println!();

    // 3. Database Configuration Examples
    println!("⚙️ Database Configuration Examples");
    println!("==================================");

    println!("📋 PostgreSQL Configuration:");
    println!("```toml");
    println!("# Cargo.toml");
    println!("sqlx = {{ version = \"0.8\", features = [\"runtime-tokio-rustls\", \"postgres\", \"uuid\", \"chrono\"] }}");
    println!("postgres-configuration = \"0.1\"");
    println!("```");
    println!();

    println!("📋 PostgreSQL Connection String:");
    println!("```env");
    println!("DATABASE_URL=postgresql://username:password@localhost:5432/auth_db");
    println!("DATABASE_MAX_CONNECTIONS=20");
    println!("DATABASE_MIN_CONNECTIONS=5");
    println!("```");
    println!();

    println!("📋 MongoDB Configuration:");
    println!("```toml");
    println!("# Cargo.toml");
    println!("mongodb = \"2.8\"");
    println!("bson = \"2.8\"");
    println!("```");
    println!();

    println!("📋 MongoDB Connection String:");
    println!("```env");
    println!("MONGODB_URL=mongodb://username:password@localhost:27017/auth_db");
    println!("MONGODB_DATABASE=auth_db");
    println!("```");
    println!();

    // 4. Migration Examples
    println!("🗄️ Database Migration Examples");
    println!("===============================");

    println!("🐘 PostgreSQL Migration (migrations/001_create_users.sql):");
    println!("```sql");
    println!("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";");
    println!();
    println!("CREATE TABLE users (");
    println!("    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),");
    println!("    email VARCHAR(255) UNIQUE NOT NULL,");
    println!("    password_hash VARCHAR(255) NOT NULL,");
    println!("    roles TEXT[] DEFAULT '{}',");
    println!("    is_active BOOLEAN DEFAULT true,");
    println!("    is_locked BOOLEAN DEFAULT false,");
    println!("    two_factor_enabled BOOLEAN DEFAULT false,");
    println!("    two_factor_methods TEXT[] DEFAULT '{}',");
    println!("    account_expires_at TIMESTAMP WITH TIME ZONE,");
    println!("    requires_password_change BOOLEAN DEFAULT false,");
    println!("    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),");
    println!("    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),");
    println!("    deleted_at TIMESTAMP WITH TIME ZONE");
    println!(");");
    println!();
    println!("CREATE INDEX idx_users_email ON users(email);");
    println!("CREATE INDEX idx_users_deleted_at ON users(deleted_at);");
    println!("```");
    println!();

    println!("🍃 MongoDB Collection Setup:");
    println!("```javascript");
    println!("db.createCollection(\"users\");");
    println!();
    println!("// Create indexes");
    println!("db.users.createIndex({ \"email\": 1 }, { unique: true });");
    println!("db.users.createIndex({ \"deleted_at\": 1 });");
    println!("db.users.createIndex({ \"created_at\": -1 });");
    println!();
    println!("// Sample document");
    println!("db.users.insertOne({");
    println!("  email: \"admin@startapp.id\",");
    println!("  passwordHash: \"$argon2id$v=19$m=19456,t=2,p=1$...\",");
    println!("  roles: [\"admin\"],");
    println!("  isActive: true,");
    println!("  isLocked: false,");
    println!("  twoFactorEnabled: true,");
    println!("  twoFactorMethods: [\"totp\"],");
    println!("  createdAt: new Date()");
    println!("});");
    println!("```");
    println!();

    // 5. Performance Comparison
    println!("📊 Performance Comparison");
    println!("=========================");

    println!("🐘 PostgreSQL Strengths:");
    println!("✅ ACID compliance and data integrity");
    println!("✅ Complex queries with JOINs");
    println!("✅ Referential integrity with foreign keys");
    println!("✅ Advanced indexing strategies");
    println!("✅ Window functions and CTEs");
    println!("✅ Mature tooling and ecosystem");
    println!("✅ Better for transactional data");
    println!();

    println!("🍃 MongoDB Strengths:");
    println!("✅ Flexible schema design");
    println!("✅ Horizontal scaling with sharding");
    println!("✅ Natural document mapping to Rust structs");
    println!("✅ Built-in replication and high availability");
    println!("✅ Aggregation pipeline for complex queries");
    println!("✅ Better for rapidly evolving schemas");
    println!("✅ TTL indexes for automatic expiration");
    println!();

    println!("📋 Recommendation:");
    println!("🎯 Use PostgreSQL for:");
    println!("   • User authentication (primary choice)");
    println!("   • Financial transactions");
    println!("   • Data requiring strong consistency");
    println!("   • Complex relational queries");
    println!();
    println!("🎯 Use MongoDB for:");
    println!("   • Session storage");
    println!("   • Audit logs");
    println!("   • User preferences");
    println!("   • Rapidly evolving data structures");
    println!();

    // 6. Connection Pooling Best Practices
    println!("🔗 Connection Pooling Best Practices");
    println!("=====================================");

    println!("🐘 PostgreSQL Pool Configuration:");
    println!("```rust");
    println!("let pool = PgPoolOptions::new()");
    println!("    .max_connections(20)");
    println!("    .min_connections(5)");
    println!("    .acquire_timeout(Duration::from_secs(30))");
    println!("    .idle_timeout(Duration::from_secs(600))");
    println!("    .max_lifetime(Duration::from_secs(1800))");
    println!("    .connect(&database_url)");
    println!("    .await?;");
    println!("```");
    println!();

    println!("🍃 MongoDB Pool Configuration:");
    println!("```rust");
    println!("let client_options = ClientOptions::builder()");
    println!("    .max_pool_size(20)");
    println!("    .min_pool_size(5)");
    println!("    .max_idle_time(Duration::from_secs(600))");
    println!("    .server_selection_timeout(Duration::from_secs(30))");
    println!("    .build()?;");
    println!();
    println!("let client = Client::with_options(client_options)?;");
    println!("let database = client.database(\"auth_db\");");
    println!("```");
    println!();

    // 7. Database Security Best Practices
    println!("🔒 Database Security Best Practices");
    println!("===================================");

    println!("🛡️ PostgreSQL Security:");
    println!("• Use SSL/TLS connections");
    println!("• Implement row-level security (RLS)");
    println!("• Use database roles and permissions");
    println!("• Enable audit logging");
    println!("• Regular security updates");
    println!("• Connection encryption with certificate validation");
    println!();

    println!("🛡️ MongoDB Security:");
    println!("• Enable authentication and authorization");
    println!("• Use SSL/TLS connections");
    println!("• Implement role-based access control");
    println!("• Enable audit logging");
    println!("• Use field-level encryption for sensitive data");
    println!("• Network security with firewall rules");
    println!();

    // 8. Monitoring and Observability
    println!("📈 Monitoring and Observability");
    println!("===============================");

    println!("🐘 PostgreSQL Monitoring:");
    println!("• Connection pool metrics");
    println!("• Query performance with pg_stat_statements");
    println!("• Lock monitoring and deadlock detection");
    println!("• Replication lag monitoring");
    println!("• Database size and growth trends");
    println!();

    println!("🍃 MongoDB Monitoring:");
    println!("• Connection pool status");
    println!("• Query performance with profiler");
    println!("• Replication lag and oplog metrics");
    println!("• Index usage statistics");
    println!("• Disk space and memory usage");
    println!();

    println!("=== Database Integration Examples Complete ===");
    println!("🎉 Both PostgreSQL and MongoDB integration patterns demonstrated!");

    println!("\n📚 Next Steps:");
    println!("1. Choose database based on your requirements (PostgreSQL recommended)");
    println!("2. Set up proper connection pooling");
    println!("3. Implement database migrations");
    println!("4. Add comprehensive error handling");
    println!("5. Set up monitoring and alerting");
    println!("6. Plan for backup and disaster recovery");

    Ok(())
}

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

    #[tokio::test]
    async fn test_postgres_repository_operations() -> Result<()> {
        let repo = MockPostgresUserRepository::new();
        let user_id = Uuid::new_v4();

        let user = User {
            id: user_id,
            email: "test@example.com".to_string(),
            password_hash: "test_hash".to_string(),
            roles: vec!["user".to_string()],
            is_active: true,
            is_locked: false,
            two_factor_enabled: false,
            two_factor_methods: vec![],
            account_expires_at: None,
            requires_password_change: false,
        };

        // Test save
        repo.save(&user).await?;

        // Test find by email
        let found_user = repo.find_by_email("test@example.com").await?;
        assert!(found_user.is_some());

        // Test update
        repo.update(&user).await?;

        // Test delete
        repo.delete(&user_id).await?;

        Ok(())
    }

    #[tokio::test]
    async fn test_mongo_repository_operations() -> Result<()> {
        let repo = MockMongoUserRepository::new();
        let user_id = Uuid::new_v4();

        let user = User {
            id: user_id,
            email: "test@example.com".to_string(),
            password_hash: "test_hash".to_string(),
            roles: vec!["user".to_string()],
            is_active: true,
            is_locked: false,
            two_factor_enabled: false,
            two_factor_methods: vec![],
            account_expires_at: None,
            requires_password_change: false,
        };

        // Test save
        repo.save(&user).await?;

        // Test find by email
        let found_user = repo.find_by_email("test@example.com").await?;
        assert!(found_user.is_some());

        // Test update
        repo.update(&user).await?;

        // Test delete
        repo.delete(&user_id).await?;

        Ok(())
    }

    #[tokio::test]
    async fn test_database_authentication_flow() -> Result<()> {
        let auth_service = AuthService::with_secret("test_secret");
        let postgres_repo = MockPostgresUserRepository::new();
        let mongo_repo = MockMongoUserRepository::new();

        let security_service = MockSecurityService;
        let auth_request = AuthRequest {
            email: "admin@startapp.id".to_string(),
            password: "SecureAdminPass123".to_string(),
            ip_address: Some("127.0.0.1".to_string()),
            device_info: DeviceInfo {
                device_id: "test_device".to_string(),
                user_agent: "Test-Agent/1.0".to_string(),
                ip_address: Some("127.0.0.1".to_string()),
                fingerprint: Some("test_fingerprint".to_string()),
            },
            remember_me: Some(false),
        };

        // Test PostgreSQL authentication
        let pg_result = auth_service.authenticate_enhanced(
            auth_request.clone(),
            &postgres_repo,
            &security_service
        ).await?;
        assert!(!pg_result.user_id.to_string().is_empty());

        // Test MongoDB authentication
        let mongo_result = auth_service.authenticate_enhanced(
            auth_request,
            &mongo_repo,
            &security_service
        ).await?;
        assert!(!mongo_result.user_id.to_string().is_empty());

        Ok(())
    }
}