actix_web_security/authentication/scheme/bearer/jwt/token/decoder/
mod.rs

1//! The decoder module provides a trait definition of `TokenDecoder` and a RSA token decoder implementation.
2
3use serde::Deserialize;
4
5use crate::authentication::error::error_type::AuthenticationError;
6use crate::authentication::scheme::bearer::jwt::token::Claims;
7
8pub mod rsa_decoder;
9
10/// Token decoder claim trait definition. Decodes a string token to either a boxed instance of `Claims`
11/// or returns an error.
12pub trait TokenDecoder<T: for<'b> Deserialize<'b> + Claims>: TokenDecoderClone<T> {
13    fn decode_token(&self, token: &str) -> Result<Box<T>, AuthenticationError>;
14}
15
16/// A token decoder must be cloneable, `send` and `sync`.
17/// Therefore it has to implement the `TokenDecoderClone` trait to be cloneable as a boxed object.
18pub trait TokenDecoderClone<T: for<'b> Deserialize<'b> + Claims>: Send + Sync {
19    fn clone_box(&self) -> Box<dyn TokenDecoder<T>>;
20}
21
22impl<T: for<'b> Deserialize<'b> + Claims, U> TokenDecoderClone<T> for U
23where
24    U: 'static + TokenDecoder<T> + Clone,
25{
26    fn clone_box(&self) -> Box<dyn TokenDecoder<T>> {
27        Box::new(self.clone())
28    }
29}
30
31impl<T: for<'b> Deserialize<'b> + Claims> Clone for Box<dyn TokenDecoder<T>> {
32    fn clone(&self) -> Self {
33        self.clone_box()
34    }
35}