mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-04 02:29:06 +00:00
feat(passwd): logicsrc passwd --email resets the vault and the keyring with one password (#302)
Email recovery reset the vault only. The SSH keyring kept its old passphrase,
so someone who had forgotten everything recovered the vault and was then
refused by `keyring restore` ("Wrong keyring passphrase").
- `logicsrc passwd --email`: opens the vault with the two emailed codes, then
puts the vault and the keyring under one new password. New vault recovery
key, email recovery re-armed, same end state as `vault recover --email`.
- Every `passwd` run stores the keyring's user key as an item in the vault
(found by a stable id, verified against a keyring entry before use), so the
vault's way back in is the keyring's too and nobody is asked a second secret.
- A keyring nothing opens is set aside (kept as keyring.bak-NNN) and started
fresh; keys whose files are still under ~/.ssh with the same fingerprint are
put back, the rest are named.
- The re-arm email after a recovery had the first-time wording ("run
logicsrc vault recover --email"), which read as an instruction to recover
again: three recoveries in five minutes on one account. It now says the
recovery is complete and there is nothing to do. The CLI says so too.
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
f10c94ca38
commit
ef965b0aaa
6 changed files with 517 additions and 20 deletions
|
|
@ -1,10 +1,26 @@
|
|||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { createVault, createVaultStore, unlockVault, type VaultStore } from "@logicsrc/opencreds";
|
||||
import {
|
||||
EmailRecoveryError,
|
||||
createItem,
|
||||
createVault,
|
||||
createVaultStore,
|
||||
encryptItem,
|
||||
enrollEmailRecovery,
|
||||
formatRecoveryCode,
|
||||
fromBase64,
|
||||
rewrapUserKey,
|
||||
unlockVault,
|
||||
unlockWithRecoveryKey,
|
||||
type EmailRecoveryClient,
|
||||
type EscrowEnrollment,
|
||||
type EscrowMaterial,
|
||||
type VaultStore
|
||||
} from "@logicsrc/opencreds";
|
||||
import { KeyringStore, createKeyring, openEntry, sealEntry, unlockKeyring } from "./keyring.js";
|
||||
import { changeAllPasswords } from "./passwd.js";
|
||||
import { changeAllPasswords, keyringEscrowId, readKeyringEscrow } from "./passwd.js";
|
||||
|
||||
const KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nAAAAfake\n-----END OPENSSH PRIVATE KEY-----\n";
|
||||
const PUB = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIy86FQN77URkkunH3W6C28+mxsyOF5E6NrigzwAZGlz test";
|
||||
|
|
@ -91,4 +107,162 @@ describe("logicsrc passwd", () => {
|
|||
expect(keyring.readSync().cloud).toBe(true);
|
||||
await expect(unlockKeyring(keyring.readMeta()!, { passphrase: "new" })).resolves.toBeInstanceOf(Uint8Array);
|
||||
}, 60_000);
|
||||
|
||||
it("keeps the keyring's key in the vault, so the next run needs the vault password only", async () => {
|
||||
await seed("vault pw", "keyring pw");
|
||||
const first = await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("vault pw", "keyring pw"), askNew: async () => "one", say: () => {} }
|
||||
);
|
||||
expect(first.keyringEscrowed).toBe(true);
|
||||
expect(vault.readEnvelope(keyringEscrowId(keyring.readMeta()!))).toBeTruthy();
|
||||
|
||||
// The keyring drifts to a passphrase nobody remembers.
|
||||
const kk = await unlockKeyring(keyring.readMeta()!, { passphrase: "one" });
|
||||
keyring.writeMeta(await rewrapUserKey(keyring.readMeta()!, kk, "forgotten"));
|
||||
|
||||
const second = await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("one"), askNew: async () => "two", say: () => {} }
|
||||
);
|
||||
expect(second.keyring).toBe("changed");
|
||||
const opened = await openEntry(await unlockKeyring(keyring.readMeta()!, { passphrase: "two" }), keyring.find("id_ed25519")!);
|
||||
expect(opened.privateKey).toBe(KEY);
|
||||
}, 60_000);
|
||||
|
||||
it("ignores a stored key that does not open the keyring", async () => {
|
||||
await seed("vault pw", "keyring pw");
|
||||
const vaultKey = await unlockVault(vault.readMeta()!, "vault pw");
|
||||
const stranger = await createKeyring("x");
|
||||
const item = createItem("note", { name: "n", notes: JSON.stringify({ kind: "logicsrc-keyring-key", userKey: Buffer.from(stranger.userKey).toString("base64") }) });
|
||||
item.id = keyringEscrowId(keyring.readMeta()!);
|
||||
vault.writeEnvelope(await encryptItem(vaultKey, item, vault.readMeta()!.namespace));
|
||||
expect(await readKeyringEscrow(vault, vault.readMeta()!, vaultKey, keyring, keyring.readMeta()!)).toBeUndefined();
|
||||
}, 60_000);
|
||||
});
|
||||
|
||||
/** A fake account server for email recovery: keeps S and the blob, "mails" U and one-time codes. */
|
||||
function fakeRecovery() {
|
||||
let enrollment: EscrowMaterial | null = null;
|
||||
let otp: { code: string; used: boolean } | null = null;
|
||||
const inbox: string[] = [];
|
||||
const client: EmailRecoveryClient = {
|
||||
label: "fake.test",
|
||||
async status() {
|
||||
return { enrolled: Boolean(enrollment), enrolledAt: null, email: "m***@example.com", purchased: true, priceUsd: 5 };
|
||||
},
|
||||
async checkout() {
|
||||
return { url: "https://pay.test/1", priceUsd: 5 };
|
||||
},
|
||||
async enroll(p: EscrowEnrollment) {
|
||||
enrollment = { escrowBlob: p.escrowBlob, escrowIv: p.escrowIv, serverShare: p.serverShare, vaultCreatedAt: p.vaultCreatedAt };
|
||||
inbox.push(await formatRecoveryCode(fromBase64(p.userShare)));
|
||||
otp = null;
|
||||
return { enrolled: true, email: "m***@example.com" };
|
||||
},
|
||||
async start() {
|
||||
if (!enrollment) throw new EmailRecoveryError("not enrolled", 404, "not_enrolled");
|
||||
otp = { code: "12345678", used: false };
|
||||
return { sent: true, email: "m***@example.com" };
|
||||
},
|
||||
async claim(code: string) {
|
||||
if (!otp || otp.used || otp.code !== code) throw new EmailRecoveryError("Wrong one-time code", 401, "wrong_code");
|
||||
otp.used = true;
|
||||
return enrollment!;
|
||||
},
|
||||
async disable() {
|
||||
enrollment = null;
|
||||
return { disabled: true };
|
||||
}
|
||||
};
|
||||
return { client, inbox };
|
||||
}
|
||||
|
||||
describe("logicsrc passwd --email", () => {
|
||||
async function enrolled(vaultPassword: string, keyringPassword: string) {
|
||||
const seeded = await seed(vaultPassword, keyringPassword);
|
||||
const server = fakeRecovery();
|
||||
await enrollEmailRecovery(server.client, vault.readMeta()!, await unlockVault(vault.readMeta()!, vaultPassword));
|
||||
return { ...seeded, server };
|
||||
}
|
||||
|
||||
it("with every password lost: the emailed codes open the vault, the keyring is reset and refilled from ~/.ssh", async () => {
|
||||
const { server, vaultRecovery } = await enrolled("lost vault pw", "lost keyring pw");
|
||||
const sshDir = join(dir, "ssh");
|
||||
mkdirSync(sshDir);
|
||||
writeFileSync(join(sshDir, "id_ed25519"), KEY);
|
||||
writeFileSync(join(sshDir, "id_ed25519.pub"), `${PUB}\n`);
|
||||
const said: string[] = [];
|
||||
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true, email: true },
|
||||
{
|
||||
vault,
|
||||
keyring,
|
||||
sshDir,
|
||||
recovery: () => server.client,
|
||||
// recovery code, one-time code, then Enter twice: keyring passphrase and recovery key both lost
|
||||
ask: answers(server.inbox[0]!, "12345678", "", ""),
|
||||
askNew: async () => "one password",
|
||||
say: (l) => said.push(l)
|
||||
}
|
||||
);
|
||||
|
||||
expect(r).toMatchObject({ vault: "changed", keyring: "created", keyringEscrowed: true, emailRearmed: true, keysReimported: ["id_ed25519"], keysLost: [] });
|
||||
await expect(unlockVault(vault.readMeta()!, "one password")).resolves.toBeInstanceOf(Uint8Array);
|
||||
await expect(unlockVault(vault.readMeta()!, "lost vault pw")).rejects.toThrow();
|
||||
// A new offline recovery key; the old one is dead.
|
||||
await expect(unlockWithRecoveryKey(vault.readMeta()!, r.vaultRecoveryKey!)).resolves.toBeInstanceOf(Uint8Array);
|
||||
await expect(unlockWithRecoveryKey(vault.readMeta()!, vaultRecovery)).rejects.toThrow();
|
||||
// The key file from disk is back in the keyring, under the one password.
|
||||
const kk = await unlockKeyring(keyring.readMeta()!, { passphrase: "one password" });
|
||||
expect((await openEntry(kk, keyring.find("id_ed25519")!)).privateKey).toBe(KEY);
|
||||
// Email recovery was re-armed with a new code.
|
||||
expect(server.inbox).toHaveLength(2);
|
||||
expect(said.join("\n")).toMatch(/old keyring is kept at/);
|
||||
}, 60_000);
|
||||
|
||||
it("the second time asks for nothing but the two emailed codes: the keyring opens from the vault", async () => {
|
||||
const { server } = await enrolled("vault pw", "keyring pw");
|
||||
await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("vault pw", "keyring pw"), askNew: async () => "first", say: () => {} }
|
||||
);
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true, email: true },
|
||||
{ vault, keyring, recovery: () => server.client, ask: answers(server.inbox[0]!, "12345678"), askNew: async () => "second", say: () => {} }
|
||||
);
|
||||
expect(r).toMatchObject({ vault: "changed", keyring: "changed", keysReimported: [] });
|
||||
const kk = await unlockKeyring(keyring.readMeta()!, { passphrase: "second" });
|
||||
expect((await openEntry(kk, keyring.find("id_ed25519")!)).privateKey).toBe(KEY);
|
||||
}, 60_000);
|
||||
|
||||
it("reports a key that is not on this machine's disk as lost from the new keyring", async () => {
|
||||
const { server } = await enrolled("a", "b");
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true, email: true },
|
||||
{ vault, keyring, sshDir: join(dir, "nowhere"), recovery: () => server.client, ask: answers(server.inbox[0]!, "12345678", "", ""), askNew: async () => "n", say: () => {} }
|
||||
);
|
||||
expect(r.keysLost).toEqual(["id_ed25519"]);
|
||||
expect(r.keysReimported).toEqual([]);
|
||||
}, 60_000);
|
||||
|
||||
it("changes nothing on a wrong one-time code", async () => {
|
||||
const { server } = await enrolled("a", "b");
|
||||
const before = JSON.stringify(vault.readMeta());
|
||||
await expect(
|
||||
changeAllPasswords(
|
||||
{ noSync: true, email: true },
|
||||
{ vault, keyring, recovery: () => server.client, ask: answers(server.inbox[0]!, "00000000"), askNew: async () => "n", say: () => {} }
|
||||
)
|
||||
).rejects.toThrow(/one-time code/);
|
||||
expect(JSON.stringify(vault.readMeta())).toBe(before);
|
||||
}, 60_000);
|
||||
|
||||
it("needs a signed-in account", async () => {
|
||||
await seed("a", "b");
|
||||
await expect(
|
||||
changeAllPasswords({ noSync: true, email: true }, { vault, keyring, recovery: () => undefined, ask: answers(), askNew: async () => "n", say: () => {} })
|
||||
).rejects.toThrow(/logicsrc login/);
|
||||
}, 60_000);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue