mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
Added an example for Basic authentication
This commit is contained in:
parent
65578ead02
commit
d5eb47c6e0
@ -2,11 +2,11 @@
|
||||
name = "actix-web-httpauth"
|
||||
version = "0.0.1"
|
||||
authors = ["svartalf <self@svartalf.info>"]
|
||||
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"
|
||||
|
@ -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)
|
||||
|
41
examples/basic.rs
Normal file
41
examples/basic.rs
Normal 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();
|
||||
}
|
@ -58,7 +58,7 @@ impl ResponseError for AuthError {
|
||||
};
|
||||
|
||||
HttpResponse::build(status)
|
||||
.header("WWW-Authenticate", "Basic, charset=\"UTF-8\"")
|
||||
.header("WWW-Authenticate", "Basic")
|
||||
.finish()
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user