pub trait SessionExtractor<ClaimsType: Claims>: Send + Sync + 'static {
    // Required method
    fn extract_token_text<'req, 'life0, 'async_trait>(
        &'life0 self,
        req: &'req mut ServiceRequest
    ) -> Pin<Box<dyn Future<Output = Option<Cow<'req, str>>> + 'async_trait>>
       where Self: 'async_trait,
             'req: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn extract_claims<'life0, 'life1, 'async_trait>(
        &'life0 self,
        req: &'life1 mut ServiceRequest,
        jwt_encoding_key: Arc<EncodingKey>,
        jwt_decoding_key: Arc<DecodingKey>,
        algorithm: Algorithm,
        storage: SessionStorage
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    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
    ) -> 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_token_text<'req, 'life0, 'async_trait>( &'life0 self, req: &'req mut ServiceRequest ) -> Pin<Box<dyn Future<Output = Option<Cow<'req, str>>> + 'async_trait>>
where Self: 'async_trait, 'req: 'async_trait, 'life0: 'async_trait,

Lookup for session data as a string in actix_web::dev::ServiceRequest

If there’s no token data in request you should returns None. This is not considered as an error and until endpoint requires Authenticated this will not results in 401.

Provided Methods§

source

fn extract_claims<'life0, 'life1, 'async_trait>( &'life0 self, req: &'life1 mut ServiceRequest, jwt_encoding_key: Arc<EncodingKey>, jwt_decoding_key: Arc<DecodingKey>, algorithm: Algorithm, storage: SessionStorage ) -> 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::*;
use std::sync::Arc;
use actix_web::HttpMessage;
use std::borrow::Cow;


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

#[async_trait::async_trait(?Send)]
impl SessionExtractor<Claims> for ExampleExtractor {
    async fn extract_claims(
        &self,
        req: &mut ServiceRequest,
        jwt_encoding_key: Arc<EncodingKey>,
        jwt_decoding_key: Arc<DecodingKey>,
        algorithm: Algorithm,
        storage: SessionStorage,
    ) -> 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(())
    }

    async fn extract_token_text<'req>(&self, req: &'req mut ServiceRequest) -> Option<Cow<'req, str>> { None }
}
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 ) -> 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>

source§

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