Add L402 payment middleware for transparent 402 handling#17
Open
joelklabo wants to merge 1 commit intosulusolutions:mainfrom
Open
Add L402 payment middleware for transparent 402 handling#17joelklabo wants to merge 1 commit intosulusolutions:mainfrom
joelklabo wants to merge 1 commit intosulusolutions:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements HTTP middleware for transparent L402 payment challenge handling, as requested in #10.
Two middleware entry points:
L402()middleware — wraps anyhttp.Handler, intercepts requests withX-Target-URLheader, and proxies them through the L402 client which handles 402 challenges automaticallyReverseProxy()— pre-configuredhttputil.ReverseProxythat routes all requests through L402 payment handling with a customRoundTripperFeatures
wallet.Wallettokenstore.Storefor reuse across requests to paid endpointsclient.ClientinternalsTests
11 test cases covering:
All existing tests continue to pass. Tested with
-raceflag.Bounty
This PR addresses the 44,000 sat bounty from #10.
Test plan
go test ./middleware/— 11 tests passinggo test -race ./middleware/— no racesgo test ./...— all existing tests still pass🤖 Generated with Claude Code