1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

implement auth parsing/verifying inside a gotham middleware

This commit is contained in:
msrd0 2020-01-22 16:53:02 +00:00
parent c025cbd8ea
commit 088774fc50
9 changed files with 721 additions and 49 deletions

View file

@ -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");
});
}));