[][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:

#[macro_use]
extern crate serde_derive;

use futures::prelude::*;
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};
use std::pin::Pin;

#[derive(Deserialize, Debug)]
struct Claims {
    sub: String,
    exp: usize,
}

fn handler(state: State) -> Pin<Box<HandlerFuture>> {
    {
        let token = AuthorizationToken::<Claims>::borrow_from(&state);
        // token -> TokenData
    }
    let res = create_empty_response(&state, StatusCode::OK);
    future::ok((state, res)).boxed()
}

fn router() -> Router {
    let pipelines = new_pipeline_set();
    let (pipelines, defaults) = pipelines.add(
        new_pipeline()
            .add(JWTMiddleware::<Claims>::new("secret"))
            .build(),
    );
    let default_chain = (defaults, ());
    let pipeline_set = finalize_pipeline_set(pipelines);
    build_router(default_chain, pipeline_set, |route| {
        route.get("/").to(handler);
    })
}

Implementations

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

pub fn new<S: Into<String>>(secret: S) -> 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.

pub fn scheme<S: Into<String>>(self, scheme: S) -> Self[src]

Create a new instance of the middleware with a custom scheme

Trait Implementations

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

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.

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,