3 releases (breaking)
0.3.0 | Aug 3, 2021 |
---|---|
0.2.0 | Aug 4, 2019 |
0.1.0 | Aug 3, 2019 |
#1296 in HTTP server
17KB
88 lines
gotham-middleware-basicauth
http basic auth middleware for Gotham framework
Usage
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.
todo
-
Add scoped protecd path -
More elegant error handle and log output -
Add doc -
Add unit test - Extend protocol to enable logout and login control
-
publish to crates.io -
doc for basic use and scoped path feature
Dependencies
~13–24MB
~334K SLoC