diff --git a/changes/feat-account-remove.md b/changes/feat-account-remove.md new file mode 100644 index 0000000..7efa741 --- /dev/null +++ b/changes/feat-account-remove.md @@ -0,0 +1,7 @@ +### Added + +- **`pbx account remove` signs a profile out and forgets it on this computer.** It forgets the saved + database or SFTP password and the profile's settings. `--keep-data` leaves its calls in the store, where search still + finds them; `--delete-data` deletes them. It asks first, or takes `--yes`; `--dry-run` lists what it would remove. + Why: WireCat Desktop removes an account with this command, and pbx could only sign out with `session end`. The + password itself stays valid on the server: only the server's owner changes it. diff --git a/contracts/wirecat.json b/contracts/wirecat.json index 9ed606e..038f48b 100644 --- a/contracts/wirecat.json +++ b/contracts/wirecat.json @@ -71,6 +71,19 @@ "local": "read" } }, + { + "path": [ + "account", + "remove" + ], + "summary": "sign this profile out and forget it, keeping or deleting its data", + "effects": { + "remote": "none", + "local": "write" + }, + "permission": "account.remove", + "entity": "account_removal" + }, { "path": [ "meetings", diff --git a/docs/commands.md b/docs/commands.md index 57ecd27..39d23b1 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -107,6 +107,30 @@ pbx session list **Example:** `pbx session list`, `pbx session list --json` +## `pbx account` + +this profile on this computer + +### `pbx account remove` + +sign the profile out, then forget it on this computer: its settings, its saved sign-in and its files; --keep-data leaves its messages in the store, where search still finds them, --delete-data deletes them; asks first, or --yes + +**Changes something on this computer only.** + +```sh +pbx account remove [options] +``` + +| Option | What it does | +|---|---| +| `--keep-data` | leave what the store holds of this account, still searchable. | +| `--delete-data` | delete what the store and the tool's own files hold of this account. | +| `--dry-run` | list what it would remove and keep, and change nothing. | + +**Returns:** account removal — profile, data, dry_run, account, revoked, config_file, removed, kept; every field: pbx commands info account remove + +**Example:** `pbx personal account remove --keep-data --dry-run`, `pbx personal account remove --delete-data --yes` + ## `pbx meetings` stored meetings and their records diff --git a/docs/dev/test-matrix.md b/docs/dev/test-matrix.md index 0d631d4..31a46dd 100644 --- a/docs/dev/test-matrix.md +++ b/docs/dev/test-matrix.md @@ -7,7 +7,7 @@ ran it · ⛔ not tested offline, with the reason and where it is checked instea Measured from the test run (`coverage/argv.jsonl`), not searched for — see [TESTING.md](TESTING.md) for how, and for the states and failures that cut across commands. -**126 ✅ · 48 ⛔ · 0 ❌** — 55 commands, 119 options. +**130 ✅ · 48 ⛔ · 0 ❌** — 56 commands, 122 options. | Command | Option | | Note | |---|---|---|---| @@ -19,6 +19,10 @@ Measured from the test run (`coverage/argv.jsonl`), not searched for — see | `session status` | `--check` | ✅ | | | `session end` | | ✅ | | | `session list` | | ✅ | | +| `account remove` | | ✅ | | +| `account remove` | `--keep-data` | ✅ | | +| `account remove` | `--delete-data` | ✅ | | +| `account remove` | `--dry-run` | ✅ | | | `meetings list` | | ✅ | | | `meetings list` | `--backend` | ⛔ | cli-core 0.660.0 src/store/meetings-read-through.test.ts drives archive, server and both on a shared source | | `meetings list` | `--limit` | ✅ | | diff --git a/src/calls.test.ts b/src/calls.test.ts index 69aeb18..8ba5039 100644 --- a/src/calls.test.ts +++ b/src/calls.test.ts @@ -1,5 +1,6 @@ import { appendFileSync, readFileSync, utimesSync, writeFileSync } from "node:fs" import { join } from "node:path" +import { memoryKeyring } from "@wirecat/cli-base" import { wallClock } from "@wirecat/cli-core/cli" import { afterEach, beforeEach, expect, it } from "vitest" import { openAsteriskStore } from "./store.js" @@ -232,3 +233,32 @@ it("a resync that misses the call naming an extension keeps the name the call wa const names = JSON.parse(await details(transferred.id)).item.participants.map(({ name }: { name: string }) => name) expect(names).toEqual(["Petr Testov", "Oleg Operatorov", null]) }) + +it("account remove forgets the saved password and keeps or deletes the profile's calls", async () => { + await office() + const keyring = memoryKeyring({ "asterisk-cli:token:office": "secret" }) + const start = () => runCli(["office", "session", "start", "--json"], { keyring }) + expect((await start()).code).toBe(0) + expect((await sync()).code).toBe(0) + const synced = (await listed()).length + expect(synced).toBeGreaterThan(0) + + const dry = await runCli(["office", "account", "remove", "--delete-data", "--dry-run", "--json"], { keyring }) + expect(dry.code, dry.err).toBe(0) + expect(dry.json().item).toMatchObject({ + removed: expect.arrayContaining([{ kind: "keyring", target: "token:office" }]), + }) + expect(keyring.get("asterisk-cli", "token:office")).toBe("secret") + + const kept = await runCli(["office", "account", "remove", "--keep-data", "--yes", "--json"], { keyring }) + expect(kept.code, kept.err).toBe(0) + expect(kept.json().item).toMatchObject({ data: "kept", revoked: null }) + expect(keyring.get("asterisk-cli", "token:office")).toBeNull() + expect((await listed()).length).toBe(synced) + + await office() + expect((await start()).code).toBe(0) + const deleted = await runCli(["office", "account", "remove", "--delete-data", "--yes", "--json"], { keyring }) + expect(deleted.code, deleted.err).toBe(0) + expect((await runCli(["office", "meetings", "list", "--json"])).json().items).toEqual([]) +}) diff --git a/src/commands/account.ts b/src/commands/account.ts new file mode 100644 index 0000000..7cf4287 --- /dev/null +++ b/src/commands/account.ts @@ -0,0 +1,40 @@ +import { resolvePaths } from "@wirecat/cli-base" +import { annotate } from "@wirecat/cli-base/commands" +import { type AccountRemoval, accountRemoveCommand, LOCAL_READ, LOCAL_WRITE } from "@wirecat/cli-core/cli" +import { Command } from "commander" +import { PROVIDER_NAME } from "../call-source.js" +import { configuration } from "../config.js" +import { IDENTITY } from "../identity.js" + +const removal: AccountRemoval = { + // The password belongs to a database or SFTP user on the owner's server; only that server can change it. + signOut: async (profile) => ({ + revoked: null, + removed: [`the saved password of ${profile} (the user on the server keeps it)`], + }), + secrets: (profile, { env, keyring }) => [ + { + credentials: { + configDir: resolvePaths({ appName: IDENTITY.appName, prefix: IDENTITY.envPrefix, env }).config, + service: IDENTITY.appName, + envVar: `${IDENTITY.envPrefix}_TOKEN`, + env, + ...(keyring ? { keyring } : {}), + }, + accounts: [`token:${profile}`], + }, + ], +} + +export const accountCommand = (): Command => + annotate(new Command("account"), { effects: LOCAL_READ }) + .description("this profile on this computer") + .addCommand( + annotate( + accountRemoveCommand(IDENTITY, removal, { + provider: PROVIDER_NAME, + resolveSettings: configuration.resolveSettings, + }), + { effects: LOCAL_WRITE }, + ), + ) diff --git a/src/program.ts b/src/program.ts index 2659373..c674ed8 100644 --- a/src/program.ts +++ b/src/program.ts @@ -19,6 +19,7 @@ import { } from "@wirecat/cli-core/cli" import { appUpdate } from "@wirecat/cli-core/update" import type { Command } from "commander" +import { accountCommand } from "./commands/account.js" import { meetingsMount } from "./commands/meetings.js" import { modelsCommand } from "./commands/models.js" import { sessionCommand } from "./commands/session.js" @@ -80,6 +81,7 @@ export const definition = (): ProgramDefinition => { commands: () => [ commandsCommand(IDENTITY), sessionCommand(), + accountCommand(), ...meetings.commands(), modelsCommand(), appDoctorCommand(IDENTITY, configuration, { packageName: PACKAGE }),