1use std::convert::Infallible;
21use std::future::Future;
22use std::pin::Pin;
23use std::sync::Arc;
24use std::task::{Context, Poll};
25use std::time::Duration;
26
27use axum_core::extract::Request;
28use axum_core::response::Response;
29use http::{HeaderMap, Uri};
30use r402::facilitator::Facilitator;
31use r402::proto::v2;
32use tower::util::BoxCloneSyncService;
33use tower::{Layer, Service};
34use url::Url;
35
36use super::facilitator::FacilitatorClient;
37use super::paygate::{Paygate, ResourceInfoBuilder};
38use super::pricing::{DynamicPriceTags, PriceTagSource, StaticPriceTags};
39
40pub struct X402Middleware<F> {
45 facilitator: F,
46 base_url: Option<Url>,
47}
48
49impl<F: Clone> Clone for X402Middleware<F> {
50 fn clone(&self) -> Self {
51 Self {
52 facilitator: self.facilitator.clone(),
53 base_url: self.base_url.clone(),
54 }
55 }
56}
57
58impl<F: std::fmt::Debug> std::fmt::Debug for X402Middleware<F> {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.debug_struct("X402Middleware")
61 .field("facilitator", &self.facilitator)
62 .field("base_url", &self.base_url)
63 .finish()
64 }
65}
66
67impl<F> X402Middleware<F> {
68 pub const fn facilitator(&self) -> &F {
70 &self.facilitator
71 }
72}
73
74impl X402Middleware<Arc<FacilitatorClient>> {
75 #[must_use]
81 pub fn new(url: &str) -> Self {
82 let facilitator = FacilitatorClient::try_from(url).expect("Invalid facilitator URL");
83 Self {
84 facilitator: Arc::new(facilitator),
85 base_url: None,
86 }
87 }
88
89 pub fn try_new(url: &str) -> Result<Self, Box<dyn std::error::Error>> {
95 let facilitator = FacilitatorClient::try_from(url)?;
96 Ok(Self {
97 facilitator: Arc::new(facilitator),
98 base_url: None,
99 })
100 }
101
102 #[must_use]
104 pub fn facilitator_url(&self) -> &Url {
105 self.facilitator.base_url()
106 }
107
108 #[must_use]
113 pub fn with_supported_cache_ttl(&self, ttl: Duration) -> Self {
114 let inner = Arc::unwrap_or_clone(Arc::clone(&self.facilitator));
115 let facilitator = Arc::new(inner.with_supported_cache_ttl(ttl));
116 Self {
117 facilitator,
118 base_url: self.base_url.clone(),
119 }
120 }
121}
122
123impl TryFrom<&str> for X402Middleware<Arc<FacilitatorClient>> {
124 type Error = Box<dyn std::error::Error>;
125
126 fn try_from(value: &str) -> Result<Self, Self::Error> {
127 Self::try_new(value)
128 }
129}
130
131impl TryFrom<String> for X402Middleware<Arc<FacilitatorClient>> {
132 type Error = Box<dyn std::error::Error>;
133
134 fn try_from(value: String) -> Result<Self, Self::Error> {
135 Self::try_new(&value)
136 }
137}
138
139impl<F> X402Middleware<F>
140where
141 F: Clone,
142{
143 #[must_use]
150 pub fn with_base_url(&self, base_url: Url) -> Self {
151 let mut this = self.clone();
152 this.base_url = Some(base_url);
153 this
154 }
155}
156
157impl<TFacilitator> X402Middleware<TFacilitator>
158where
159 TFacilitator: Clone,
160{
161 #[must_use]
166 pub fn with_price_tag(
167 &self,
168 price_tag: v2::PriceTag,
169 ) -> X402LayerBuilder<StaticPriceTags, TFacilitator> {
170 X402LayerBuilder {
171 facilitator: self.facilitator.clone(),
172 price_source: StaticPriceTags::new(vec![price_tag]),
173 base_url: self.base_url.clone().map(Arc::new),
174 resource: Arc::new(ResourceInfoBuilder::default()),
175 }
176 }
177
178 #[must_use]
183 pub fn with_dynamic_price<F, Fut>(
184 &self,
185 callback: F,
186 ) -> X402LayerBuilder<DynamicPriceTags, TFacilitator>
187 where
188 F: Fn(&HeaderMap, &Uri, Option<&Url>) -> Fut + Send + Sync + 'static,
189 Fut: Future<Output = Vec<v2::PriceTag>> + Send + 'static,
190 {
191 X402LayerBuilder {
192 facilitator: self.facilitator.clone(),
193 price_source: DynamicPriceTags::new(callback),
194 base_url: self.base_url.clone().map(Arc::new),
195 resource: Arc::new(ResourceInfoBuilder::default()),
196 }
197 }
198}
199
200#[derive(Clone)]
205#[allow(missing_debug_implementations)] pub struct X402LayerBuilder<TSource, TFacilitator> {
207 facilitator: TFacilitator,
208 base_url: Option<Arc<Url>>,
209 price_source: TSource,
210 resource: Arc<ResourceInfoBuilder>,
211}
212
213impl<TFacilitator> X402LayerBuilder<StaticPriceTags, TFacilitator> {
214 #[must_use]
220 pub fn with_price_tag(mut self, price_tag: v2::PriceTag) -> Self {
221 self.price_source = self.price_source.with_price_tag(price_tag);
222 self
223 }
224}
225
226#[allow(missing_debug_implementations)] impl<TSource, TFacilitator> X402LayerBuilder<TSource, TFacilitator> {
228 #[must_use]
232 pub fn with_description(mut self, description: String) -> Self {
233 let mut new_resource = (*self.resource).clone();
234 new_resource.description = description;
235 self.resource = Arc::new(new_resource);
236 self
237 }
238
239 #[must_use]
243 pub fn with_mime_type(mut self, mime: String) -> Self {
244 let mut new_resource = (*self.resource).clone();
245 new_resource.mime_type = mime;
246 self.resource = Arc::new(new_resource);
247 self
248 }
249
250 #[must_use]
255 #[allow(clippy::needless_pass_by_value)] pub fn with_resource(mut self, resource: Url) -> Self {
257 let mut new_resource = (*self.resource).clone();
258 new_resource.url = Some(resource.to_string());
259 self.resource = Arc::new(new_resource);
260 self
261 }
262}
263
264impl<S, TSource, TFacilitator> Layer<S> for X402LayerBuilder<TSource, TFacilitator>
265where
266 S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + Sync + 'static,
267 S::Future: Send + 'static,
268 TFacilitator: Facilitator + Clone,
269 TSource: PriceTagSource,
270{
271 type Service = X402MiddlewareService<TSource, TFacilitator>;
272
273 fn layer(&self, inner: S) -> Self::Service {
274 X402MiddlewareService {
275 facilitator: self.facilitator.clone(),
276 base_url: self.base_url.clone(),
277 price_source: self.price_source.clone(),
278 resource: Arc::clone(&self.resource),
279 inner: BoxCloneSyncService::new(inner),
280 }
281 }
282}
283
284#[derive(Clone)]
289#[allow(missing_debug_implementations)] pub struct X402MiddlewareService<TSource, TFacilitator> {
291 facilitator: TFacilitator,
293 base_url: Option<Arc<Url>>,
295 price_source: TSource,
297 resource: Arc<ResourceInfoBuilder>,
299 inner: BoxCloneSyncService<Request, Response, Infallible>,
301}
302
303impl<TSource, TFacilitator> Service<Request> for X402MiddlewareService<TSource, TFacilitator>
304where
305 TSource: PriceTagSource,
306 TFacilitator: Facilitator + Clone + Send + Sync + 'static,
307{
308 type Response = Response;
309 type Error = Infallible;
310 type Future = Pin<Box<dyn Future<Output = Result<Response, Infallible>> + Send>>;
311
312 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
314 self.inner.poll_ready(cx)
315 }
316
317 fn call(&mut self, req: Request) -> Self::Future {
319 let price_source = self.price_source.clone();
320 let facilitator = self.facilitator.clone();
321 let base_url = self.base_url.clone();
322 let resource_builder = Arc::clone(&self.resource);
323 let mut inner = self.inner.clone();
324
325 Box::pin(async move {
326 let accepts = price_source
328 .resolve(req.headers(), req.uri(), base_url.as_deref())
329 .await;
330
331 if accepts.is_empty() {
333 return inner.call(req).await;
334 }
335
336 let resource = resource_builder.as_resource_info(base_url.as_deref(), &req);
337
338 let gate = {
339 let mut gate = Paygate::builder(facilitator)
340 .accepts(accepts)
341 .resource(resource)
342 .build();
343 gate.enrich_accepts().await;
344 gate
345 };
346 gate.handle_request(inner, req).await
347 })
348 }
349}