mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-29 15:51:52 +00:00
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:
parent
d8a08c006b
commit
b20afe6f50
10 changed files with 44 additions and 12 deletions
|
|
@ -17,7 +17,7 @@
|
||||||
"@profullstack/autoblog": "^0.5.0",
|
"@profullstack/autoblog": "^0.5.0",
|
||||||
"@profullstack/footer": "latest",
|
"@profullstack/footer": "latest",
|
||||||
"@profullstack/stack": "^0.5.0",
|
"@profullstack/stack": "^0.5.0",
|
||||||
"@profullstack/x402-gateway": "^0.18.1",
|
"@profullstack/x402-gateway": "^0.18.2",
|
||||||
"@supabase/supabase-js": "^2.118.0",
|
"@supabase/supabase-js": "^2.118.0",
|
||||||
"marked": "^18.1.1",
|
"marked": "^18.1.1",
|
||||||
"next": "16.4.3",
|
"next": "16.4.3",
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
import { json } from "@/lib/http";
|
import { json } from "@/lib/http";
|
||||||
|
import { COINPAY_UA } from "@/lib/coinpay-ua";
|
||||||
import { COINPAY_SESSION_COOKIE, parseJson, verifySession } from "@/lib/coinpay";
|
import { COINPAY_SESSION_COOKIE, parseJson, verifySession } from "@/lib/coinpay";
|
||||||
import { HIRE_US_PRODUCT } from "@/lib/coinpay-webhook";
|
import { HIRE_US_PRODUCT } from "@/lib/coinpay-webhook";
|
||||||
import { hireUsCheckoutStore } from "@/lib/hire-us-payments";
|
import { hireUsCheckoutStore } from "@/lib/hire-us-payments";
|
||||||
|
|
@ -75,7 +76,8 @@ export async function POST(request: NextRequest) {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
authorization: `Bearer ${apiKey}`,
|
authorization: `Bearer ${apiKey}`,
|
||||||
"content-type": "application/json"
|
"content-type": "application/json",
|
||||||
|
"user-agent": COINPAY_UA
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
amount: amountUsdDue,
|
amount: amountUsdDue,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
import { json } from "@/lib/http";
|
import { json } from "@/lib/http";
|
||||||
|
import { COINPAY_UA } from "@/lib/coinpay-ua";
|
||||||
import {
|
import {
|
||||||
COINPAY_OAUTH_STATE_COOKIE,
|
COINPAY_OAUTH_STATE_COOKIE,
|
||||||
COINPAY_SESSION_COOKIE,
|
COINPAY_SESSION_COOKIE,
|
||||||
|
|
@ -49,7 +50,7 @@ export async function GET(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const tokenResponse = await fetch(new URL("/api/oauth/token", config.issuer), {
|
const tokenResponse = await fetch(new URL("/api/oauth/token", config.issuer), {
|
||||||
method: "POST",
|
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({
|
body: new URLSearchParams({
|
||||||
grant_type: "authorization_code",
|
grant_type: "authorization_code",
|
||||||
code,
|
code,
|
||||||
|
|
@ -70,7 +71,7 @@ export async function GET(request: NextRequest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const userResponse = await fetch(new URL("/api/oauth/userinfo", config.issuer), {
|
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 userText = await userResponse.text();
|
||||||
const userInfo = userResponse.ok ? parseJson(userText) : {};
|
const userInfo = userResponse.ok ? parseJson(userText) : {};
|
||||||
|
|
|
||||||
4
apps/logicsrc-web/src/lib/coinpay-ua.ts
Normal file
4
apps/logicsrc-web/src/lib/coinpay-ua.ts
Normal 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)`;
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
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
|
// Server-side CoinPay + session helpers, ported from the legacy server.js so the
|
||||||
// Next API routes keep identical behavior (eligibility-driven payment rails,
|
// 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);
|
const url = new URL("/api/payments/merchant-eligibility", apiUrl);
|
||||||
url.searchParams.set("merchant_id", merchantId);
|
url.searchParams.set("merchant_id", merchantId);
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
headers: { authorization: `Bearer ${apiKey ?? ""}` }
|
headers: { authorization: `Bearer ${apiKey ?? ""}`, "user-agent": COINPAY_UA }
|
||||||
});
|
});
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
const payload = parseJson(text);
|
const payload = parseJson(text);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
// rotation_auto_paid_until NULL with the switch on means it was flipped by hand
|
// 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.
|
// (a comp): it never expires and a payment does not cap it.
|
||||||
import { CoinPay, readWebhook } from "@profullstack/coinpay-onboard";
|
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 { createAutoRotation, sqlStore, resendMailer, DAY_MS } from "@profullstack/key-rotation";
|
||||||
import { get, all, run } from "../db.mjs";
|
import { get, all, run } from "../db.mjs";
|
||||||
import { config } from "../config.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. */
|
/** A CoinPay client on app.logicsrc.com's business key. */
|
||||||
export function coinpayClient(env = process.env) {
|
export function coinpayClient(env = process.env) {
|
||||||
const token = env.COINPAY_API_KEY;
|
const token = env.COINPAY_API_KEY;
|
||||||
if (!token) throw new Error("COINPAY_API_KEY is not configured");
|
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). */
|
/** The module bound to this app; `origin` only changes the links (tests). */
|
||||||
|
|
|
||||||
10
apps/pwa/src/lib/coinpay-ua.mjs
Normal file
10
apps/pwa/src/lib/coinpay-ua.mjs
Normal 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)`;
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import { config } from "../config.mjs";
|
import { config } from "../config.mjs";
|
||||||
|
import { COINPAY_UA } from "../lib/coinpay-ua.mjs";
|
||||||
import { token } from "../lib/crypto.mjs";
|
import { token } from "../lib/crypto.mjs";
|
||||||
import { createSession, setCeremony, getCeremony, clearCeremony, takeNext } from "../lib/session.mjs";
|
import { createSession, setCeremony, getCeremony, clearCeremony, takeNext } from "../lib/session.mjs";
|
||||||
import { userByCoinpay, createUserForCoinpay } from "../lib/users.mjs";
|
import { userByCoinpay, createUserForCoinpay } from "../lib/users.mjs";
|
||||||
|
|
@ -39,7 +40,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const tokenRes = await fetch(config.coinpay.oauth.tokenUrl, {
|
const tokenRes = await fetch(config.coinpay.oauth.tokenUrl, {
|
||||||
method: "POST",
|
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({
|
body: new URLSearchParams({
|
||||||
grant_type: "authorization_code",
|
grant_type: "authorization_code",
|
||||||
code: String(req.query.code),
|
code: String(req.query.code),
|
||||||
|
|
@ -52,7 +53,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => {
|
||||||
const tok = await tokenRes.json();
|
const tok = await tokenRes.json();
|
||||||
|
|
||||||
const infoRes = await fetch(config.coinpay.oauth.userinfoUrl, {
|
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}`);
|
if (!infoRes.ok) throw new Error(`userinfo ${infoRes.status}`);
|
||||||
const info = await infoRes.json();
|
const info = await infoRes.json();
|
||||||
|
|
|
||||||
4
bun.lock
4
bun.lock
|
|
@ -59,7 +59,7 @@
|
||||||
"@profullstack/autoblog": "^0.5.0",
|
"@profullstack/autoblog": "^0.5.0",
|
||||||
"@profullstack/footer": "latest",
|
"@profullstack/footer": "latest",
|
||||||
"@profullstack/stack": "^0.5.0",
|
"@profullstack/stack": "^0.5.0",
|
||||||
"@profullstack/x402-gateway": "^0.18.1",
|
"@profullstack/x402-gateway": "^0.18.2",
|
||||||
"@supabase/supabase-js": "^2.118.0",
|
"@supabase/supabase-js": "^2.118.0",
|
||||||
"marked": "^18.1.1",
|
"marked": "^18.1.1",
|
||||||
"next": "16.4.3",
|
"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/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=="],
|
"@rolldown/binding-android-arm-eabi": ["@rolldown/binding-android-arm-eabi@1.2.15", "", { "os": "android", "cpu": "arm" }, "sha512-aLcgK9gIJ3YRvrrtfSBbRa27F5WrpiTaPulnFpsHPxpUHgFlSgnU4854MHwoRGEBhfRJDWCS2WQ7lIjskE9Olw=="],
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { createHash, createHmac, generateKeyPairSync, pbkdf2Sync, randomBytes } from "node:crypto";
|
import { createHash, createHmac, generateKeyPairSync, pbkdf2Sync, randomBytes } from "node:crypto";
|
||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
import { generateRandomSecret } from "./rotation-catalog.js";
|
import { generateRandomSecret } from "./rotation-catalog.js";
|
||||||
|
import { localVersion } from "./update.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Built-in rotators for `method: "provider"`. Each one is two-phase:
|
* 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);
|
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 }> {
|
async function call(ctx: RotatorContext, url: string, init: RequestInit & { token: string }): Promise<{ status: number; body: any }> {
|
||||||
const { token, headers, ...rest } = init;
|
const { token, headers, ...rest } = init;
|
||||||
const res = await ctx.fetch(url, {
|
const res = await ctx.fetch(url, {
|
||||||
...rest,
|
...rest,
|
||||||
headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json", ...(headers as Record<string, string> | undefined) }
|
headers: { "user-agent": ROTATOR_USER_AGENT, authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json", ...(headers as Record<string, string> | undefined) }
|
||||||
});
|
});
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
let body: any = null;
|
let body: any = null;
|
||||||
|
|
@ -249,7 +253,7 @@ async function coinpayLogin(ctx: RotatorContext): Promise<CoinpaySession> {
|
||||||
try {
|
try {
|
||||||
res = await ctx.fetch(`${coinpayBase(ctx)}/api/auth/login`, {
|
res = await ctx.fetch(`${coinpayBase(ctx)}/api/auth/login`, {
|
||||||
method: "POST",
|
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 })
|
body: JSON.stringify({ email: ctx.env.COINPAY_ROTATE_EMAIL, password: ctx.env.COINPAY_ROTATE_PASSWORD })
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue