gotham-middleware-jwt

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

Usage

First, add gotham_middleware_jwt = "0.0.1" to Cargo.toml.

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

```rust extern crate gotham; extern crate gothammiddlewarejwt; extern crate hyper; extern crate serde;

[macro_use]

extern crate serde_derive;

use gotham::{ http::response::createresponse, pipeline::{ newpipeline, set::{finalizepipelineset, newpipelineset}, }, router::{builder::*, Router}, state::State, }; use gothammiddlewarejwt::{JWTMiddleware, AuthorizationToken}; use hyper::{Response, StatusCode};

[derive(Deserialize, Serialize, Debug)]

pub struct Claims { sub: String, }

fn handler(state: State) -> (State, Response) { { let auth = AuthorizationToken::::borrowfrom(&state); // auth.token -> TokenData } let res = createresponse(&state, StatusCode::Ok, None); (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

Copyright 2018 Uptime Ventures, Ltd. All rights reserved. Released under a 3-Clause BSD License.