[][src]Struct gotham_middleware_jwt::JWTMiddleware

pub struct JWTMiddleware<T> { /* fields omitted */ }

This middleware verifies that JSON Web Token credentials, provided via the HTTP Authorization header, are extracted, parsed, and validated according to best practices before passing control to middleware beneath this middleware for a given mount point.

Requests that lack the Authorization header are returned with the Status Code 400: Bad Request. Tokens that fail validation cause the middleware to return Status Code 401: Unauthorized.

Example:

extern crate futures;
extern crate gotham;
extern crate gotham_middleware_jwt;
extern crate hyper;
extern crate serde;
#[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 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);
        // auth.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);
    })
}

Methods

impl<T> JWTMiddleware<T> where
    T: for<'de> Deserialize<'de> + Send + Sync
[src]

pub fn new(secret: &'static str) -> Self
[src]

Creates a JWTMiddleware instance from the provided secret, which, by default, uses HS256 as the crypto scheme.

pub fn validation(self, validation: Validation) -> Self
[src]

Create a new instance of the middleware by appending new validation constraints.

Trait Implementations

impl<T> NewMiddleware for JWTMiddleware<T> where
    T: for<'de> Deserialize<'de> + RefUnwindSafe + Send + Sync + 'static, 
[src]

type Instance = JWTMiddleware<T>

The type of Middleware created by the NewMiddleware.

impl<T> Middleware for JWTMiddleware<T> where
    T: for<'de> Deserialize<'de> + Send + Sync + 'static, 
[src]

Auto Trait Implementations

impl<T> Send for JWTMiddleware<T> where
    T: Send

impl<T> Sync for JWTMiddleware<T> where
    T: Sync

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T