a2a_protocol_client/webhook/
token.rs1use base64::Engine as _;
17
18pub const NOTIFICATION_TOKEN_HEADER: &str = "x-a2a-notification-token";
21
22pub const NOTIFICATION_TOKEN_HEADER_UNPREFIXED: &str = "a2a-notification-token";
24
25#[must_use]
37pub fn notification_token(headers: &hyper::HeaderMap) -> Option<&str> {
38 let mut found: Option<&str> = None;
39 for name in [
40 NOTIFICATION_TOKEN_HEADER,
41 NOTIFICATION_TOKEN_HEADER_UNPREFIXED,
42 ] {
43 for value in headers.get_all(name) {
44 let value = value.to_str().ok()?;
45 match found {
46 Some(seen) if seen != value => return None,
47 _ => found = Some(value),
48 }
49 }
50 }
51 found
52}
53
54#[must_use]
64pub fn new_notification_token() -> String {
65 let mut bytes = [0u8; 32];
66 bytes[..16].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
67 bytes[16..].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
68 base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
69}
70
71pub(super) fn is_sendable_token(token: &str) -> bool {
76 !token.is_empty() && token.bytes().all(|b| b.is_ascii_graphic())
77}
78
79pub(super) fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
86 use subtle::ConstantTimeEq as _;
87 a.ct_eq(b).into()
88}
89
90pub(super) fn authorization_matches(
97 headers: &hyper::HeaderMap,
98 scheme: &str,
99 credentials: &str,
100) -> bool {
101 let mut values = headers.get_all(hyper::header::AUTHORIZATION).iter();
102 let (Some(value), None) = (values.next(), values.next()) else {
103 return false;
104 };
105 let Ok(value) = value.to_str() else {
106 return false;
107 };
108 let Some((got_scheme, got_credentials)) = value.split_once(' ') else {
109 return false;
110 };
111 let got_credentials = got_credentials.trim_start_matches(' ');
113 let scheme_ok = got_scheme.eq_ignore_ascii_case(scheme);
114 let credentials_ok = constant_time_eq(got_credentials.as_bytes(), credentials.as_bytes());
115 scheme_ok & credentials_ok
116}
117
118pub(super) fn digest(parts: &[&[u8]]) -> [u8; 32] {
121 let mut ctx = ring::digest::Context::new(&ring::digest::SHA256);
122 for part in parts {
123 ctx.update(&(part.len() as u64).to_le_bytes());
124 ctx.update(part);
125 }
126 let mut out = [0u8; 32];
127 out.copy_from_slice(ctx.finish().as_ref());
128 out
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 fn headers(pairs: &[(&'static str, &'static str)]) -> hyper::HeaderMap {
136 let mut h = hyper::HeaderMap::new();
137 for (k, v) in pairs {
138 h.append(*k, v.parse().unwrap());
139 }
140 h
141 }
142
143 #[test]
144 fn token_under_either_spelling_or_both_agreeing() {
145 let python = headers(&[("X-A2A-Notification-Token", "t")]);
146 let go = headers(&[("A2A-Notification-Token", "t")]);
147 let both = headers(&[
148 ("x-a2a-notification-token", "t"),
149 ("a2a-notification-token", "t"),
150 ]);
151 for h in [&python, &go, &both] {
152 assert_eq!(notification_token(h), Some("t"));
153 }
154 }
155
156 #[test]
157 fn two_different_tokens_fail_closed() {
158 let across = headers(&[
159 ("x-a2a-notification-token", "t"),
160 ("a2a-notification-token", "u"),
161 ]);
162 assert_eq!(notification_token(&across), None);
163 let repeated = headers(&[
164 ("a2a-notification-token", "t"),
165 ("a2a-notification-token", "u"),
166 ]);
167 assert_eq!(notification_token(&repeated), None);
168 let mut non_ascii = hyper::HeaderMap::new();
169 non_ascii.insert(
170 NOTIFICATION_TOKEN_HEADER,
171 hyper::header::HeaderValue::from_bytes(b"caf\xc3\xa9").unwrap(),
172 );
173 assert_eq!(notification_token(&non_ascii), None);
174 assert_eq!(notification_token(&hyper::HeaderMap::new()), None);
175 }
176
177 #[test]
178 fn constant_time_eq_compares_every_byte_and_the_length() {
179 assert!(constant_time_eq(b"abc", b"abc"));
180 assert!(!constant_time_eq(b"abc", b"abd"));
181 assert!(!constant_time_eq(b"xbc", b"abc"));
182 assert!(!constant_time_eq(b"abc", b"ab"));
183 assert!(constant_time_eq(b"", b""));
184 }
185
186 #[test]
187 fn authorization_scheme_is_case_insensitive_and_credentials_exact() {
188 let ok = headers(&[("authorization", "bearer s3cret")]);
189 assert!(authorization_matches(&ok, "Bearer", "s3cret"));
190 let wrong_scheme = headers(&[("authorization", "Basic s3cret")]);
191 assert!(!authorization_matches(&wrong_scheme, "Bearer", "s3cret"));
192 let wrong_cred = headers(&[("authorization", "Bearer s3creT")]);
193 assert!(!authorization_matches(&wrong_cred, "Bearer", "s3cret"));
194 let bare = headers(&[("authorization", "Bearer")]);
195 assert!(!authorization_matches(&bare, "Bearer", "s3cret"));
196 let twice = headers(&[
197 ("authorization", "Bearer s3cret"),
198 ("authorization", "Bearer s3cret"),
199 ]);
200 assert!(!authorization_matches(&twice, "Bearer", "s3cret"));
201 assert!(!authorization_matches(
202 &hyper::HeaderMap::new(),
203 "Bearer",
204 "s3cret"
205 ));
206 }
207
208 #[test]
209 fn sendable_tokens_are_visible_ascii_without_spaces() {
210 assert!(is_sendable_token("abc-123_~"));
211 for bad in ["", " abc", "a b", "abc\n", "café"] {
212 assert!(!is_sendable_token(bad), "{bad:?}");
213 }
214 }
215
216 #[test]
217 fn digest_separates_its_parts() {
218 assert_ne!(digest(&[b"ab", b"c"]), digest(&[b"a", b"bc"]));
219 assert_eq!(digest(&[b"ab", b"c"]), digest(&[b"ab", b"c"]));
220 }
221}