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
use crate::Permissions;
use argon2::{Config, Variant};
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct User {
pub username: String,
pub password: String,
pub permissions: Permissions,
#[cfg_attr(feature = "serde", serde(skip))]
pub logged_in: Option<std::time::Instant>,
#[cfg_attr(feature = "serde", serde(skip))]
pub expire: Option<std::time::Duration>,
}
impl User {
pub fn new_basic(
username: String,
password: String,
permissions: Permissions,
) -> Self {
Self {
username,
password,
permissions,
logged_in: None,
expire: None,
}
}
pub fn new(
username: String,
password: String,
permissions: Permissions,
) -> Self {
let password = password.as_bytes();
let salt = crate::salt();
let salt = salt.as_bytes();
let config = Config {
variant: Variant::Argon2id,
..Default::default()
};
let password = argon2::hash_encoded(password, salt, &config).unwrap();
Self {
username,
password,
permissions,
logged_in: None,
expire: None,
}
}
pub fn reset_pass(&mut self, password: String) {
let salt = crate::salt();
let salt = salt.as_bytes();
let config = Config {
variant: Variant::Argon2id,
..Default::default()
};
self.password =
argon2::hash_encoded(password.as_bytes(), salt, &config).unwrap();
}
pub fn validate(&self, password: &str) -> bool {
argon2::verify_encoded(&self.password, password.as_bytes()).unwrap()
}
pub fn log_in(
&mut self,
password: &str,
expire: std::time::Duration,
) -> bool {
match self.validate(password) {
true => {
self.logged_in = Some(std::time::Instant::now());
self.expire = Some(expire);
true
}
false => false,
}
}
pub fn check_login(&self) -> bool {
self.logged_in != None
}
pub fn check_valid_login(&self) -> bool {
self.logged_in != None
&& match self.expire {
None => false,
Some(expire) => match self.logged_in {
None => false,
Some(logged_in) => logged_in.elapsed() < expire,
},
}
}
pub fn get_permission(&self, path: &str, r#mut: bool) -> bool {
self.permissions.get_permission(path, r#mut)
}
pub fn get_valid_permissions(
&self,
path: &str,
r#mut: bool,
) -> Result<bool, SessionExpired> {
if !self.check_valid_login() {
return Err(SessionExpired {});
}
Ok(self.get_permission(path, r#mut))
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct SessionExpired {}
impl fmt::Display for SessionExpired {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "session_expired")
}
}