From d5eb47c6e0b543e3f95a8c713b69d4195dfbade9 Mon Sep 17 00:00:00 2001 From: svartalf Date: Sun, 20 May 2018 22:10:48 +0300 Subject: [PATCH] Added an example for Basic authentication --- Cargo.toml | 4 ++-- README.md | 9 ++++++++- examples/basic.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/errors.rs | 2 +- src/schemes/basic.rs | 2 ++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 examples/basic.rs diff --git a/Cargo.toml b/Cargo.toml index ca6bde05c..7ac8b4243 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,11 @@ name = "actix-web-httpauth" version = "0.0.1" authors = ["svartalf "] -description = "HTTP authorization schemes for actix-web" +description = "HTTP authentication schemes for actix-web" readme = "README.md" keywords = ["http", "web", "framework"] homepage = "https://github.com/svartalf/actix-web-httpauth" -repository = "https://github.com/svartlaf/actix-web-httpauth.git" +repository = "https://github.com/svartalf/actix-web-httpauth.git" documentation = "https://docs.rs/actix-web-httpauth/" categories = ["web-programming::http-server"] license = "MIT/Apache-2.0" diff --git a/README.md b/README.md index fa9fa396f..32011ba0d 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,11 @@ ![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. +HTTP authentication schemes for [actix-web](https://github.com/actix/actix-web) framework. + +All supported schemas are actix [Extractors](https://docs.rs/actix-web/0.6.7/actix_web/trait.FromRequest.html), +and can be used both in middlewares and request handlers, check the `examples/` folder. + +## Supported schemes + + * [Basic](https://tools.ietf.org/html/rfc7617) diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 000000000..2ff715fd9 --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,41 @@ +extern crate actix_web; +extern crate actix_web_httpauth; + +use actix_web::http::StatusCode; +use actix_web::{server, App, HttpRequest, FromRequest, Result}; +use actix_web::middleware::{Middleware, Started}; +use actix_web_httpauth::BasicAuth; + +struct AuthMiddleware; + +impl Middleware for AuthMiddleware { + fn start(&self, req: &mut HttpRequest) -> Result { + let auth = BasicAuth::extract(&req)?; + + // Please note that this is only an example, + // do not ever hardcode your credentials! + if auth.username == "root" && auth.password == "pass" { + Ok(Started::Done) + } else { + let response = req.build_response(StatusCode::UNAUTHORIZED) + .header("WWW-Authenticate", "Basic") + .finish(); + Ok(Started::Response(response)) + } + } + +} + +fn index(auth: BasicAuth) -> String { + format!("Hello, {}", auth.username) +} + +fn main() { + server::new(|| App::new() + // Comment line below to pass authentication handling + // directly to `index` handler. + .middleware(AuthMiddleware) + .resource("/", |r| r.with(index))) + .bind("127.0.0.1:8088").unwrap() + .run(); +} diff --git a/src/errors.rs b/src/errors.rs index 8870d0a19..59ccd3db5 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -58,7 +58,7 @@ impl ResponseError for AuthError { }; HttpResponse::build(status) - .header("WWW-Authenticate", "Basic, charset=\"UTF-8\"") + .header("WWW-Authenticate", "Basic") .finish() } diff --git a/src/schemes/basic.rs b/src/schemes/basic.rs index 28afbf7df..89990c5bf 100644 --- a/src/schemes/basic.rs +++ b/src/schemes/basic.rs @@ -9,11 +9,13 @@ use errors::AuthError; /// /// # 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,