use std::error::Error as StdError;
use std::fmt;
use std::result;
use std::sync::Arc;
pub(crate) fn new_error(kind: ErrorKind) -> Error {
Error(Box::new(kind))
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error(Box<ErrorKind>);
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum ErrorKind {
InvalidToken,
InvalidSignature,
InvalidEcdsaKey,
InvalidRsaKey(String),
RsaFailedSigning,
InvalidAlgorithmName,
InvalidKeyFormat,
MissingRequiredClaim(String),
ExpiredSignature,
InvalidIssuer,
InvalidAudience,
InvalidSubject,
ImmatureSignature,
InvalidAlgorithm,
MissingAlgorithm,
Base64(base64::DecodeError),
Json(Arc<serde_json::Error>),
Utf8(::std::string::FromUtf8Error),
Crypto(::ring::error::Unspecified),
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match &*self.0 {
ErrorKind::InvalidToken => None,
ErrorKind::InvalidSignature => None,
ErrorKind::InvalidEcdsaKey => None,
ErrorKind::RsaFailedSigning => None,
ErrorKind::InvalidRsaKey(_) => None,
ErrorKind::ExpiredSignature => None,
ErrorKind::MissingAlgorithm => None,
ErrorKind::MissingRequiredClaim(_) => None,
ErrorKind::InvalidIssuer => None,
ErrorKind::InvalidAudience => None,
ErrorKind::InvalidSubject => None,
ErrorKind::ImmatureSignature => None,
ErrorKind::InvalidAlgorithm => None,
ErrorKind::InvalidAlgorithmName => None,
ErrorKind::InvalidKeyFormat => None,
ErrorKind::Base64(err) => Some(err),
ErrorKind::Json(err) => Some(err.as_ref()),
ErrorKind::Utf8(err) => Some(err),
ErrorKind::Crypto(err) => Some(err),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &*self.0 {
ErrorKind::InvalidToken
| ErrorKind::InvalidSignature
| ErrorKind::InvalidEcdsaKey
| ErrorKind::ExpiredSignature
| ErrorKind::RsaFailedSigning
| ErrorKind::MissingAlgorithm
| ErrorKind::InvalidIssuer
| ErrorKind::InvalidAudience
| ErrorKind::InvalidSubject
| ErrorKind::ImmatureSignature
| ErrorKind::InvalidAlgorithm
| ErrorKind::InvalidKeyFormat
| ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),
ErrorKind::MissingRequiredClaim(c) => write!(f, "Missing required claim: {}", c),
ErrorKind::InvalidRsaKey(msg) => write!(f, "RSA key invalid: {}", msg),
ErrorKind::Json(err) => write!(f, "JSON error: {}", err),
ErrorKind::Utf8(err) => write!(f, "UTF-8 error: {}", err),
ErrorKind::Crypto(err) => write!(f, "Crypto error: {}", err),
ErrorKind::Base64(err) => write!(f, "Base64 error: {}", err),
}
}
}
impl PartialEq for ErrorKind {
fn eq(&self, other: &Self) -> bool {
format!("{:?}", self) == format!("{:?}", other)
}
}
impl Eq for ErrorKind {}
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Error {
new_error(ErrorKind::Base64(err))
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
new_error(ErrorKind::Json(Arc::new(err)))
}
}
impl From<::std::string::FromUtf8Error> for Error {
fn from(err: ::std::string::FromUtf8Error) -> Error {
new_error(ErrorKind::Utf8(err))
}
}
impl From<::ring::error::Unspecified> for Error {
fn from(err: ::ring::error::Unspecified) -> Error {
new_error(ErrorKind::Crypto(err))
}
}
impl From<::ring::error::KeyRejected> for Error {
fn from(_err: ::ring::error::KeyRejected) -> Error {
new_error(ErrorKind::InvalidEcdsaKey)
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
new_error(kind)
}
}
#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;
use super::*;
#[test]
#[wasm_bindgen_test]
fn test_error_rendering() {
assert_eq!(
"InvalidAlgorithmName",
Error::from(ErrorKind::InvalidAlgorithmName).to_string()
);
}
}