diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-13 19:44:17 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-13 19:44:17 +0000 |
commit | d94f67e0c87b07bd747f357983253eee5c4a81b5 (patch) | |
tree | 7abd7ffccb5e7239f0f943465a07d11e75269eb5 /src/auth.rs | |
parent | Made code more idiomatic (diff) | |
download | miniserve-d94f67e0c87b07bd747f357983253eee5c4a81b5.tar.gz miniserve-d94f67e0c87b07bd747f357983253eee5c4a81b5.zip |
Fixed auth check
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/src/auth.rs b/src/auth.rs index e8600fb..10e7a4a 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -7,7 +7,6 @@ pub struct Auth; /// HTTP Basic authentication errors pub enum BasicAuthError { Base64DecodeError, - InvalidUsernameFormat, } #[derive(Clone, Debug)] @@ -24,13 +23,14 @@ pub fn parse_basic_auth( let basic_removed = authorization_header.to_str().unwrap().replace("Basic ", ""); let decoded = base64::decode(&basic_removed).map_err(|_| BasicAuthError::Base64DecodeError)?; let decoded_str = String::from_utf8_lossy(&decoded); - let strings: Vec<&str> = decoded_str.splitn(2, ':').collect(); - if strings.len() != 2 { - return Err(BasicAuthError::InvalidUsernameFormat); - } + let credentials: Vec<&str> = decoded_str.splitn(2, ':').collect(); + + // If argument parsing went fine, it means the HTTP credentials string is well formatted + // So we can safely unpack the username and the password + Ok(BasicAuthParams { - username: strings[0].to_owned(), - password: strings[1].to_owned(), + username: credentials[0].to_owned(), + password: credentials[1].to_owned(), }) } @@ -50,11 +50,6 @@ impl Middleware<crate::MiniserveConfig> for Auth { auth_headers.to_str().unwrap() )))); } - Err(BasicAuthError::InvalidUsernameFormat) => { - return Ok(Response::Done( - HttpResponse::BadRequest().body("Invalid basic auth format"), - )); - } }; if auth_req.username != required_auth.username || auth_req.password != required_auth.password |