use crate::jwt;
use std::{error::Error, fmt};
#[derive(Debug)]
#[non_exhaustive]
pub enum AuthErr {
InvalidState,
InvalidTokenState,
TokenMismatch,
InvalidToken(jwt::errors::Error),
#[cfg_attr(docsrs, doc(cfg(any(feature = "async", feature = "blocking"))))]
#[cfg(any(feature = "async", feature = "blocking"))]
ConnectionError(reqwest::Error),
Other(String),
ParseError(String),
}
impl Error for AuthErr {}
impl fmt::Display for AuthErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use AuthErr::*;
match self {
InvalidState => write!(f, "No public keys found"),
InvalidTokenState => write!(f, "No `kid` in token"),
TokenMismatch => write!(f, "No public keys matched `kid` from token"),
InvalidToken(err) => write!(f, "Invalid token. {err}"),
#[cfg(any(feature = "async", feature = "blocking"))]
ConnectionError(err) => write!(f, "Could not connect to Microsoft. {err}"),
Other(msg) => write!(f, "An error occurred: {msg}"),
ParseError(msg) => write!(f, "Could not parse token. {msg}"),
}
}
}
#[cfg(any(feature = "async", feature = "blocking"))]
impl From<reqwest::Error> for AuthErr {
fn from(e: reqwest::Error) -> AuthErr {
AuthErr::ConnectionError(e)
}
}
impl From<jwt::errors::Error> for AuthErr {
fn from(e: jwt::errors::Error) -> AuthErr {
AuthErr::InvalidToken(e)
}
}