From 5964f4d767b8ea45cebefa2614ea750fdd1a9f8b Mon Sep 17 00:00:00 2001 From: svartalf Date: Sun, 20 May 2018 13:15:59 +0300 Subject: [PATCH] Badges and stable fixes --- Cargo.toml | 4 ++++ README.md | 6 ++++++ src/errors.rs | 46 +++++++++++++++++++++++--------------------- src/lib.rs | 2 +- src/schemes/basic.rs | 24 +++++++++++------------ 5 files changed, 47 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 773d4c264..ca6bde05c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ repository = "https://github.com/svartlaf/actix-web-httpauth.git" documentation = "https://docs.rs/actix-web-httpauth/" categories = ["web-programming::http-server"] license = "MIT/Apache-2.0" +exclude = [".travis.yml", ".gitignore"] + +[badges] +travis-ci = { repository = "svartalf/actix-web-httpauth", branch = "master" } [dependencies] actix-web = "0.6" diff --git a/README.md b/README.md index cd9f91f30..89d3a8f63 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # actix-web-httpauth + +[![Build Status](https://travis-ci.org/svartalf/actix-web-httpauth.svg?branch=master)] +[![Docs](https://docs.rs/actix-web-httpauth/badge.svg)] +[![Crates.io](https://img.shields.io/crates/v/actix-web-httpauth.svg)] + +HTTP authorization schemes for [actix-web](https://github.com/actix/actix-web) framework. diff --git a/src/errors.rs b/src/errors.rs index f85fd7fd9..8870d0a19 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::error; +use std::error::Error; use std::string; use base64; @@ -8,51 +8,53 @@ use actix_web::error::ResponseError; use actix_web::http::{StatusCode, header}; #[derive(Debug, PartialEq)] -pub enum Error { +pub enum AuthError { HeaderMissing, // HTTP 401 // TODO: Ensure that 401 should be returned if not a `Basic` mechanism is received InvalidMechanism, // HTTP 401 ? HeaderMalformed, // HTTP 400 } -impl fmt::Display for Error { +impl fmt::Display for AuthError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let msg = match self { - Error::HeaderMissing => "HTTP 'Authorization' header is missing", - Error::InvalidMechanism => "Wrong mechanizm for a HTTP 'Authorization' header, expected 'Basic'", - Error::HeaderMalformed => "Malformed HTTP 'Authorization' header", - }; - - f.write_str(msg) + f.write_str(self.description()) } } -impl error::Error for Error {} +impl Error for AuthError { + fn description(&self) -> &str { + match *self { + AuthError::HeaderMissing => "HTTP 'Authorization' header is missing", + AuthError::InvalidMechanism => "Wrong mechanism for a HTTP 'Authorization' header, expected 'Basic'", + AuthError::HeaderMalformed => "Malformed HTTP 'Authorization' header", + } + } +} -impl From for Error { +impl From for AuthError { fn from(_: header::ToStrError) -> Self { - Error::HeaderMalformed + AuthError::HeaderMalformed } } -impl From for Error { +impl From for AuthError { fn from(_: base64::DecodeError) -> Self { - Error::HeaderMalformed + AuthError::HeaderMalformed } } -impl From for Error { +impl From for AuthError { fn from(_: string::FromUtf8Error) -> Self { - Error::HeaderMalformed + AuthError::HeaderMalformed } } -impl ResponseError for Error { +impl ResponseError for AuthError { fn error_response(&self) -> HttpResponse { - let status = match self { - Error::HeaderMissing => StatusCode::UNAUTHORIZED, - Error::InvalidMechanism => StatusCode::UNAUTHORIZED, - Error::HeaderMalformed => StatusCode::BAD_REQUEST, + let status = match *self { + AuthError::HeaderMissing => StatusCode::UNAUTHORIZED, + AuthError::InvalidMechanism => StatusCode::UNAUTHORIZED, + AuthError::HeaderMalformed => StatusCode::BAD_REQUEST, }; HttpResponse::build(status) diff --git a/src/lib.rs b/src/lib.rs index 67bbfb6f1..1b1d0654b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,4 @@ mod schemes; mod errors; pub use schemes::*; -pub use errors::Error; +pub use errors::AuthError; diff --git a/src/schemes/basic.rs b/src/schemes/basic.rs index 3000abaec..28afbf7df 100644 --- a/src/schemes/basic.rs +++ b/src/schemes/basic.rs @@ -1,7 +1,7 @@ use base64; use actix_web::{HttpRequest, HttpMessage, FromRequest}; -use errors::Error; +use errors::AuthError; /// Extractor for `Authorization: Basic {payload}` HTTP request header. /// @@ -23,28 +23,28 @@ pub struct BasicAuth { impl FromRequest for BasicAuth { type Config = (); - type Result = Result; + type Result = Result; fn from_request(req: &HttpRequest, _cfg: &>::Config) -> >::Result { let header = req.headers().get("Authorization") - .ok_or(Error::HeaderMissing)? + .ok_or(AuthError::HeaderMissing)? .to_str()?; let mut parts = header.splitn(2, ' '); // Authorization mechanism match parts.next() { - None => return Err(Error::InvalidMechanism), - Some(mechanism) if mechanism != "Basic" => return Err(Error::InvalidMechanism), + None => return Err(AuthError::InvalidMechanism), + Some(mechanism) if mechanism != "Basic" => return Err(AuthError::InvalidMechanism), _ => () } // Authorization payload - let payload = parts.next().ok_or(Error::HeaderMalformed)?; + 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(Error::HeaderMalformed)?; - let password = parts.next().ok_or(Error::HeaderMalformed)?; + let user = parts.next().ok_or(AuthError::HeaderMalformed)?; + let password = parts.next().ok_or(AuthError::HeaderMalformed)?; Ok(BasicAuth{ username: user.to_string(), @@ -59,7 +59,7 @@ mod tests { use actix_web::FromRequest; use actix_web::test::TestRequest; - use super::{BasicAuth, Error}; + use super::{BasicAuth, AuthError}; #[test] fn test_valid_auth() { @@ -80,7 +80,7 @@ mod tests { assert!(auth.is_err()); let err = auth.err().unwrap(); - assert_eq!(err, Error::HeaderMissing); + assert_eq!(err, AuthError::HeaderMissing); } #[test] @@ -91,7 +91,7 @@ mod tests { assert!(auth.is_err()); let err = auth.err().unwrap(); - assert_eq!(err, Error::InvalidMechanism); + assert_eq!(err, AuthError::InvalidMechanism); } #[test] @@ -102,7 +102,7 @@ mod tests { assert!(auth.is_err()); let err = auth.err().unwrap(); - assert_eq!(err, Error::HeaderMalformed); + assert_eq!(err, AuthError::HeaderMalformed); } #[test]