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
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.
//! 密码策略套件子模块(吸收 keycloak PasswordPolicyRule)。
//! 提供可插拔密码策略规则 + 统一引擎执行,支持企业合规场景。
//! 详见 spec `password-policy`。
//!
//! # 核心类型
//!
//! - `PasswordPolicyRule` trait:规则抽象(同步纯函数,`name` + `validate`)
//! - `PolicyContext`:策略校验上下文(user_id/tenant_id/username/email/password_history)
//! - `PasswordPolicyEngine`:规则引擎(FirstError/AllErrors 两种模式)
//! - `PolicyError`:规则校验错误(rule_name + message)
//!
//! # 设计
//!
//! - 规则为同步纯函数(无 IO/await),可在任意上下文调用
//! - 引擎按 `ErrorMode` 执行:`FirstError` 短路,`AllErrors` 收集全部
//! - 规则参数由构造器注入(v0.6.5 支持 `GarrisonConfig` 加载)
pub use PolicyError;
use Arc;
// ============================================================================
// PasswordPolicyRule trait
// ============================================================================
/// 密码策略规则 trait。
///
/// 每条规则为同步纯函数(非 async),校验密码是否符合规则。
/// 无 `#[async_trait]`(规则校验不执行 IO/await)。
///
/// # 对象安全
///
/// trait 仅含同步方法,对象安全,可作 `Box<dyn PasswordPolicyRule>` /
/// `Vec<Box<dyn PasswordPolicyRule>>` 使用。
///
/// # 示例
///
/// ```ignore
/// use garrison::account::policy::{PasswordPolicyRule, PolicyContext, PolicyError};
///
/// struct AlwaysPassRule;
/// impl PasswordPolicyRule for AlwaysPassRule {
/// fn name(&self) -> &'static str { "always_pass" }
/// fn validate(&self, _ctx: &PolicyContext, _password: &str) -> Result<(), PolicyError> {
/// Ok(())
/// }
/// }
/// ```
// ============================================================================
// PolicyContext
// ============================================================================
/// 策略校验上下文。
///
/// 5 字段 schema(pre-1.0 锁定,与 design.md §3.2 严格一致)。
/// 提供规则校验所需的用户上下文信息。
///
/// # 字段说明
///
/// | 字段 | 类型 | 说明 |
/// |:---|:---|:---|
/// | `user_id` | `String` | 用户 ID |
/// | `tenant_id` | `Option<String>` | 租户 ID(可选) |
/// | `username` | `Option<String>` | 用户名(用于相似度检测) |
/// | `email` | `Option<String>` | 邮箱(用于邮箱检测) |
/// | `password_history` | `Vec<String>` | 密码历史(存 hash,非明文) |
// ============================================================================
// ErrorMode + PasswordPolicyEngine
// ============================================================================
/// 引擎错误返回模式。
///
/// - `FirstError`: 首条规则失败即返回 `Err(vec![error])` 并短路
/// - `AllErrors`: 执行所有规则,收集全部错误返回 `Err(errors)`
/// 密码策略引擎。
///
/// 持有规则列表 + 错误模式,按 `mode` 执行规则校验。
///
/// # 示例
///
/// ```ignore
/// use garrison::account::policy::{ErrorMode, PasswordPolicyEngine, PolicyContext};
/// // T008/T009 实现具体规则后注入
/// let engine = PasswordPolicyEngine::new(Vec::new(), ErrorMode::FirstError);
/// let ctx = PolicyContext {
/// user_id: "alice".into(),
/// tenant_id: None,
/// username: None,
/// email: None,
/// password_history: Vec::new(),
/// };
/// assert!(engine.validate(&ctx, "password").is_ok()); // 空规则集 → Ok
/// ```