r402-http 0.20.0

HTTP transport layer for the x402 payment protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! [`X402Layer`]: Tower layer after a successful `with_price_tag`.

use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use axum_core::extract::Request;
use axum_core::response::Response;
use r402_protocol::network::ChainIdPattern;
use r402_protocol::payment::{PriceTag, ResourceInfo};
use r402_server::{BackgroundSettlementTracker, ResourceServer, SchemeNetworkServer};
use tower::util::BoxCloneSyncService;
use tower::{Layer, Service};
use url::Url;

use super::SettlementMode;
use super::builder::{BuildError, validate_static_layer};
use super::fail::abort_response;
use super::gate::Gate;
use super::hooks::{DynGateHooks, GateHooks, ProtectedRequestOutcome};
use super::pricing::{DynamicPriceTags, PriceTagSource, StaticPriceTags};

/// Route-level resource metadata. `url` pins `ResourceInfo.url`; it does not waive `base_url`.
#[derive(Debug, Clone)]
pub(crate) struct ResourceTemplate {
    pub(crate) description: String,
    pub(crate) mime_type: String,
    pub(crate) url: Option<Url>,
}

impl Default for ResourceTemplate {
    fn default() -> Self {
        Self {
            description: String::new(),
            mime_type: "application/json".to_owned(),
            url: None,
        }
    }
}

impl ResourceTemplate {
    /// Builds `ResourceInfo` from the pinned URL or `base_url` + request path/query.
    ///
    /// Never reads `Host` and never falls back to `http://localhost`.
    pub(crate) fn resolve(&self, base_url: &Url, req: &Request) -> ResourceInfo {
        let url = self.url.as_ref().map_or_else(
            || {
                let mut url = base_url.clone();
                url.set_path(req.uri().path());
                url.set_query(req.uri().query());
                url.to_string()
            },
            ToString::to_string,
        );
        let mut info = ResourceInfo::new(url);
        if !self.description.is_empty() {
            info = info.with_description(self.description.clone());
        }
        if !self.mime_type.is_empty() {
            info = info.with_mime_type(self.mime_type.clone());
        }
        info
    }
}

/// Tower layer produced by [`super::X402Middleware::with_price_tag`].
#[derive(Clone)]
pub struct X402Layer<TSource> {
    pub(crate) server: ResourceServer,
    pub(crate) base_url: Arc<Url>,
    pub(crate) price_source: TSource,
    pub(crate) resource: Arc<ResourceTemplate>,
    pub(crate) settlement_mode: SettlementMode,
    pub(crate) settlement_tracker: Option<BackgroundSettlementTracker>,
    pub(crate) hooks: Option<Arc<dyn DynGateHooks>>,
    #[cfg(feature = "siwx")]
    pub(crate) siwx: Option<Arc<super::SiwxGate>>,
}

impl X402Layer<StaticPriceTags> {
    /// Adds another static payment option.
    ///
    /// # Errors
    ///
    /// [`BuildError`] when the appended tag (or the existing list with the
    /// current settlement mode) is invalid.
    pub fn with_price_tag(mut self, price_tag: PriceTag) -> Result<Self, BuildError> {
        self.price_source = self.price_source.with_price_tag(price_tag);
        self.validate_static()?;
        Ok(self)
    }

    /// After-handler settle scheduler for the `authorization` payment flow.
    ///
    /// Not a `paymentFlow`. Sequential (default) is spec `authorization`:
    /// settle after the handler, then attach `Payment-Response`. Concurrent
    /// overlaps that settle with the handler and still waits. Background does
    /// not wait and does not attach a receipt. Concurrent and Background are
    /// illegal with `upfront` / `escrow`.
    ///
    /// # Errors
    ///
    /// [`BuildError::Mode`] when `mode` is incompatible with a static tag's flow.
    pub fn with_settlement_mode(mut self, mode: SettlementMode) -> Result<Self, BuildError> {
        self.settlement_mode = mode;
        self.validate_static()?;
        Ok(self)
    }

    /// Enables SIWX access-grant and 402 challenges on this route.
    ///
    /// # Errors
    ///
    /// [`BuildError::EmptyPriceTags`] when this replacement is not auth-only
    /// and the static tag list is empty.
    #[cfg(feature = "siwx")]
    pub fn with_siwx(mut self, gate: super::SiwxGate) -> Result<Self, BuildError> {
        self.siwx = Some(Arc::new(gate));
        self.validate_static()?;
        Ok(self)
    }

    /// Enables SIWX with auth-only access-grant on this route.
    ///
    /// # Errors
    ///
    /// [`BuildError`] from static re-validation after the replacement.
    #[cfg(feature = "siwx")]
    pub fn with_auth_only(self, gate: super::SiwxGate) -> Result<Self, BuildError> {
        self.with_siwx(gate.with_auth_only())
    }

