Skip to main content

easy_auth_sdk/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum AuthError {
5    #[error("Invalid token: {0}")]
6    InvalidToken(String),
7
8    #[error("Token signature verification failed")]
9    InvalidSignature,
10
11    #[error("Invalid key: {0}")]
12    InvalidKey(String),
13
14    #[error("Key not found for kid: {0}")]
15    KeyNotFound(String),
16
17    #[error("Missing required claim: {0}")]
18    MissingClaim(String),
19
20    #[error("Token has expired")]
21    TokenExpired,
22
23    #[error("JSON decoding error: {0}")]
24    JsonError(#[from] serde_json::Error),
25}
26
27impl From<jsonwebtoken::errors::Error> for AuthError {
28    fn from(err: jsonwebtoken::errors::Error) -> Self {
29        use jsonwebtoken::errors::ErrorKind;
30
31        match err.kind() {
32            ErrorKind::ExpiredSignature => AuthError::TokenExpired,
33            ErrorKind::InvalidSignature => AuthError::InvalidSignature,
34            ErrorKind::InvalidToken => AuthError::InvalidToken("Malformed token".to_string()),
35            ErrorKind::InvalidAlgorithm => AuthError::InvalidToken("Invalid algorithm".to_string()),
36            ErrorKind::Base64(_) => AuthError::InvalidToken("Base64 decoding failed".to_string()),
37            ErrorKind::Json(e) => AuthError::InvalidToken(format!("JSON error: {}", e)),
38            ErrorKind::Utf8(_) => AuthError::InvalidToken("UTF-8 decoding failed".to_string()),
39            ErrorKind::MissingRequiredClaim(claim) => AuthError::MissingClaim(claim.to_string()),
40            _ => AuthError::InvalidToken(err.to_string()),
41        }
42    }
43}