Hastic standalone https://hastic.io
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.

41 lines
975 B

3 years ago
use std::collections::HashSet;
3 years ago
use serde::{Deserialize, Serialize};
3 years ago
use std::iter::repeat_with;
3 years ago
pub type AccessToken = String;
3 years ago
const TOKEN_LENGTH: usize = 30;
3 years ago
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User {
pub username: String,
3 years ago
pub password: String,
3 years ago
}
pub struct UserService {
3 years ago
tokens: HashSet<AccessToken>,
3 years ago
}
impl UserService {
pub fn new() -> UserService {
3 years ago
UserService {
tokens: HashSet::new(),
}
3 years ago
}
pub fn login(&mut self, user: &User) -> Option<AccessToken> {
3 years ago
if user.username == "admin" && user.password == "admin" {
3 years ago
let token: AccessToken = repeat_with(fastrand::alphanumeric)
.take(TOKEN_LENGTH)
.collect();
self.tokens.insert(token.to_owned());
return Some(token);
3 years ago
}
return None;
}
3 years ago
pub fn check_token(&self, token: &AccessToken) -> bool {
return self.tokens.contains(token);
}
3 years ago
}