authrs 0.1.2

A comprehensive authentication library for Rust
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
//! OTP (One-Time Password) 实现
//!
//! 提供基于一次性验证码的无密码认证功能,适用于邮件和短信验证。
//!
//! ## 与 MFA TOTP 的区别
//!
//! - **MFA TOTP**: 基于时间的算法,用户使用 Authenticator App 生成
//! - **本模块 OTP**: 服务端生成随机码,通过邮件/短信发送给用户
//!
//! ## 工作流程
//!
//! 1. 用户请求验证(输入邮箱或手机号)
//! 2. 系统生成随机数字验证码
//! 3. 应用层将验证码通过邮件/短信发送给用户
//! 4. 用户输入收到的验证码
//! 5. 系统验证码是否正确且未过期
//! 6. 验证成功后,验证码失效
//!
//! ## 示例
//!
//! ```rust
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! use authrs::passwordless::{OtpManager, OtpConfig, OtpPurpose};
//!
//! // 创建管理器
//! let manager = OtpManager::new(OtpConfig::default());
//!
//! // 生成 OTP
//! let otp_data = manager
//!     .generate("user@example.com", OtpPurpose::Login)
//!     .await
//!     .unwrap();
//! println!("验证码: {}", otp_data.code);  // 例如: "847291"
//!
//! // 应用层发送验证码(邮件/短信)
//! // send_email(user_email, otp_data.code);
//!
//! // 验证用户输入的验证码
//! match manager
//!     .verify("user@example.com", &otp_data.code, OtpPurpose::Login)
//!     .await
//! {
//!     Ok(()) => println!("验证成功"),
//!     Err(e) => println!("验证失败: {}", e),
//! }
//! # });
//! ```
//!
//! ## 自定义配置
//!
//! ```rust
//! use authrs::passwordless::OtpConfig;
//! use std::time::Duration;
//!
//! let config = OtpConfig::default()
//!     .with_code_length(6)              // 6 位数字
//!     .with_ttl(Duration::from_secs(300))   // 5 分钟过期
//!     .with_max_attempts(3);            // 最多尝试 3 次
//! ```

use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use crate::error::{Error, Result};
use crate::random::{constant_time_compare_str, generate_random_in_range};

// ============================================================================
// OTP 用途
// ============================================================================

/// OTP 用途
///
/// 不同用途的 OTP 是相互独立的,防止混用。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OtpPurpose {
    /// 登录验证
    Login,
    /// 注册验证
    Registration,
    /// 密码重置
    PasswordReset,
    /// 邮箱验证
    EmailVerification,
    /// 手机号验证
    PhoneVerification,
    /// 交易确认
    TransactionConfirmation,
    /// 双因素认证
    TwoFactor,
    /// 自定义用途
    Custom(u8),
}

impl std::fmt::Display for OtpPurpose {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OtpPurpose::Login => write!(f, "login"),
            OtpPurpose::Registration => write!(f, "registration"),
            OtpPurpose::PasswordReset => write!(f, "password_reset"),
            OtpPurpose::EmailVerification => write!(f, "email_verification"),
            OtpPurpose::PhoneVerification => write!(f, "phone_verification"),
            OtpPurpose::TransactionConfirmation => write!(f, "transaction_confirmation"),
            OtpPurpose::TwoFactor => write!(f, "two_factor"),
            OtpPurpose::Custom(id) => write!(f, "custom_{}", id),
        }
    }
}

// ============================================================================
// 配置
// ============================================================================

/// OTP 配置
#[derive(Debug, Clone)]
pub struct OtpConfig {
    /// 验证码长度(数字位数)
    pub code_length: usize,

    /// 验证码有效期
    pub ttl: std::time::Duration,

    /// 最大尝试次数(超过后需要重新生成)
    pub max_attempts: u32,

    /// 验证成功后是否自动删除
    pub consume_on_verify: bool,

