use thiserror::Error;
use reallyme_codec::base64url::Base64UrlError;
use reallyme_crypto::dispatch::AlgorithmError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum JwtTemporalClaim {
Exp,
Nbf,
Iat,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum JwtError {
#[error("invalid JWT format")]
InvalidJwtFormat,
#[error("invalid JWT header")]
InvalidHeader,
#[error("invalid JWT claims")]
InvalidClaims,
#[error("invalid JWT signature")]
InvalidSignature,
#[error("JWT input too large")]
InputTooLarge,
#[error("unsupported JWT algorithm")]
UnsupportedAlgorithm,
#[error("JWT algorithm mismatch")]
AlgorithmMismatch,
#[error("JWT key identifier mismatch")]
KeyIdMismatch,
#[error("JWT public key mismatch")]
PublicKeyMismatch,
#[error("JWT signing key mismatch")]
SigningKeyMismatch,
#[error("missing algorithm in JWK")]
MissingAlgorithm,
#[error("missing private key in JWK")]
MissingPrivateKey,
#[error("missing public key in JWK")]
MissingPublicKey,
#[error("invalid public key")]
InvalidPublicKey,
#[error("serialization error")]
Serialization,
#[error("base64url decoding error")]
Base64Url,
#[error("JWT serialization length overflow")]
LengthOverflow,
#[error("cryptographic error")]
Crypto,
#[error("missing required temporal claim: {0:?}")]
MissingRequiredTemporalClaim(JwtTemporalClaim),
#[error("invalid temporal claim value: {0:?}")]
InvalidTemporalClaimValue(JwtTemporalClaim),
#[error("JWT is expired")]
Expired,
#[error("JWT is not yet valid")]
NotYetValid,
#[error("JWT issued-at is in the future")]
IssuedAtInFuture,
#[error("invalid JWT temporal validation policy")]
InvalidTemporalPolicy,
}
impl From<Base64UrlError> for JwtError {
fn from(_: Base64UrlError) -> Self {
JwtError::Base64Url
}
}
impl From<AlgorithmError> for JwtError {
fn from(_: AlgorithmError) -> Self {
JwtError::Crypto
}
}
impl From<serde_json::Error> for JwtError {
fn from(_: serde_json::Error) -> Self {
JwtError::Serialization
}
}