From 8af3ff10e2347da70c35eb45046f8a04843f7256 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Sun, 21 Apr 2019 16:27:34 +0200 Subject: Rework error system + avoid panics in main() --- src/auth.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index 10e7a4a..8cedaae 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -2,12 +2,9 @@ use actix_web::http::header; use actix_web::middleware::{Middleware, Response}; use actix_web::{HttpRequest, HttpResponse, Result}; -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 @@ -19,9 +16,17 @@ pub struct BasicAuthParams { /// Decode a HTTP basic auth string into a tuple of username and password. pub fn parse_basic_auth( authorization_header: &header::HeaderValue, -) -> Result { - let basic_removed = authorization_header.to_str().unwrap().replace("Basic ", ""); - let decoded = base64::decode(&basic_removed).map_err(|_| BasicAuthError::Base64DecodeError)?; +) -> Result { + 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(); @@ -44,11 +49,11 @@ impl Middleware 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) => { + Err(err) => { return Ok(Response::Done(HttpResponse::BadRequest().body(format!( - "Error decoding basic auth base64: '{}'", - auth_headers.to_str().unwrap() - )))); + "An error occured during HTTP authentication\ncaused by: {}", + err + )))) } }; if auth_req.username != required_auth.username -- cgit v1.2.3 From 4ddade5bb351b454ebc7c9d6ad69ea40549cf9b1 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Mon, 22 Apr 2019 12:08:40 +0200 Subject: Improved errors --- src/auth.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index 8cedaae..1bdf0be 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -50,10 +50,12 @@ impl Middleware for Auth { let auth_req = match parse_basic_auth(auth_headers) { Ok(auth_req) => auth_req, Err(err) => { - return Ok(Response::Done(HttpResponse::BadRequest().body(format!( - "An error occured during HTTP authentication\ncaused by: {}", - err - )))) + let auth_err = ContextualError::new( + ContextualErrorKind::HTTPAuthenticationError(Box::new(err)), + ); + return Ok(Response::Done( + HttpResponse::BadRequest().body(auth_err.to_string()), + )); } }; if auth_req.username != required_auth.username -- cgit v1.2.3