mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 01:14:47 +00:00
vault: opt-in paid email recovery for the personal vault ($5 lifetime)
A second way back into a logicsrc personal vault when the master password and
the offline recovery key are both gone, without the server holding anything
that alone (even with the DB and server env) opens the vault.
Client (packages/opencreds): random escrow key E encrypts the user key K
(AES-256-GCM, AAD "<ns>:vault:email-escrow:v1|<createdAt>"); E is split into
S (random, to the server) and U = E XOR S (emailed once as a checksummed
Crockford recovery code, never stored). `recover --email` checks the code's
check group first, asks the server for a one-time code, claims S, recombines
E, proves K against an item, sets a new password, issues a new offline
recovery key and re-enrolls with fresh shares (warns loudly if that fails).
New `recovery status|buy|enable|disable` commands; the CLI client lives in
packages/cli/src/vault-sync.ts.
Server (apps/pwa): /api/opencreds/recovery/{checkout,enroll,start,claim} +
status/disable. S is sealed under VAULT_ESCROW_KEY (versioned for rotation,
AAD binds the user). Enrollment is pending until the email with U is sent,
else rolled back. One-time codes: 8 digits, HMAC at rest, 15 min, 5 tries,
single use, 3 starts/hour; claims are audited and announced by email.
Purchase via CoinPay checkout (purpose vault_recovery); /api/coinpay/webhook
now verifies once and dispatches by purpose, auto_rotation unchanged.
Settings card with status and a Buy button. Migration 014 in both dialects.
Docs: security.md and crypto.md "Email recovery (opt-in, paid)" sections with
the honest trade-off; cli.md command reference.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4e4fedd35e
commit
8528634e09
21 changed files with 2368 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import type { Command } from "commander";
|
||||
import { registerCredsCommands } from "@logicsrc/opencreds/commands";
|
||||
import { accountRemoteFor } from "./vault-sync.js";
|
||||
import { accountRecoveryFor, accountRemoteFor } from "./vault-sync.js";
|
||||
|
||||
/**
|
||||
* `logicsrc vault …`
|
||||
|
|
@ -53,6 +53,14 @@ gets it with: logicsrc login, then logicsrc vault sync.
|
|||
logicsrc vault sync --status what is synced, what is waiting
|
||||
LOGICSRC_VAULT_SYNC=off turn it off (a --home vault never syncs)
|
||||
|
||||
Lost the master password? The offline recovery key from init works:
|
||||
logicsrc vault recover
|
||||
Email recovery (opt-in, $5 lifetime) is a second way back in, through your account email:
|
||||
logicsrc vault recovery buy CoinPay checkout link
|
||||
logicsrc vault recovery enable emails you a recovery code to keep
|
||||
logicsrc vault recover --email that code + a fresh emailed one-time code
|
||||
logicsrc vault recovery status | disable
|
||||
|
||||
Help for any command: logicsrc vault <command> --help (or: logicsrc vault help <command>)
|
||||
`;
|
||||
|
||||
|
|
@ -68,5 +76,5 @@ export function registerOpenCredsCommands(program: Command): void {
|
|||
.addHelpText("after", VAULT_GUIDE);
|
||||
|
||||
// Logged in: the vault syncs to the account around every command.
|
||||
registerCredsCommands(vault, { remote: accountRemoteFor });
|
||||
registerCredsCommands(vault, { remote: accountRemoteFor, emailRecovery: accountRecoveryFor });
|
||||
}
|
||||
|
|
|
|||
60
packages/cli/src/vault-recovery-client.test.ts
Normal file
60
packages/cli/src/vault-recovery-client.test.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { EmailRecoveryError } from "@logicsrc/opencreds";
|
||||
import { createRecoveryClient } from "./vault-sync.js";
|
||||
|
||||
type Call = { url: string; method: string; auth: string | null; body: unknown };
|
||||
|
||||
function fakeFetch(answers: Record<string, { status: number; body: unknown }>) {
|
||||
const calls: Call[] = [];
|
||||
const impl = (async (url: string, init: RequestInit = {}) => {
|
||||
const method = init.method ?? "GET";
|
||||
calls.push({
|
||||
url,
|
||||
method,
|
||||
auth: new Headers(init.headers).get("authorization"),
|
||||
body: init.body ? JSON.parse(String(init.body)) : undefined,
|
||||
});
|
||||
const answer = answers[`${method} ${new URL(url).pathname}`] ?? { status: 404, body: { error: "nope" } };
|
||||
return new Response(JSON.stringify(answer.body), { status: answer.status });
|
||||
}) as unknown as typeof fetch;
|
||||
return { impl, calls };
|
||||
}
|
||||
|
||||
describe("the email-recovery client", () => {
|
||||
it("talks to /api/opencreds/recovery with the person's key", async () => {
|
||||
const { impl, calls } = fakeFetch({
|
||||
"GET /api/opencreds/recovery": { status: 200, body: { enrolled: true, enrolledAt: "2026-11-30T00:00:00.000Z", email: "m***@x.test", purchased: true, priceUsd: 5 } },
|
||||
"POST /api/opencreds/recovery/claim": { status: 200, body: { escrowBlob: "b", escrowIv: "i", serverShare: "s", vaultCreatedAt: "c" } },
|
||||
"DELETE /api/opencreds/recovery": { status: 200, body: { disabled: true } },
|
||||
});
|
||||
const client = createRecoveryClient("https://app.example/", "lsk_test", "me@x.test", impl);
|
||||
expect(client.label).toBe("app.example (me@x.test)");
|
||||
expect((await client.status()).enrolled).toBe(true);
|
||||
expect(await client.claim("12345678")).toEqual({ escrowBlob: "b", escrowIv: "i", serverShare: "s", vaultCreatedAt: "c" });
|
||||
expect(await client.disable()).toEqual({ disabled: true });
|
||||
expect(calls.map((c) => `${c.method} ${c.url}`)).toEqual([
|
||||
"GET https://app.example/api/opencreds/recovery",
|
||||
"POST https://app.example/api/opencreds/recovery/claim",
|
||||
"DELETE https://app.example/api/opencreds/recovery",
|
||||
]);
|
||||
expect(calls.every((c) => c.auth === "Bearer lsk_test")).toBe(true);
|
||||
expect(calls[1]!.body).toEqual({ code: "12345678" });
|
||||
});
|
||||
|
||||
it("surfaces a 402 with the checkout link", async () => {
|
||||
const { impl } = fakeFetch({
|
||||
"POST /api/opencreds/recovery/enroll": {
|
||||
status: 402,
|
||||
body: { error: "Email recovery is a $5 lifetime add-on and is not purchased yet.", code: "payment_required", checkoutUrl: "https://coinpay.test/pay/cs_1" },
|
||||
},
|
||||
});
|
||||
const client = createRecoveryClient("https://app.example", "lsk_test", undefined, impl);
|
||||
const err = await client
|
||||
.enroll({ escrowBlob: "", escrowIv: "", serverShare: "", userShare: "", vaultCreatedAt: "" })
|
||||
.catch((e: unknown) => e);
|
||||
expect(err).toBeInstanceOf(EmailRecoveryError);
|
||||
expect((err as EmailRecoveryError).status).toBe(402);
|
||||
expect((err as EmailRecoveryError).code).toBe("payment_required");
|
||||
expect((err as EmailRecoveryError).checkoutUrl).toBe("https://coinpay.test/pay/cs_1");
|
||||
});
|
||||
});
|
||||
|
|
@ -1,6 +1,18 @@
|
|||
import { join, resolve } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import type { Envelope, RemoteItem, RemoteVault, SyncRemote, VaultMeta, VaultStore } from "@logicsrc/opencreds";
|
||||
import { EmailRecoveryError } from "@logicsrc/opencreds";
|
||||
import type {
|
||||
EmailRecoveryClient,
|
||||
EmailRecoveryStatus,
|
||||
Envelope,
|
||||
EscrowEnrollment,
|
||||
EscrowMaterial,
|
||||
RemoteItem,
|
||||
RemoteVault,
|
||||
SyncRemote,
|
||||
VaultMeta,
|
||||
VaultStore,
|
||||
} from "@logicsrc/opencreds";
|
||||
import { readIdentity, resolveApiUrl } from "@logicsrc/plugin-credential-sharing";
|
||||
|
||||
/**
|
||||
|
|
@ -92,3 +104,80 @@ export function createAccountRemote(apiUrl: string, token: string, email?: strin
|
|||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Email recovery for the personal vault (`logicsrc vault recovery …`,
|
||||
* `logicsrc vault recover --email`): apps/pwa /api/opencreds/recovery.
|
||||
*
|
||||
* Same scope rule as sync: it protects the account's vault, so a --home vault
|
||||
* gets it only with LOGICSRC_VAULT_SYNC=on. Turning sync off does not turn
|
||||
* recovery off; the two are independent.
|
||||
*/
|
||||
export function accountRecoveryFor(store: VaultStore): EmailRecoveryClient | undefined {
|
||||
const setting = (process.env.LOGICSRC_VAULT_SYNC || "").toLowerCase();
|
||||
if (setting !== "on" && resolve(store.baseDir) !== resolve(defaultVaultDir())) return undefined;
|
||||
const identity = readIdentity();
|
||||
if (!identity?.apiToken) return undefined;
|
||||
return createRecoveryClient(resolveApiUrl(identity), identity.apiToken, identity.email);
|
||||
}
|
||||
|
||||
export function createRecoveryClient(
|
||||
apiUrl: string,
|
||||
token: string,
|
||||
email?: string,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
): EmailRecoveryClient {
|
||||
const base = apiUrl.replace(/\/+$/, "");
|
||||
const host = (() => {
|
||||
try {
|
||||
return new URL(base).host;
|
||||
} catch {
|
||||
return base;
|
||||
}
|
||||
})();
|
||||
|
||||
async function call(method: string, path: string, body?: unknown): Promise<Record<string, unknown>> {
|
||||
const res = await fetchImpl(`${base}/api/opencreds/recovery${path}`, {
|
||||
method,
|
||||
headers: { authorization: `Bearer ${token}`, accept: "application/json", ...(body ? { "content-type": "application/json" } : {}) },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
});
|
||||
const json = (await res.json().catch(() => ({}))) as Record<string, unknown>;
|
||||
if (!res.ok) {
|
||||
throw new EmailRecoveryError(
|
||||
String(json.error || `${host} answered HTTP ${res.status}`),
|
||||
res.status,
|
||||
typeof json.code === "string" ? json.code : undefined,
|
||||
typeof json.checkoutUrl === "string" ? json.checkoutUrl : undefined,
|
||||
);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
return {
|
||||
label: email ? `${host} (${email})` : host,
|
||||
async status() {
|
||||
return (await call("GET", "")) as unknown as EmailRecoveryStatus;
|
||||
},
|
||||
async checkout() {
|
||||
const body = await call("POST", "/checkout");
|
||||
return { url: String(body.url), priceUsd: Number(body.priceUsd) };
|
||||
},
|
||||
async enroll(payload: EscrowEnrollment) {
|
||||
const body = await call("POST", "/enroll", payload);
|
||||
return { enrolled: Boolean(body.enrolled), email: String(body.email ?? "") };
|
||||
},
|
||||
async start() {
|
||||
const body = await call("POST", "/start");
|
||||
return { sent: Boolean(body.sent), email: typeof body.email === "string" ? body.email : undefined };
|
||||
},
|
||||
async claim(code: string) {
|
||||
return (await call("POST", "/claim", { code })) as unknown as EscrowMaterial;
|
||||
},
|
||||
async disable() {
|
||||
const body = await call("DELETE", "");
|
||||
return { disabled: Boolean(body.disabled) };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue