mirror of
https://github.com/bitbaum/accountkit.git
synced 2026-12-04 05:17:14 +01:00
A free, Bitcoin-native way in: the person's own Lightning wallet signs a one-time challenge shown as a QR code (or opened through a lightning: link on the same device). No key, no third party, nothing paid. The signed key is a sign-in method (providerId "lightning"), never an email. - /lightning/start mints a challenge (five minutes, used once) and binds it to the asking browser with a signed cookie; /lightning/callback is the wallet's LUD-04 call and checks the DER secp256k1 signature of k1; /sign-in/lightning (polled) signs in or creates the person; /lightning/link adds a wallet to the signed-in person, never one that already signs in to someone else. - A second factor applies; minting and the wallet's call spend the credential bucket. - SignInForm and SignUpForm offer "Sign in with a Lightning wallet", AccountPanel "Add Lightning wallet"; words in all six languages. - Off until ACCOUNTKIT_LIGHTNING=1. Tests: LUD-01's own LNURL both ways, a signature made by OpenSSL, and the whole flow on a real database (PGlite) with a wallet that signs as LUD-04 says: sign-in and sign-in again, a forged signature, an onlooker without the browser's cookie, a second factor, linking and a taken wallet, and nothing mounted without the opt-in. Browser: QR, wallet link, 44px, no sideways scroll at 320 and 390px. Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 Co-authored-by: Claude <noreply@anthropic.com>
97 lines
3.8 KiB
JavaScript
97 lines
3.8 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
import { clientIp, slidingWindow, toHeaders } from "limitkit";
|
|
/**
|
|
* Rate limits for the account endpoints, the same in every product.
|
|
*
|
|
* KEYED ON THE PROXY'S HOP, NEVER THE FIRST X-Forwarded-For ENTRY. limitkit's
|
|
* `clientIp()` reads the LAST hop, the one Caddy appended, which the caller
|
|
* cannot choose. `trustedProxies: 1` is Caddy on the box.
|
|
*/
|
|
export const authLimits = {
|
|
/** Guessing a password, a code or a passkey assertion. */
|
|
credential: slidingWindow({ limit: 10, windowMs: 15 * 60_000 }),
|
|
/** Anything that sends an email: protects inboxes and the shared quota. */
|
|
mail: slidingWindow({ limit: 5, windowMs: 60 * 60_000 }),
|
|
/** The same, per recipient, so rotating IPs cannot flood one inbox. */
|
|
mailTo: slidingWindow({ limit: 3, windowMs: 60 * 60_000 }),
|
|
/** Everything else under /api/auth (session reads, sign-out, listing). */
|
|
auth: slidingWindow({ limit: 300, windowMs: 10 * 60_000 }),
|
|
};
|
|
const MAIL = new Set([
|
|
"/sign-up/email",
|
|
"/sign-in/magic-link",
|
|
"/send-verification-email",
|
|
"/request-password-reset",
|
|
"/forget-password",
|
|
"/change-email",
|
|
]);
|
|
const CREDENTIAL_PREFIXES = [
|
|
"/sign-in/email",
|
|
"/sign-in/domain",
|
|
"/provision/claim",
|
|
"/sign-in/social",
|
|
"/reset-password",
|
|
"/change-password",
|
|
"/verify-password",
|
|
"/two-factor/",
|
|
"/passkey/verify-authentication",
|
|
"/passkey/generate-authenticate-options",
|
|
"/magic-link/verify",
|
|
// Lightning login: minting a challenge and a wallet's signature. Claiming
|
|
// (/sign-in/lightning, /lightning/link) is polled by the page and bound to
|
|
// the browser's own cookie, so it spends the ordinary bucket.
|
|
"/lightning/start",
|
|
"/lightning/callback",
|
|
"/delete-user",
|
|
];
|
|
/** Which bucket a better-auth path (after /api/auth) spends from. */
|
|
export function authBucket(path) {
|
|
if (MAIL.has(path))
|
|
return "mail";
|
|
if (CREDENTIAL_PREFIXES.some((p) => path === p || path.startsWith(p)))
|
|
return "credential";
|
|
return "auth";
|
|
}
|
|
export function callerKey(headers, scope) {
|
|
return `${scope}:${clientIp(headers, { trustedProxies: 1 })}`;
|
|
}
|
|
/** A recipient as a bucket key: hashed, so the limiter never holds an address. */
|
|
export function recipientKey(email) {
|
|
return `mail-to:${createHash("sha256").update(email.trim().toLowerCase()).digest("hex")}`;
|
|
}
|
|
export function tooMany(result) {
|
|
return Response.json({ error: "rate_limited", retryAfterSeconds: result.retryAfterSeconds }, {
|
|
status: 429,
|
|
headers: { ...toHeaders(result), "Cache-Control": "no-store" },
|
|
});
|
|
}
|
|
/** The address a mail-sending request would write to, read from a CLONE of its body. */
|
|
async function recipientOf(request) {
|
|
try {
|
|
const body = await request.clone().json();
|
|
const fields = body ?? {};
|
|
const value = typeof fields.email === "string" ? fields.email : fields.newEmail;
|
|
return typeof value === "string" && value.includes("@") ? value : null;
|
|
}
|
|
catch {
|
|
return null;
|
|
}
|
|
}
|
|
/** Returns a Response to send INSTEAD of handling the request, or null to go on. */
|
|
export async function rateLimitAuthRequest(request) {
|
|
const path = new URL(request.url).pathname.replace(/^\/api\/auth/, "") || "/";
|
|
const bucket = request.method === "GET" ? "auth" : authBucket(path);
|
|
const byCaller = authLimits[bucket].check(callerKey(request.headers, `auth-${bucket}`));
|
|
if (!byCaller.allowed)
|
|
return tooMany(byCaller);
|
|
if (bucket === "mail") {
|
|
const email = await recipientOf(request);
|
|
if (email) {
|
|
const byRecipient = authLimits.mailTo.check(recipientKey(email));
|
|
if (!byRecipient.allowed)
|
|
return tooMany(byRecipient);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
//# sourceMappingURL=limits.js.map
|