mpp 0.15.0

Rust SDK for the Machine Payments Protocol (MPP)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Multi-method challenge composition and credential dispatch.
//!
//! Combines challenges from multiple [`Mpp`](super::Mpp) instances into a
//! single ranked list, optionally reordered by the client's `Accept-Payment`
//! header preferences. Also dispatches incoming credentials to the correct
//! verifier by matching `method`/`intent`.
//!
//! # Example
//!
//! ```ignore
//! use mpp::server::{compose, compose_verify_with_expected_requests};
//! use mpp::format_www_authenticate_many;
//!
//! let mut challenges = tempo_mpp.charge("0.10")?;
//! challenges.push(stripe_mpp.stripe_charge("0.10")?);
//!
//! // Challenge path: rank and format multiple WWW-Authenticate headers
//! let ranked = compose(
//!     challenges,
//!     req.headers().get("Accept-Payment").and_then(|h| h.to_str().ok()),
//! );
//! let headers = format_www_authenticate_many(&ranked)?;
//!
//! // Credential path: dispatch to the correct verifier, which checks the
//! // credential against the request this route charges with that method
//! let tempo_expected = tempo_mpp.expected_charge_request("0.10", Default::default())?;
//! let stripe_expected = stripe_mpp.stripe_expected_charge_request("0.10", Default::default())?;
//! let receipt = compose_verify_with_expected_requests(
//!     &[(&tempo_mpp, &tempo_expected), (&stripe_mpp, &stripe_expected)],
//!     &credential,
//! )
//! .await?;
//! ```

use std::future::Future;
use std::pin::Pin;

use crate::protocol::core::accept_payment;
use crate::protocol::core::{PaymentChallenge, PaymentCredential, Receipt};
use crate::protocol::intents::ChargeRequest;
use crate::protocol::traits::VerificationError;

/// Rank pre-generated challenges by client `Accept-Payment` preferences.
///
/// Returns challenges reordered by the client's declared preferences.
/// Challenges matched only by `q=0` (explicit opt-out) are excluded.
///
/// If `accept_payment` is `None` or fails to parse, the original order
/// is preserved (matching `mppx`'s fallback behavior).
///
/// The returned list is ready to pass to
/// [`format_www_authenticate_many()`](crate::protocol::core::format_www_authenticate_many).
pub fn compose(
    challenges: Vec<PaymentChallenge>,
    accept_payment: Option<&str>,
) -> Vec<PaymentChallenge> {
    let preferences = accept_payment
        .and_then(|header| accept_payment::parse(header).ok())
        .unwrap_or_default();

    if preferences.is_empty() {
        return challenges;
    }

    let ranked_refs = accept_payment::rank(&challenges, &preferences);

    // If ranking yields nothing (e.g. all offers q=0), fall back to original order
    if ranked_refs.is_empty() {
        return challenges;
    }

    // Map ranked references back to owned values by position.
    // Build an index from pointer identity to original vec index.
    let ranked_indices: Vec<usize> = ranked_refs
        .iter()
        .map(|r| {
            challenges
                .iter()
                .position(|c| std::ptr::eq(c, *r))
                .expect("ranked ref must point into challenges slice")
        })
        .collect();

    // Consume the original vec and pick by index
    let mut slots: Vec<Option<PaymentChallenge>> = challenges.into_iter().map(Some).collect();
    ranked_indices
        .into_iter()
        .map(|i| slots[i].take().expect("each index used exactly once"))
        .collect()
}

/// Object-safe trait for type-erased charge verification.
///
/// This enables multi-method credential dispatch: multiple `Mpp` instances
/// with different `ChargeMethod` types can be collected as `&dyn ChargeVerifier`
/// and used with [`compose_verify_with_expected_requests()`].
///
/// Automatically implemented for all [`Mpp<M, S>`](super::Mpp) where
/// `M: ChargeMethod`.
pub trait ChargeVerifier: Send + Sync {
    /// Payment method name (e.g., `"tempo"`, `"stripe"`).
    fn method_name(&self) -> &str;

    /// Verify a payment credential without checking it against a route.
    fn verify_credential<'a>(
        &'a self,
        credential: &'a PaymentCredential,
    ) -> Pin<Box<dyn Future<Output = Result<Receipt, VerificationError>> + Send + 'a>>;

    /// Verify a payment credential against the request the route charges.
    ///
    /// The default rejects every credential, so a custom verifier that does
    /// not compare the request cannot pass for a route-bound one.
    fn verify_credential_with_expected_request<'a>(
        &'a self,
        credential: &'a PaymentCredential,
        expected: &'a ChargeRequest,
    ) -> Pin<Box<dyn Future<Output = Result<Receipt, VerificationError>> + Send + 'a>> {
        let _ = (credential, expected);
        Box::pin(std::future::ready(Err(VerificationError::new(
            "expected request verification is not implemented for this ChargeVerifier",
        ))))
    }
}

