keygate_jwt/
error.rs

1// pub use anyhow::{anyhow, bail, ensure, JWTError};
2
3#[macro_export]
4macro_rules! ensure {
5    ($cond:expr, $err:expr $(,)?) => {
6        if !$cond {
7            return Err($err);
8        }
9    };
10}
11
12#[derive(Debug, thiserror::Error)]
13pub enum JWTError {
14    #[error("Internal error: [{0}]")]
15    InternalError(String),
16    #[error("JWT compact encoding error")]
17    CompactEncodingError,
18    #[error("JWT header too large")]
19    HeaderTooLarge,
20    #[error("JWT algorithm mismatch")]
21    AlgorithmMismatch,
22    #[error("JWT key identifier mismatch")]
23    KeyIdentifierMismatch,
24    #[error("Missing JWT key identifier")]
25    MissingJWTKeyIdentifier,
26    #[error("Authentication tag didn't verify")]
27    InvalidAuthenticationTag,
28    #[error("Signature tag didn't verify")]
29    InvalidSignature,
30    #[error("Old token reused")]
31    OldTokenReused,
32    #[error("Clock drift detected")]
33    ClockDrift,
34    #[error("Token is too old")]
35    TokenIsTooOld,
36    #[error("Token not valid yet")]
37    TokenNotValidYet,
38    #[error("Token has expired")]
39    TokenHasExpired,
40    #[error("Required nonce missing")]
41    RequiredNonceMissing,
42    #[error("Required nonce mismatch")]
43    RequiredNonceMismatch,
44    #[error("Required issuer mismatch")]
45    RequiredIssuerMismatch,
46    #[error("Required issuer missing")]
47    RequiredIssuerMissing,
48    #[error("Required subject mismatch")]
49    RequiredSubjectMismatch,
50    #[error("Required subject missing")]
51    RequiredSubjectMissing,
52    #[error("Required audience missing")]
53    RequiredAudienceMissing,
54    #[error("Required audience mismatch")]
55    RequiredAudienceMismatch,
56    #[error("Unsupported RSA modulus")]
57    UnsupportedRSAModulus,
58    #[error("Invalid public key")]
59    InvalidPublicKey,
60    #[error("Invalid key pair")]
61    InvalidKeyPair,
62    #[error("At most one audience can be represented as a string instead of a set")]
63    TooManyAudiences,
64    #[error("Too many issuers to be represented as a string")]
65    TooManyIssuers,
66    #[error("Invalid certificate thumbprint")]
67    InvalidCertThumprint,
68    #[error("Not a JWT token")]
69    NotJWT,
70    #[error("Token is too long")]
71    TokenTooLong,
72
73    #[error("codec error: {0}")]
74    Codec(String),
75
76    #[error(transparent)]
77    Serde(#[from] serde_json::Error),
78
79    #[cfg(feature = "eddsa")]
80    #[error(transparent)]
81    Ed25519(#[from] ed25519_compact::Error),
82}
83
84impl From<base64ct::Error> for JWTError {
85    fn from(e: base64ct::Error) -> JWTError {
86        JWTError::Codec(e.to_string())
87    }
88}
89
90impl From<hex::FromHexError> for JWTError {
91    fn from(e: hex::FromHexError) -> JWTError {
92        JWTError::Codec(e.to_string())
93    }
94}
95
96impl From<&str> for JWTError {
97    fn from(e: &str) -> JWTError {
98        JWTError::InternalError(e.into())
99    }
100}
101
102pub type Error = JWTError;