Trait actix_jwt_session::SessionExtractor
source · 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
- HeaderExtractor which is best for any PWA or micro services requests
- CookieExtractor which is best for simple server with session stored in cookie
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§
sourcefn 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,
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§
sourcefn decode(
&self,
value: &str,
jwt_decoding_key: Arc<DecodingKey>,
algorithm: Algorithm
) -> Result<ClaimsType, Error>
fn decode( &self, value: &str, jwt_decoding_key: Arc<DecodingKey>, algorithm: Algorithm ) -> Result<ClaimsType, Error>
Decode encrypted JWT to structure
sourcefn 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,
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