@zivis/cli 0.1.0-alpha.7 → 0.1.0-alpha.8
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/dist/commands/auth/init.js +91 -116
- package/dist/commands/auth/init.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import * as readline from "node:readline/promises";
|
|
2
|
-
import { login, getStorageName, saveSessionEntry
|
|
3
|
-
import { getValidAccessToken } from "@zivis/mcp/auth/token-refresh";
|
|
2
|
+
import { login, getStorageName, saveSessionEntry } from "@zivis/mcp/auth";
|
|
4
3
|
import { DEFAULT_CONFIG } from "@zivis/mcp/types";
|
|
5
4
|
import { fetchUserOrgs, extractEmail } from "../../internal/orgs.js";
|
|
6
5
|
import { ensureProjectBinding } from "../../internal/project-binding.js";
|
|
7
|
-
|
|
8
|
-
function parseReceivedOrgId(msg) {
|
|
9
|
-
const m = msg.match(/received (org_\w+)/);
|
|
10
|
-
return m ? m[1] : null;
|
|
11
|
-
}
|
|
6
|
+
import { decodeJwtPayload } from "@zivis/mcp/auth/jwt";
|
|
12
7
|
async function prompt(question) {
|
|
13
8
|
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
|
|
14
9
|
try {
|
|
@@ -21,134 +16,114 @@ async function prompt(question) {
|
|
|
21
16
|
export async function initCommand(opts) {
|
|
22
17
|
const config = DEFAULT_CONFIG;
|
|
23
18
|
const session = opts.session || "default";
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const stored = await loadTokens(session);
|
|
29
|
-
const email = extractEmail(stored?.id_token);
|
|
30
|
-
console.log(`Using existing session (${email}).`);
|
|
31
|
-
console.log(`To force re-login first: zivis auth logout && zivis auth init\n`);
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
console.log("Step 1/2: Authenticate (browser will open)...");
|
|
35
|
-
try {
|
|
36
|
-
const tokens = await login(config, session, undefined, { skipConfirm: true });
|
|
37
|
-
bootstrapToken = tokens.access_token;
|
|
38
|
-
const email = extractEmail(tokens.id_token);
|
|
39
|
-
console.log(`✓ Authenticated as ${email}`);
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
console.error("Login failed:", err instanceof Error ? err.message : err);
|
|
43
|
-
process.exit(1);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
// Fetch the user's orgs
|
|
47
|
-
let orgs;
|
|
19
|
+
// Step 1: Open browser — no org pre-selection. User picks the org in their browser.
|
|
20
|
+
console.log("Opening browser for authentication...");
|
|
21
|
+
console.log("Log into the org you want to bind this project to.\n");
|
|
22
|
+
let tokens;
|
|
48
23
|
try {
|
|
49
|
-
|
|
24
|
+
tokens = await login(config, session, undefined, { skipConfirm: true });
|
|
50
25
|
}
|
|
51
26
|
catch (err) {
|
|
52
|
-
console.error("
|
|
53
|
-
process.exit(1);
|
|
54
|
-
}
|
|
55
|
-
if (orgs.length === 0) {
|
|
56
|
-
console.error("No organizations found for this account. Contact support.");
|
|
27
|
+
console.error("Login failed:", err instanceof Error ? err.message : err);
|
|
57
28
|
process.exit(1);
|
|
58
29
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
30
|
+
const email = extractEmail(tokens.id_token);
|
|
31
|
+
const storageName = getStorageName();
|
|
32
|
+
await saveSessionEntry(session, email);
|
|
33
|
+
console.log(`\n✓ Authenticated as ${email}`);
|
|
34
|
+
console.log(`✓ Credentials stored in ${storageName}`);
|
|
35
|
+
// Step 2: Determine which org the JWT is scoped to
|
|
36
|
+
const claims = decodeJwtPayload(tokens.access_token);
|
|
37
|
+
const jwtOrgId = claims?.org_id;
|
|
38
|
+
if (jwtOrgId) {
|
|
39
|
+
// JWT already has an org — look up the name and write the binding
|
|
40
|
+
let orgName;
|
|
41
|
+
try {
|
|
42
|
+
const orgs = await fetchUserOrgs(config, tokens.access_token);
|
|
43
|
+
orgName = orgs.find((o) => o.workosOrgId === jwtOrgId)?.name;
|
|
68
44
|
}
|
|
69
|
-
|
|
45
|
+
catch {
|
|
46
|
+
// non-fatal — binding still works with just the org ID
|
|
47
|
+
}
|
|
48
|
+
const orgLabel = orgName ? `${orgName}` : jwtOrgId;
|
|
49
|
+
console.log(`✓ JWT scoped to org: ${orgLabel}`);
|
|
50
|
+
await ensureProjectBinding({
|
|
51
|
+
config,
|
|
52
|
+
session,
|
|
53
|
+
accessToken: tokens.access_token,
|
|
54
|
+
organizationId: jwtOrgId,
|
|
55
|
+
cwd: process.cwd(),
|
|
56
|
+
autoBind: true,
|
|
57
|
+
skipBind: false,
|
|
58
|
+
});
|
|
70
59
|
}
|
|
71
60
|
else {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
console.
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
// Attempt org-scoped login, with graceful handling for browser-session mismatches
|
|
79
|
-
let scopedTokens;
|
|
80
|
-
let finalOrgId = selectedOrg.workosOrgId;
|
|
81
|
-
let finalOrgName = selectedOrg.name;
|
|
82
|
-
let printUrlOnly = false;
|
|
83
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
84
|
-
const stepLabel = `Step ${orgs.length > 1 || attempt > 0 ? "2/2" : "1/1"}`;
|
|
85
|
-
const browserNote = printUrlOnly ? "(URL will be printed — paste into a private/incognito window)" : "(browser will open)";
|
|
86
|
-
console.log(`\n${stepLabel}: Logging in to ${finalOrgName} ${browserNote}...`);
|
|
61
|
+
// No org in JWT — user is authenticated but not scoped to an org yet.
|
|
62
|
+
// Fetch orgs and let them pick, then do a scoped login.
|
|
63
|
+
console.log("\nYour account isn't scoped to a specific org yet.");
|
|
64
|
+
console.log("Fetching your organizations...\n");
|
|
65
|
+
let orgs;
|
|
87
66
|
try {
|
|
88
|
-
|
|
89
|
-
orgName: finalOrgName,
|
|
90
|
-
skipConfirm: true,
|
|
91
|
-
printUrlOnly,
|
|
92
|
-
});
|
|
93
|
-
break; // success
|
|
67
|
+
orgs = await fetchUserOrgs(config, tokens.access_token);
|
|
94
68
|
}
|
|
95
69
|
catch (err) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
70
|
+
console.error("Failed to fetch organizations:", err instanceof Error ? err.message : err);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
if (orgs.length === 0) {
|
|
74
|
+
console.error("No organizations found. Contact support.");
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
if (orgs.length === 1) {
|
|
78
|
+
console.log(`One org found: ${orgs[0].name}. Re-authenticating scoped to that org...`);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
console.log("Your organizations:");
|
|
82
|
+
orgs.forEach((org, i) => console.log(` ${i + 1}. ${org.name}`));
|
|
83
|
+
const answer = await prompt(`\nSelect org to bind this project to [1-${orgs.length}]: `);
|
|
84
|
+
const idx = parseInt(answer, 10) - 1;
|
|
85
|
+
if (isNaN(idx) || idx < 0 || idx >= orgs.length) {
|
|
86
|
+
console.error("Invalid selection.");
|
|
100
87
|
process.exit(1);
|
|
101
88
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
console.error(`\n⚠ Browser authenticated to "${receivedName}" instead of "${finalOrgName}".`);
|
|
106
|
-
console.error(` Your browser's active session is for a different org.`);
|
|
107
|
-
if (attempt >= 1) {
|
|
108
|
-
console.error("\nStill mismatched after retry — your browser may not support private-window auto-open.");
|
|
109
|
-
console.error("Run `zivis auth init` again and paste the printed URL into a private/incognito window manually.");
|
|
89
|
+
const selected = orgs[idx];
|
|
90
|
+
if (!selected.workosOrgId) {
|
|
91
|
+
console.error(`Org "${selected.name}" has no WorkOS org ID.`);
|
|
110
92
|
process.exit(1);
|
|
111
93
|
}
|
|
112
|
-
console.
|
|
113
|
-
console.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
94
|
+
console.log(`\nRe-opening browser scoped to ${selected.name}...`);
|
|
95
|
+
console.log("(Tip: if your browser logs into the wrong org, use option 2 to get a URL for a private/incognito window)\n");
|
|
96
|
+
try {
|
|
97
|
+
const scopedTokens = await login(config, session, selected.workosOrgId, {
|
|
98
|
+
orgName: selected.name,
|
|
99
|
+
skipConfirm: true,
|
|
100
|
+
});
|
|
101
|
+
await saveSessionEntry(session, email);
|
|
102
|
+
await ensureProjectBinding({
|
|
103
|
+
config,
|
|
104
|
+
session,
|
|
105
|
+
accessToken: scopedTokens.access_token,
|
|
106
|
+
organizationId: selected.workosOrgId,
|
|
107
|
+
cwd: process.cwd(),
|
|
108
|
+
autoBind: true,
|
|
109
|
+
skipBind: false,
|
|
110
|
+
});
|
|
128
111
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
112
|
+
catch (err) {
|
|
113
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
114
|
+
const m = msg.match(/received (org_\w+)/);
|
|
115
|
+
if (m) {
|
|
116
|
+
console.error(`\n⚠ Browser authenticated to a different org than selected.`);
|
|
117
|
+
console.error(` Your browser has an active session for a different org.\n`);
|
|
118
|
+
console.error(` To fix: run \`zivis auth init\` again and paste the URL into a private/incognito window.`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
console.error("Login failed:", msg);
|
|
122
|
+
}
|
|
123
|
+
process.exit(1);
|
|
132
124
|
}
|
|
133
125
|
}
|
|
134
126
|
}
|
|
135
|
-
if (!scopedTokens) {
|
|
136
|
-
process.exit(1);
|
|
137
|
-
}
|
|
138
|
-
const email = extractEmail(scopedTokens.id_token);
|
|
139
|
-
const storageName = getStorageName();
|
|
140
|
-
await saveSessionEntry(session, email);
|
|
141
|
-
console.log(`\n✓ Logged in as ${email} → ${finalOrgName}`);
|
|
142
|
-
console.log(`✓ Credentials stored in ${storageName}`);
|
|
143
|
-
await ensureProjectBinding({
|
|
144
|
-
config,
|
|
145
|
-
session,
|
|
146
|
-
accessToken: scopedTokens.access_token,
|
|
147
|
-
organizationId: finalOrgId,
|
|
148
|
-
cwd: process.cwd(),
|
|
149
|
-
autoBind: true,
|
|
150
|
-
skipBind: false,
|
|
151
|
-
});
|
|
152
127
|
process.exit(0);
|
|
153
128
|
}
|
|
154
129
|
//# sourceMappingURL=init.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../../src/commands/auth/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../../src/commands/auth/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAMvD,KAAK,UAAU,MAAM,CAAC,QAAgB;IACpC,MAAM,EAAE,GAAG,QAAQ,CAAC,eAAe,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACtF,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC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