mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-20 23:07:01 +00:00
implement auth parsing/verifying inside a gotham middleware
This commit is contained in:
parent
c025cbd8ea
commit
088774fc50
9 changed files with 721 additions and 49 deletions
|
@ -17,7 +17,7 @@ gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
|
|||
fake = "2.2"
|
||||
gotham = "0.4"
|
||||
gotham_derive = "0.4"
|
||||
gotham_restful = { version = "0.0.1", features = ["openapi"] }
|
||||
gotham_restful = { version = "0.0.1", features = ["auth", "openapi"] }
|
||||
hyper = "0.12"
|
||||
log = "0.4"
|
||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
||||
|
|
|
@ -23,6 +23,12 @@ struct Users
|
|||
{
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
#[rest_resource(ReadAll)]
|
||||
struct Auth
|
||||
{
|
||||
}
|
||||
|
||||
#[derive(Deserialize, OpenapiType, Serialize, StateData, StaticResponseExtender)]
|
||||
struct User
|
||||
{
|
||||
|
@ -82,8 +88,24 @@ fn delete(_state : &mut State, id : u64)
|
|||
info!("Delete User {}", id);
|
||||
}
|
||||
|
||||
#[rest_read_all(Auth)]
|
||||
fn auth_read_all(auth : AuthStatus<()>) -> Success<String>
|
||||
{
|
||||
format!("{:?}", auth).into()
|
||||
}
|
||||
|
||||
const ADDR : &str = "127.0.0.1:18080";
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct Handler;
|
||||
impl<T> AuthHandler<T> for Handler
|
||||
{
|
||||
fn jwt_secret<F : FnOnce() -> Option<T>>(&self, _state : &mut State, _decode_data : F) -> Option<Vec<u8>>
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
let encoder = PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S%.3f %Z)} [{l}] {M} - {m}\n");
|
||||
|
@ -99,9 +121,11 @@ fn main()
|
|||
.unwrap();
|
||||
log4rs::init_config(config).unwrap();
|
||||
|
||||
let auth = <AuthMiddleware<(), Handler>>::from_source(AuthSource::AuthorizationHeader);
|
||||
let logging = RequestLogger::new(log::Level::Info);
|
||||
let (chain, pipelines) = single_pipeline(
|
||||
new_pipeline()
|
||||
.add(auth)
|
||||
.add(logging)
|
||||
.build()
|
||||
);
|
||||
|
@ -109,6 +133,7 @@ fn main()
|
|||
gotham::start(ADDR, build_router(chain, pipelines, |route| {
|
||||
route.with_openapi("Users Example", "0.0.1", format!("http://{}", ADDR), |mut route| {
|
||||
route.resource::<Users, _>("users");
|
||||
route.resource::<Auth, _>("auth");
|
||||
route.get_openapi("openapi");
|
||||
});
|
||||
}));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue