From aa131d009f7411e9305458d581f17bd063c9e6c1 Mon Sep 17 00:00:00 2001 From: Hubert Date: Wed, 21 Jul 2021 13:16:09 +0200 Subject: [PATCH] save --- src/webutils/auth.rs | 30 +++++++++++++++++++++--------- src/webutils/mod.rs | 3 ++- src/webutils/user.rs | 0 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/webutils/user.rs diff --git a/src/webutils/auth.rs b/src/webutils/auth.rs index 03d158b..c236602 100644 --- a/src/webutils/auth.rs +++ b/src/webutils/auth.rs @@ -1,13 +1,19 @@ +use actix_session::Session; +use actix_web::Error; use actix_web_httpauth::extractors::basic::BasicAuth; -use std::borrow::Cow; pub trait AuthValidator { - fn check_user(&self, name : &String, pwd : &String) -> bool; + fn check_user(&self, name : &String, pwd : &String) -> Option; - fn check_basic(&self, basic : BasicAuth) -> bool { - match basic.password() { - None => {false} - Some(pwd) => {self.check_user(&basic.user_id().to_string(), &pwd.to_string())} + fn check_basic(&self, basic : &BasicAuth) -> Option { + basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string())) + } + + fn check_session(&self, session : &Session) -> Option { + let result = session.get::("user"); + match result { + Ok(username) => {username.map(|u| User(u))} + Err(e) => {None} } } } @@ -15,7 +21,13 @@ pub trait AuthValidator { pub struct TestValidator; impl AuthValidator for TestValidator { - fn check_user(&self, name: &String, pwd: &String) -> bool { - pwd.eq(&(name.clone() + "pwd")) //stub! + fn check_user(&self, name: &String, pwd: &String) -> Option { + if pwd.eq(&(name.clone() + "pwd")) { + Some(User(name.clone())) + } else { + None + } } -} \ No newline at end of file +} + +pub struct User(String); diff --git a/src/webutils/mod.rs b/src/webutils/mod.rs index 5696e21..e879062 100644 --- a/src/webutils/mod.rs +++ b/src/webutils/mod.rs @@ -1 +1,2 @@ -pub mod auth; \ No newline at end of file +pub mod auth; +pub mod user; \ No newline at end of file diff --git a/src/webutils/user.rs b/src/webutils/user.rs new file mode 100644 index 0000000..e69de29