6 releases (3 breaking)

✓ Uses Rust 2018 edition

0.4.1 May 20, 2020
0.3.3 Aug 29, 2019
0.3.1 Jan 11, 2019
0.3.0 Nov 8, 2018
0.0.1 Jul 6, 2018

#13 in Authentication

Download history 7/week @ 2020-02-07 11/week @ 2020-02-14 63/week @ 2020-02-21 13/week @ 2020-02-28 20/week @ 2020-03-06 12/week @ 2020-03-13 34/week @ 2020-03-20 6/week @ 2020-03-27 2/week @ 2020-04-03 25/week @ 2020-04-10 10/week @ 2020-04-17 3/week @ 2020-04-24 2/week @ 2020-05-01 4/week @ 2020-05-08 13/week @ 2020-05-15 12/week @ 2020-05-22

65 downloads per month

MIT/Apache

525KB
9K SLoC

Ruby HTML 9K SLoC // 0.4% comments Scons 8 SLoC

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, 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:

#[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);
  })
}

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:

Dependencies

~18MB
~428K SLoC