From 09f9a82a0443cf58d141e1d045ff02676bafa7cc Mon Sep 17 00:00:00 2001 From: svartalf Date: Wed, 23 May 2018 15:37:44 +0300 Subject: [PATCH] Cleaning up --- src/basic/config.rs | 4 -- src/challenge.rs | 6 --- src/lib.rs | 1 - src/schemes/basic.rs | 122 ------------------------------------------- src/schemes/mod.rs | 3 -- 5 files changed, 136 deletions(-) delete mode 100644 src/challenge.rs delete mode 100644 src/schemes/basic.rs delete mode 100644 src/schemes/mod.rs diff --git a/src/basic/config.rs b/src/basic/config.rs index 298ffac3e..a11816c7a 100644 --- a/src/basic/config.rs +++ b/src/basic/config.rs @@ -4,8 +4,6 @@ use bytes::Bytes; use percent_encoding; use actix_web::http::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue}; -use challenge::Challenge; - /// Challenge configuration for [BasicAuth](./struct.BasicAuth.html) extractor. #[derive(Debug, Clone)] pub struct Config { @@ -57,5 +55,3 @@ impl Default for Config { } } } - -impl Challenge for Config {} diff --git a/src/challenge.rs b/src/challenge.rs deleted file mode 100644 index de2eb566b..000000000 --- a/src/challenge.rs +++ /dev/null @@ -1,6 +0,0 @@ -use std::fmt::Debug; -use std::default::Default; - -use actix_web::http::header::IntoHeaderValue; - -pub trait Challenge: 'static + Debug + Clone + Send + Sync + IntoHeaderValue + Default {} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 40aa75125..bcd182dcd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,5 +9,4 @@ extern crate actix_web; extern crate base64; mod errors; -mod challenge; pub mod basic; diff --git a/src/schemes/basic.rs b/src/schemes/basic.rs deleted file mode 100644 index 89990c5bf..000000000 --- a/src/schemes/basic.rs +++ /dev/null @@ -1,122 +0,0 @@ -use base64; -use actix_web::{HttpRequest, HttpMessage, FromRequest}; - -use errors::AuthError; - -/// Extractor for `Authorization: Basic {payload}` HTTP request header. -/// -/// If header is not present, HTTP 401 will be returned. -/// -/// # Example -/// -/// ```rust -/// use actix_web_httpauth::BasicAuth; -/// -/// pub fn handler(auth: BasicAuth) -> String { -/// format!("Hello, {}", auth.username) -/// } -/// ``` -#[derive(Debug, PartialEq)] -pub struct BasicAuth { - pub username: String, - pub password: String, -} - - -impl FromRequest for BasicAuth { - type Config = (); - type Result = Result; - - fn from_request(req: &HttpRequest, _cfg: &>::Config) -> >::Result { - let header = req.headers().get("Authorization") - .ok_or(AuthError::HeaderMissing)? - .to_str()?; - let mut parts = header.splitn(2, ' '); - - // Authorization mechanism - match parts.next() { - None => return Err(AuthError::InvalidMechanism), - Some(mechanism) if mechanism != "Basic" => return Err(AuthError::InvalidMechanism), - _ => () - } - - // Authorization payload - let payload = parts.next().ok_or(AuthError::HeaderMalformed)?; - let payload = base64::decode(payload)?; - let payload = String::from_utf8(payload)?; - let mut parts = payload.splitn(2, ':'); - let user = parts.next().ok_or(AuthError::HeaderMalformed)?; - let password = parts.next().ok_or(AuthError::HeaderMalformed)?; - - Ok(BasicAuth{ - username: user.to_string(), - password: password.to_string(), - }) - } -} - -#[cfg(test)] -mod tests { - use base64; - use actix_web::FromRequest; - use actix_web::test::TestRequest; - - use super::{BasicAuth, AuthError}; - - #[test] - fn test_valid_auth() { - let value = format!("Basic {}", base64::encode("user:pass")); - let req = TestRequest::with_header("Authorization", value).finish(); - let auth = BasicAuth::extract(&req); - - assert!(auth.is_ok()); - let auth = auth.unwrap(); - assert_eq!(auth.username, "user".to_string()); - assert_eq!(auth.password, "pass".to_string()); - } - - #[test] - fn test_missing_header() { - let req = TestRequest::default().finish(); - let auth = BasicAuth::extract(&req); - - assert!(auth.is_err()); - let err = auth.err().unwrap(); - assert_eq!(err, AuthError::HeaderMissing); - } - - #[test] - fn test_invalid_mechanism() { - let value = format!("Digest {}", base64::encode("user:pass")); - let req = TestRequest::with_header("Authorization", value).finish(); - let auth = BasicAuth::extract(&req); - - assert!(auth.is_err()); - let err = auth.err().unwrap(); - assert_eq!(err, AuthError::InvalidMechanism); - } - - #[test] - fn test_invalid_format() { - let value = format!("Basic {}", base64::encode("user")); - let req = TestRequest::with_header("Authorization", value).finish(); - let auth = BasicAuth::extract(&req); - - assert!(auth.is_err()); - let err = auth.err().unwrap(); - assert_eq!(err, AuthError::HeaderMalformed); - } - - #[test] - fn test_user_without_password() { - let value = format!("Basic {}", base64::encode("user:")); - let req = TestRequest::with_header("Authorization", value).finish(); - let auth = BasicAuth::extract(&req); - - assert!(auth.is_ok()); - assert_eq!(auth.unwrap(), BasicAuth { - username: "user".to_string(), - password: "".to_string(), - }) - } -} diff --git a/src/schemes/mod.rs b/src/schemes/mod.rs deleted file mode 100644 index 43d195913..000000000 --- a/src/schemes/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod basic; - -pub use self::basic::BasicAuth;