use std::fmt;
#[derive(Debug)]
pub enum PasswordHashError {
InvalidParams {
detail: String,
},
Hash {
source: argon2::password_hash::Error,
},
}
impl fmt::Display for PasswordHashError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidParams { detail } => {
write!(formatter, "invalid Argon2id parameters: {detail}")
}
Self::Hash { source } => write!(formatter, "Argon2id hashing failed: {source}"),
}
}
}
impl std::error::Error for PasswordHashError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Hash { source } => Some(source),
Self::InvalidParams { .. } => None,
}
}
}
#[derive(Debug)]
pub enum PasswordVerifyError {
MalformedHash {
detail: String,
},
PasswordMismatch,
Verify {
source: argon2::password_hash::Error,
},
}
impl fmt::Display for PasswordVerifyError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MalformedHash { detail } => {
write!(formatter, "malformed stored password hash: {detail}")
}
Self::PasswordMismatch => write!(formatter, "password does not match stored hash"),
Self::Verify { source } => write!(formatter, "Argon2id verification failed: {source}"),
}
}
}
impl std::error::Error for PasswordVerifyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Verify { source } => Some(source),
Self::MalformedHash { .. } | Self::PasswordMismatch => None,
}
}
}
#[derive(Debug)]
pub enum SessionConfigError {
ZeroDuration {
field: &'static str,
},
InvalidSigningKey {
reason: SigningKeyReason,
},
EmptyCookieAttribute {
attribute: &'static str,
},
InsecureHostPrefixedCookie {
cookie_name: String,
},
}
#[derive(Debug, Clone, Copy)]
pub enum SigningKeyReason {
WrongLength,
}
impl fmt::Display for SigningKeyReason {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::WrongLength => write!(formatter, "must be exactly 64 bytes"),
}
}
}
impl fmt::Display for SessionConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroDuration { field } => write!(formatter, "{field} must not be zero"),
Self::InvalidSigningKey { reason } => {
write!(formatter, "invalid session signing key: {reason}")
}
Self::EmptyCookieAttribute { attribute } => {
write!(formatter, "session cookie {attribute} must not be empty")
}
Self::InsecureHostPrefixedCookie { cookie_name } => write!(
formatter,
"session cookie `{cookie_name}` uses the __Host- prefix which mandates \
Secure = true; use SessionConfig::dev() for a development policy"
),
}
}
}
impl std::error::Error for SessionConfigError {}
#[derive(Debug)]
pub struct SessionBuildError {
source: SessionConfigError,
}
impl SessionBuildError {
pub(crate) fn new(source: SessionConfigError) -> Self {
Self { source }
}
}
impl fmt::Display for SessionBuildError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"session layer construction failed: {}",
self.source
)
}
}
impl std::error::Error for SessionBuildError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[derive(Debug)]
pub enum CsrfError {
MissingHeader,
TokenMismatch,
MissingCookie,
MalformedCookie,
}
impl fmt::Display for CsrfError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingHeader => write!(formatter, "missing CSRF header token"),
Self::TokenMismatch => write!(formatter, "CSRF token mismatch"),
Self::MissingCookie => write!(formatter, "missing CSRF cookie"),
Self::MalformedCookie => write!(formatter, "malformed CSRF cookie"),
}
}
}
impl std::error::Error for CsrfError {}
#[derive(Debug)]
pub enum CsrfConfigError {
InsecureHostPrefixedCookie {
cookie_name: String,
},
}
impl fmt::Display for CsrfConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InsecureHostPrefixedCookie { cookie_name } => write!(
formatter,
"CSRF cookie \"{cookie_name}\" carries the __Host- prefix, which requires \
Secure = true (RFC 6265bis). Use CsrfConfig::dev() for HTTP development, \
or set a non-__Host- cookie name before with_secure(false)."
),
}
}
}
impl std::error::Error for CsrfConfigError {}