    /// 是否允许同一用户同一用途同时存在多个 OTP
    pub allow_multiple: bool,

    /// 生成新 OTP 的最小间隔(防止滥用)
    pub min_interval: Option<std::time::Duration>,
}

impl Default for OtpConfig {
    fn default() -> Self {
        Self {
            code_length: 6,
            ttl: std::time::Duration::from_secs(5 * 60), // 5 分钟
            max_attempts: 3,
            consume_on_verify: true,
            allow_multiple: false,
            min_interval: Some(std::time::Duration::from_secs(60)), // 1 分钟
        }
    }
}

impl OtpConfig {
    /// 创建新配置
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置验证码长度
    pub fn with_code_length(mut self, length: usize) -> Self {
        assert!(
            (4..=10).contains(&length),
            "code length must be between 4 and 10"
        );
        self.code_length = length;
        self
    }

    /// 设置有效期
    pub fn with_ttl(mut self, ttl: std::time::Duration) -> Self {
        self.ttl = ttl;
        self
    }

    /// 设置最大尝试次数
    pub fn with_max_attempts(mut self, max: u32) -> Self {
        self.max_attempts = max;
        self
    }

    /// 设置是否在验证后消费 OTP
    pub fn with_consume_on_verify(mut self, consume: bool) -> Self {
        self.consume_on_verify = consume;
        self
    }

    /// 设置是否允许同时多个 OTP
    pub fn with_allow_multiple(mut self, allow: bool) -> Self {
        self.allow_multiple = allow;
        self
    }

    /// 设置最小生成间隔
    pub fn with_min_interval(mut self, interval: Option<std::time::Duration>) -> Self {
        self.min_interval = interval;
        self
    }

    /// 高安全性配置
    ///
    /// - 8 位验证码
    /// - 3 分钟过期
    /// - 最多 3 次尝试
    /// - 2 分钟生成间隔
    pub fn high_security() -> Self {
        Self {
            code_length: 8,
            ttl: std::time::Duration::from_secs(3 * 60),
            max_attempts: 3,
            consume_on_verify: true,
            allow_multiple: false,
            min_interval: Some(std::time::Duration::from_secs(120)),
        }
    }

    /// 宽松配置(适用于开发/测试)
    ///
    /// - 4 位验证码
    /// - 30 分钟过期
    /// - 10 次尝试
    /// - 无生成间隔限制
    pub fn relaxed() -> Self {
        Self {
            code_length: 4,
            ttl: std::time::Duration::from_secs(30 * 60),
            max_attempts: 10,
            consume_on_verify: true,
            allow_multiple: true,
            min_interval: None,
        }
    }
}

// ============================================================================
// 数据结构
// ============================================================================

/// OTP 数据
#[derive(Debug, Clone)]
pub struct OtpData {
    /// 生成的验证码
    pub code: String,

    /// 关联的用户标识
    pub identifier: String,

    /// OTP 用途
    pub purpose: OtpPurpose,

    /// 创建时间
    pub created_at: DateTime<Utc>,

    /// 过期时间
    pub expires_at: DateTime<Utc>,

    /// 剩余尝试次数
    pub remaining_attempts: u32,
}

impl OtpData {
    /// 检查是否已过期
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }

    /// 获取剩余有效时间(秒)
    pub fn remaining_seconds(&self) -> i64 {
        let remaining = self.expires_at - Utc::now();
        remaining.num_seconds().max(0)
    }
}

/// 内部存储的 OTP 记录
#[derive(Debug, Clone)]
pub struct StoredOtp {
    /// 验证码(存储用于比较)
    pub code: String,

    /// 过期时间
    pub expires_at: DateTime<Utc>,

    /// 创建时间
    pub created_at: DateTime<Utc>,

    /// 剩余尝试次数
    pub remaining_attempts: u32,
}

/// OTP 存储键
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct OtpKey {
    identifier: String,
    purpose: OtpPurpose,
}

// ============================================================================
// 存储接口
// ============================================================================

