@muhkoo/cli 0.10.10-alpha.0 → 0.10.11-alpha.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/package.json +3 -2
- package/src/commands/tokens.js +128 -0
- package/src/index.js +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muhkoo/cli",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.11-alpha.0",
|
|
4
4
|
"description": "Command-line interface for the Muhkoo platform — provision backends, deploy apps, and manage agents, functions, databases, and domains.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"prepublishOnly": "node scripts/sync-version.mjs --check"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@muhkoo/connect": "0.10.
|
|
41
|
+
"@muhkoo/connect": "0.10.11-alpha.0",
|
|
42
|
+
"snarkjs": "^0.7.0"
|
|
42
43
|
},
|
|
43
44
|
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
|
|
44
45
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo tokens` — create, list, and revoke scoped access tokens for an app.
|
|
3
|
+
* Access tokens are non-ZK machine credentials (mk_<env>_at_<hex>) that are
|
|
4
|
+
* scoped and expiring. The plaintext is shown only once, on creation.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { devContext } from "../lib/config.js";
|
|
8
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
9
|
+
import { table, json, ok, info, warn, die, c } from "../lib/ui.js";
|
|
10
|
+
|
|
11
|
+
/** Valid token scopes, surfaced in help so callers know the vocabulary. */
|
|
12
|
+
const SCOPES = [
|
|
13
|
+
"db:read",
|
|
14
|
+
"db:write",
|
|
15
|
+
"kv:read",
|
|
16
|
+
"kv:write",
|
|
17
|
+
"storage:read",
|
|
18
|
+
"storage:write",
|
|
19
|
+
"messages:read",
|
|
20
|
+
"messages:write",
|
|
21
|
+
"functions:invoke",
|
|
22
|
+
"ai:infer",
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
const DEFAULT_SCOPES = ["db:read", "db:write"];
|
|
26
|
+
|
|
27
|
+
export const help = `muhkoo tokens — manage scoped access tokens
|
|
28
|
+
|
|
29
|
+
Usage:
|
|
30
|
+
muhkoo tokens ls <appId> [--json]
|
|
31
|
+
muhkoo tokens create <appId> [--scopes db:read,db:write] [--env test|live] [--expires-in <days>] [--label <name>] [--json]
|
|
32
|
+
muhkoo tokens revoke <appId> <keyId>
|
|
33
|
+
|
|
34
|
+
Scopes:
|
|
35
|
+
${SCOPES.join(", ")}`;
|
|
36
|
+
|
|
37
|
+
export default async function tokens(args) {
|
|
38
|
+
const sub = args._[1];
|
|
39
|
+
const ctx = await devContext(args);
|
|
40
|
+
const appId = args._[2] || args.app;
|
|
41
|
+
if (!appId) die("Missing app id. Usage: muhkoo tokens <ls|create|revoke> <appId>");
|
|
42
|
+
|
|
43
|
+
switch (sub) {
|
|
44
|
+
case "ls":
|
|
45
|
+
case undefined:
|
|
46
|
+
return list(ctx, appId, args);
|
|
47
|
+
case "create":
|
|
48
|
+
return create(ctx, appId, args);
|
|
49
|
+
case "revoke":
|
|
50
|
+
case "rm":
|
|
51
|
+
return revoke(ctx, appId, args);
|
|
52
|
+
default:
|
|
53
|
+
die(`Unknown subcommand "tokens ${sub}". See \`muhkoo tokens --help\`.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function list(ctx, appId, args) {
|
|
58
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/access-tokens`), "List tokens");
|
|
59
|
+
if (args.json) return json(r.body);
|
|
60
|
+
const items = r.body?.tokens || [];
|
|
61
|
+
if (!items.length) {
|
|
62
|
+
return info("No access tokens. Create one with `muhkoo tokens create <appId>`.");
|
|
63
|
+
}
|
|
64
|
+
table(
|
|
65
|
+
["LABEL", "KEY ID", "ENV", "SCOPES", "EXPIRES", "STATUS"],
|
|
66
|
+
items.map((t) => [
|
|
67
|
+
t.label || "",
|
|
68
|
+
truncate(t.keyId),
|
|
69
|
+
t.env ?? "",
|
|
70
|
+
Array.isArray(t.scopes) ? t.scopes.join(",") : t.scopes ?? "",
|
|
71
|
+
fmtDate(t.expiresAt),
|
|
72
|
+
statusOf(t),
|
|
73
|
+
]),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function create(ctx, appId, args) {
|
|
78
|
+
const scopes = args.scopes
|
|
79
|
+
? String(args.scopes)
|
|
80
|
+
.split(",")
|
|
81
|
+
.map((s) => s.trim())
|
|
82
|
+
.filter(Boolean)
|
|
83
|
+
: DEFAULT_SCOPES;
|
|
84
|
+
|
|
85
|
+
const body = { scopes };
|
|
86
|
+
body.env = args.env === "test" ? "test" : "live";
|
|
87
|
+
if (args["expires-in"] !== undefined) body.expiresInDays = Number(args["expires-in"]);
|
|
88
|
+
if (args.label) body.label = args.label;
|
|
89
|
+
|
|
90
|
+
const r = await devCall(ctx, "POST", `/api/apps/${appId}/access-tokens`, body);
|
|
91
|
+
if (r.status === 402) die("Access tokens need a paid plan.");
|
|
92
|
+
ensure(r, "Create token");
|
|
93
|
+
if (args.json) return json(r.body);
|
|
94
|
+
|
|
95
|
+
const t = r.body;
|
|
96
|
+
ok(`Created ${t.env} access token${t.label ? ` ${c.bold(t.label)}` : ""} (${t.keyId})`);
|
|
97
|
+
info(` scopes: ${(t.scopes || scopes).join(", ")}`);
|
|
98
|
+
if (t.expiresAt) info(` expires: ${fmtDate(t.expiresAt)}`);
|
|
99
|
+
info(`\n ${c.bold(t.plaintext)}`);
|
|
100
|
+
warn("Save this token now — it is shown only once.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function revoke(ctx, appId, args) {
|
|
104
|
+
const keyId = args._[3] || args.keyId;
|
|
105
|
+
if (!keyId) die("Usage: muhkoo tokens revoke <appId> <keyId>");
|
|
106
|
+
ensure(
|
|
107
|
+
await devCall(ctx, "DELETE", `/api/apps/${appId}/access-tokens/${encodeURIComponent(keyId)}`),
|
|
108
|
+
"Revoke token",
|
|
109
|
+
);
|
|
110
|
+
ok(`Revoked token ${keyId}.`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function statusOf(t) {
|
|
114
|
+
if (t.revoked) return c.red("revoked");
|
|
115
|
+
if (t.expired) return c.yellow("expired");
|
|
116
|
+
return c.green("active");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function truncate(keyId, n = 20) {
|
|
120
|
+
const s = String(keyId ?? "");
|
|
121
|
+
return s.length > n ? s.slice(0, n) + "…" : s;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function fmtDate(v) {
|
|
125
|
+
if (!v) return "never";
|
|
126
|
+
const d = new Date(v);
|
|
127
|
+
return Number.isNaN(d.getTime()) ? String(v) : d.toISOString().slice(0, 10);
|
|
128
|
+
}
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const COMMANDS = {
|
|
|
16
16
|
whoami: () => import("./commands/whoami.js"),
|
|
17
17
|
apps: () => import("./commands/apps.js"),
|
|
18
18
|
keys: () => import("./commands/apps.js"), // `keys rotate` lives in apps.js
|
|
19
|
+
tokens: () => import("./commands/tokens.js"),
|
|
19
20
|
provision: () => import("./commands/provision.js"),
|
|
20
21
|
deploy: () => import("./commands/deploy.js"),
|
|
21
22
|
promote: () => import("./commands/promote.js"),
|
|
@@ -41,6 +42,7 @@ Account
|
|
|
41
42
|
Apps
|
|
42
43
|
apps ls|get|create|slug|rm manage apps
|
|
43
44
|
keys rotate <appId> rotate an app's keys
|
|
45
|
+
tokens ls|create|revoke manage scoped access tokens
|
|
44
46
|
|
|
45
47
|
Backend
|
|
46
48
|
provision --spec <f> create/update tables, agents, functions from a spec
|