adk_harness/website/signing.rs
1//! Outbound Web Bot Auth: the site signs its own crawls, verifier requests and, when the
2//! operator opts in, `web_fetch` calls (spec 3.5 §7.3 "Outbound").
3
4use std::sync::Arc;
5
6use adk_auth::web_bot_auth::{Ed25519SigningKey, HttpSignatureSigner, KeyDirectory};
7use adk_core::{AdkError, Result};
8
9use crate::web::RequestSigner;
10
11/// A [`RequestSigner`] over `adk-auth`'s Web Bot Auth signer: every request, every redirect
12/// hop included, carries `Signature-Agent`, `Signature-Input` and `Signature` (spec 3.5 §7.3
13/// "Outbound").
14///
15/// # Example
16///
17/// ```rust
18/// use std::sync::Arc;
19/// use adk_auth::web_bot_auth::{Ed25519SigningKey, HttpSignatureSigner};
20/// use adk_harness::web::RequestSigner;
21/// use adk_harness::website::WebBotAuthSigner;
22///
23/// let key = Arc::new(Ed25519SigningKey::generate().unwrap());
24/// let signer = WebBotAuthSigner::new(HttpSignatureSigner::builder(key, "https://bakery.example").build().unwrap());
25/// let mut request = reqwest::Client::new().get("https://supplier.example/prices").build().unwrap();
26/// signer.sign(&mut request).unwrap();
27/// assert_eq!(request.headers()["signature-agent"], "sig1=\"https://bakery.example\"");
28/// ```
29#[derive(Debug)]
30pub struct WebBotAuthSigner {
31 inner: HttpSignatureSigner,
32}
33
34impl WebBotAuthSigner {
35 /// Signs with `inner`.
36 pub fn new(inner: HttpSignatureSigner) -> Self {
37 Self { inner }
38 }
39}
40
41impl RequestSigner for WebBotAuthSigner {
42 fn sign(&self, request: &mut reqwest::Request) -> std::result::Result<(), String> {
43 self.inner.sign_reqwest(request).map_err(|error| error.to_string())
44 }
45}
46
47/// Signs the requests of `adk-awp`'s verifier, so `awp verify` identifies the site
48/// (spec 3.5 §7.3 "Outbound").
49#[async_trait::async_trait]
50impl adk_awp::verify::fetch::RequestSigner for WebBotAuthSigner {
51 async fn sign(
52 &self,
53 url: &str,
54 headers: &mut reqwest::header::HeaderMap,
55 ) -> std::result::Result<(), String> {
56 let url = reqwest::Url::parse(url).map_err(|error| error.to_string())?;
57 let mut request = reqwest::Request::new(reqwest::Method::GET, url);
58 *request.headers_mut() = headers.clone();
59 self.inner.sign_reqwest(&mut request).map_err(|error| error.to_string())?;
60 *headers = request.headers().clone();
61 Ok(())
62 }
63}
64
65/// The site's Web Bot Auth identity (spec 3.5 §7.3 "Outbound"): its signing key and the
66/// origin whose `/.well-known/http-message-signatures-directory` publishes it.
67///
68/// The key never reaches a model context, a tool result or a log (A-I5): `Debug` names only
69/// its thumbprint, and persistence goes through `Ed25519SigningKey::to_private_jwk`.
70///
71/// # Example
72///
73/// ```rust
74/// use std::sync::Arc;
75/// use adk_auth::web_bot_auth::Ed25519SigningKey;
76/// use adk_harness::website::SiteSigning;
77///
78/// let signing = SiteSigning::new(Arc::new(Ed25519SigningKey::generate().unwrap()), "https://bakery.example").unwrap();
79/// assert!(signing.directory_json().contains("\"kty\":\"OKP\""));
80/// ```
81#[derive(Clone)]
82pub struct SiteSigning {
83 key: Arc<Ed25519SigningKey>,
84 origin: String,
85 signer: Arc<WebBotAuthSigner>,
86}
87
88impl std::fmt::Debug for SiteSigning {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 f.debug_struct("SiteSigning")
91 .field("origin", &self.origin)
92 .field("keyid", &self.key.thumbprint())
93 .finish()
94 }
95}
96
97impl SiteSigning {
98 /// The identity of `key`, published at `origin`.
99 ///
100 /// # Errors
101 ///
102 /// Returns a configuration error when `origin` is not an origin the signer accepts.
103 pub fn new(key: Arc<Ed25519SigningKey>, origin: impl Into<String>) -> Result<Self> {
104 let origin = origin.into();
105 let signer = HttpSignatureSigner::builder(key.clone(), origin.clone())
106 .build()
107 .map_err(|error| AdkError::config(format!("web bot auth signer: {error}")))?;
108 Ok(Self { key, origin, signer: Arc::new(WebBotAuthSigner::new(signer)) })
109 }
110
111 /// The identity of the key a site persisted as a private JWK.
112 ///
113 /// # Errors
114 ///
115 /// As [`new`](Self::new), and when `jwk` is not an Ed25519 private JWK.
116 pub fn from_private_jwk(jwk: &str, origin: impl Into<String>) -> Result<Self> {
117 let key = Ed25519SigningKey::from_jwk(jwk)
118 .map_err(|error| AdkError::config(format!("web bot auth key: {error}")))?;
119 Self::new(Arc::new(key), origin)
120 }
121
122 /// The origin that publishes the key directory.
123 pub fn origin(&self) -> &str {
124 &self.origin
125 }
126
127 /// The key's JWK thumbprint, sent as `keyid`.
128 pub fn keyid(&self) -> &str {
129 self.key.thumbprint()
130 }
131
132 /// The signer for the harness's fetches: pass it to
133 /// [`HarnessAgentBuilder::web_signer`](crate::HarnessAgentBuilder::web_signer) to sign
134 /// `web_fetch`, and to the site runtime and verifier for crawls and `awp verify`.
135 pub fn request_signer(&self) -> Arc<dyn RequestSigner> {
136 self.signer.clone()
137 }
138
139 /// The signer for `adk-awp`'s verifier: pass it to `HttpFetcher::with_signer`.
140 pub fn verifier_signer(&self) -> Arc<dyn adk_awp::verify::fetch::RequestSigner> {
141 self.signer.clone()
142 }
143
144 /// The key directory the site publishes at
145 /// `/.well-known/http-message-signatures-directory`.
146 pub fn directory(&self) -> KeyDirectory {
147 KeyDirectory::from_keys([self.key.as_ref()])
148 }
149
150 /// [`directory`](Self::directory) as JSON.
151 pub fn directory_json(&self) -> String {
152 self.directory().to_json()
153 }
154}