1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-25 11:32:50 +01:00

Added an example for Basic authentication

This commit is contained in:
svartalf 2018-05-20 22:10:48 +03:00
parent 65578ead02
commit d5eb47c6e0
5 changed files with 54 additions and 4 deletions

View File

@ -2,11 +2,11 @@
name = "actix-web-httpauth" name = "actix-web-httpauth"
version = "0.0.1" version = "0.0.1"
authors = ["svartalf <self@svartalf.info>"] authors = ["svartalf <self@svartalf.info>"]
description = "HTTP authorization schemes for actix-web" description = "HTTP authentication schemes for actix-web"
readme = "README.md" readme = "README.md"
keywords = ["http", "web", "framework"] keywords = ["http", "web", "framework"]
homepage = "https://github.com/svartalf/actix-web-httpauth" 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/" documentation = "https://docs.rs/actix-web-httpauth/"
categories = ["web-programming::http-server"] categories = ["web-programming::http-server"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"

View File

@ -4,4 +4,11 @@
![Docs](https://docs.rs/actix-web-httpauth/badge.svg) ![Docs](https://docs.rs/actix-web-httpauth/badge.svg)
![Crates.io](https://img.shields.io/crates/v/actix-web-httpauth.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)

41
examples/basic.rs Normal file
View File

@ -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<S> Middleware<S> for AuthMiddleware {
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
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();
}

View File

@ -58,7 +58,7 @@ impl ResponseError for AuthError {
}; };
HttpResponse::build(status) HttpResponse::build(status)
.header("WWW-Authenticate", "Basic, charset=\"UTF-8\"") .header("WWW-Authenticate", "Basic")
.finish() .finish()
} }

View File

@ -9,11 +9,13 @@ use errors::AuthError;
/// ///
/// # Example /// # Example
/// ///
/// ```rust
/// use actix_web_httpauth::BasicAuth; /// use actix_web_httpauth::BasicAuth;
/// ///
/// pub fn handler(auth: BasicAuth) -> String { /// pub fn handler(auth: BasicAuth) -> String {
/// format!("Hello, {}", auth.username) /// format!("Hello, {}", auth.username)
/// } /// }
/// ```
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct BasicAuth { pub struct BasicAuth {
pub username: String, pub username: String,