/// OTP 存储接口
///
/// 实现此 trait 以提供自定义的存储后端(如 Redis、数据库等)
#[async_trait]
pub trait OtpStore: Send + Sync {
    /// 保存 OTP
    async fn save(
        &self,
        identifier: &str,
        purpose: OtpPurpose,
        code: &str,
        expires_at: DateTime<Utc>,
        max_attempts: u32,
    ) -> Result<()>;

    /// 获取 OTP
    async fn get(&self, identifier: &str, purpose: OtpPurpose) -> Result<Option<StoredOtp>>;

    /// 更新剩余尝试次数
    async fn decrement_attempts(&self, identifier: &str, purpose: OtpPurpose) -> Result<()>;

    /// 删除 OTP
    async fn delete(&self, identifier: &str, purpose: OtpPurpose) -> Result<()>;

    /// 获取最后一次生成时间
    async fn get_last_generated(
        &self,
        identifier: &str,
        purpose: OtpPurpose,
    ) -> Result<Option<DateTime<Utc>>>;

    /// 清理过期的 OTP
    async fn cleanup_expired(&self) -> Result<usize>;
}

// ============================================================================
// 内存存储实现
// ============================================================================

/// 内存存储实现
///
/// 适用于单实例部署或测试环境。
/// 生产环境建议使用 Redis 等分布式存储。
#[derive(Debug, Clone, Default)]
pub struct InMemoryOtpStore {
    /// (identifier, purpose) -> 记录
    records: Arc<RwLock<HashMap<OtpKey, StoredOtp>>>,
}

impl InMemoryOtpStore {
    /// 创建新的内存存储
    pub fn new() -> Self {
        Self::default()
    }

    /// 获取当前存储的 OTP 数量
    pub fn len(&self) -> usize {
        self.records.read().unwrap().len()
    }

    /// 检查存储是否为空
    pub fn is_empty(&self) -> bool {
        self.records.read().unwrap().is_empty()
    }
}

#[async_trait]
impl OtpStore for InMemoryOtpStore {
    async fn save(
        &self,
        identifier: &str,
        purpose: OtpPurpose,
        code: &str,
        expires_at: DateTime<Utc>,
        max_attempts: u32,
    ) -> Result<()> {
        let mut records = self.records.write().unwrap();
        let key = OtpKey {
            identifier: identifier.to_string(),
            purpose,
        };
        records.insert(
            key,
            StoredOtp {
                code: code.to_string(),
                expires_at,
                created_at: Utc::now(),
                remaining_attempts: max_attempts,
            },
        );
        Ok(())
    }

    async fn get(&self, identifier: &str, purpose: OtpPurpose) -> Result<Option<StoredOtp>> {
        let records = self.records.read().unwrap();
        let key = OtpKey {
            identifier: identifier.to_string(),
            purpose,
        };
        Ok(records.get(&key).cloned())
    }

    async fn decrement_attempts(&self, identifier: &str, purpose: OtpPurpose) -> Result<()> {
        let mut records = self.records.write().unwrap();
        let key = OtpKey {
            identifier: identifier.to_string(),
            purpose,
        };
        if let Some(record) = records.get_mut(&key) {
            record.remaining_attempts = record.remaining_attempts.saturating_sub(1);
        }
        Ok(())
    }

    async fn delete(&self, identifier: &str, purpose: OtpPurpose) -> Result<()> {
        let mut records = self.records.write().unwrap();
        let key = OtpKey {
            identifier: identifier.to_string(),
            purpose,
        };
        records.remove(&key);
        Ok(())
    }

    async fn get_last_generated(
        &self,
        identifier: &str,
        purpose: OtpPurpose,
    ) -> Result<Option<DateTime<Utc>>> {
        let records = self.records.read().unwrap();
        let key = OtpKey {
            identifier: identifier.to_string(),
            purpose,
        };
        Ok(records.get(&key).map(|r| r.created_at))
    }

