[−][src]Struct gotham_middleware_jwt::JWTMiddleware
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]
T: for<'de> Deserialize<'de> + Send + Sync,
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]
T: for<'de> Deserialize<'de> + Send + Sync + 'static,
fn call<Chain>(self, state: State, chain: Chain) -> Pin<Box<HandlerFuture>> where
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>> + 'static,
Self: Sized,
[src]
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>> + 'static,
Self: Sized,
impl<T> NewMiddleware for JWTMiddleware<T> where
T: for<'de> Deserialize<'de> + RefUnwindSafe + Send + Sync + 'static,
[src]
T: for<'de> Deserialize<'de> + RefUnwindSafe + Send + Sync + 'static,
type Instance = JWTMiddleware<T>
The type of Middleware
created by the NewMiddleware
.
fn new_middleware(&self) -> Result<Self::Instance>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for JWTMiddleware<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for JWTMiddleware<T> where
T: Send,
T: Send,
impl<T> Sync for JWTMiddleware<T> where
T: Sync,
T: Sync,
impl<T> Unpin for JWTMiddleware<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for JWTMiddleware<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,