From 251f20858d8e48ebc045cfdd997c1e40dffdbf11 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Wed, 13 Oct 2021 05:09:04 +0300 Subject: revert unnecessary changes --- src/auth.rs | 64 +++++++++++++++++-------------------------------------------- 1 file changed, 18 insertions(+), 46 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index 82b407c..b4717d1 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,8 +1,7 @@ -use actix_web::dev::{Service, ServiceRequest, ServiceResponse}; -use actix_web::{HttpRequest, ResponseError}; -use futures::future::Either; +use actix_web::dev::ServiceRequest; +use actix_web::HttpMessage; +use actix_web_httpauth::extractors::basic::BasicAuth; use sha2::{Digest, Sha256, Sha512}; -use std::future::{ready, Future}; use crate::errors::ContextualError; @@ -13,16 +12,12 @@ pub struct BasicAuthParams { pub password: String, } -impl BasicAuthParams { - fn try_from_request(req: &HttpRequest) -> actix_web::Result { - use actix_web::http::header::Header; - use actix_web_httpauth::headers::authorization::{Authorization, Basic}; - - let auth = Authorization::::parse(req)?.into_scheme(); - Ok(Self { +impl From for BasicAuthParams { + fn from(auth: BasicAuth) -> Self { + Self { username: auth.user_id().to_string(), password: auth.password().unwrap_or(&"".into()).to_string(), - }) + } } } @@ -74,47 +69,24 @@ pub fn get_hash(text: &str) -> Vec { hasher.update(text); hasher.finalize().to_vec() } + pub struct CurrentUser { pub name: String, } -fn handle_auth(req: &HttpRequest) -> Result<(), ContextualError> { +pub async fn handle_auth( + req: ServiceRequest, + cred: BasicAuth, +) -> actix_web::Result { let required_auth = &req.app_data::().unwrap().auth; - if required_auth.is_empty() { - // auth is disabled by configuration - return Ok(()); - } - - match BasicAuthParams::try_from_request(req) { - Ok(cred) => match match_auth(&cred, required_auth) { - true => { - req.extensions_mut().insert(CurrentUser { - name: cred.username, - }); - Ok(()) - } - false => Err(ContextualError::InvalidHttpCredentials), - }, - Err(_) => Err(ContextualError::RequireHttpCredentials), - } -} + req.extensions_mut().insert(CurrentUser { + name: cred.user_id().to_string(), + }); -pub fn auth_middleware( - mut req: ServiceRequest, - srv: &S, -) -> impl Future> + 'static -where - S: Service, - S::Future: 'static, -{ - match handle_auth(req.parts_mut().0) { - Ok(_) => Either::Left(srv.call(req)), - Err(err) => { - let resp = req.into_response(err.error_response()); - Either::Right(ready(Ok(resp))) - } - } + match_auth(&cred.into(), required_auth) + .then(|| req) + .ok_or_else(|| ContextualError::InvalidHttpCredentials.into()) } #[rustfmt::skip] -- cgit v1.2.3