From b20afe6f5033c7abca1f8ae5778da44e48121a18 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Sun, 22 Nov 2026 04:15:30 -0800 Subject: [PATCH] Name logicsrc apps in the User-Agent of CoinPay calls (#293) logicsrc.com (Next), app.logicsrc.com (pwa) and the rotation CLI now send a descriptive User-Agent on every server-side CoinPay request, so CoinPay's logs and throttle can tell them apart from the other apps on dev2: - logicsrc.com: logicsrc.com/ (+https://logicsrc.com) on merchant eligibility, checkout sessions, OAuth token and userinfo; x402-gateway ^0.18.2 names the site on crawl-gateway calls. - app.logicsrc.com: app.logicsrc.com/ on the CoinPay OAuth calls and, through coinpay-onboard 0.3.1's fetch hook, on the auto-rotation client. - logicsrc CLI rotators: logicsrc-cli/ (+https://logicsrc.com). Co-authored-by: riotcoder Co-authored-by: Claude Opus 5.5 --- apps/logicsrc-web/package.json | 2 +- .../src/app/api/hire-us/coinpay-checkout/route.ts | 4 +++- .../src/app/api/oauth/coinpay/callback/route.ts | 5 +++-- apps/logicsrc-web/src/lib/coinpay-ua.ts | 4 ++++ apps/logicsrc-web/src/lib/coinpay.ts | 3 ++- apps/pwa/src/lib/auto-rotation.mjs | 11 ++++++++++- apps/pwa/src/lib/coinpay-ua.mjs | 10 ++++++++++ apps/pwa/src/routes/coinpay.mjs | 5 +++-- bun.lock | 4 ++-- packages/cli/src/rotators.ts | 8 ++++++-- 10 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 apps/logicsrc-web/src/lib/coinpay-ua.ts create mode 100644 apps/pwa/src/lib/coinpay-ua.mjs diff --git a/apps/logicsrc-web/package.json b/apps/logicsrc-web/package.json index 860af9e2..f4b3ec61 100644 --- a/apps/logicsrc-web/package.json +++ b/apps/logicsrc-web/package.json @@ -17,7 +17,7 @@ "@profullstack/autoblog": "^0.5.0", "@profullstack/footer": "latest", "@profullstack/stack": "^0.5.0", - "@profullstack/x402-gateway": "^0.18.1", + "@profullstack/x402-gateway": "^0.18.2", "@supabase/supabase-js": "^2.118.0", "marked": "^18.1.1", "next": "16.4.3", diff --git a/apps/logicsrc-web/src/app/api/hire-us/coinpay-checkout/route.ts b/apps/logicsrc-web/src/app/api/hire-us/coinpay-checkout/route.ts index dd768744..3e069ba8 100644 --- a/apps/logicsrc-web/src/app/api/hire-us/coinpay-checkout/route.ts +++ b/apps/logicsrc-web/src/app/api/hire-us/coinpay-checkout/route.ts @@ -1,5 +1,6 @@ import type { NextRequest } from "next/server"; import { json } from "@/lib/http"; +import { COINPAY_UA } from "@/lib/coinpay-ua"; import { COINPAY_SESSION_COOKIE, parseJson, verifySession } from "@/lib/coinpay"; import { HIRE_US_PRODUCT } from "@/lib/coinpay-webhook"; import { hireUsCheckoutStore } from "@/lib/hire-us-payments"; @@ -75,7 +76,8 @@ export async function POST(request: NextRequest) { method: "POST", headers: { authorization: `Bearer ${apiKey}`, - "content-type": "application/json" + "content-type": "application/json", + "user-agent": COINPAY_UA }, body: JSON.stringify({ amount: amountUsdDue, diff --git a/apps/logicsrc-web/src/app/api/oauth/coinpay/callback/route.ts b/apps/logicsrc-web/src/app/api/oauth/coinpay/callback/route.ts index da49833f..a0c89d91 100644 --- a/apps/logicsrc-web/src/app/api/oauth/coinpay/callback/route.ts +++ b/apps/logicsrc-web/src/app/api/oauth/coinpay/callback/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { json } from "@/lib/http"; +import { COINPAY_UA } from "@/lib/coinpay-ua"; import { COINPAY_OAUTH_STATE_COOKIE, COINPAY_SESSION_COOKIE, @@ -49,7 +50,7 @@ export async function GET(request: NextRequest) { try { const tokenResponse = await fetch(new URL("/api/oauth/token", config.issuer), { method: "POST", - headers: { "content-type": "application/x-www-form-urlencoded" }, + headers: { "content-type": "application/x-www-form-urlencoded", "user-agent": COINPAY_UA }, body: new URLSearchParams({ grant_type: "authorization_code", code, @@ -70,7 +71,7 @@ export async function GET(request: NextRequest) { } const userResponse = await fetch(new URL("/api/oauth/userinfo", config.issuer), { - headers: { authorization: `Bearer ${tokenPayload.access_token}` } + headers: { authorization: `Bearer ${tokenPayload.access_token}`, "user-agent": COINPAY_UA } }); const userText = await userResponse.text(); const userInfo = userResponse.ok ? parseJson(userText) : {}; diff --git a/apps/logicsrc-web/src/lib/coinpay-ua.ts b/apps/logicsrc-web/src/lib/coinpay-ua.ts new file mode 100644 index 00000000..d3e42cba --- /dev/null +++ b/apps/logicsrc-web/src/lib/coinpay-ua.ts @@ -0,0 +1,4 @@ +import pkg from "../../package.json"; + +/** Sent on every server-side call to CoinPay so its logs and throttle can tell this app apart. */ +export const COINPAY_UA = `logicsrc.com/${pkg.version} (+https://logicsrc.com)`; diff --git a/apps/logicsrc-web/src/lib/coinpay.ts b/apps/logicsrc-web/src/lib/coinpay.ts index f71c7cf6..fbaf7b61 100644 --- a/apps/logicsrc-web/src/lib/coinpay.ts +++ b/apps/logicsrc-web/src/lib/coinpay.ts @@ -1,4 +1,5 @@ import { createHmac, timingSafeEqual } from "node:crypto"; +import { COINPAY_UA } from "./coinpay-ua"; // Server-side CoinPay + session helpers, ported from the legacy server.js so the // Next API routes keep identical behavior (eligibility-driven payment rails, @@ -48,7 +49,7 @@ export async function fetchMerchantEligibility( const url = new URL("/api/payments/merchant-eligibility", apiUrl); url.searchParams.set("merchant_id", merchantId); const response = await fetch(url, { - headers: { authorization: `Bearer ${apiKey ?? ""}` } + headers: { authorization: `Bearer ${apiKey ?? ""}`, "user-agent": COINPAY_UA } }); const text = await response.text(); const payload = parseJson(text); diff --git a/apps/pwa/src/lib/auto-rotation.mjs b/apps/pwa/src/lib/auto-rotation.mjs index 5da9a657..bd36c3ae 100644 --- a/apps/pwa/src/lib/auto-rotation.mjs +++ b/apps/pwa/src/lib/auto-rotation.mjs @@ -8,6 +8,7 @@ // rotation_auto_paid_until NULL with the switch on means it was flipped by hand // (a comp): it never expires and a payment does not cap it. import { CoinPay, readWebhook } from "@profullstack/coinpay-onboard"; +import { COINPAY_UA } from "./coinpay-ua.mjs"; import { createAutoRotation, sqlStore, resendMailer, DAY_MS } from "@profullstack/key-rotation"; import { get, all, run } from "../db.mjs"; import { config } from "../config.mjs"; @@ -33,11 +34,19 @@ export const store = sqlStore({ ), }); +/** + * coinpay-onboard 0.3.x has no userAgent option, so name this app on every + * CoinPay call through its fetch hook (CoinPay's logs and throttle key on it). + */ +export function coinpayFetch(url, init = {}) { + return fetch(url, { ...init, headers: { ...(init.headers ?? {}), "user-agent": COINPAY_UA } }); +} + /** A CoinPay client on app.logicsrc.com's business key. */ export function coinpayClient(env = process.env) { const token = env.COINPAY_API_KEY; if (!token) throw new Error("COINPAY_API_KEY is not configured"); - return new CoinPay({ token, base: env.COINPAY_API_URL || undefined }); + return new CoinPay({ token, base: env.COINPAY_API_URL || undefined, fetch: coinpayFetch }); } /** The module bound to this app; `origin` only changes the links (tests). */ diff --git a/apps/pwa/src/lib/coinpay-ua.mjs b/apps/pwa/src/lib/coinpay-ua.mjs new file mode 100644 index 00000000..b610e958 --- /dev/null +++ b/apps/pwa/src/lib/coinpay-ua.mjs @@ -0,0 +1,10 @@ +import { readFileSync } from "node:fs"; + +// Sent on every server-side call to CoinPay so its logs and throttle can tell +// app.logicsrc.com apart from the other apps on the same box. +let version = ""; +try { + version = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version || ""; +} catch {} + +export const COINPAY_UA = `app.logicsrc.com${version ? `/${version}` : ""} (+https://app.logicsrc.com)`; diff --git a/apps/pwa/src/routes/coinpay.mjs b/apps/pwa/src/routes/coinpay.mjs index de76b022..d5d325a0 100644 --- a/apps/pwa/src/routes/coinpay.mjs +++ b/apps/pwa/src/routes/coinpay.mjs @@ -2,6 +2,7 @@ import { Router } from "express"; import crypto from "node:crypto"; import { config } from "../config.mjs"; +import { COINPAY_UA } from "../lib/coinpay-ua.mjs"; import { token } from "../lib/crypto.mjs"; import { createSession, setCeremony, getCeremony, clearCeremony, takeNext } from "../lib/session.mjs"; import { userByCoinpay, createUserForCoinpay } from "../lib/users.mjs"; @@ -39,7 +40,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => { try { const tokenRes = await fetch(config.coinpay.oauth.tokenUrl, { method: "POST", - headers: { "content-type": "application/x-www-form-urlencoded" }, + headers: { "content-type": "application/x-www-form-urlencoded", "user-agent": COINPAY_UA }, body: new URLSearchParams({ grant_type: "authorization_code", code: String(req.query.code), @@ -52,7 +53,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => { const tok = await tokenRes.json(); const infoRes = await fetch(config.coinpay.oauth.userinfoUrl, { - headers: { authorization: `Bearer ${tok.access_token}` }, + headers: { authorization: `Bearer ${tok.access_token}`, "user-agent": COINPAY_UA }, }); if (!infoRes.ok) throw new Error(`userinfo ${infoRes.status}`); const info = await infoRes.json(); diff --git a/bun.lock b/bun.lock index 9c03ed82..a2c910cb 100644 --- a/bun.lock +++ b/bun.lock @@ -59,7 +59,7 @@ "@profullstack/autoblog": "^0.5.0", "@profullstack/footer": "latest", "@profullstack/stack": "^0.5.0", - "@profullstack/x402-gateway": "^0.18.1", + "@profullstack/x402-gateway": "^0.18.2", "@supabase/supabase-js": "^2.118.0", "marked": "^18.1.1", "next": "16.4.3", @@ -787,7 +787,7 @@ "@profullstack/stack": ["@profullstack/stack@0.5.0", "", { "dependencies": { "@profullstack/emailer": "^1.0.1", "@profullstack/form-guard": "^0.1.1", "@profullstack/referrals": "^0.1.0" }, "peerDependencies": { "@supabase/ssr": ">=0.5.0", "next": ">=13.0.0", "react": ">=18.0.0" }, "optionalPeers": ["@supabase/ssr", "next", "react"] }, "sha512-LbXovIhyzLEq+a+y3q8KWC+jN1tJjvSoUiFEhpiEBisQn5U+iutIGHDgg16Lul74kQaFZeFlbp7CMPAVJ8KL6g=="], - "@profullstack/x402-gateway": ["@profullstack/x402-gateway@0.18.1", "", { "dependencies": { "@profullstack/keys": "^0.1.0" } }, "sha512-zc2JGLXMX8eCk7Mp4X91E175enOTNcWPZncKf3LXzoGE+XEhHDJHM1LRZkkY3x2pjLYX3AB11Vx/TP3cobSQ/g=="], + "@profullstack/x402-gateway": ["@profullstack/x402-gateway@0.18.2", "", { "dependencies": { "@profullstack/keys": "^0.1.0" } }, "sha512-PM0S2ADXPKPvPLLlWVbEqMq+EhKxiNxg1wp0jeGf9S5tz2ZGaNIFqun69cO5lJGZgfh03ST4UkJZGr8CmvCWyg=="], "@rolldown/binding-android-arm-eabi": ["@rolldown/binding-android-arm-eabi@1.2.15", "", { "os": "android", "cpu": "arm" }, "sha512-aLcgK9gIJ3YRvrrtfSBbRa27F5WrpiTaPulnFpsHPxpUHgFlSgnU4854MHwoRGEBhfRJDWCS2WQ7lIjskE9Olw=="], diff --git a/packages/cli/src/rotators.ts b/packages/cli/src/rotators.ts index 06fd46e1..ed5091c1 100644 --- a/packages/cli/src/rotators.ts +++ b/packages/cli/src/rotators.ts @@ -1,6 +1,7 @@ import { createHash, createHmac, generateKeyPairSync, pbkdf2Sync, randomBytes } from "node:crypto"; import { spawnSync } from "node:child_process"; import { generateRandomSecret } from "./rotation-catalog.js"; +import { localVersion } from "./update.js"; /** * Built-in rotators for `method: "provider"`. Each one is two-phase: @@ -61,11 +62,14 @@ export class RotatorError extends Error {} const day = (d: Date) => d.toISOString().slice(0, 10); +/** Names this CLI on every provider call, so CoinPay's logs and throttle can tell it apart from the apps. */ +export const ROTATOR_USER_AGENT = `logicsrc-cli/${localVersion()} (+https://logicsrc.com)`; + async function call(ctx: RotatorContext, url: string, init: RequestInit & { token: string }): Promise<{ status: number; body: any }> { const { token, headers, ...rest } = init; const res = await ctx.fetch(url, { ...rest, - headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json", ...(headers as Record | undefined) } + headers: { "user-agent": ROTATOR_USER_AGENT, authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json", ...(headers as Record | undefined) } }); const text = await res.text(); let body: any = null; @@ -249,7 +253,7 @@ async function coinpayLogin(ctx: RotatorContext): Promise { try { res = await ctx.fetch(`${coinpayBase(ctx)}/api/auth/login`, { method: "POST", - headers: { "content-type": "application/json" }, + headers: { "content-type": "application/json", "user-agent": ROTATOR_USER_AGENT }, body: JSON.stringify({ email: ctx.env.COINPAY_ROTATE_EMAIL, password: ctx.env.COINPAY_ROTATE_PASSWORD }) }); } catch (e) {