Newer
Older
use rand::{distributions::Alphanumeric, Rng};
#[derive(Clone)]
pub struct AppState {
pub config: AppConfig,
pub preferences: AppPreferences,
}
impl AppState {
pub fn new() -> Self {
let config = AppConfig::new(AppArgs::from_args());
let admin_cookie_name = config.admin_cookie_name.to_owned();
admin_auth_token: AdminAuthToken::new(admin_cookie_name),
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
#[derive(Clone)]
pub struct AdminAuthToken {
pub value: Option<String>,
pub cookie_name: String,
}
impl AdminAuthToken {
pub fn new(cookie_name: String) -> Self {
AdminAuthToken {
value: None,
cookie_name,
}
}
pub fn match_value(&self, value: String) -> bool {
match &self.value {
Some(token) => token.eq(&value),
None => false,
}
}
pub fn generate(&mut self) {
// Thanks to https://stackoverflow.com/questions/54275459/how-do-i-create-a-random-string-by-sampling-from-alphanumeric-characters#54277357
self.value = Some(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(36)
.map(char::from)
.collect::<String>()
.to_ascii_lowercase(),
)
}
}