http_auth_basic/
error.rs

1use base64::DecodeError;
2use std::fmt;
3use std::string::FromUtf8Error;
4
5/// Authorization Header Error
6#[derive(Debug)]
7pub enum AuthBasicError {
8    /// The HTTP Authorization header value is invalid
9    InvalidAuthorizationHeader,
10    /// The HTTP Authorization header contains a valid
11    /// value but the scheme is other than `Basic`
12    InvalidScheme(String),
13    /// The value expected as a base64 encoded `String` is not
14    /// encoded correctly
15    InvalidBase64Value(String),
16    /// The provided binary is not a valid UTF-8 character
17    InvalidUtf8Value(String),
18}
19
20impl fmt::Display for AuthBasicError {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        match self {
23            AuthBasicError::InvalidAuthorizationHeader => write!(
24                f,
25                "Invalid value provided for the HTTP Authorization header"
26            ),
27            AuthBasicError::InvalidScheme(scheme) => {
28                write!(f, "The scheme provided ({}) is not Basic", scheme)
29            }
30            AuthBasicError::InvalidBase64Value(message) => {
31                write!(f, "The value have an invalid base64 encoding\n{}", message)
32            }
33            AuthBasicError::InvalidUtf8Value(message) => {
34                write!(f, "Invalid UTF-8 Provided\n{}", message)
35            }
36        }
37    }
38}
39
40impl From<DecodeError> for AuthBasicError {
41    fn from(decode_error: DecodeError) -> Self {
42        AuthBasicError::InvalidBase64Value(decode_error.to_string())
43    }
44}
45
46impl From<FromUtf8Error> for AuthBasicError {
47    fn from(utf8_err: FromUtf8Error) -> Self {
48        AuthBasicError::InvalidUtf8Value(utf8_err.to_string())
49    }
50}