impl<M, S> ChargeVerifier for super::Mpp<M, S>
where
    M: crate::protocol::traits::ChargeMethod,
    S: Send + Sync,
{
    fn method_name(&self) -> &str {
        self.method_name()
    }

    fn verify_credential<'a>(
        &'a self,
        credential: &'a PaymentCredential,
    ) -> Pin<Box<dyn Future<Output = Result<Receipt, VerificationError>> + Send + 'a>> {
        Box::pin(self.broadcast_credential(credential))
    }

    fn verify_credential_with_expected_request<'a>(
        &'a self,
        credential: &'a PaymentCredential,
        expected: &'a ChargeRequest,
    ) -> Pin<Box<dyn Future<Output = Result<Receipt, VerificationError>> + Send + 'a>> {
        Box::pin(self.broadcast_credential_with_expected_request(credential, expected))
    }
}

/// Dispatch a charge credential to the matching verifier by method name.
///
/// Non-charge intents are rejected immediately. If no verifier matches
/// the credential's method, falls back to the first verifier (which
/// will reject via HMAC mismatch).
#[deprecated(
    since = "0.15.0",
    note = "accepts a credential paid for any route of the matching handler, whatever its price; use `compose_verify_with_expected_requests`"
)]
pub async fn compose_verify(
    verifiers: &[&dyn ChargeVerifier],
    credential: &PaymentCredential,
) -> Result<Receipt, VerificationError> {
    select_verifier(verifiers, credential, |verifier| verifier.method_name())?
        .verify_credential(credential)
        .await
}

/// Dispatch a charge credential to the matching verifier by method name and
/// verify it against the request this route charges with that method.
///
/// Each entry pairs a verifier with its expected request (for example from
/// [`Mpp::expected_charge_request()`](super::Mpp::expected_charge_request)).
/// A credential whose challenge was issued for another amount, currency or
/// recipient is rejected, so a payment for a cheaper route cannot be replayed
/// here. Dispatch follows the same rules as [`compose_verify()`].
pub async fn compose_verify_with_expected_requests(
    routes: &[(&dyn ChargeVerifier, &ChargeRequest)],
    credential: &PaymentCredential,
) -> Result<Receipt, VerificationError> {
    let (verifier, expected) =
        select_verifier(routes, credential, |(verifier, _)| verifier.method_name())?;
    verifier
        .verify_credential_with_expected_request(credential, expected)
        .await
}

/// Pick the entry whose method matches the charge credential, else the first.
fn select_verifier<'a, T>(
    verifiers: &'a [T],
    credential: &PaymentCredential,
    method_name: impl Fn(&T) -> &str,
) -> Result<&'a T, VerificationError> {
    if verifiers.is_empty() {
        return Err(VerificationError::new("No verifiers configured"));
    }

    let cred_method = credential.challenge.method.as_str();
    let cred_intent = credential.challenge.intent.as_str();

    if cred_intent != "charge" {
        return Err(VerificationError::with_code(
            format!("compose_verify only supports charge credentials, got intent '{cred_intent}'"),
            crate::protocol::traits::ErrorCode::InvalidCredential,
        ));
    }

    Ok(verifiers
        .iter()
        .find(|v| method_name(v) == cred_method)
        .unwrap_or(&verifiers[0]))
}

#[cfg(test)]
#[allow(deprecated)]
mod tests {
    use super::*;
    use crate::protocol::core::types::Base64UrlJson;
    use crate::protocol::core::{ChallengeEcho, PaymentPayload};
    use crate::protocol::intents::ChargeRequest;
    use std::future::Future;

    fn challenge(method: &str, intent: &str) -> PaymentChallenge {
        PaymentChallenge {
            id: format!("id-{method}-{intent}"),
            realm: "test".into(),
            method: method.into(),
            intent: intent.into(),
            request: Base64UrlJson::from_value(&serde_json::json!({})).unwrap(),
            expires: None,
            description: None,
            digest: None,
            opaque: None,
            header: None,
        }
    }

    #[test]
    fn no_header_preserves_order() {
        let challenges = vec![challenge("stripe", "charge"), challenge("tempo", "charge")];
        let result = compose(challenges, None);
        assert_eq!(result[0].method.as_str(), "stripe");
        assert_eq!(result[1].method.as_str(), "tempo");
    }

