Repository navigation
Expand file tree
/
Copy pathcredentials.ts
More file actions
179 lines (163 loc) · 5.43 KB
/
Copy pathcredentials.ts
File metadata and controls
179 lines (163 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bun
/**
* Credentials skill CLI
* AES-256-GCM encrypted credential storage at ~/.aibtc/credentials.json
*
* Usage: bun run credentials/credentials.ts <subcommand> [options]
*/
import { Command } from "commander";
import {
addCredential,
getCredential,
listCredentials,
deleteCredential,
rotatePassword,
} from "./store.js";
import { printJson, handleError } from "../src/lib/utils/cli.js";
// ---------------------------------------------------------------------------
// Program
// ---------------------------------------------------------------------------
const program = new Command();
program
.name("credentials")
.description(
"AES-256-GCM encrypted credential storage. Store, retrieve, and manage named secrets."
)
.version("0.1.0");
// ---------------------------------------------------------------------------
// add
// ---------------------------------------------------------------------------
program
.command("add")
.description("Add or update an encrypted credential")
.requiredOption("--id <id>", "Credential identifier (e.g. hiro-api-key)")
.requiredOption("--value <value>", "Plaintext secret value (sensitive)")
.requiredOption("--password <password>", "Master password for encryption (sensitive)")
.option("--label <label>", "Human-readable label (default: same as id)")
.option("--category <category>", "Category tag (e.g. api-key, token, url, secret)", "secret")
.action(
async (opts: {
id: string;
value: string;
password: string;
label?: string;
category: string;
}) => {
try {
const cred = await addCredential(
opts.id,
opts.value,
opts.password,
opts.label,
opts.category
);
printJson({
success: true,
id: cred.id,
label: cred.label,
category: cred.category,
createdAt: cred.createdAt,
updatedAt: cred.updatedAt,
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// get
// ---------------------------------------------------------------------------
program
.command("get")
.description("Decrypt and retrieve a credential value")
.requiredOption("--id <id>", "Credential identifier")
.requiredOption("--password <password>", "Master password for decryption (sensitive)")
.action(async (opts: { id: string; password: string }) => {
try {
const cred = await getCredential(opts.id, opts.password);
printJson({
id: cred.id,
label: cred.label,
category: cred.category,
value: cred.value,
createdAt: cred.createdAt,
updatedAt: cred.updatedAt,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// list
// ---------------------------------------------------------------------------
program
.command("list")
.description("List all credential IDs and labels (no secret values returned)")
.action(async () => {
try {
const credentials = await listCredentials();
printJson({
count: credentials.length,
credentials,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// delete
// ---------------------------------------------------------------------------
program
.command("delete")
.description("Permanently delete a credential (requires password + confirmation)")
.requiredOption("--id <id>", "Credential identifier to delete")
.requiredOption("--password <password>", "Master password (sensitive)")
.requiredOption(
"--confirm <word>",
'Must be exactly "DELETE" to confirm permanent deletion'
)
.action(
async (opts: { id: string; password: string; confirm: string }) => {
try {
if (opts.confirm !== "DELETE") {
handleError(
new Error(
'Confirmation required: pass --confirm DELETE to permanently delete a credential'
)
);
}
await deleteCredential(opts.id, opts.password);
printJson({
success: true,
deleted: opts.id,
message: `Credential "${opts.id}" has been permanently deleted.`,
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// rotate-password
// ---------------------------------------------------------------------------
program
.command("rotate-password")
.description("Re-encrypt all credentials with a new master password")
.requiredOption("--old-password <password>", "Current master password (sensitive)")
.requiredOption("--new-password <password>", "New master password, min 8 chars (sensitive)")
.action(async (opts: { oldPassword: string; newPassword: string }) => {
try {
const count = await rotatePassword(opts.oldPassword, opts.newPassword);
printJson({
success: true,
message: `Password rotated. ${count} credential${count === 1 ? "" : "s"} re-encrypted.`,
count,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// Parse
// ---------------------------------------------------------------------------
program.parse(process.argv);