open-standards/openpay/crates/op-bnpl/tests/webhook_verification.rs
Transaction Science 3a9216fa0e unbreak four standards that had never been compiled
Updating the toolchain surfaced five workspaces that did not build. Every one
was verified against its ORIGINAL pre-update lockfile and manifest before being
touched: all of them were already broken. None of this was caused by the
dependency update.

The same handful of upstream migrations account for nearly all of it:

  hmac 0.13 moved new_from_slice from Mac to KeyInit
    openpay: op-webhook, op-bnpl, op-ewallets-asia, op-mobile-wallets — plus
    the op-bnpl and op-ewallets-asia *test* targets, which a src-only search
    misses and `--all-targets` does not.

  reqwest 0.13 gates builder methods behind features
    eoc-tools needed "query"; openpay/op-tax needed "form".

  cipher 0.4 -> 0.5
    openpay/op-mobile-wallets declared aes 0.9 (cipher 0.5) while ctr 0.9 and
    aes-gcm 0.10 both sit on cipher 0.4. Aligned aes to 0.8.

  jcp-schema::Grant gained budget_funds and caveats
    mesh-core and joule-ui-interaction examples were never updated. A path
    dependency, so no lockfile change could have caused or fixed this.

Not one line of this is a behaviour change. Every fix is an import, a feature
flag, a version alignment, or two struct fields.

The finding is larger than the diff. smart-byte (47k LOC, fixed earlier today),
openpay (six separate breakages across five crates), eoc, mesh and joule-ui were
all silently broken, all by the same small set of migrations, all invisible
because nothing ever compiled them. The build gate added in this branch
currently covers three workspaces; on this evidence it should cover every one.

Verified after fixing: openpay 1935 tests pass, eoc/mesh/joule-ui build clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-08-26 08:34:06 -04:00

101 lines
3.4 KiB
Rust

//! Integration test for inbound webhook verification across all three
//! providers. Positive cases verify the round trip end-to-end; negative
//! cases exercise the constant-time signature comparison.
use hmac::digest::KeyInit;
use hmac::{Hmac, Mac};
use op_bnpl::{
BnplEventKind, WebhookProvider, verify_affirm_webhook, verify_afterpay_webhook,
verify_klarna_webhook,
};
use sha2::Sha256;
type HmacSha256 = Hmac<Sha256>;
fn sign_b64(secret: &[u8], body: &[u8]) -> String {
let mut mac = <HmacSha256 as KeyInit>::new_from_slice(secret).expect("hmac");
mac.update(body);
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(mac.finalize().into_bytes())
}
fn sign_hex(secret: &[u8], body: &[u8]) -> String {
let mut mac = <HmacSha256 as KeyInit>::new_from_slice(secret).expect("hmac");
mac.update(body);
hex::encode(mac.finalize().into_bytes())
}
#[test]
fn affirm_event_round_trip_with_signature() {
let secret = b"affirm-shared";
let body = br#"{"id":"e1","type":"capture.created","charge_id":"CHG_9"}"#;
let sig = sign_b64(secret, body);
assert!(verify_affirm_webhook(secret, &sig, body).is_ok());
let evt = op_bnpl::webhook::parse_affirm_event(secret, &sig, body).unwrap();
assert_eq!(evt.provider, WebhookProvider::Affirm);
assert_eq!(evt.kind, BnplEventKind::CaptureCreated);
assert_eq!(evt.resource_ref.as_deref(), Some("CHG_9"));
}
#[test]
fn klarna_event_round_trip_with_signature() {
let secret = b"klarna-shared";
let body =
br#"{"event_id":"evt","event_type":"refund.created","order_id":"ORD_9"}"#;
let sig = sign_b64(secret, body);
assert!(verify_klarna_webhook(secret, &sig, body).is_ok());
let evt = op_bnpl::webhook::parse_klarna_event(secret, &sig, body).unwrap();
assert_eq!(evt.kind, BnplEventKind::RefundCreated);
assert_eq!(evt.resource_ref.as_deref(), Some("ORD_9"));
}
#[test]
fn afterpay_event_round_trip_with_signature() {
let secret = b"afterpay-shared";
let body =
br#"{"id":"evt_a","eventType":"AUTHORISED","paymentId":"PAY_9"}"#;
let sig = sign_hex(secret, body);
assert!(verify_afterpay_webhook(secret, &sig, body).is_ok());
let evt = op_bnpl::webhook::parse_afterpay_event(secret, &sig, body).unwrap();
assert_eq!(evt.kind, BnplEventKind::AuthorizationCreated);
assert_eq!(evt.resource_ref.as_deref(), Some("PAY_9"));
}
#[test]
fn tampered_affirm_body_rejected() {
let secret = b"k";
let body = br#"{"id":"e","type":"capture.created"}"#;
let sig = sign_b64(secret, body);
let tampered = br#"{"id":"e","type":"refund.created"}"#;
let r = verify_affirm_webhook(secret, &sig, tampered);
assert!(matches!(r, Err(op_bnpl::Error::InvalidSignature)));
}
#[test]
fn tampered_afterpay_hex_signature_rejected() {
let secret = b"k";
let body = br#"{"id":"e","eventType":"CAPTURED"}"#;
let mut sig = sign_hex(secret, body);
// Flip one hex digit to a different one
let first = sig.chars().next().unwrap();
let new = if first == 'a' { 'b' } else { 'a' };
sig.replace_range(..1, &new.to_string());
let r = verify_afterpay_webhook(secret, &sig, body);
assert!(matches!(r, Err(op_bnpl::Error::InvalidSignature)));
}
#[test]
fn klarna_malformed_header_rejected() {
let r = verify_klarna_webhook(b"k", "!!! not base64 !!!", b"{}");
assert!(matches!(
r,
Err(op_bnpl::Error::MalformedSignatureHeader(_))
));
}