@vatio-ai/cli 0.113.0 → 0.114.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api-client.mjs +6 -2
- package/lib/commands/workspace-admin.mjs +77 -15
- package/lib/help.mjs +6 -1
- package/package.json +1 -1
package/lib/api-client.mjs
CHANGED
|
@@ -176,10 +176,14 @@ export class ApiClient extends HttpClient {
|
|
|
176
176
|
return this.get("/api_keys");
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
179
|
+
// `scopes` (a secret key's) left null takes the platform's default: every
|
|
180
|
+
// scope but `team`. `confirmation` is { code } or { recovery_code }, which a
|
|
181
|
+
// `team` key needs.
|
|
182
|
+
createApiKey({ kind, environment, label = null, allowedOrigins = [], scopes = null, confirmation = {} }) {
|
|
183
|
+
const body = { kind, environment, ...confirmation };
|
|
181
184
|
if (String(label ?? "").trim() !== "") body.label = String(label).trim();
|
|
182
185
|
if (allowedOrigins.length > 0) body.allowed_origins = allowedOrigins;
|
|
186
|
+
if (Array.isArray(scopes) && scopes.length > 0) body.scopes = scopes;
|
|
183
187
|
return this.post("/api_keys", body);
|
|
184
188
|
}
|
|
185
189
|
|
|
@@ -104,27 +104,39 @@ function secretScope({ environment, tests }) {
|
|
|
104
104
|
|
|
105
105
|
// Repeatable: `--origin https://a.com --origin https://b.com`.
|
|
106
106
|
function takeOrigins(args) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
return takeRepeated(args, "--origin");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function takeRepeated(args, flag) {
|
|
111
|
+
const values = [];
|
|
112
|
+
for (let value = takeValue(args, flag); value !== null; value = takeValue(args, flag)) {
|
|
113
|
+
values.push(value);
|
|
110
114
|
}
|
|
111
|
-
return
|
|
115
|
+
return values;
|
|
112
116
|
}
|
|
113
117
|
|
|
114
118
|
const KINDS = ["publishable", "secret"];
|
|
119
|
+
// What a secret key may call on the server API. The platform validates them;
|
|
120
|
+
// this list is only for the help text. `team` is never given by default.
|
|
121
|
+
const SCOPES = ["messages", "instagram", "chats", "team"];
|
|
115
122
|
const KEYS_USAGE =
|
|
116
|
-
"Usage: vatio keys list [--json]|create publishable|secret [--env live|preview] [--label NAME] [--origin URL]...
|
|
117
|
-
"origins PREFIX URL...|revoke PREFIX";
|
|
123
|
+
"Usage: vatio keys list [--json]|create publishable|secret [--env live|preview] [--label NAME] [--origin URL]... " +
|
|
124
|
+
"[--scope NAME]... [--code CODE | --recovery-code CODE] [--json]|origins PREFIX URL...|revoke PREFIX";
|
|
118
125
|
|
|
119
126
|
// API keys, of two kinds. A publishable key is for a page or an app: public by
|
|
120
127
|
// design, accepted only from the origins it lists. A secret key is for the
|
|
121
|
-
// developer's own backend (
|
|
122
|
-
// it never goes in a page or an app, and unlike the
|
|
123
|
-
// to the workspace, so it outlives whoever created
|
|
128
|
+
// developer's own backend (to have the agent write to a contact first, read a
|
|
129
|
+
// chat, keep the team): it never goes in a page or an app, and unlike the
|
|
130
|
+
// CLI's own token it belongs to the workspace, so it outlives whoever created
|
|
131
|
+
// it. A secret key carries scopes; without --scope it gets every one but
|
|
132
|
+
// `team`.
|
|
124
133
|
export async function keys(config, args) {
|
|
125
134
|
const environment = environmentOption(args, "live");
|
|
126
135
|
const label = takeValue(args, "--label") ?? takeValue(args, "-l");
|
|
127
136
|
const origins = takeOrigins(args);
|
|
137
|
+
const scopes = takeRepeated(args, "--scope");
|
|
138
|
+
const code = takeValue(args, "--code");
|
|
139
|
+
const recoveryCode = takeValue(args, "--recovery-code");
|
|
128
140
|
const json = takeFlag(args, "--json");
|
|
129
141
|
const workspace = config.resolveWorkspaceRequired();
|
|
130
142
|
const client = deployClient(config, workspace);
|
|
@@ -151,20 +163,29 @@ export async function keys(config, args) {
|
|
|
151
163
|
parts.push(key.last_used_at ? `last used ${key.last_used_at}` : "never used");
|
|
152
164
|
console.log(` ${parts.filter(Boolean).join(" ")}`);
|
|
153
165
|
if (key.kind === "publishable") console.log(` origins: ${originsLine(key.allowed_origins)}`);
|
|
166
|
+
if (key.kind === "secret") console.log(` scopes: ${asArray(key.scopes).join(", ") || "none"}`);
|
|
154
167
|
}
|
|
155
168
|
return;
|
|
156
169
|
}
|
|
157
170
|
if (sub === "create") {
|
|
158
171
|
const kind = args.shift();
|
|
159
|
-
if (!KINDS.includes(kind))
|
|
172
|
+
if (!KINDS.includes(kind)) {
|
|
173
|
+
fail("Say which kind: vatio keys create publishable|secret [--env NAME] [--label NAME] [--origin URL]... [--scope NAME]...");
|
|
174
|
+
}
|
|
160
175
|
if (kind === "secret" && origins.length > 0) {
|
|
161
176
|
fail("--origin only applies to a publishable key. A secret key is for your backend and has no origins.");
|
|
162
177
|
}
|
|
163
|
-
|
|
178
|
+
if (kind === "publishable" && scopes.length > 0) {
|
|
179
|
+
fail(`--scope only applies to a secret key (${SCOPES.join(", ")}).`);
|
|
180
|
+
}
|
|
181
|
+
const payload = await createKey(client, {
|
|
164
182
|
kind,
|
|
165
183
|
environment,
|
|
166
184
|
label,
|
|
167
|
-
allowedOrigins: kind === "publishable" ? origins : []
|
|
185
|
+
allowedOrigins: kind === "publishable" ? origins : [],
|
|
186
|
+
scopes: kind === "secret" && scopes.length > 0 ? scopes : null,
|
|
187
|
+
code,
|
|
188
|
+
recoveryCode
|
|
168
189
|
});
|
|
169
190
|
// For a script that hands the key straight to something else: the key or a
|
|
170
191
|
// failed command, never a line of prose to grep it out of.
|
|
@@ -174,6 +195,7 @@ export async function keys(config, args) {
|
|
|
174
195
|
}
|
|
175
196
|
console.log(`Created a ${kind} key for ${environment} on workspace ${workspace}`);
|
|
176
197
|
if (kind === "publishable") console.log(` origins: ${originsLine(payload.api_key?.allowed_origins)}`);
|
|
198
|
+
if (kind === "secret") console.log(` scopes: ${asArray(payload.api_key?.scopes).join(", ")}`);
|
|
177
199
|
console.log("");
|
|
178
200
|
// Shown in full exactly once: the platform stores a digest, so this is the
|
|
179
201
|
// only moment the value exists anywhere the developer can copy it.
|
|
@@ -187,9 +209,16 @@ export async function keys(config, args) {
|
|
|
187
209
|
console.log("");
|
|
188
210
|
console.log(` VATIO_SERVER_KEY=${payload.key}`);
|
|
189
211
|
console.log("");
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
212
|
+
// A first call the key's scopes allow, that changes nothing.
|
|
213
|
+
const granted = asArray(payload.api_key?.scopes);
|
|
214
|
+
const [what, path] = granted.includes("messages")
|
|
215
|
+
? ["lists the templates your number can send", "templates"]
|
|
216
|
+
: granted.includes("team") ? ["lists the workspace's team", "team_members"] : [null, null];
|
|
217
|
+
if (path) {
|
|
218
|
+
console.log(`Try it (${what}):`);
|
|
219
|
+
console.log(` curl ${config.resolveBaseUrl()}/api/server/v1/${workspace}/${path} \\`);
|
|
220
|
+
console.log(' -H "Authorization: Bearer $VATIO_SERVER_KEY"');
|
|
221
|
+
}
|
|
193
222
|
return;
|
|
194
223
|
}
|
|
195
224
|
if (sub === "origins") {
|
|
@@ -210,6 +239,39 @@ export async function keys(config, args) {
|
|
|
210
239
|
fail(KEYS_USAGE);
|
|
211
240
|
}
|
|
212
241
|
|
|
242
|
+
// A `team` key needs the owner's second factor, as `vatio team invite` does:
|
|
243
|
+
// from a terminal the code is asked for; a coding agent has none, so it fails
|
|
244
|
+
// with what to ask the person for.
|
|
245
|
+
async function createKey(client, { code, recoveryCode, ...fields }) {
|
|
246
|
+
const confirmation = {};
|
|
247
|
+
if (code) confirmation.code = code;
|
|
248
|
+
if (recoveryCode) confirmation.recovery_code = recoveryCode;
|
|
249
|
+
try {
|
|
250
|
+
return await client.createApiKey({ ...fields, confirmation }).catch(explainKeyError);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
const needsCode = error?.status === 403 && error?.body?.error?.code === "reauthentication_required";
|
|
253
|
+
// explainKeyError let it through only when the terminal can answer it.
|
|
254
|
+
if (!needsCode || code || recoveryCode) throw error;
|
|
255
|
+
const typed = await askSecret(`${paint("green", "?")} Code from your authenticator app: `);
|
|
256
|
+
return await client.createApiKey({ ...fields, confirmation: { code: typed } }).catch(explainKeyError);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// The answers whose own words say what to do, which the generic 403 and 422
|
|
261
|
+
// lines would hide. Left to throw: everything else, and a missing code the
|
|
262
|
+
// terminal can still ask for.
|
|
263
|
+
const KEY_ERRORS_THAT_SAY_WHY = new Set(["owner_only", "invalid_confirmation", "invalid_scopes"]);
|
|
264
|
+
|
|
265
|
+
function explainKeyError(error) {
|
|
266
|
+
const body = error?.body ?? {};
|
|
267
|
+
const code = body.error?.code ?? body.error_key;
|
|
268
|
+
if (code === "reauthentication_required" && !(process.stdin.isTTY && /--code/.test(error.message))) {
|
|
269
|
+
fail(error.message);
|
|
270
|
+
}
|
|
271
|
+
if (KEY_ERRORS_THAT_SAY_WHY.has(code)) fail(body.error_description ?? error.message);
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
|
|
213
275
|
// A flag whose value is optional: `--flag`, `--flag VALUE` or `--flag=VALUE`.
|
|
214
276
|
// Returns null when it is absent and "" when it is there with no value, so
|
|
215
277
|
// the caller can tell those two apart. A following token that starts with `-`
|
package/lib/help.mjs
CHANGED
|
@@ -162,9 +162,14 @@ the two you are in. Commands default as listed.
|
|
|
162
162
|
otherwise the value everywhere. With --tests, the
|
|
163
163
|
value preview and every test run use, like a staging URL
|
|
164
164
|
keys list [--json] API keys of both kinds: publishable (for a page or
|
|
165
|
-
app) and secret (for your backend)
|
|
165
|
+
app) and secret (for your backend, with its scopes)
|
|
166
166
|
keys create publishable|secret [--env NAME] [--label NAME] [--origin URL]... [--json]
|
|
167
167
|
--origin only for a publishable key
|
|
168
|
+
keys create secret --scope NAME... [--code CODE | --recovery-code CODE]
|
|
169
|
+
What the secret key may call: messages, instagram,
|
|
170
|
+
chats, team (repeatable). Without --scope, all but
|
|
171
|
+
team. team (add people to the team) is the owner's,
|
|
172
|
+
with a code from their authenticator app
|
|
168
173
|
keys origins PREFIX URL...
|
|
169
174
|
The origins a publishable key is accepted from
|
|
170
175
|
(replaces the list)
|