    async fn cleanup_expired(&self) -> Result<usize> {
        let mut records = self.records.write().unwrap();
        let now = Utc::now();
        let before = records.len();
        records.retain(|_, record| record.expires_at > now);
        Ok(before - records.len())
    }
}

// ============================================================================
// OTP 管理器
// ============================================================================

/// OTP 管理器
///
/// 负责生成和验证一次性密码。
///
/// ## 示例
///
/// ```rust
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// use authrs::passwordless::{OtpManager, OtpConfig, OtpPurpose};
///
/// let manager = OtpManager::new(OtpConfig::default());
///
/// // 生成 OTP
/// let otp = manager
///     .generate("user@example.com", OtpPurpose::Login)
///     .await
///     .unwrap();
/// println!("验证码: {}", otp.code);
///
/// // 验证 OTP
/// manager
///     .verify("user@example.com", &otp.code, OtpPurpose::Login)
///     .await
///     .unwrap();
/// # });
/// ```
pub struct OtpManager<S: OtpStore = InMemoryOtpStore> {
    store: S,
    config: OtpConfig,
}

impl OtpManager<InMemoryOtpStore> {
    /// 使用默认内存存储创建管理器
    pub fn new(config: OtpConfig) -> Self {
        Self {
            store: InMemoryOtpStore::new(),
            config,
        }
    }

    /// 使用默认配置创建管理器
    pub fn with_default_config() -> Self {
        Self::new(OtpConfig::default())
    }
}

impl<S: OtpStore> OtpManager<S> {
    /// 使用自定义存储创建管理器
    pub fn with_store(store: S, config: OtpConfig) -> Self {
        Self { store, config }
    }

    /// 生成随机验证码
    fn generate_code(&self) -> String {
        let min = 10u64.pow((self.config.code_length - 1) as u32);
        let max = 10u64.pow(self.config.code_length as u32);
        let code = generate_random_in_range(min, max);
        format!("{:0>width$}", code, width = self.config.code_length)
    }

    /// 生成 OTP
    ///
    /// # Arguments
    ///
    /// * `identifier` - 用户标识(邮箱或手机号)
    /// * `purpose` - OTP 用途
    ///
    /// # Returns
    ///
    /// 返回包含验证码和元数据的 `OtpData`
    ///
    /// # Errors
    ///
    /// - 如果设置了最小间隔且距上次生成时间不足
    ///
    /// # Example
    ///
    /// ```rust
    /// use authrs::passwordless::{OtpManager, OtpConfig, OtpPurpose};
    ///
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// let manager = OtpManager::new(OtpConfig::default());
    /// let otp = manager
    ///     .generate("user@example.com", OtpPurpose::Login)
    ///     .await
    ///     .unwrap();
    ///
    /// // 发送验证码给用户
    /// println!("请输入验证码: {}", otp.code);
    /// # });
    /// ```
    pub async fn generate(
        &self,
        identifier: impl Into<String>,
        purpose: OtpPurpose,
    ) -> Result<OtpData> {
        let identifier = identifier.into();

        // 检查最小间隔
        if let Some(min_interval) = self.config.min_interval
            && let Some(last_generated) =
                self.store.get_last_generated(&identifier, purpose).await?
        {
            let elapsed = Utc::now() - last_generated;
            let min_seconds = min_interval.as_secs() as i64;
            if elapsed.num_seconds() < min_seconds {
                let wait_seconds = min_seconds - elapsed.num_seconds();
                return Err(Error::validation(format!(
                    "please wait {} seconds before requesting a new code",
                    wait_seconds
                )));
            }
        }

        // 如果不允许多个,先删除现有的
        if !self.config.allow_multiple {
            self.store.delete(&identifier, purpose).await?;
        }

        // 生成验证码
        let code = self.generate_code();

        // 计算过期时间
        let created_at = Utc::now();
        let expires_at = created_at + Duration::seconds(self.config.ttl.as_secs() as i64);

        // 保存
        self.store
            .save(
                &identifier,
                purpose,
                &code,
                expires_at,
                self.config.max_attempts,
            )
            .await?;

        Ok(OtpData {
            code,
            identifier,
            purpose,
            created_at,
            expires_at,
            remaining_attempts: self.config.max_attempts,
        })
    }

