Crates.io | gotham_middleware_jwt |
lib.rs | gotham_middleware_jwt |
version | 0.8.0 |
source | src |
created_at | 2018-07-06 22:39:59.620682 |
updated_at | 2022-11-06 19:57:50.237189 |
description | JWT middleware for the Gotham web framework. |
homepage | https://gotham.rs |
repository | https://github.com/gotham-rs/gotham |
max_upload_size | |
id | 73157 |
size | 16,169 |
A middleware for the Gotham Web
Framework that verifies JSON Web Tokens, returning
StatusCode::UNAUTHORIZED
if a request fails validation.
Create a struct you wish to deserialize into. For our example below,
we've used Claims
:
#[macro_use]
extern crate serde_derive;
use futures::future;
use gotham::{
helpers::http::response::create_empty_response,
handler::HandlerFuture,
pipeline::{
new_pipeline,
set::{finalize_pipeline_set, new_pipeline_set},
},
router::{builder::*, Router},
state::{State, FromState},
};
use gotham_middleware_jwt::{JwtMiddleware, AuthorizationToken};
use gotham::hyper::{Response, StatusCode};
#[derive(Deserialize, Debug)]
struct Claims {
sub: String,
exp: usize,
}
fn handler(state: State) -> Box<HandlerFuture> {
{
let token = AuthorizationToken::<Claims>::borrow_from(&state);
// token -> TokenData
}
let res = create_empty_response(&state, StatusCode::OK);
Box::new(future::ok((state, res)))
}
fn router() -> Router {
let pipelines = new_pipeline_set();
let (pipelines, defaults) = pipelines.add(
new_pipeline()
.add(JwtMiddleware::<Claims>::new("secret".as_ref()))
.build(),
);
let default_chain = (defaults, ());
let pipeline_set = finalize_pipeline_set(pipelines);
build_router(default_chain, pipeline_set, |route| {
route.get("/").to(handler);
})
}
This middleware crate was originally created by Nicholas Young of Uptime Ventures, Ltd., and is maintained by the Gotham core team.
Licensed under your option of: