save
This commit is contained in:
parent
9af4fa56e1
commit
aa131d009f
|
@ -1,13 +1,19 @@
|
||||||
|
use actix_session::Session;
|
||||||
|
use actix_web::Error;
|
||||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
pub trait AuthValidator {
|
pub trait AuthValidator {
|
||||||
fn check_user(&self, name : &String, pwd : &String) -> bool;
|
fn check_user(&self, name : &String, pwd : &String) -> Option<User>;
|
||||||
|
|
||||||
fn check_basic(&self, basic : BasicAuth) -> bool {
|
fn check_basic(&self, basic : &BasicAuth) -> Option<User> {
|
||||||
match basic.password() {
|
basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
|
||||||
None => {false}
|
}
|
||||||
Some(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}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +21,13 @@ pub trait AuthValidator {
|
||||||
pub struct TestValidator;
|
pub struct TestValidator;
|
||||||
|
|
||||||
impl AuthValidator for TestValidator {
|
impl AuthValidator for TestValidator {
|
||||||
fn check_user(&self, name: &String, pwd: &String) -> bool {
|
fn check_user(&self, name: &String, pwd: &String) -> Option<User> {
|
||||||
pwd.eq(&(name.clone() + "pwd")) //stub!
|
if pwd.eq(&(name.clone() + "pwd")) {
|
||||||
|
Some(User(name.clone()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct User(String);
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod user;
|
Loading…
Reference in New Issue