You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use std::iter::repeat_with;
|
|
|
|
|
|
|
|
pub type AccessToken = String;
|
|
|
|
|
|
|
|
const TOKEN_LENGTH: usize = 30;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
|
|
pub struct User {
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UserService {
|
|
|
|
tokens: HashSet<AccessToken>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserService {
|
|
|
|
pub fn new() -> UserService {
|
|
|
|
UserService {
|
|
|
|
tokens: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn login(&mut self, user: &User) -> Option<AccessToken> {
|
|
|
|
if user.username == "admin" && user.password == "admin" {
|
|
|
|
let token: AccessToken = repeat_with(fastrand::alphanumeric)
|
|
|
|
.take(TOKEN_LENGTH)
|
|
|
|
.collect();
|
|
|
|
self.tokens.insert(token.to_owned());
|
|
|
|
return Some(token);
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
pub fn check_token(&self, token: &AccessToken) -> bool {
|
|
|
|
return self.tokens.contains(token);
|
|
|
|
}
|
|
|
|
}
|