1mod axum;
17mod local;
18mod remote;
19
20use std::sync::Arc;
21
22use async_trait::async_trait;
23use jsonwebtoken::TokenData;
24use serde::de::DeserializeOwned;
25use thiserror::Error;
26
27pub use crate::axum::{AuthError, Claims, JwtDecoderState};
28pub use crate::local::LocalDecoder;
29pub use crate::remote::{
30 RemoteJwksDecoder, RemoteJwksDecoderBuilder, RemoteJwksDecoderConfig,
31 RemoteJwksDecoderConfigBuilder,
32};
33
34#[derive(Debug, thiserror::Error)]
35pub enum Error {
36 #[error("JWT key not found (kid: {0:?})")]
37 KeyNotFound(Option<String>),
38
39 #[error("Configuration error: {0}")]
40 Configuration(String),
41
42 #[error("JWT error: {0}")]
43 Jwt(#[from] jsonwebtoken::errors::Error),
44
45 #[error("HTTP request error: {0}")]
46 Reqwest(#[from] reqwest::Error),
47
48 #[error("JWKS refresh failed after {retry_count} attempts: {message}")]
49 JwksRefresh {
50 message: String,
51 retry_count: usize,
52 #[source]
53 source: Option<Box<dyn std::error::Error + Send + Sync>>,
54 },
55}
56
57#[async_trait]
61pub trait JwtDecoder<T>
62where
63 T: for<'de> DeserializeOwned,
64{
65 async fn decode(&self, token: &str) -> Result<TokenData<T>, Error>;
66}
67
68pub type Decoder<T> = Arc<dyn JwtDecoder<T> + Send + Sync>;