gothammiddlewarejwt

A middleware for the Gotham Web Framework that verifies JSON Web Tokens, returning StatusCode::UNAUTHORIZED if a request fails validation.

Usage

First, ensure you're using at least Gotham version 0.3. Then, add the following to your Cargo.toml: gotham_middleware_jwt = "0.3".

Second, create a struct you wish to deserialize into. For our example below, we've used Claims:

```rust

[macro_use]

extern crate serde_derive;

use futures::future; use gotham::{ helpers::http::response::createemptyresponse, handler::HandlerFuture, pipeline::{ newpipeline, set::{finalizepipelineset, newpipelineset}, }, router::{builder::*, Router}, state::{State, FromState}, }; use gothammiddleware_jwt::{JWTMiddleware, AuthorizationToken}; use gotham::hyper::{Response, StatusCode};

[derive(Deserialize, Debug)]

struct Claims { sub: String, exp: usize, }

fn handler(state: State) -> Box { { let token = AuthorizationToken::::borrowfrom(&state); // token -> TokenData } let res = createempty_response(&state, StatusCode::OK); Box::new(future::ok((state, res))) }

fn router() -> Router { let pipelines = newpipelineset(); let (pipelines, defaults) = pipelines.add( newpipeline() .add(JWTMiddleware::::new("secret".asref())) .build(), ); let defaultchain = (defaults, ()); let pipelineset = finalizepipelineset(pipelines); buildrouter(defaultchain, pipeline_set, |route| { route.get("/").to(handler); }) } ```

License

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: