diff options
author | khai96_ <hvksmr1996@gmail.com> | 2019-04-26 06:04:16 +0000 |
---|---|---|
committer | khai96_ <hvksmr1996@gmail.com> | 2019-04-26 06:04:16 +0000 |
commit | 838d86655fb39b5cdf63b2d3823ce047e63afaf4 (patch) | |
tree | cf5306f1050108f872d6d7d020606f5dc5c90206 /src/auth.rs | |
parent | Use rstest_parametrize for unit tests (diff) | |
parent | Use rstest test fixtures to cut down on code duplication in integration tests (diff) | |
download | miniserve-838d86655fb39b5cdf63b2d3823ce047e63afaf4.tar.gz miniserve-838d86655fb39b5cdf63b2d3823ce047e63afaf4.zip |
Merge remote-tracking branch 'mainrepo/master' into pullrequest.hashed-password
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/auth.rs b/src/auth.rs index 2db422d..432f6ce 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -3,12 +3,9 @@ use actix_web::middleware::{Middleware, Response}; use actix_web::{HttpRequest, HttpResponse, Result}; use sha2::{Digest, Sha256, Sha512}; -pub struct Auth; +use crate::errors::{ContextualError, ContextualErrorKind}; -/// HTTP Basic authentication errors -pub enum BasicAuthError { - Base64DecodeError, -} +pub struct Auth; #[derive(Clone, Debug)] /// HTTP Basic authentication parameters @@ -34,9 +31,17 @@ pub struct RequiredAuth { /// Decode a HTTP basic auth string into a tuple of username and password. pub fn parse_basic_auth( authorization_header: &header::HeaderValue, -) -> Result<BasicAuthParams, BasicAuthError> { - let basic_removed = authorization_header.to_str().unwrap().replace("Basic ", ""); - let decoded = base64::decode(&basic_removed).map_err(|_| BasicAuthError::Base64DecodeError)?; +) -> Result<BasicAuthParams, ContextualError> { + let basic_removed = authorization_header + .to_str() + .map_err(|e| { + ContextualError::new(ContextualErrorKind::ParseError( + "HTTP authentication header".to_string(), + e.to_string(), + )) + })? + .replace("Basic ", ""); + let decoded = base64::decode(&basic_removed).map_err(ContextualErrorKind::Base64DecodeError)?; let decoded_str = String::from_utf8_lossy(&decoded); let credentials: Vec<&str> = decoded_str.splitn(2, ':').collect(); @@ -87,11 +92,13 @@ impl Middleware<crate::MiniserveConfig> for Auth { if let Some(auth_headers) = req.headers().get(header::AUTHORIZATION) { let auth_req = match parse_basic_auth(auth_headers) { Ok(auth_req) => auth_req, - Err(BasicAuthError::Base64DecodeError) => { - return Ok(Response::Done(HttpResponse::BadRequest().body(format!( - "Error decoding basic auth base64: '{}'", - auth_headers.to_str().unwrap() - )))); + Err(err) => { + let auth_err = ContextualError::new( + ContextualErrorKind::HTTPAuthenticationError(Box::new(err)), + ); + return Ok(Response::Done( + HttpResponse::BadRequest().body(auth_err.to_string()), + )); } }; if !match_auth(auth_req, required_auth) { |