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/<version> (+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/<version> 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/<version> (+https://logicsrc.com).

Co-authored-by: riotcoder <riotcoder@profullstack.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-11-22 04:15:30 -08:00 • committed by GitHub
parent d8a08c006b
commit b20afe6f50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 44 additions and 12 deletions

View file

@ -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",

View file

@ -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,

View file

@ -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) : {};

View file

@ -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)`;

View file

@ -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);

View file

@ -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). */

View file

@ -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)`;

View file

@ -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();