use core::fmt;
use crate::alloc::String;
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseError {
InvalidTokenStructure,
InvalidBase64Encoding,
MalformedHeader(serde_json::Error),
UnsupportedContentType(String),
}
impl fmt::Display for ParseError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidTokenStructure => formatter.write_str("Invalid token structure"),
Self::InvalidBase64Encoding => write!(formatter, "invalid base64 decoding"),
Self::MalformedHeader(e) => write!(formatter, "Malformed token header: {}", e),
Self::UnsupportedContentType(ty) => {
write!(formatter, "Unsupported content type: {}", ty)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::MalformedHeader(e) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ValidationError {
AlgorithmMismatch {
expected: String,
actual: String,
},
InvalidSignatureLen {
expected: usize,
actual: usize,
},
MalformedSignature(anyhow::Error),
InvalidSignature,
MalformedClaims(serde_json::Error),
#[cfg(feature = "serde_cbor")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_cbor")))]
MalformedCborClaims(serde_cbor::error::Error),
NoClaim(Claim),
Expired,
NotMature,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Claim {
Expiration,
NotBefore,
}
impl fmt::Display for Claim {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::Expiration => "exp",
Self::NotBefore => "nbf",
})
}
}
impl fmt::Display for ValidationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AlgorithmMismatch { expected, actual } => write!(
formatter,
"Token algorithm ({actual}) differs from expected ({expected})",
expected = expected,
actual = actual
),
Self::InvalidSignatureLen { expected, actual } => write!(
formatter,
"Invalid signature length: expected {expected} bytes, got {actual} bytes",
expected = expected,
actual = actual
),
Self::MalformedSignature(e) => write!(formatter, "Malformed token signature: {}", e),
Self::InvalidSignature => formatter.write_str("Signature has failed verification"),
Self::MalformedClaims(e) => write!(formatter, "Cannot deserialize claims: {}", e),
#[cfg(feature = "serde_cbor")]
Self::MalformedCborClaims(e) => write!(formatter, "Cannot deserialize claims: {}", e),
Self::NoClaim(claim) => write!(
formatter,
"Claim `{}` requested during validation is not present in the token",
claim
),
Self::Expired => formatter.write_str("Token has expired"),
Self::NotMature => formatter.write_str("Token is not yet ready"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ValidationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::MalformedSignature(e) => Some(e.as_ref()),
Self::MalformedClaims(e) => Some(e),
#[cfg(feature = "serde_cbor")]
Self::MalformedCborClaims(e) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum CreationError {
Header(serde_json::Error),
Claims(serde_json::Error),
#[cfg(feature = "serde_cbor")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_cbor")))]
CborClaims(serde_cbor::error::Error),
}
impl fmt::Display for CreationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Header(e) => write!(formatter, "Cannot serialize header: {}", e),
Self::Claims(e) => write!(formatter, "Cannot serialize claims: {}", e),
#[cfg(feature = "serde_cbor")]
Self::CborClaims(e) => write!(formatter, "Cannot serialize claims into CBOR: {}", e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for CreationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Header(e) | Self::Claims(e) => Some(e),
#[cfg(feature = "serde_cbor")]
Self::CborClaims(e) => Some(e),
}
}
}