Skip to content

feat(errors): improve error handling with thiserror #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reqwest = { version = "0.12", default-features = false, features = [
"rustls-tls",
] }
serde = { version = "1", features = ["derive"] }
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["time"] }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::Deserialize;

use crate::{Decoder, JwtDecoder};

/// A generic struct for holding the claims of a JWT token.
#[derive(Debug, Deserialize)]
pub struct Claims<T>(pub T);

Expand Down
43 changes: 26 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
//! Provides a thin layer over jsonwebtoken crate to manage remote JWKS and local secret keys.
//! A Rust library for JWT authentication with support for both local keys and remote JWKS (JSON Web Key Sets).
//!
//! This crate provides a flexible JWT authentication system that can:
//! - Validate tokens using local RSA/HMAC keys
//! - Automatically fetch and cache remote JWKS endpoints
//! - Integrate seamlessly with the Axum web framework
//! - Handle token validation with configurable options
//!
//! It builds on top of the `jsonwebtoken` crate to provide higher-level authentication primitives
//! while maintaining full compatibility with standard JWT implementations.
//!
//! # Example
//!
//! For a full example, see the [examples](https://github.com/cmackenzie1/axum-jwt-auth/blob/main/examples).

mod axum;
mod local;
Expand All @@ -8,31 +21,27 @@ use std::sync::Arc;

use jsonwebtoken::TokenData;
use serde::de::DeserializeOwned;
use thiserror::Error;

pub use crate::axum::{AuthError, Claims, JwtDecoderState};
pub use crate::local::LocalDecoder;
pub use crate::remote::{RemoteJwksDecoder, RemoteJwksDecoderBuilder};

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("JWT key not found (kid: {0:?})")]
KeyNotFound(Option<String>),
Jwt(jsonwebtoken::errors::Error),
Reqwest(reqwest::Error),
#[error("JWT error: {0}")]
Jwt(#[from] jsonwebtoken::errors::Error),
#[error("HTTP request error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("JWKS refresh failed: {0}")]
JwksRefresh(String),
}

impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Self::Reqwest(err)
}
}

impl From<jsonwebtoken::errors::Error> for Error {
fn from(err: jsonwebtoken::errors::Error) -> Self {
Self::Jwt(err)
}
}

/// A trait for decoding JWT tokens.
/// A generic trait for decoding JWT tokens.
///
/// This trait is implemented for both `LocalDecoder` and `RemoteJwksDecoder`
pub trait JwtDecoder<T>
where
T: for<'de> DeserializeOwned,
Expand Down
Loading