    fn validate_static(&self) -> Result<(), BuildError> {
        validate_static_layer(
            &self.server,
            self.price_source.tags(),
            self.settlement_mode,
            self.static_auth_only(),
        )
    }

    fn static_auth_only(&self) -> bool {
        #[cfg(feature = "siwx")]
        {
            self.siwx.as_ref().is_some_and(|g| g.is_auth_only())
        }
        #[cfg(not(feature = "siwx"))]
        {
            let _ = self;
            false
        }
    }
}

impl X402Layer<DynamicPriceTags> {
    /// After-handler settle scheduler for the `authorization` payment flow.
    ///
    /// Same semantics as [`X402Layer<StaticPriceTags>::with_settlement_mode`].
    /// Dynamic tags are resolved per request; incompatible `upfront` /
    /// `escrow` combinations fail at request time, not here.
    ///
    /// # Errors
    ///
    /// Always `Ok` after storing `mode`.
    #[allow(
        clippy::unnecessary_wraps,
        clippy::missing_const_for_fn,
        reason = "same Result signature as Static"
    )]
    pub fn with_settlement_mode(mut self, mode: SettlementMode) -> Result<Self, BuildError> {
        self.settlement_mode = mode;
        Ok(self)
    }

    /// Enables SIWX access-grant and 402 challenges on this route.
    #[cfg(feature = "siwx")]
    #[must_use]
    pub fn with_siwx(mut self, gate: super::SiwxGate) -> Self {
        self.siwx = Some(Arc::new(gate));
        self
    }

    /// Enables SIWX with auth-only access-grant on this route.
    #[cfg(feature = "siwx")]
    #[must_use]
    pub fn with_auth_only(self, gate: super::SiwxGate) -> Self {
        self.with_siwx(gate.with_auth_only())
    }
}

impl<TSource: std::fmt::Debug> std::fmt::Debug for X402Layer<TSource> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("X402Layer")
            .field("base_url", &self.base_url)
            .field("price_source", &self.price_source)
            .field("settlement_mode", &self.settlement_mode)
            .finish_non_exhaustive()
    }
}

impl<TSource> X402Layer<TSource> {
    /// Registers a scheme/network adapter on the owned resource server.
    #[must_use]
    pub fn with_scheme(
        mut self,
        network: ChainIdPattern,
        scheme: impl SchemeNetworkServer + 'static,
    ) -> Self {
        self.server.register_scheme(network, scheme);
        self
    }

    /// Description copied onto 402 `resource.description`.
    #[must_use]
    pub fn with_description(mut self, description: String) -> Self {
        let mut new_resource = (*self.resource).clone();
        new_resource.description = description;
        self.resource = Arc::new(new_resource);
        self
    }

    /// MIME type copied onto 402 `resource.mimeType`.
    #[must_use]
    pub fn with_mime_type(mut self, mime: String) -> Self {
        let mut new_resource = (*self.resource).clone();
        new_resource.mime_type = mime;
        self.resource = Arc::new(new_resource);
        self
    }

    /// Pins `ResourceInfo.url` for this route. Does **not** waive `base_url`.
    #[must_use]
    pub fn with_resource(mut self, resource: Url) -> Self {
        let mut new_resource = (*self.resource).clone();
        new_resource.url = Some(resource);
        self.resource = Arc::new(new_resource);
        self
    }

    /// In-flight counter for [`SettlementMode::Background`] settle tasks.
    #[must_use]
    pub fn with_settlement_tracker(mut self, tracker: BackgroundSettlementTracker) -> Self {
        self.settlement_tracker = Some(tracker);
        self
    }

    /// HTTP-layer hooks (`GrantAccess` slot).
    #[must_use]
    pub fn with_hooks<H>(mut self, hooks: H) -> Self
    where
        H: GateHooks + 'static,
    {
        self.hooks = Some(Arc::new(hooks));
        self
    }
}

impl<S, TSource> Layer<S> for X402Layer<TSource>
where
    S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + Sync + 'static,
    S::Future: Send + 'static,
    TSource: PriceTagSource,
{
    type Service = X402MiddlewareService<TSource>;

    fn layer(&self, inner: S) -> Self::Service {
        X402MiddlewareService {
            server: self.server.clone(),
            base_url: Arc::clone(&self.base_url),
            price_source: self.price_source.clone(),
            resource: Arc::clone(&self.resource),
            settlement_mode: self.settlement_mode,
            settlement_tracker: self.settlement_tracker.clone(),
            hooks: self.hooks.clone(),
            #[cfg(feature = "siwx")]
            siwx: self.siwx.clone(),
            inner: BoxCloneSyncService::new(inner),
        }
    }
}