    /// 验证 OTP
    ///
    /// 使用常量时间比较防止时序攻击。
    ///
    /// # Arguments
    ///
    /// * `identifier` - 用户标识
    /// * `code` - 用户输入的验证码
    /// * `purpose` - OTP 用途
    ///
    /// # Returns
    ///
    /// 验证成功返回 `Ok(())`,失败返回错误
    ///
    /// # Errors
    ///
    /// - OTP 不存在
    /// - OTP 已过期
    /// - 验证码错误
    /// - 超过最大尝试次数
    ///
    /// # Example
    ///
    /// ```rust
    /// use authrs::passwordless::{OtpManager, OtpConfig, OtpPurpose};
    ///
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// let manager = OtpManager::new(OtpConfig::default());
    /// let otp = manager
    ///     .generate("user@example.com", OtpPurpose::Login)
    ///     .await
    ///     .unwrap();
    ///
    /// // 验证正确的验证码
    /// assert!(
    ///     manager
    ///         .verify("user@example.com", &otp.code, OtpPurpose::Login)
    ///         .await
    ///         .is_ok()
    /// );
    ///
    /// // 验证码已被消费,再次验证会失败
    /// assert!(
    ///     manager
    ///         .verify("user@example.com", &otp.code, OtpPurpose::Login)
    ///         .await
    ///         .is_err()
    /// );
    /// # });
    /// ```
    pub async fn verify(&self, identifier: &str, code: &str, purpose: OtpPurpose) -> Result<()> {
        // 获取存储的 OTP
        let stored = self
            .store
            .get(identifier, purpose)
            .await?
            .ok_or_else(|| Error::validation("no OTP found for this identifier and purpose"))?;

        // 检查是否过期
        if Utc::now() > stored.expires_at {
            self.store.delete(identifier, purpose).await?;
            return Err(Error::validation("OTP has expired"));
        }

        // 检查剩余尝试次数
        if stored.remaining_attempts == 0 {
            self.store.delete(identifier, purpose).await?;
            return Err(Error::validation("maximum attempts exceeded"));
        }

        // 使用常量时间比较验证码
        if !constant_time_compare_str(code, &stored.code) {
            // 减少尝试次数
            self.store.decrement_attempts(identifier, purpose).await?;

            let remaining = stored.remaining_attempts.saturating_sub(1);
            if remaining == 0 {
                self.store.delete(identifier, purpose).await?;
                return Err(Error::validation("invalid OTP, maximum attempts exceeded"));
            }

            return Err(Error::validation(format!(
                "invalid OTP, {} attempts remaining",
                remaining
            )));
        }

        // 验证成功,根据配置决定是否删除
        if self.config.consume_on_verify {
            self.store.delete(identifier, purpose).await?;
        }

        Ok(())
    }

    /// 检查是否可以生成新的 OTP
    ///
    /// 检查是否超过了最小生成间隔。
    pub async fn can_generate(&self, identifier: &str, purpose: OtpPurpose) -> Result<bool> {
        if let Some(min_interval) = self.config.min_interval
            && let Some(last_generated) = self.store.get_last_generated(identifier, purpose).await?
        {
            let elapsed = Utc::now() - last_generated;
            let min_seconds = min_interval.as_secs() as i64;
            return Ok(elapsed.num_seconds() >= min_seconds);
        }
        Ok(true)
    }

