Compare commits

...

2 Commits

Author SHA1 Message Date
hubert e3008262fc change validator api 2021-07-22 13:24:11 +02:00
hubert abc1f367bb print error when validating 2021-07-22 13:14:58 +02:00
1 changed files with 19 additions and 10 deletions

View File

@ -1,19 +1,32 @@
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_user(&self, name : &String, pwd : &String) -> bool;
fn check_basic(&self, basic : &BasicAuth) -> Option<User> {
basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
match basic.password() {
Option::Some(pwd) => {
let username = basic.user_id().to_string();
if self.check_user(&username, &pwd.to_string()) {
Some(User(username))
} else {
None
}
}
Option::None => {None}
}
//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}
Err(e) => {
println!("{}", e);
None
}
}
}
}
@ -21,12 +34,8 @@ pub trait AuthValidator {
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
}
fn check_user(&self, name: &String, pwd: &String) -> bool {
pwd.eq(&(name.clone() + "pwd"))
}
}