rocket_auth/forms/
mod.rs

1use crate::prelude::*;
2
3
4/// The `Login` form is used along with the [`Auth`] guard to authenticate users.
5#[derive(FromForm, Deserialize, Clone, Hash, PartialEq, Eq, Validate)]
6pub struct Login {
7    #[validate(email)]
8    pub email: String,
9    pub(crate) password: String,
10}
11
12/// The `Signup` form is used along with the [`Auth`] guard to create new users.
13#[derive(FromForm, Deserialize, Clone, PartialEq, Eq, Hash, Validate)]
14pub struct Signup {
15    #[validate(email)]
16    pub email: String,
17    #[validate(
18        custom = "is_long",
19        custom = "has_number",
20        custom = "has_lowercase",
21        custom = "has_uppercase"
22    )]
23    pub(crate) password: String,
24}
25impl Debug for Signup {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        write!(
28            f,
29            "Signup {{ email: {:?}, password: \"*****\" }}",
30            self.email
31        )
32    }
33}
34impl Debug for Login {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(
37            f,
38            "Signup {{ email: {:?}, password: \"*****\" }}",
39            self.email
40        )
41    }
42}
43
44impl From<Signup> for Login {
45    fn from(form: Signup) -> Login {
46        Login {
47            email: form.email,
48            password: form.password,
49        }
50    }
51}
52
53impl From<Login> for Signup {
54    fn from(form: Login) -> Signup {
55        Self {
56            email: form.email,
57            password: form.password,
58        }
59    }
60}
61
62impl<T: Deref<Target = Signup>> From<T> for Login {
63    fn from(form: T) -> Login {
64        Login {
65            email: form.email.clone(),
66            password: form.password.clone(),
67        }
68    }
69}
70#[throws(ValidationError)]
71pub(crate) fn is_secure(password: &str) {
72    is_long(password)?;
73    has_uppercase(password)?;
74    has_lowercase(password)?;
75    has_number(password)?;
76}
77
78#[throws(ValidationError)]
79fn is_long(password: &str) {
80    if password.len() < 8 {
81        throw!(ValidationError::new(
82            "The password must be at least 8 characters long.\n"
83        ));
84    }
85}
86#[allow(unreachable_code)]
87#[throws(ValidationError)]
88fn has_uppercase(password: &str) {
89    for c in password.chars() {
90        if c.is_uppercase() {
91            return;
92        }
93    }
94    throw!(ValidationError::new(
95        "The password must include least one uppercase caracter.\n"
96    ));
97}
98#[allow(unreachable_code)]
99#[throws(ValidationError)]
100fn has_lowercase(password: &str) {
101    for c in password.chars() {
102        if c.is_lowercase() {
103            return;
104        }
105    }
106    // throw!(Error::UnsafePasswordHasNoLower)
107    throw!(ValidationError::new(
108        "The password must include least one uppercase caracter.\n"
109    ))
110}
111#[allow(unreachable_code)]
112#[throws(ValidationError)]
113fn has_number(password: &str) {
114    for c in password.chars() {
115        if c.is_numeric() {
116            return;
117        }
118    }
119    throw!(ValidationError::new(
120        "The password has to contain at least one digit.\n"
121    ))
122    // throw!(Error::UnsafePasswordHasNoDigit)
123}