auth_headers/
error.rs

1//! Error type for the authorization header library
2
3use std::string::FromUtf8Error;
4
5use http::header::InvalidHeaderValue;
6use thiserror::Error;
7
8/// Credentials decoding error
9#[derive(Debug, Error)]
10pub enum CredentialsDecodeError {
11	/// Base64 header payload couldn't be decoded
12	#[error(transparent)]
13	Base64DecodeError(#[from] base64::DecodeError),
14	/// decoded header isn't unicode
15	#[error(transparent)]
16	UnicodeDecodeError(#[from] FromUtf8Error),
17	/// Password payload is missing a delimiter
18	#[error("Delimiter missing, credentials must be in a user:pass format")]
19	DelimiterNotFound,
20}
21
22#[derive(Debug, Error)]
23/// Authorization header error
24pub enum ParseError {
25	/// Authorization header is missing from the header map
26	#[error("Authorization header is missing from the header map")]
27	AuthHeaderMissing,
28	/// Authorization header contains invalid characters
29	#[error("Authorization header contains invalid characters")]
30	InvalidCharacters,
31	/// Bad authorization header format
32	#[error("Bad authorization header format")]
33	BadFormat,
34	/// The authentication type is unsupported
35	#[error("The authentication type {0} is unsupported")]
36	UnknownAuthenticationType(String),
37	/// User credentials format is invalid
38	#[error("User credentials format is invalid")]
39	InvalidUserCredentialsFormat,
40	/// Credentials can't be decoded
41	#[error(transparent)]
42	CredentialsDecodeError(#[from] CredentialsDecodeError),
43	/// Invalid header value
44	#[error(transparent)]
45	InvalidHeaderValue(#[from] InvalidHeaderValue),
46}
47
48impl ParseError {
49	/// The authentication type is unsupported
50	pub fn unknown_authentication_type<S: Into<String>>(auth_type: S) -> Self {
51		Self::UnknownAuthenticationType(auth_type.into())
52	}
53}