/// Service produced by [`X402Layer`].
#[derive(Clone)]
#[allow(
    missing_debug_implementations,
    reason = "BoxCloneSyncService does not impl Debug"
)]
pub struct X402MiddlewareService<TSource> {
    server: ResourceServer,
    base_url: Arc<Url>,
    price_source: TSource,
    resource: Arc<ResourceTemplate>,
    settlement_mode: SettlementMode,
    settlement_tracker: Option<BackgroundSettlementTracker>,
    hooks: Option<Arc<dyn DynGateHooks>>,
    #[cfg(feature = "siwx")]
    siwx: Option<Arc<super::SiwxGate>>,
    inner: BoxCloneSyncService<Request, Response, Infallible>,
}

impl<TSource> Service<Request> for X402MiddlewareService<TSource>
where
    TSource: PriceTagSource,
{
    type Response = Response;
    type Error = Infallible;
    type Future = Pin<Box<dyn Future<Output = Result<Response, Infallible>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        Box::pin(enforce(
            self.price_source.clone(),
            self.server.clone(),
            Arc::clone(&self.base_url),
            Arc::clone(&self.resource),
            self.settlement_mode,
            self.settlement_tracker.clone(),
            self.hooks.clone(),
            #[cfg(feature = "siwx")]
            self.siwx.clone(),
            self.inner.clone(),
            req,
        ))
    }
}

#[allow(
    clippy::too_many_arguments,
    clippy::excessive_nesting,
    reason = "cloned service fields; hook match must borrow Request only for the await"
)]
async fn enforce<TSource: PriceTagSource>(
    price_source: TSource,
    server: ResourceServer,
    base_url: Arc<Url>,
    resource_builder: Arc<ResourceTemplate>,
    settlement_mode: SettlementMode,
    settlement_tracker: Option<BackgroundSettlementTracker>,
    hooks: Option<Arc<dyn DynGateHooks>>,
    #[cfg(feature = "siwx")] siwx: Option<Arc<super::SiwxGate>>,
    mut inner: BoxCloneSyncService<Request, Response, Infallible>,
    req: Request,
) -> Result<Response, Infallible> {
    #[cfg(feature = "siwx")]
    if let Some(siwx_gate) = siwx.as_ref() {
        let header = req
            .headers()
            .get(crate::headers::SIGN_IN_WITH_X)
            .and_then(|v| v.to_str().ok())
            .map(str::to_owned);
        let path = req.uri().path().to_owned();
        if let Some(header) = header
            && siwx_gate.try_grant(&header, &path).await
        {
            return inner.call(req).await;
        }
    }

    if let Some(h) = hooks.as_ref() {
        match h.on_protected_request(&req).await {
            ProtectedRequestOutcome::Continue => {}
            ProtectedRequestOutcome::GrantAccess => return inner.call(req).await,
            ProtectedRequestOutcome::Abort { status, body } => {
                return Ok(abort_response(status, body));
            }
        }
    }

    let accepts = price_source
        .resolve(req.headers(), req.uri(), base_url.as_ref())
        .await;
    #[cfg(feature = "siwx")]
    let auth_only = siwx.as_ref().is_some_and(|g| g.is_auth_only());
    #[cfg(not(feature = "siwx"))]
    let auth_only = false;
    if accepts.is_empty() && !auth_only {
        return inner.call(req).await;
    }

    let resource = resource_builder.resolve(base_url.as_ref(), &req);
    let mut gate = Gate::from_parts(
        server,
        accepts,
        resource,
        settlement_mode,
        settlement_tracker,
        hooks,
        #[cfg(feature = "siwx")]
        siwx.clone(),
    );
    if let Err(err) = gate.require_schemes().await {
        return Ok(gate.error_response(err));
    }
    if let Err(err) = gate.assert_mode_compatible(settlement_mode) {
        return Ok(gate.error_response(err));
    }
    if let Err(err) = gate.build_payment_required().await {
        return Ok(gate.error_response(err));
    }
    #[cfg(feature = "siwx")]
    if let Some(siwx) = siwx.as_ref()
        && let Err(err) = gate.attach_siwx_challenge(siwx, req.uri().path())
    {
        return Ok(gate.error_response(err));
    }

    let result = match settlement_mode {
        SettlementMode::Sequential => gate.handle_request(inner, req).await,
        SettlementMode::Concurrent => {
            // Join future holds handler + settle state (~17 KiB).
            Box::pin(gate.handle_request_concurrent(inner, req)).await
        }
        SettlementMode::Background => gate.handle_request_background(inner, req).await,
    };
    Ok(result.unwrap_or_else(|err| gate.error_response(err)))
}