1use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
23#[non_exhaustive]
24pub enum WebBotAuthError {
25 #[error(
27 "malformed `{field}` field: {reason}; send it as an RFC 8941 Dictionary as RFC 9421 §4 and the Web Bot Auth draft define"
28 )]
29 MalformedField {
30 field: String,
32 reason: String,
34 },
35 #[error(
37 "signature `{label}` has an invalid `{parameter}` parameter: {reason}; Web Bot Auth signatures carry created, expires, keyid and tag=\"web-bot-auth\""
38 )]
39 InvalidParameter {
40 label: String,
42 parameter: String,
44 reason: String,
46 },
47 #[error(
49 "signature `{label}` does not cover `{component}`; sign it, as the Web Bot Auth draft §5.2 and the verifier's policy require"
50 )]
51 MissingComponent {
52 label: String,
54 component: String,
56 },
57 #[error("signature `{label}` uses algorithm `{alg}`; only alg=\"ed25519\" is accepted")]
59 UnsupportedAlgorithm {
60 label: String,
62 alg: String,
64 },
65 #[error(
67 "signature `{label}` was created at {created}, after the verifier's time {now} plus the allowed skew; check the signer's clock"
68 )]
69 NotYetValid {
70 label: String,
72 created: i64,
74 now: i64,
76 },
77 #[error("signature `{label}` expired at {expires} (now {now}); sign each request afresh")]
79 Expired {
80 label: String,
82 expires: i64,
84 now: i64,
86 },
87 #[error(
89 "signature `{label}` is valid for {lifetime_secs} s, more than the {max_secs} s the verifier accepts; use a shorter expiry"
90 )]
91 LifetimeTooLong {
92 label: String,
94 lifetime_secs: i64,
96 max_secs: u64,
98 },
99 #[error(
101 "signature `{label}` has an unusable Signature-Agent: {reason}; send an https origin such as \"https://bot.example\""
102 )]
103 InvalidSignatureAgent {
104 label: String,
106 reason: String,
108 },
109 #[error(
111 "signature `{label}` uses Signature-Agent discovery type `{kind}`; only the `directory` type is supported"
112 )]
113 UnsupportedDiscovery {
114 label: String,
116 kind: String,
118 },
119 #[error("cannot build the signature base: {reason}")]
121 SignatureBase {
122 reason: String,
124 },
125 #[error(
127 "signature `{label}` does not verify against its key; the message changed or the key is wrong"
128 )]
129 BadSignature {
130 label: String,
132 },
133 #[error(
135 "the key directory at {origin} has no Ed25519 key with thumbprint `{keyid}`; publish the key before signing with it"
136 )]
137 UnknownKey {
138 origin: String,
140 keyid: String,
142 },
143 #[error("key `{keyid}` at {origin} is outside its validity window")]
145 KeyNotCurrent {
146 origin: String,
148 keyid: String,
150 },
151 #[error(
153 "key `{keyid}` is the RFC 9421 test key, which verifiers reject in production; generate a key of your own"
154 )]
155 TestKey {
156 keyid: String,
158 },
159 #[error("the key directory at {origin} is unavailable: {reason}")]
161 DirectoryUnavailable {
162 origin: String,
164 reason: String,
166 },
167 #[error(
169 "the key directory at {origin} is malformed: {reason}; serve a JWKS as application/http-message-signatures-directory+json"
170 )]
171 MalformedDirectory {
172 origin: String,
174 reason: String,
176 },
177 #[error("refused to fetch the key directory at {origin}: {reason}")]
179 EgressRefused {
180 origin: String,
182 reason: String,
184 },
185 #[error("invalid key material: {reason}")]
187 InvalidKey {
188 reason: String,
190 },
191 #[error("invalid Web Bot Auth configuration: {reason}")]
193 InvalidConfig {
194 reason: String,
196 },
197}
198
199impl WebBotAuthError {
200 pub fn is_unverified(&self) -> bool {
203 match self {
204 Self::UnknownKey { .. }
205 | Self::UnsupportedDiscovery { .. }
206 | Self::DirectoryUnavailable { .. }
207 | Self::MalformedDirectory { .. }
208 | Self::EgressRefused { .. } => true,
209 Self::MalformedField { .. }
210 | Self::InvalidParameter { .. }
211 | Self::MissingComponent { .. }
212 | Self::UnsupportedAlgorithm { .. }
213 | Self::NotYetValid { .. }
214 | Self::Expired { .. }
215 | Self::LifetimeTooLong { .. }
216 | Self::InvalidSignatureAgent { .. }
217 | Self::SignatureBase { .. }
218 | Self::BadSignature { .. }
219 | Self::KeyNotCurrent { .. }
220 | Self::TestKey { .. }
221 | Self::InvalidKey { .. }
222 | Self::InvalidConfig { .. } => false,
223 }
224 }
225
226 fn category_and_code(&self) -> (ErrorCategory, &'static str) {
227 match self {
228 Self::MalformedField { .. } => {
229 (ErrorCategory::InvalidInput, "auth.web_bot_auth.malformed_field")
230 }
231 Self::InvalidParameter { .. } => {
232 (ErrorCategory::Unauthorized, "auth.web_bot_auth.invalid_parameter")
233 }
234 Self::MissingComponent { .. } => {
235 (ErrorCategory::Unauthorized, "auth.web_bot_auth.missing_component")
236 }
237 Self::UnsupportedAlgorithm { .. } => {
238 (ErrorCategory::Unauthorized, "auth.web_bot_auth.unsupported_algorithm")
239 }
240 Self::NotYetValid { .. } => {
241 (ErrorCategory::Unauthorized, "auth.web_bot_auth.not_yet_valid")
242 }
243 Self::Expired { .. } => (ErrorCategory::Unauthorized, "auth.web_bot_auth.expired"),
244 Self::LifetimeTooLong { .. } => {
245 (ErrorCategory::Unauthorized, "auth.web_bot_auth.lifetime_too_long")
246 }
247 Self::InvalidSignatureAgent { .. } => {
248 (ErrorCategory::Unauthorized, "auth.web_bot_auth.invalid_signature_agent")
249 }
250 Self::UnsupportedDiscovery { .. } => {
251 (ErrorCategory::Unauthorized, "auth.web_bot_auth.unsupported_discovery")
252 }
253 Self::SignatureBase { .. } => {
254 (ErrorCategory::Unauthorized, "auth.web_bot_auth.signature_base")
255 }
256 Self::BadSignature { .. } => {
257 (ErrorCategory::Unauthorized, "auth.web_bot_auth.bad_signature")
258 }
259 Self::UnknownKey { .. } => {
260 (ErrorCategory::Unauthorized, "auth.web_bot_auth.unknown_key")
261 }
262 Self::KeyNotCurrent { .. } => {
263 (ErrorCategory::Unauthorized, "auth.web_bot_auth.key_not_current")
264 }
265 Self::TestKey { .. } => (ErrorCategory::Unauthorized, "auth.web_bot_auth.test_key"),
266 Self::DirectoryUnavailable { .. } => {
267 (ErrorCategory::Unavailable, "auth.web_bot_auth.directory_unavailable")
268 }
269 Self::MalformedDirectory { .. } => {
270 (ErrorCategory::Unauthorized, "auth.web_bot_auth.malformed_directory")
271 }
272 Self::EgressRefused { .. } => {
273 (ErrorCategory::Forbidden, "auth.web_bot_auth.egress_refused")
274 }
275 Self::InvalidKey { .. } => {
276 (ErrorCategory::InvalidInput, "auth.web_bot_auth.invalid_key")
277 }
278 Self::InvalidConfig { .. } => {
279 (ErrorCategory::InvalidInput, "auth.web_bot_auth.invalid_config")
280 }
281 }
282 }
283}
284
285impl From<WebBotAuthError> for AdkError {
286 fn from(err: WebBotAuthError) -> Self {
287 let (category, code) = err.category_and_code();
288 AdkError::new(ErrorComponent::Auth, category, code, err.to_string()).with_source(err)
289 }
290}