    /// 获取距离可以重新生成的剩余秒数
    pub async fn seconds_until_can_generate(
        &self,
        identifier: &str,
        purpose: OtpPurpose,
    ) -> Result<i64> {
        if let Some(min_interval) = self.config.min_interval
            && let Some(last_generated) = self.store.get_last_generated(identifier, purpose).await?
        {
            let elapsed = Utc::now() - last_generated;
            let min_seconds = min_interval.as_secs() as i64;
            let remaining = min_seconds - elapsed.num_seconds();
            return Ok(remaining.max(0));
        }
        Ok(0)
    }

    /// 撤销 OTP
    pub async fn revoke(&self, identifier: &str, purpose: OtpPurpose) -> Result<()> {
        self.store.delete(identifier, purpose).await
    }

    /// 清理过期的 OTP
    pub async fn cleanup(&self) -> Result<usize> {
        self.store.cleanup_expired().await
    }

    /// 获取配置
    pub fn config(&self) -> &OtpConfig {
        &self.config
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread::sleep;
    use std::time::Duration as StdDuration;

    #[tokio::test]
    async fn test_generate_and_verify() {
        let manager = OtpManager::new(OtpConfig::default().with_min_interval(None));

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();
        assert_eq!(otp.code.len(), 6);
        assert_eq!(otp.identifier, "user@example.com");
        assert_eq!(otp.purpose, OtpPurpose::Login);
        assert!(!otp.is_expired());

        // 验证成功
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn test_otp_consumed_after_verify() {
        let manager = OtpManager::new(OtpConfig::default().with_min_interval(None));

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 第一次验证成功
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );

        // 第二次验证失败
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn test_otp_not_consumed_when_disabled() {
        let config = OtpConfig::default()
            .with_consume_on_verify(false)
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 多次验证都成功
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn test_wrong_code() {
        let manager = OtpManager::new(OtpConfig::default().with_min_interval(None));

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 错误的验证码
        assert!(
            manager
                .verify("user@example.com", "000000", OtpPurpose::Login)
                .await
                .is_err()
        );

        // 正确的验证码仍然有效(还有尝试次数)
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn test_max_attempts() {
        let config = OtpConfig::default()
            .with_max_attempts(2)
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 错误尝试 1
        assert!(
            manager
                .verify("user@example.com", "000000", OtpPurpose::Login)
                .await
                .is_err()
        );

        // 错误尝试 2 - 超过最大次数
        assert!(
            manager
                .verify("user@example.com", "000000", OtpPurpose::Login)
                .await
                .is_err()
        );

        // 正确的验证码也无效了
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn test_otp_expiration() {
        let config = OtpConfig::default()
            .with_ttl(StdDuration::from_millis(100))
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 等待过期
        sleep(StdDuration::from_millis(150));

        // 验证失败
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn test_different_purposes_independent() {
        let manager = OtpManager::new(OtpConfig::default().with_min_interval(None));

        let login_otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();
        let reset_otp = manager
            .generate("user@example.com", OtpPurpose::PasswordReset)
            .await
            .unwrap();

        // 不同用途的验证码不能混用
        assert!(
            manager
                .verify(
                    "user@example.com",
                    &login_otp.code,
                    OtpPurpose::PasswordReset
                )
                .await
                .is_err()
        );
        assert!(
            manager
                .verify("user@example.com", &reset_otp.code, OtpPurpose::Login)
                .await
                .is_err()
        );

        // 正确的用途可以验证
        assert!(
            manager
                .verify("user@example.com", &login_otp.code, OtpPurpose::Login)
                .await
                .is_ok()
        );
        assert!(
            manager
                .verify(
                    "user@example.com",
                    &reset_otp.code,
                    OtpPurpose::PasswordReset
                )
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn test_min_interval() {
        let config = OtpConfig::default().with_min_interval(Some(StdDuration::from_secs(1)));
        let manager = OtpManager::new(config);

        // 第一次生成成功
        manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 立即再次生成失败
        assert!(
            manager
                .generate("user@example.com", OtpPurpose::Login)
                .await
                .is_err()
        );

        // 等待间隔后成功
        sleep(StdDuration::from_millis(1100));
        assert!(
            manager
                .generate("user@example.com", OtpPurpose::Login)
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn test_code_length() {
        let config = OtpConfig::default()
            .with_code_length(8)
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();
        assert_eq!(otp.code.len(), 8);
    }

    #[tokio::test]
    async fn test_revoke() {
        let manager = OtpManager::new(OtpConfig::default().with_min_interval(None));

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 撤销
        manager
            .revoke("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 验证失败
        assert!(
            manager
                .verify("user@example.com", &otp.code, OtpPurpose::Login)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn test_cleanup_expired() {
        let config = OtpConfig::default()
            .with_ttl(StdDuration::from_millis(100))
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        manager
            .generate("user1@example.com", OtpPurpose::Login)
            .await
            .unwrap();
        manager
            .generate("user2@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 等待过期
        sleep(StdDuration::from_millis(150));

        // 清理
        let cleaned = manager.cleanup().await.unwrap();
        assert_eq!(cleaned, 2);
    }

    #[tokio::test]
    async fn test_can_generate() {
        let config = OtpConfig::default().with_min_interval(Some(StdDuration::from_secs(1)));
        let manager = OtpManager::new(config);

        // 初始可以生成
        assert!(
            manager
                .can_generate("user@example.com", OtpPurpose::Login)
                .await
                .unwrap()
        );

        manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 生成后不能立即再生成
        assert!(
            !manager
                .can_generate("user@example.com", OtpPurpose::Login)
                .await
                .unwrap()
        );

        // 等待后可以
        sleep(StdDuration::from_millis(1100));
        assert!(
            manager
                .can_generate("user@example.com", OtpPurpose::Login)
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_seconds_until_can_generate() {
        let config = OtpConfig::default().with_min_interval(Some(StdDuration::from_secs(60)));
        let manager = OtpManager::new(config);

        // 初始为 0
        assert_eq!(
            manager
                .seconds_until_can_generate("user@example.com", OtpPurpose::Login)
                .await
                .unwrap(),
            0
        );

        manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        // 生成后接近 60 秒
        let seconds = manager
            .seconds_until_can_generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();
        assert!(seconds > 55 && seconds <= 60);
    }

    #[test]
    fn test_high_security_config() {
        let config = OtpConfig::high_security();
        assert_eq!(config.code_length, 8);
        assert_eq!(config.ttl, StdDuration::from_secs(3 * 60));
        assert_eq!(config.max_attempts, 3);
    }

    #[test]
    fn test_relaxed_config() {
        let config = OtpConfig::relaxed();
        assert_eq!(config.code_length, 4);
        assert_eq!(config.ttl, StdDuration::from_secs(30 * 60));
        assert_eq!(config.max_attempts, 10);
    }

    #[test]
    fn test_otp_purpose_display() {
        assert_eq!(OtpPurpose::Login.to_string(), "login");
        assert_eq!(OtpPurpose::PasswordReset.to_string(), "password_reset");
        assert_eq!(OtpPurpose::Custom(42).to_string(), "custom_42");
    }

    #[tokio::test]
    async fn test_remaining_seconds() {
        let config = OtpConfig::default()
            .with_ttl(StdDuration::from_secs(300))
            .with_min_interval(None);
        let manager = OtpManager::new(config);

        let otp = manager
            .generate("user@example.com", OtpPurpose::Login)
            .await
            .unwrap();

        let remaining = otp.remaining_seconds();
        assert!(remaining > 295 && remaining <= 300);
    }

    #[tokio::test]
    async fn test_store_len_and_is_empty() {
        let store = InMemoryOtpStore::new();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);

        store
            .save(
                "user@example.com",
                OtpPurpose::Login,
                "123456",
                Utc::now() + Duration::hours(1),
                3,
            )
            .await
            .unwrap();
        assert!(!store.is_empty());
        assert_eq!(store.len(), 1);
    }
}