Skip to content

Commit 803b389

Browse files
committed
add configurable token TTL flag
Add --token-ttl flag to allow customizing token expiration duration while keeping the default at 24 hours. The flag accepts standard Go duration formats (e.g. 24h, 1h30m, 48h). Changes: - Add --token-ttl CLI flag with 24h default - Move token TTL from constant to Config struct - Update tests to use configurable TTL - Update README documentation Signed-off-by: Gustavo Chain <me@qustavo.cc>
1 parent 1c6b776 commit 803b389

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ l402proxy \
4040
| `--lnd-cert` | `~/.lnd/tls.cert` | Path to LND TLS cert |
4141
| `--service-name` | `l402proxy` | Label used in invoice memos |
4242
| `--secret-key` | auto-generated | Hex-encoded 32-byte HMAC secret (tokens won't survive restarts if omitted) |
43+
| `--token-ttl` | `24h` | Token expiration duration (e.g. `24h`, `1h30m`, `48h`) |
4344

4445
## L402 flow (curl example)
4546

@@ -72,7 +73,7 @@ HTTP/1.1 200 OK
7273

7374
## Token format
7475

75-
`base64url(json_payload).hex(hmac_sha256)` — stateless, no database required. Token TTL is 24 hours.
76+
`base64url(json_payload).hex(hmac_sha256)` — stateless, no database required. Token TTL is 24 hours by default (configurable via `--token-ttl`).
7677

7778
## Comparison with Aperture
7879

cmd/l402proxy/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/url"
1111
"os"
1212
"strings"
13+
"time"
1314

1415
"github.com/qustavo/l402proxy/pkg/lightning"
1516
"github.com/qustavo/l402proxy/pkg/proxy"
@@ -60,6 +61,11 @@ func main() {
6061
Name: "secret-key",
6162
Usage: "Hex-encoded 32-byte HMAC secret (auto-generated if omitted — tokens won't survive restarts)",
6263
},
64+
&cli.DurationFlag{
65+
Name: "token-ttl",
66+
Usage: "Token expiration duration (e.g. 24h, 1h30m, 48h)",
67+
Value: 24 * time.Hour,
68+
},
6369
},
6470
Action: run,
6571
}
@@ -106,6 +112,7 @@ func run(c *cli.Context) error {
106112
Upstream: upstream,
107113
PriceMsat: priceMsat,
108114
ServiceName: c.String("service-name"),
115+
TokenTTL: c.Duration("token-ttl"),
109116
}, backend, secret, log)
110117

111118
addr := c.String("listen")

pkg/proxy/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ import (
1818
"github.com/qustavo/l402proxy/pkg/macaroon"
1919
)
2020

21-
const tokenTTL = 24 * time.Hour
22-
2321
// Config holds the proxy handler settings.
2422
type Config struct {
2523
Upstream *url.URL
2624
PriceMsat int64
2725
ServiceName string
26+
TokenTTL time.Duration
2827
}
2928

3029
// Handler is an http.Handler that enforces L402 payment before proxying.
@@ -105,7 +104,7 @@ func (h *Handler) challenge(w http.ResponseWriter, r *http.Request) error {
105104
return fmt.Errorf("creating invoice: %w", err)
106105
}
107106

108-
token, err := h.tokens.Issue(inv.PaymentHash, tokenTTL)
107+
token, err := h.tokens.Issue(inv.PaymentHash, h.cfg.TokenTTL)
109108
if err != nil {
110109
return fmt.Errorf("issuing token: %w", err)
111110
}

pkg/proxy/handler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func newTestHandler(t *testing.T, upstream *httptest.Server, settled bool) (*Han
5959
Upstream: upstreamURL,
6060
PriceMsat: 10_000,
6161
ServiceName: "test",
62+
TokenTTL: 24 * time.Hour,
6263
}, backend, secret, slog.New(slog.NewTextHandler(io_discard{}, nil)))
6364
return h, tokens
6465
}

0 commit comments

Comments
 (0)