| [9e9e6f9] | 1 | // Paid posts (klonkt-demo-aki) slice 3: passkey registration + verification for
|
|---|
| 2 | // pseudonymous entitlements. Uses @simplewebauthn/server. Cookie-less: the
|
|---|
| 3 | // challenge is not kept in a session but travels inside a signed blob
|
|---|
| 4 | // (CryptoBox.signBlob) that the client returns, so there is nothing to store
|
|---|
| 5 | // between the two requests. An entitlement is {passkey, site, cents, expiry}
|
|---|
| 6 | // with NO patron identity.
|
|---|
| 7 | import crypto from 'crypto';
|
|---|
| 8 | import db from '../config/database.js';
|
|---|
| [d43230f] | 9 |
|
|---|
| 10 | // Lazy so a not-yet-installed dependency can never crash app boot; only the
|
|---|
| 11 | // paid passkey flow fails until `npm ci` has run.
|
|---|
| 12 | let _lib = null;
|
|---|
| 13 | async function lib() { if (!_lib) _lib = await import('@simplewebauthn/server'); return _lib; }
|
|---|
| [9e9e6f9] | 14 |
|
|---|
| 15 | const DEFAULT_TTL_DAYS = 32; // aligns with Patreon's monthly cycle; re-link after
|
|---|
| 16 |
|
|---|
| 17 | // rpID is the site host; origin is the full base URL.
|
|---|
| 18 | export function rpFor(base) {
|
|---|
| 19 | let host = ''; try { host = new URL(base).host.split(':')[0]; } catch { /* keep empty */ }
|
|---|
| 20 | return { rpID: host, origin: String(base).replace(/\/+$/, '') };
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | // Registration options for a fresh, discoverable (usernameless) passkey. The
|
|---|
| 24 | // user handle is random: the credential is pseudonymous by design.
|
|---|
| 25 | export async function registrationOptions(base, siteSlug) {
|
|---|
| 26 | const { rpID } = rpFor(base);
|
|---|
| [d43230f] | 27 | const { generateRegistrationOptions } = await lib();
|
|---|
| [9e9e6f9] | 28 | return generateRegistrationOptions({
|
|---|
| 29 | rpName: `Supporter of ${siteSlug}`,
|
|---|
| 30 | rpID,
|
|---|
| 31 | userName: 'supporter',
|
|---|
| 32 | userDisplayName: 'Supporter',
|
|---|
| 33 | userID: crypto.randomBytes(16),
|
|---|
| 34 | attestationType: 'none',
|
|---|
| 35 | authenticatorSelection: { residentKey: 'required', userVerification: 'preferred' },
|
|---|
| 36 | timeout: 120000,
|
|---|
| 37 | });
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // Verify a registration response against the challenge (read from the signed
|
|---|
| 41 | // blob by the caller). Returns the credential to store, or null.
|
|---|
| 42 | export async function verifyRegistration(base, response, expectedChallenge) {
|
|---|
| 43 | const { rpID, origin } = rpFor(base);
|
|---|
| 44 | let v;
|
|---|
| 45 | try {
|
|---|
| [d43230f] | 46 | const { verifyRegistrationResponse } = await lib();
|
|---|
| [9e9e6f9] | 47 | v = await verifyRegistrationResponse({
|
|---|
| 48 | response,
|
|---|
| 49 | expectedChallenge,
|
|---|
| 50 | expectedOrigin: origin,
|
|---|
| 51 | expectedRPID: rpID,
|
|---|
| 52 | requireUserVerification: false,
|
|---|
| 53 | });
|
|---|
| 54 | } catch { return null; }
|
|---|
| 55 | if (!v || !v.verified || !v.registrationInfo) return null;
|
|---|
| 56 | const cred = v.registrationInfo.credential;
|
|---|
| 57 | return {
|
|---|
| 58 | credentialId: cred.id, // base64url string
|
|---|
| 59 | publicKey: Buffer.from(cred.publicKey).toString('base64url'), // COSE key bytes
|
|---|
| 60 | counter: cred.counter || 0,
|
|---|
| 61 | transports: response.response && response.response.transports ? JSON.stringify(response.response.transports) : null,
|
|---|
| 62 | };
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| [6cbd014] | 65 | // Authentication (assertion) options for the unlock. Discoverable credentials,
|
|---|
| 66 | // so allowCredentials is empty and the browser offers the site's passkeys.
|
|---|
| 67 | export async function authenticationOptions(base) {
|
|---|
| 68 | const { rpID } = rpFor(base);
|
|---|
| 69 | const { generateAuthenticationOptions } = await lib();
|
|---|
| 70 | return generateAuthenticationOptions({ rpID, userVerification: 'preferred', allowCredentials: [] });
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // Verify an assertion against a stored entitlement row. Returns { newCounter }
|
|---|
| 74 | // or null. Challenge is read from the signed blob by the caller.
|
|---|
| 75 | export async function verifyAssertion(base, response, expectedChallenge, ent) {
|
|---|
| 76 | const { rpID, origin } = rpFor(base);
|
|---|
| 77 | let v;
|
|---|
| 78 | try {
|
|---|
| 79 | const { verifyAuthenticationResponse } = await lib();
|
|---|
| 80 | v = await verifyAuthenticationResponse({
|
|---|
| 81 | response,
|
|---|
| 82 | expectedChallenge,
|
|---|
| 83 | expectedOrigin: origin,
|
|---|
| 84 | expectedRPID: rpID,
|
|---|
| 85 | requireUserVerification: false,
|
|---|
| 86 | credential: {
|
|---|
| 87 | id: ent.credential_id,
|
|---|
| 88 | publicKey: Buffer.from(ent.public_key, 'base64url'),
|
|---|
| 89 | counter: ent.counter || 0,
|
|---|
| 90 | transports: ent.transports ? JSON.parse(ent.transports) : undefined,
|
|---|
| 91 | },
|
|---|
| 92 | });
|
|---|
| 93 | } catch { return null; }
|
|---|
| 94 | if (!v || !v.verified) return null;
|
|---|
| 95 | return { newCounter: v.authenticationInfo.newCounter };
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | // Bump the signature counter after a successful assertion (clone detection).
|
|---|
| 99 | export function bumpCounter(credentialId, newCounter) {
|
|---|
| 100 | db.prepare('UPDATE paid_entitlements SET counter = ? WHERE credential_id = ?').run(newCounter || 0, credentialId);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| [9e9e6f9] | 103 | // Store (or refresh) a pseudonymous entitlement for this passkey.
|
|---|
| 104 | export function storeEntitlement({ credentialId, siteId, publicKey, counter, transports, minCents, ttlDays = DEFAULT_TTL_DAYS }) {
|
|---|
| 105 | const expiresAt = Math.floor(Date.now() / 1000) + ttlDays * 86400;
|
|---|
| 106 | db.prepare(`INSERT INTO paid_entitlements
|
|---|
| 107 | (credential_id, site_id, public_key, counter, transports, min_cents, expires_at, created_at)
|
|---|
| 108 | VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)
|
|---|
| 109 | ON CONFLICT(credential_id) DO UPDATE SET
|
|---|
| 110 | public_key=excluded.public_key, counter=excluded.counter, transports=excluded.transports,
|
|---|
| 111 | min_cents=excluded.min_cents, expires_at=excluded.expires_at`)
|
|---|
| 112 | .run(credentialId, siteId, publicKey, counter || 0, transports || null, Math.max(0, minCents || 0), expiresAt);
|
|---|
| 113 | return expiresAt;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | // A valid, unexpired entitlement for this passkey on this site, else null.
|
|---|
| 117 | export function getEntitlement(credentialId, siteId) {
|
|---|
| 118 | const row = db.prepare('SELECT * FROM paid_entitlements WHERE credential_id = ? AND site_id = ?').get(credentialId, siteId);
|
|---|
| 119 | if (!row) return null;
|
|---|
| 120 | if ((row.expires_at || 0) < Math.floor(Date.now() / 1000)) return null;
|
|---|
| 121 | return row;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | export function deleteEntitlement(credentialId) {
|
|---|
| 125 | return db.prepare('DELETE FROM paid_entitlements WHERE credential_id = ?').run(credentialId).changes > 0;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | // Prune expired entitlements (Scheduler, slice 5).
|
|---|
| 129 | export function pruneExpired() {
|
|---|
| 130 | return db.prepare('DELETE FROM paid_entitlements WHERE expires_at < ?').run(Math.floor(Date.now() / 1000)).changes;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | export default {
|
|---|
| 134 | rpFor, registrationOptions, verifyRegistration, storeEntitlement,
|
|---|
| 135 | getEntitlement, deleteEntitlement, pruneExpired,
|
|---|
| [6cbd014] | 136 | authenticationOptions, verifyAssertion, bumpCounter,
|
|---|
| [9e9e6f9] | 137 | };
|
|---|