gitust/src/webutils/auth.rs

40 lines
1.1 KiB
Rust

use actix_session::Session;
use actix_web::Error;
use actix_web_httpauth::extractors::basic::BasicAuth;
pub trait AuthValidator {
fn check_user(&self, name : &String, pwd : &String) -> Option<User>;
fn check_basic(&self, basic : &BasicAuth) -> Option<User> {
basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
}
fn check_session(&self, session : &Session) -> Option<User> {
let result = session.get::<String>("user");
match result {
Ok(username) => {username.map(|u| User(u))}
Err(e) => {None}
}
}
}
pub struct TestValidator;
impl AuthValidator for TestValidator {
fn check_user(&self, name: &String, pwd: &String) -> Option<User> {
if pwd.eq(&(name.clone() + "pwd")) {
Some(User(name.clone()))
} else {
None
}
}
}
pub struct User(String);
impl User {
pub fn get_name(&self) -> String {
self.0.clone()
}
}