    #[test]
    fn ranks_by_client_preference() {
        let challenges = vec![challenge("stripe", "charge"), challenge("tempo", "charge")];
        let result = compose(challenges, Some("tempo/charge, stripe/charge;q=0.5"));
        assert_eq!(result[0].method.as_str(), "tempo");
        assert_eq!(result[1].method.as_str(), "stripe");
    }

    #[test]
    fn excludes_q_zero_offers() {
        let challenges = vec![challenge("tempo", "charge"), challenge("stripe", "charge")];
        let result = compose(challenges, Some("tempo/charge;q=0, stripe/charge"));
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].method.as_str(), "stripe");
    }

    #[test]
    fn all_q_zero_falls_back_to_original_order() {
        let challenges = vec![challenge("tempo", "charge"), challenge("stripe", "charge")];
        let result = compose(challenges, Some("tempo/charge;q=0, stripe/charge;q=0"));
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].method.as_str(), "tempo");
    }

    #[test]
    fn parse_error_preserves_original_order() {
        let challenges = vec![challenge("stripe", "charge"), challenge("tempo", "charge")];
        let result = compose(challenges, Some(";;;invalid;;;"));
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].method.as_str(), "stripe");
    }

    // ── compose_verify tests ──────────────────────────────────────────

    #[derive(Clone)]
    struct MockMethod(&'static str);

    #[allow(clippy::manual_async_fn)]
    impl crate::protocol::traits::ChargeMethod for MockMethod {
        fn method(&self) -> &str {
            self.0
        }
        fn verify(
            &self,
            _credential: &PaymentCredential,
            _request: &ChargeRequest,
        ) -> impl Future<Output = Result<Receipt, VerificationError>> + Send {
            let name = self.0;
            async move { Ok(Receipt::success(name, format!("{name}_ref"))) }
        }
    }

    fn test_credential(method: &str, secret: &str) -> PaymentCredential {
        let request = Base64UrlJson::from_value(&serde_json::json!({
            "amount": "1000",
            "currency": "USD"
        }))
        .unwrap();
        let request_raw = request.raw();
        let expires = (time::OffsetDateTime::now_utc() + time::Duration::minutes(5))
            .format(&time::format_description::well_known::Rfc3339)
            .unwrap();
        let id = crate::protocol::core::compute_challenge_id(
            secret,
            "test.example.com",
            method,
            "charge",
            request_raw,
            Some(&expires),
            None,
            None,
        );
        let echo = ChallengeEcho {
            id,
            realm: "test.example.com".into(),
            method: method.into(),
            intent: "charge".into(),
            request: Base64UrlJson::from_raw(request_raw),
            expires: Some(expires),
            description: None,
            digest: None,
            opaque: None,
            header: None,
        };
        PaymentCredential::new(echo, PaymentPayload::hash("0x123"))
    }

    #[tokio::test]
    async fn verify_dispatches_to_matching_method() {
        let mpp_a = super::super::Mpp::new(MockMethod("alpha"), "test.example.com", "secret");
        let mpp_b = super::super::Mpp::new(MockMethod("beta"), "test.example.com", "secret");
        let verifiers: Vec<&dyn ChargeVerifier> = vec![&mpp_a, &mpp_b];

        let cred = test_credential("beta", "secret");
        let receipt = compose_verify(&verifiers, &cred).await.unwrap();
        assert_eq!(receipt.method.as_str(), "beta");
    }

    #[tokio::test]
    async fn verify_falls_back_to_first_on_no_match() {
        let mpp = super::super::Mpp::new(MockMethod("alpha"), "test.example.com", "secret");
        let verifiers: Vec<&dyn ChargeVerifier> = vec![&mpp];

        // Credential for "unknown" method — falls back to first verifier.
        // Use a different secret so the HMAC check fails.
        let cred = test_credential("unknown", "wrong-secret");
        let result = compose_verify(&verifiers, &cred).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn verify_empty_verifiers_returns_error() {
        let cred = test_credential("tempo", "secret");
        let result = compose_verify(&[], &cred).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("No verifiers"));
    }

    #[tokio::test]
    async fn verify_rejects_non_charge_intent() {
        let mpp = super::super::Mpp::new(MockMethod("tempo"), "test.example.com", "secret");
        let verifiers: Vec<&dyn ChargeVerifier> = vec![&mpp];

        // Build a credential with intent="session"
        let request = Base64UrlJson::from_value(&serde_json::json!({
            "amount": "1000",
            "currency": "USD"
        }))
        .unwrap();
        let expires = (time::OffsetDateTime::now_utc() + time::Duration::minutes(5))
            .format(&time::format_description::well_known::Rfc3339)
            .unwrap();
        let id = crate::protocol::core::compute_challenge_id(
            "secret",
            "test.example.com",
            "tempo",
            "session",
            request.raw(),
            Some(&expires),
            None,
            None,
        );
        let echo = ChallengeEcho {
            id,
            realm: "test.example.com".into(),
            method: "tempo".into(),
            intent: "session".into(),
            request: Base64UrlJson::from_raw(request.raw()),
            expires: Some(expires),
            description: None,
            digest: None,
            opaque: None,
            header: None,
        };
        let cred = PaymentCredential::new(echo, PaymentPayload::hash("0x123"));

        let result = compose_verify(&verifiers, &cred).await;
        let err = result.unwrap_err();
        assert!(err.message.contains("only supports charge"));
    }

    fn expected(amount: &str) -> ChargeRequest {
        ChargeRequest {
            amount: amount.into(),
            currency: "USD".into(),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn verify_with_expected_requests_rejects_other_routes_credential() {
        let mpp = super::super::Mpp::new(MockMethod("alpha"), "test.example.com", "secret");
        // Issued by this handler for a route that charges 1000.
        let cred = test_credential("alpha", "secret");

        let expensive = expected("100000");
        let err = compose_verify_with_expected_requests(&[(&mpp, &expensive)], &cred)
            .await
            .unwrap_err();
        assert!(err.message.contains("Amount mismatch"), "{}", err.message);

        let route = expected("1000");
        let receipt = compose_verify_with_expected_requests(&[(&mpp, &route)], &cred)
            .await
            .unwrap();
        assert_eq!(receipt.method.as_str(), "alpha");

        // The deprecated dispatcher has no expectation to compare against.
        let verifiers: Vec<&dyn ChargeVerifier> = vec![&mpp];
        assert!(compose_verify(&verifiers, &cred).await.is_ok());
    }

    #[tokio::test]
    async fn verify_with_expected_requests_uses_matching_verifiers_expectation() {
        let mpp_a = super::super::Mpp::new(MockMethod("alpha"), "test.example.com", "secret");
        let mpp_b = super::super::Mpp::new(MockMethod("beta"), "test.example.com", "secret");
        let (alpha_expected, beta_expected) = (expected("5"), expected("1000"));
        let cred = test_credential("beta", "secret");

        let receipt = compose_verify_with_expected_requests(
            &[(&mpp_a, &alpha_expected), (&mpp_b, &beta_expected)],
            &cred,
        )
        .await
        .unwrap();
        assert_eq!(receipt.method.as_str(), "beta");

        let result = compose_verify_with_expected_requests(
            &[(&mpp_a, &beta_expected), (&mpp_b, &alpha_expected)],
            &cred,
        )
        .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn verify_with_expected_requests_rejects_verifier_without_comparison() {
        struct Unbound;
        impl ChargeVerifier for Unbound {
            fn method_name(&self) -> &str {
                "alpha"
            }
            fn verify_credential<'a>(
                &'a self,
                _credential: &'a PaymentCredential,
            ) -> Pin<Box<dyn Future<Output = Result<Receipt, VerificationError>> + Send + 'a>>
            {
                Box::pin(async { Ok(Receipt::success("alpha", "alpha_ref")) })
            }
        }

        let cred = test_credential("alpha", "secret");
        let route = expected("1000");
        let result = compose_verify_with_expected_requests(&[(&Unbound, &route)], &cred).await;
        assert!(result.is_err());
    }

    #[test]
    fn empty_challenges_returns_empty() {
        let result = compose(vec![], None);
        assert!(result.is_empty());

        let result = compose(vec![], Some("tempo/charge"));
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn verify_same_method_picks_first() {
        let mpp_a = super::super::Mpp::new(MockMethod("tempo"), "test.example.com", "secret");
        let mpp_b = super::super::Mpp::new(MockMethod("tempo"), "test.example.com", "secret");
        let verifiers: Vec<&dyn ChargeVerifier> = vec![&mpp_a, &mpp_b];

        let cred = test_credential("tempo", "secret");
        let receipt = compose_verify(&verifiers, &cred).await.unwrap();
        // Both are "tempo" — first one wins, both return same receipt
        assert_eq!(receipt.method.as_str(), "tempo");
    }
}