Skip to content

Comments

Add L402 payment middleware for transparent 402 handling#17

Open
joelklabo wants to merge 1 commit intosulusolutions:mainfrom
joelklabo:feature/l402-middleware
Open

Add L402 payment middleware for transparent 402 handling#17
joelklabo wants to merge 1 commit intosulusolutions:mainfrom
joelklabo:feature/l402-middleware

Conversation

@joelklabo
Copy link

Summary

Implements HTTP middleware for transparent L402 payment challenge handling, as requested in #10.

Two middleware entry points:

  • L402() middleware — wraps any http.Handler, intercepts requests with X-Target-URL header, and proxies them through the L402 client which handles 402 challenges automatically
  • ReverseProxy() — pre-configured httputil.ReverseProxy that routes all requests through L402 payment handling with a custom RoundTripper

Features

  • Automatic HTTP 402 detection and Lightning invoice payment via the configured wallet.Wallet
  • Token caching via configurable tokenstore.Store for reuse across requests to paid endpoints
  • Hop-by-hop header filtering for correct HTTP proxy behavior
  • Modular design: middleware logic is fully separate from client.Client internals
  • Works with all existing wallet implementations (LND, Alby, Mock)

Tests

11 test cases covering:

  • Pass-through (non-proxy requests go to next handler)
  • Proxy without 402 (direct upstream response)
  • Proxy with 402 + successful payment + retry
  • Payment failure handling (502 Bad Gateway)
  • Invalid target URL validation
  • Header forwarding and X-Target-URL stripping
  • Token storage and reuse after payment
  • Reverse proxy non-402 and 402 flows
  • Hop-by-hop header filtering

All existing tests continue to pass. Tested with -race flag.

Bounty

This PR addresses the 44,000 sat bounty from #10.

Test plan

  • go test ./middleware/ — 11 tests passing
  • go test -race ./middleware/ — no races
  • go test ./... — all existing tests still pass

🤖 Generated with Claude Code

Implements HTTP middleware that wraps http.Handler to transparently handle
L402 payment challenges. Two entry points:

1. L402() middleware: wraps any handler, intercepts requests with
   X-Target-URL header, proxies them through the L402 client
2. ReverseProxy(): pre-configured reverse proxy using httputil that
   routes all requests through L402 payment handling

Features:
- Automatic 402 detection and Lightning invoice payment via wallet
- Token caching via configurable Store for paid endpoint reuse
- Hop-by-hop header filtering for proper HTTP proxy behavior
- Modular design: middleware is separate from client internals

Tests: 11 test cases covering pass-through, proxy with/without 402,
payment failure, invalid URLs, header forwarding, token reuse,
reverse proxy modes, and hop-by-hop header filtering.

Closes sulusolutions#10

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@joelklabo joelklabo marked this pull request as ready for review February 8, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant