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.

39 lines
961 B

use std::{collections::HashSet};
3 years ago
use serde::{ Deserialize, Serialize };
use std::iter::repeat_with;
3 years ago
pub type AccessToken = String;
const TOKEN_LENGTH: usize = 20;
3 years ago
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User {
pub username: String,
pub password: String
}
pub struct UserService {
tokens: HashSet<AccessToken>
3 years ago
}
impl UserService {
pub fn new() -> UserService {
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" {
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;
}
pub fn check_token(&self, username: &String, token: &AccessToken ) -> bool {
return self.tokens.contains(token);
}
3 years ago
}