Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"format:check": "prettier --check .",
"lint": "npm run typecheck && npm run format:check && eslint .",
"lint:fix": "prettier --write . && eslint . --fix",
"test": "tsx --test src/auth.test.ts",
"test:client": "tsx test-client.ts",
"typecheck": "tsc --noEmit"
},
Expand Down
41 changes: 41 additions & 0 deletions src/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { Hono } from "hono";
import { createProtectedRoute } from "./auth";
import type { AppContext, Env } from "./env";

test("protected routes return an MPP payment challenge", async () => {
const app = new Hono<AppContext>();
app.use(
"/paid",
createProtectedRoute({
amount: "0.01",
description: "Test payment",
pattern: "/paid",
}),
);
app.get("/paid", (c) => c.text("paid"));

const env: Env = {
JWT_SECRET: "test-jwt-secret-000000000000000000000000000000000000",
MPP_SECRET_KEY: "test-mpp-secret-000000000000000000000000000000000000",
PAYMENT_CURRENCY: "0x20c0000000000000000000000000000000000000",
PAY_TO: "0x000000000000000000000000000000000000dEaD",
PROTECTED_PATTERNS: [
{
amount: "0.01",
description: "Access to premium content for 1 hour",
pattern: "/premium/*",
},
],
TEMPO_TESTNET: true,
};

const response = await app.request("http://localhost/paid", undefined, env);

assert.equal(response.status, 402);
assert.match(
response.headers.get("www-authenticate") ?? "",
/intent="charge"/,
);
});
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function createProtectedRoute(config: ProtectedRouteConfig) {
return async (c: Context<AppContext>, next: Next) => {
const mppx = Mppx.create({
methods: [
tempo({
tempo.charge({
currency: c.env.PAYMENT_CURRENCY,
recipient: c.env.PAY_TO,
testnet: c.env.TEMPO_TESTNET,
Expand Down