mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 17:24:48 +00:00
vault: opt-in paid email recovery for the personal vault ($5 lifetime) (#298)
* 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>
* vault email recovery: state that the mailbox alone is enough
The account password resets by email, so mailbox control = sign-in + one-time
code + the enrollment code U. Say so in security.md and stop the one-time-code
email from implying the vault stays safe if someone else reads the mailbox.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* vault email recovery: leave .env.example as on master
Touching it made the credential gate re-scan the existing SESSION_SECRET
placeholder (false positive). VAULT_ESCROW_KEY / _VERSION / VAULT_RECOVERY_PRICE_USD
are documented in docs/opencreds/security.md and crypto.md; prod values live in
vault app-logicsrc-com--prod.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4e4fedd35e
commit
41ad9f0987
20 changed files with 2363 additions and 7 deletions
|
|
@ -58,6 +58,13 @@ import { createVault, resetRecoveryKey, rewrapUserKey, unlockVault, unlockWithRe
|
|||
import { formatDiagnostics, hasErrors, validateDocument } from "./validate.js";
|
||||
import { DivergedVaultError, describeSync, pullVault, pushVault, readSyncState, syncVault, type SyncRemote } from "./sync.js";
|
||||
import type { VaultStore } from "./store.js";
|
||||
import {
|
||||
enrollEmailRecovery,
|
||||
runEmailRecovery,
|
||||
type EmailRecoveryClient,
|
||||
type EmailRecoveryError,
|
||||
type EmailRecoveryResult,
|
||||
} from "./email-recovery.js";
|
||||
|
||||
/** Exit codes are part of the contract; see docs/opencreds/cli.md. */
|
||||
export const EXIT = {
|
||||
|
|
@ -302,6 +309,12 @@ export interface RegisterOptions {
|
|||
* `opencreds` binary passes nothing, so it never syncs.
|
||||
*/
|
||||
remote?: (store: VaultStore) => SyncRemote | undefined;
|
||||
/**
|
||||
* The account's email-recovery endpoints, if any (opt-in, paid). Called per
|
||||
* command; return undefined when there is no logged-in account. Without it,
|
||||
* `recover --email` and `recovery …` explain that they need `logicsrc login`.
|
||||
*/
|
||||
emailRecovery?: (store: VaultStore) => EmailRecoveryClient | undefined;
|
||||
}
|
||||
|
||||
// Commands that never touch a vault on disk, plus `sync`, which syncs itself.
|
||||
|
|
@ -474,12 +487,33 @@ export function registerCredsCommands(parent: Command, options: RegisterOptions
|
|||
});
|
||||
});
|
||||
|
||||
const recoveryClient = (store: VaultStore): EmailRecoveryClient => {
|
||||
const client = options.emailRecovery?.(store);
|
||||
if (!client) {
|
||||
fail(
|
||||
options.emailRecovery
|
||||
? "Email recovery needs your account: run `logicsrc login` first. It protects the default vault only."
|
||||
: "Email recovery is an account feature: use `logicsrc vault` after `logicsrc login`.",
|
||||
EXIT.USAGE,
|
||||
);
|
||||
}
|
||||
return client;
|
||||
};
|
||||
|
||||
parent
|
||||
.command("recover")
|
||||
.description("unlock with the recovery key and set a new master password")
|
||||
.action(async function (this: Command) {
|
||||
.description("unlock with the recovery key (or --email) and set a new master password")
|
||||
.option("--email", "recover through your account email instead of the offline recovery key (needs `recovery enable` first)")
|
||||
.addHelpText("after", examples(`
|
||||
$CLI recover type the offline recovery key printed by \`init\`
|
||||
$CLI recover --email the code from your enrollment email + a fresh emailed one-time code (works locked)`))
|
||||
.action(async function (this: Command, opts: { email?: boolean }) {
|
||||
await run(async () => {
|
||||
const store = storeFor(this);
|
||||
if (opts.email) {
|
||||
await recoverByEmail(store, recoveryClient(store));
|
||||
return;
|
||||
}
|
||||
const meta = requireMeta(store);
|
||||
const recoveryKey = await promptSecret("Recovery key: ");
|
||||
let userKey: Uint8Array;
|
||||
|
|
@ -499,6 +533,154 @@ export function registerCredsCommands(parent: Command, options: RegisterOptions
|
|||
});
|
||||
});
|
||||
|
||||
// ------------------------------------------------------ email recovery ---
|
||||
|
||||
async function recoverByEmail(store: VaultStore, client: EmailRecoveryClient): Promise<void> {
|
||||
requireMeta(store);
|
||||
process.stderr.write(
|
||||
'Email recovery needs the recovery code from the "Your LogicSRC vault email-recovery code" email.\n',
|
||||
);
|
||||
let result: EmailRecoveryResult;
|
||||
try {
|
||||
result = await runEmailRecovery({
|
||||
store,
|
||||
client,
|
||||
say: (line) => process.stderr.write(`${line}\n`),
|
||||
prompts: {
|
||||
recoveryCode: () => promptSecret("Recovery code from the enrollment email: "),
|
||||
oneTimeCode: () => promptSecret("One-time code from the email just sent: "),
|
||||
newPassword: () => promptNewSecret("New master password: ", "Repeat: "),
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
const code = (err as EmailRecoveryError).code;
|
||||
fail((err as Error).message, code === "recovery_code_mismatch" || code === "escrow_stale" ? EXIT.CRYPTO : EXIT.USAGE);
|
||||
}
|
||||
unlockedKey = result.userKey;
|
||||
store.appendAudit(
|
||||
auditEvent({ action: "vault.recovery_reset", namespace: result.meta.namespace, reason: "email-recovery" }),
|
||||
);
|
||||
process.stdout.write(`Master password changed through email recovery. Not one item was re-encrypted.\n\n`);
|
||||
process.stdout.write(` New recovery key ${result.recoveryKey}\n\n`);
|
||||
process.stdout.write("Write it down now; it will not be shown again. The old recovery key no longer works.\n");
|
||||
if (result.reenrolled) {
|
||||
process.stdout.write(
|
||||
`Email recovery is re-armed: a NEW recovery code was emailed to ${result.reenrolledEmail}. ` +
|
||||
"The code you just used no longer works.\n",
|
||||
);
|
||||
return;
|
||||
}
|
||||
process.stderr.write(
|
||||
"\n!!! WARNING: your vault is recovered, but email recovery could NOT be re-armed" +
|
||||
(result.reenrollError ? ` (${result.reenrollError})` : "") +
|
||||
".\n!!! The recovery code you just used may still work on the server. Run `" + cli + " recovery enable`\n" +
|
||||
"!!! now to replace it, or `" + cli + " recovery disable` to turn email recovery off.\n",
|
||||
);
|
||||
}
|
||||
|
||||
const recovery = parent
|
||||
.command("recovery")
|
||||
.description("email recovery (opt-in, $5 lifetime): get back in through your account email if the password is lost")
|
||||
.addHelpText("after", examples(`
|
||||
$CLI recovery status enrolled? since when? which email? purchased?
|
||||
$CLI recovery buy print the CoinPay checkout link ($5, lifetime)
|
||||
$CLI recovery enable enroll this vault (unlocks it); emails you a recovery code to keep
|
||||
$CLI recovery disable turn it off; the server deletes its escrow share and copy
|
||||
$CLI recover --email the day you need it: works with the vault locked
|
||||
|
||||
What the server keeps: a copy of your vault key encrypted under a random key E,
|
||||
and one half of E (S), encrypted under the server's own key. The other half is
|
||||
the recovery code in the enrollment email; the server never stores it.
|
||||
Recovering needs that email plus a fresh one-time code. Without enrollment the
|
||||
vault stays zero-knowledge. See docs/opencreds/security.md for the trade-off.`));
|
||||
|
||||
recovery
|
||||
.command("status")
|
||||
.description("is email recovery enrolled and purchased (works locked)")
|
||||
.option("--json", "machine-readable output")
|
||||
.action(async function (this: Command, opts: { json?: boolean }) {
|
||||
await run(async () => {
|
||||
const client = recoveryClient(storeFor(this));
|
||||
const status = await client.status();
|
||||
if (opts.json) {
|
||||
process.stdout.write(`${JSON.stringify(status, null, 2)}\n`);
|
||||
return;
|
||||
}
|
||||
process.stdout.write(` Account ${client.label}\n`);
|
||||
process.stdout.write(
|
||||
` Purchased ${status.purchased ? "yes (lifetime)" : `no ($${status.priceUsd} lifetime: \`${cli} recovery buy\`)`}\n`,
|
||||
);
|
||||
process.stdout.write(
|
||||
` Enrolled ${status.enrolled ? `yes, since ${status.enrolledAt ?? "?"}` : `no${status.purchased ? ` (\`${cli} recovery enable\`)` : ""}`}\n`,
|
||||
);
|
||||
if (status.email) process.stdout.write(` Email ${status.email}\n`);
|
||||
});
|
||||
});
|
||||
|
||||
recovery
|
||||
.command("buy")
|
||||
.description("print the CoinPay checkout link for email recovery ($5 lifetime)")
|
||||
.action(async function (this: Command) {
|
||||
await run(async () => {
|
||||
const client = recoveryClient(storeFor(this));
|
||||
const status = await client.status();
|
||||
if (status.purchased) {
|
||||
process.stdout.write(`Email recovery is already purchased. Turn it on with \`${cli} recovery enable\`.\n`);
|
||||
return;
|
||||
}
|
||||
const { url, priceUsd } = await client.checkout();
|
||||
process.stdout.write(`Pay $${priceUsd} (lifetime) here, then run \`${cli} recovery enable\`:\n\n ${url}\n`);
|
||||
});
|
||||
});
|
||||
|
||||
recovery
|
||||
.command("enable")
|
||||
.description("enroll this vault in email recovery; running it again re-keys and emails a new code")
|
||||
.action(async function (this: Command) {
|
||||
await run(async () => {
|
||||
const store = storeFor(this);
|
||||
const client = recoveryClient(store);
|
||||
const meta = requireMeta(store);
|
||||
const status = await client.status();
|
||||
if (!status.purchased) {
|
||||
const checkout = await client.checkout().catch(() => undefined);
|
||||
fail(
|
||||
`Email recovery is a $${status.priceUsd} lifetime add-on and is not purchased yet.` +
|
||||
(checkout ? ` Pay here, then run this again:\n\n ${checkout.url}\n` : ` Run \`${cli} recovery buy\`.`),
|
||||
EXIT.USAGE,
|
||||
);
|
||||
}
|
||||
const userKey = await unlock(store);
|
||||
let result: { enrolled: boolean; email: string };
|
||||
try {
|
||||
result = await enrollEmailRecovery(client, meta, userKey);
|
||||
} catch (err) {
|
||||
const e = err as EmailRecoveryError;
|
||||
fail(`${e.message}${e.checkoutUrl ? `\n\n ${e.checkoutUrl}\n` : ""}`, EXIT.USAGE);
|
||||
}
|
||||
process.stdout.write(
|
||||
`Email recovery is on. Your recovery code was emailed to ${result.email}.\n` +
|
||||
"Keep that email: it is half of the way back in, and it is not stored anywhere else.\n" +
|
||||
(status.enrolled ? "The previous recovery code no longer works.\n" : ""),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
recovery
|
||||
.command("disable")
|
||||
.description("turn email recovery off; the server deletes its escrow share and copy")
|
||||
.action(async function (this: Command) {
|
||||
await run(async () => {
|
||||
const client = recoveryClient(storeFor(this));
|
||||
const { disabled } = await client.disable();
|
||||
process.stdout.write(
|
||||
disabled
|
||||
? "Email recovery is off. The server deleted its share, so the emailed recovery code is useless now.\n"
|
||||
: "Email recovery was not enabled.\n",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------- items ---
|
||||
|
||||
const add = parent
|
||||
|
|
@ -1109,7 +1291,9 @@ function registerSync(
|
|||
: `vault sync: skipped (${message}); changes stay on this machine and go up next time.`,
|
||||
);
|
||||
};
|
||||
const autosync = (command: Command) => !NO_AUTOSYNC.has(command.name()) && process.env.OPENCREDS_SYNC !== "off";
|
||||
// `recovery …` talks to the account, not the vault files: nothing to sync around it.
|
||||
const autosync = (command: Command) =>
|
||||
!NO_AUTOSYNC.has(command.name()) && command.parent?.name() !== "recovery" && process.env.OPENCREDS_SYNC !== "off";
|
||||
|
||||
parent.hook("preAction", async (_self, command) => {
|
||||
if (!autosync(command)) return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue