Skip to main content

passay_rs/rule/
mod.rs

1use crate::dictionary::Dictionary;
2use crate::rule::reference::Reference;
3use crate::rule::rule_result::RuleResult;
4
5pub mod allowed_character;
6pub mod allowed_regex;
7pub mod character;
8pub mod character_characteristics;
9pub mod character_data;
10pub mod character_occurrences;
11mod character_sequence;
12pub mod dictionary;
13pub mod dictionary_substring;
14mod digest_dictionary;
15pub mod digest_history;
16pub mod digest_source;
17pub mod history;
18pub mod illegal_character;
19pub mod illegal_regex;
20pub mod illegal_sequence;
21pub mod length;
22pub mod length_complexity;
23pub mod message_resolver;
24pub mod number_range;
25mod password_utils;
26pub mod password_validator;
27pub mod reference;
28pub mod repeat_character;
29pub mod repeat_characters;
30pub mod rule_result;
31pub mod sequence_data;
32pub mod source;
33pub mod username;
34pub mod whitespace;
35
36pub trait Rule {
37    fn validate(&self, password_data: &PasswordData) -> RuleResult;
38    fn as_has_characters(&self) -> Option<&dyn HasCharacters> {
39        None
40    }
41    fn as_dictionary_rule(&self) -> Option<&dyn DictionaryRuleTrait> {
42        None
43    }
44}
45
46pub trait HasCharacters: Rule {
47    fn characters(&self) -> String;
48}
49
50pub trait DictionaryRuleTrait: Rule {
51    fn dictionary(&self) -> &dyn Dictionary;
52}
53
54/// Contains password related information used by rules to perform password validation.
55#[derive(Debug)]
56pub struct PasswordData {
57    password: String,
58    username: Option<String>,
59    password_references: Vec<Box<dyn Reference>>,
60}
61
62impl PasswordData {
63    pub fn with_password(password: String) -> Self {
64        Self {
65            password,
66            username: None,
67            password_references: Vec::new(),
68        }
69    }
70    pub fn with_password_and_user(password: String, username: Option<String>) -> Self {
71        Self {
72            password,
73            username,
74            password_references: Vec::new(),
75        }
76    }
77    pub fn new(
78        password: String,
79        username: Option<String>,
80        password_references: Vec<Box<dyn Reference>>,
81    ) -> Self {
82        Self {
83            password,
84            username,
85            password_references,
86        }
87    }
88
89    pub fn password(&self) -> &str {
90        &self.password
91    }
92
93    pub fn password_references(&self) -> &Vec<Box<dyn Reference>> {
94        &self.password_references
95    }
96
97    pub fn username(&self) -> Option<&str> {
98        self.username.as_deref()
99    }
100}