Crates.io | gotham-middleware-basicauth |
lib.rs | gotham-middleware-basicauth |
version | 0.3.0 |
source | src |
created_at | 2019-08-03 02:32:40.11797 |
updated_at | 2021-08-03 02:22:03.471503 |
description | http basic auth middleware for gotham framework |
homepage | https://github.com/PrivateRookie/gotham-middleware-basicauth |
repository | https://github.com/PrivateRookie/gotham-middleware-basicauth |
max_upload_size | |
id | 153901 |
size | 56,333 |
http basic auth middleware for Gotham framework
this code take from examples/basic-auth/main.rs
use gotham::pipeline::new_pipeline;
use gotham::pipeline::single::single_pipeline;
use gotham::router::builder::*;
use gotham::router::Router;
use gotham::state::State;
use gotham_middleware_basicauth::AuthMiddleware;
fn router() -> Router {
// default allow user admin login with password "admin"
// and protect all paths
let (chain, pipeline) = single_pipeline(
new_pipeline().add(AuthMiddleware::default()).build(),
);
build_router(chain, pipeline, |route| {
route.get("/").to(say_hello);
route.get("/abc").to(say_hello);
})
}
fn say_hello(state: State) -> (State, &'static str) {
(state, "Hello Auththorized User!")
}
fn main() {
let addr = "0.0.0.0:8000";
println!("gotham is running on 0.0.0.0:8000, login with admin/admin");
gotham::start(addr, router());
}
as you can ses, it's eazy to use, default
method return a middleware that required
to login as admin with password admin when visiting web site at the first time.
You can create a new middleware manually, codes in example scoped-auth
show how to create a new middleware:
let middleware: AuthMiddleware = AuthMiddleware {
userlist: vec!["admin:admin".to_owned()],
scopes: vec!["/scoped".to_owned()],
};
You can pass a list of user with format "username:password", and a list of path you want to protect by basic auth. Note that if a path is protected, it's subpath will be protected too.
To run these examples, run
cargo run --example basic-auth
or
cargo run --example scoped-auth
and then open http://localhost:8000
on your browser.