pub trait SessionExtractor<ClaimsType: Claims>: Send + Sync + 'static {
    // Required method
    fn extract_jwt<'life0, 'life1, 'async_trait>(
        &'life0 self,
        req: &'life1 ServiceRequest,
        jwt_encoding_key: Arc<EncodingKey>,
        jwt_decoding_key: Arc<DecodingKey>,
        algorithm: Algorithm,
        storage: SessionStorage<ClaimsType>
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn decode(
        &self,
        value: &str,
        jwt_decoding_key: Arc<DecodingKey>,
        algorithm: Algorithm
    ) -> Result<ClaimsType, Error> { ... }
    fn validate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        claims: &'life1 ClaimsType,
        storage: SessionStorage<ClaimsType>
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Trait allowing to extract JWt token from actix_web::dev::ServiceRequest

Two extractor are implemented by default

It’s possible to implement GraphQL, JSON payload or query using req.extract::<JSON<YourStruct>>() if this is needed.

All implementation can use SessionExtractor::decode method for decoding raw JWT string into Claims and then SessionExtractor::validate to validate claims agains session stored in SessionStorage

Required Methods§

source

fn extract_jwt<'life0, 'life1, 'async_trait>( &'life0 self, req: &'life1 ServiceRequest, jwt_encoding_key: Arc<EncodingKey>, jwt_decoding_key: Arc<DecodingKey>, algorithm: Algorithm, storage: SessionStorage<ClaimsType> ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Extract claims from actix_web::dev::ServiceRequest

Examples:

use actix_web::dev::ServiceRequest;
use jsonwebtoken::*;
use actix_jwt_session::{Extractor, Authenticated, Error, SessionStorage};
use std::sync::Arc;
use actix_web::HttpMessage;

#[derive(Debug, Clone, Copy, Default)]
struct ExampleExtractor;

#[async_trait::async_trait(?Send)]
impl Extractor<Claims> for ExampleExtractor {
    async fn extract_jwt(
        &self,
        req: &ServiceRequest,
        jwt_encoding_key: Arc<EncodingKey>,
        jwt_decoding_key: Arc<DecodingKey>,
        algorithm: Algorithm,
        storage: SessionStorage<Claims>,
    ) -> Result<(), Error> {
        if req.peer_addr().unwrap().ip().is_multicast() {
           req.extensions_mut().insert(Authenticated {
               claims: Arc::new(Claims { id: uuid::Uuid::default(), sub: "HUB".into() }),
               jwt_encoding_key,
               algorithm,
           });
        }
        Ok(())
    }
}

Provided Methods§

source

fn decode( &self, value: &str, jwt_decoding_key: Arc<DecodingKey>, algorithm: Algorithm ) -> Result<ClaimsType, Error>

Decode encrypted JWT to structure

source

fn validate<'life0, 'life1, 'async_trait>( &'life0 self, claims: &'life1 ClaimsType, storage: SessionStorage<ClaimsType> ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Validate JWT Claims agains stored in storage tokens.

  • Token must exists in storage
  • Token must be exactly the same as token from storage

Implementors§

source§

impl<ClaimsType: Claims> SessionExtractor<ClaimsType> for CookieExtractor<ClaimsType>

source§

impl<ClaimsType: Claims> SessionExtractor<ClaimsType> for HeaderExtractor<ClaimsType>