actix_web_httpauth/headers/authorization/
errors.rs

1use std::{error::Error, fmt, str};
2
3use actix_web::http::header;
4
5/// Possible errors while parsing `Authorization` header.
6///
7/// Should not be used directly unless you are implementing your own
8/// [authentication scheme](super::Scheme).
9#[derive(Debug)]
10pub enum ParseError {
11    /// Header value is malformed.
12    Invalid,
13
14    /// Authentication scheme is missing.
15    MissingScheme,
16
17    /// Required authentication field is missing.
18    MissingField(&'static str),
19
20    /// Unable to convert header into the str.
21    ToStrError(header::ToStrError),
22
23    /// Malformed base64 string.
24    Base64DecodeError(base64::DecodeError),
25
26    /// Malformed UTF-8 string.
27    Utf8Error(str::Utf8Error),
28}
29
30impl fmt::Display for ParseError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            ParseError::Invalid => f.write_str("Invalid header value"),
34            ParseError::MissingScheme => f.write_str("Missing authorization scheme"),
35            ParseError::MissingField(field) => write!(f, "Missing header field ({field})"),
36            ParseError::ToStrError(err) => fmt::Display::fmt(err, f),
37            ParseError::Base64DecodeError(err) => fmt::Display::fmt(err, f),
38            ParseError::Utf8Error(err) => fmt::Display::fmt(err, f),
39        }
40    }
41}
42
43impl Error for ParseError {
44    fn source(&self) -> Option<&(dyn Error + 'static)> {
45        match self {
46            ParseError::Invalid => None,
47            ParseError::MissingScheme => None,
48            ParseError::MissingField(_) => None,
49            ParseError::ToStrError(err) => Some(err),
50            ParseError::Base64DecodeError(err) => Some(err),
51            ParseError::Utf8Error(err) => Some(err),
52        }
53    }
54}
55
56impl From<header::ToStrError> for ParseError {
57    fn from(err: header::ToStrError) -> Self {
58        ParseError::ToStrError(err)
59    }
60}
61
62impl From<base64::DecodeError> for ParseError {
63    fn from(err: base64::DecodeError) -> Self {
64        ParseError::Base64DecodeError(err)
65    }
66}
67
68impl From<str::Utf8Error> for ParseError {
69    fn from(err: str::Utf8Error) -> Self {
70        ParseError::Utf8Error(err)
71    }
72}