use core::fmt;
use core::time::Duration;
#[must_use]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VerifyError {
MissingHeader {
header: &'static str,
},
MalformedHeader {
header: &'static str,
reason: &'static str,
},
BadEncoding {
reason: &'static str,
},
SignatureMismatch,
TimestampOutOfTolerance {
skew: Duration,
max_age: Duration,
},
UnsupportedProvider,
InvalidSecret {
reason: &'static str,
},
MissingContext {
reason: &'static str,
},
}
impl fmt::Display for VerifyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VerifyError::MissingHeader { header } => write!(f, "missing header `{header}`"),
VerifyError::MalformedHeader { header, reason } => {
write!(f, "malformed header `{header}`: {reason}")
}
VerifyError::BadEncoding { reason } => write!(f, "bad encoding: {reason}"),
VerifyError::SignatureMismatch => write!(f, "signature mismatch"),
VerifyError::TimestampOutOfTolerance { skew, max_age } => write!(
f,
"timestamp out of tolerance: {}s outside the allowed {}s window",
skew.as_secs(),
max_age.as_secs()
),
VerifyError::UnsupportedProvider => write!(f, "provider not implemented yet"),
VerifyError::InvalidSecret { reason } => write!(f, "invalid secret: {reason}"),
VerifyError::MissingContext { reason } => {
write!(f, "missing verification context: {reason}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for VerifyError {}
#[cfg(test)]
mod tests {
use super::VerifyError;
#[test]
fn display_never_contains_secret_or_signature_material() {
let e = VerifyError::MalformedHeader {
header: "X-Hub-Signature-256",
reason: "empty",
};
assert_eq!(
e.to_string(),
"malformed header `X-Hub-Signature-256`: empty"
);
}
}