diff options
author | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-05-01 11:17:13 +0000 |
---|---|---|
committer | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-05-01 11:17:13 +0000 |
commit | f53a09b8d79c85b86b161c1ec7ca6f7cff5e169f (patch) | |
tree | 91debd6f936e9da20e5bdcd6fb7afa363bcff61c /src | |
parent | Make error page uniform (diff) | |
download | miniserve-f53a09b8d79c85b86b161c1ec7ca6f7cff5e169f.tar.gz miniserve-f53a09b8d79c85b86b161c1ec7ca6f7cff5e169f.zip |
Display HTTP authentication errors
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 43 | ||||
-rw-r--r-- | src/errors.rs | 4 | ||||
-rw-r--r-- | src/renderer.rs | 17 |
3 files changed, 55 insertions, 9 deletions
diff --git a/src/auth.rs b/src/auth.rs index c786d4b..b5a9867 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -3,7 +3,8 @@ use actix_web::middleware::{Middleware, Response}; use actix_web::{HttpRequest, HttpResponse, Result}; use sha2::{Digest, Sha256, Sha512}; -use crate::errors::ContextualError; +use crate::errors::{self, ContextualError}; +use crate::renderer; pub struct Auth; @@ -96,13 +97,19 @@ impl Middleware<crate::MiniserveConfig> for Auth { Err(err) => { let auth_err = ContextualError::HTTPAuthenticationError(Box::new(err)); return Ok(Response::Done( - HttpResponse::BadRequest().body(auth_err.to_string()), + HttpResponse::BadRequest() + .body(build_unauthorized_response(&req, auth_err, true)), )); } }; if !match_auth(auth_req, required_auth) { - let new_resp = HttpResponse::Unauthorized().finish(); - return Ok(Response::Done(new_resp)); + return Ok(Response::Done(HttpResponse::Unauthorized().body( + build_unauthorized_response( + &req, + ContextualError::InvalidHTTPCredentials, + true, + ), + ))); } } else { let new_resp = HttpResponse::Unauthorized() @@ -110,7 +117,11 @@ impl Middleware<crate::MiniserveConfig> for Auth { header::WWW_AUTHENTICATE, header::HeaderValue::from_static("Basic realm=\"miniserve\""), ) - .finish(); + .body(build_unauthorized_response( + &req, + ContextualError::InvalidHTTPCredentials, + false, + )); return Ok(Response::Done(new_resp)); } } @@ -118,6 +129,28 @@ impl Middleware<crate::MiniserveConfig> for Auth { } } +fn build_unauthorized_response( + req: &HttpRequest<crate::MiniserveConfig>, + error: ContextualError, + log_error_chain: bool, +) -> String { + let error = ContextualError::HTTPAuthenticationError(Box::new(error)); + + if log_error_chain { + errors::log_error_chain(error.to_string()); + } + renderer::render_error( + &error.to_string(), + req.path(), + None, + None, + req.state().default_color_scheme, + req.state().default_color_scheme, + false, + ) + .into_string() +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/errors.rs b/src/errors.rs index 8264de0..a22127e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -65,6 +65,10 @@ pub enum ContextualError { )] HTTPAuthenticationError(Box<ContextualError>), + /// This error might occur when the HTTP credentials are not correct + #[fail(display = "Invalid credentials for HTTP authentication")] + InvalidHTTPCredentials, + /// This error might occur when an HTTP request is invalid #[fail(display = "Invalid HTTP request\ncaused by: {}", _0)] InvalidHTTPRequestError(String), diff --git a/src/renderer.rs b/src/renderer.rs index ecc6bfe..03c46d8 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -32,7 +32,7 @@ pub fn page( ); html! { - (page_header(serve_path, color_scheme, file_upload)) + (page_header(serve_path, color_scheme, file_upload, false)) body#drop-container { @if file_upload { div.drag-form { @@ -760,14 +760,23 @@ fn chevron_down() -> Markup { } /// Partial: page header -fn page_header(serve_path: &str, color_scheme: ColorScheme, file_upload: bool) -> Markup { +fn page_header( + serve_path: &str, + color_scheme: ColorScheme, + file_upload: bool, + is_error: bool, +) -> Markup { html! { (DOCTYPE) html { meta charset="utf-8"; meta http-equiv="X-UA-Compatible" content="IE=edge"; meta name="viewport" content="width=device-width, initial-scale=1"; - title { "Index of " (serve_path) } + @if is_error { + title { (serve_path) } + } else { + title { "Index of " (serve_path) } + } style { (css(color_scheme)) } @if file_upload { (PreEscaped(r#" @@ -858,7 +867,7 @@ pub fn render_error( html! { body { - (page_header("Error", color_scheme, false)) + (page_header("Error", color_scheme, false, true)) div.error { p { "Error" } @for error in error_description.lines() { |