use adk_core::{AdkError, ErrorCategory, ErrorComponent};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WebBotAuthError {
#[error(
"malformed `{field}` field: {reason}; send it as an RFC 8941 Dictionary as RFC 9421 §4 and the Web Bot Auth draft define"
)]
MalformedField {
field: String,
reason: String,
},
#[error(
"signature `{label}` has an invalid `{parameter}` parameter: {reason}; Web Bot Auth signatures carry created, expires, keyid and tag=\"web-bot-auth\""
)]
InvalidParameter {
label: String,
parameter: String,
reason: String,
},
#[error(
"signature `{label}` does not cover `{component}`; sign it, as the Web Bot Auth draft §5.2 and the verifier's policy require"
)]
MissingComponent {
label: String,
component: String,
},
#[error("signature `{label}` uses algorithm `{alg}`; only alg=\"ed25519\" is accepted")]
UnsupportedAlgorithm {
label: String,
alg: String,
},
#[error(
"signature `{label}` was created at {created}, after the verifier's time {now} plus the allowed skew; check the signer's clock"
)]
NotYetValid {
label: String,
created: i64,
now: i64,
},
#[error("signature `{label}` expired at {expires} (now {now}); sign each request afresh")]
Expired {
label: String,
expires: i64,
now: i64,
},
#[error(
"signature `{label}` is valid for {lifetime_secs} s, more than the {max_secs} s the verifier accepts; use a shorter expiry"
)]
LifetimeTooLong {
label: String,
lifetime_secs: i64,
max_secs: u64,
},
#[error(
"signature `{label}` has an unusable Signature-Agent: {reason}; send an https origin such as \"https://bot.example\""
)]
InvalidSignatureAgent {
label: String,
reason: String,
},
#[error(
"signature `{label}` uses Signature-Agent discovery type `{kind}`; only the `directory` type is supported"
)]
UnsupportedDiscovery {
label: String,
kind: String,
},
#[error("cannot build the signature base: {reason}")]
SignatureBase {
reason: String,
},
#[error(
"signature `{label}` does not verify against its key; the message changed or the key is wrong"
)]
BadSignature {
label: String,
},
#[error(
"the key directory at {origin} has no Ed25519 key with thumbprint `{keyid}`; publish the key before signing with it"
)]
UnknownKey {
origin: String,
keyid: String,
},
#[error("key `{keyid}` at {origin} is outside its validity window")]
KeyNotCurrent {
origin: String,
keyid: String,
},
#[error(
"key `{keyid}` is the RFC 9421 test key, which verifiers reject in production; generate a key of your own"
)]
TestKey {
keyid: String,
},
#[error("the key directory at {origin} is unavailable: {reason}")]
DirectoryUnavailable {
origin: String,
reason: String,
},
#[error(
"the key directory at {origin} is malformed: {reason}; serve a JWKS as application/http-message-signatures-directory+json"
)]
MalformedDirectory {
origin: String,
reason: String,
},
#[error("refused to fetch the key directory at {origin}: {reason}")]
EgressRefused {
origin: String,
reason: String,
},
#[error("invalid key material: {reason}")]
InvalidKey {
reason: String,
},
#[error("invalid Web Bot Auth configuration: {reason}")]
InvalidConfig {
reason: String,
},
}
impl WebBotAuthError {
pub fn is_unverified(&self) -> bool {
match self {
Self::UnknownKey { .. }
| Self::UnsupportedDiscovery { .. }
| Self::DirectoryUnavailable { .. }
| Self::MalformedDirectory { .. }
| Self::EgressRefused { .. } => true,
Self::MalformedField { .. }
| Self::InvalidParameter { .. }
| Self::MissingComponent { .. }
| Self::UnsupportedAlgorithm { .. }
| Self::NotYetValid { .. }
| Self::Expired { .. }
| Self::LifetimeTooLong { .. }
| Self::InvalidSignatureAgent { .. }
| Self::SignatureBase { .. }
| Self::BadSignature { .. }
| Self::KeyNotCurrent { .. }
| Self::TestKey { .. }
| Self::InvalidKey { .. }
| Self::InvalidConfig { .. } => false,
}
}
fn category_and_code(&self) -> (ErrorCategory, &'static str) {
match self {
Self::MalformedField { .. } => {
(ErrorCategory::InvalidInput, "auth.web_bot_auth.malformed_field")
}
Self::InvalidParameter { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.invalid_parameter")
}
Self::MissingComponent { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.missing_component")
}
Self::UnsupportedAlgorithm { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.unsupported_algorithm")
}
Self::NotYetValid { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.not_yet_valid")
}
Self::Expired { .. } => (ErrorCategory::Unauthorized, "auth.web_bot_auth.expired"),
Self::LifetimeTooLong { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.lifetime_too_long")
}
Self::InvalidSignatureAgent { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.invalid_signature_agent")
}
Self::UnsupportedDiscovery { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.unsupported_discovery")
}
Self::SignatureBase { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.signature_base")
}
Self::BadSignature { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.bad_signature")
}
Self::UnknownKey { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.unknown_key")
}
Self::KeyNotCurrent { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.key_not_current")
}
Self::TestKey { .. } => (ErrorCategory::Unauthorized, "auth.web_bot_auth.test_key"),
Self::DirectoryUnavailable { .. } => {
(ErrorCategory::Unavailable, "auth.web_bot_auth.directory_unavailable")
}
Self::MalformedDirectory { .. } => {
(ErrorCategory::Unauthorized, "auth.web_bot_auth.malformed_directory")
}
Self::EgressRefused { .. } => {
(ErrorCategory::Forbidden, "auth.web_bot_auth.egress_refused")
}
Self::InvalidKey { .. } => {
(ErrorCategory::InvalidInput, "auth.web_bot_auth.invalid_key")
}
Self::InvalidConfig { .. } => {
(ErrorCategory::InvalidInput, "auth.web_bot_auth.invalid_config")
}
}
}
}
impl From<WebBotAuthError> for AdkError {
fn from(err: WebBotAuthError) -> Self {
let (category, code) = err.category_and_code();
AdkError::new(ErrorComponent::Auth, category, code, err.to_string()).with_source(err)
}
}