-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbin.ts
More file actions
507 lines (491 loc) · 18 KB
/
bin.ts
File metadata and controls
507 lines (491 loc) · 18 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/env node
// Load .env.local for local development when --local flag is used
if (process.argv.includes('--local') || process.env.INSTALLER_DEV) {
const { config } = await import('dotenv');
// bin.ts compiles to dist/bin.js, so go up one level to find .env.local
config({ path: new URL('../.env.local', import.meta.url).pathname });
}
import { satisfies } from 'semver';
import { red } from './utils/logging.js';
import { getConfig, getVersion } from './lib/settings.js';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import chalk from 'chalk';
import { ensureAuthenticated } from './lib/ensure-auth.js';
import { checkForUpdates } from './lib/version-check.js';
const NODE_VERSION_RANGE = getConfig().nodeVersion;
// Have to run this above the other imports because they are importing clack that
// has the problematic imports.
if (!satisfies(process.version, NODE_VERSION_RANGE)) {
red(
`WorkOS AuthKit installer requires Node.js ${NODE_VERSION_RANGE}. You are using Node.js ${process.version}. Please upgrade your Node.js version.`,
);
process.exit(1);
}
import { isNonInteractiveEnvironment } from './utils/environment.js';
import clack from './utils/clack.js';
/** Apply insecure storage flag if set */
async function applyInsecureStorage(insecureStorage?: boolean): Promise<void> {
if (insecureStorage) {
const { setInsecureStorage } = await import('./lib/credentials.js');
const { setInsecureConfigStorage } = await import('./lib/config-store.js');
setInsecureStorage(true);
setInsecureConfigStorage(true);
}
}
/** Shared insecure-storage option for commands that access credentials */
const insecureStorageOption = {
'insecure-storage': {
default: false,
describe: 'Store credentials in plaintext file instead of system keyring',
type: 'boolean' as const,
},
} as const;
/**
* Wrap a command handler with authentication check.
* Ensures valid auth before executing the handler.
* Respects --skip-auth flag for CI/testing.
*/
function withAuth<T>(handler: (argv: T) => Promise<void>): (argv: T) => Promise<void> {
return async (argv: T) => {
const typedArgv = argv as { skipAuth?: boolean; insecureStorage?: boolean };
await applyInsecureStorage(typedArgv.insecureStorage);
if (!typedArgv.skipAuth) await ensureAuthenticated();
await handler(argv);
};
}
const installerOptions = {
direct: {
alias: 'D',
default: false,
describe: 'Use your own Anthropic API key (bypass llm-gateway)',
type: 'boolean' as const,
},
debug: {
default: false,
describe: 'Enable verbose logging',
type: 'boolean' as const,
},
...insecureStorageOption,
// Hidden dev/automation flags (use env vars)
local: {
default: false,
type: 'boolean' as const,
hidden: true,
},
ci: {
default: false,
type: 'boolean' as const,
hidden: true,
},
'skip-auth': {
default: false,
type: 'boolean' as const,
hidden: true,
},
'api-key': {
type: 'string' as const,
hidden: true,
},
'client-id': {
type: 'string' as const,
hidden: true,
},
inspect: {
default: false,
type: 'boolean' as const,
hidden: true,
},
// User-facing flags
'homepage-url': {
describe: 'App homepage URL for WorkOS (defaults to http://localhost:{port})',
type: 'string' as const,
},
'redirect-uri': {
describe: 'Redirect URI for WorkOS callback (defaults to framework convention)',
type: 'string' as const,
},
'no-validate': {
default: false,
describe: 'Skip post-installation validation (includes build check)',
type: 'boolean' as const,
},
'install-dir': {
describe: 'Directory to install WorkOS AuthKit in',
type: 'string' as const,
},
integration: {
describe: 'Integration to set up',
type: 'string' as const,
},
'force-install': {
default: false,
describe: 'Force install packages even if peer dependency checks fail',
type: 'boolean' as const,
},
dashboard: {
alias: 'd',
default: false,
describe: 'Run with visual dashboard mode',
type: 'boolean' as const,
},
};
// Check for updates (blocks up to 500ms)
await checkForUpdates();
yargs(hideBin(process.argv))
.env('WORKOS_INSTALLER')
.command('login', 'Authenticate with WorkOS', insecureStorageOption, async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { runLogin } = await import('./commands/login.js');
await runLogin();
process.exit(0);
})
.command('logout', 'Remove stored credentials', insecureStorageOption, async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { runLogout } = await import('./commands/logout.js');
await runLogout();
})
.command(
'install-skill',
'Install bundled AuthKit skills to coding agents',
(yargs) => {
return yargs
.option('list', {
alias: 'l',
type: 'boolean',
description: 'List available skills without installing',
})
.option('skill', {
alias: 's',
type: 'array',
string: true,
description: 'Install specific skill(s)',
})
.option('agent', {
alias: 'a',
type: 'array',
string: true,
description: 'Target specific agent(s): claude-code, codex, cursor, goose',
});
},
withAuth(async (argv) => {
const { runInstallSkill } = await import('./commands/install-skill.js');
await runInstallSkill({
list: argv.list as boolean | undefined,
skill: argv.skill as string[] | undefined,
agent: argv.agent as string[] | undefined,
});
}),
)
.command(
'doctor',
'Diagnose WorkOS integration issues',
(yargs) =>
yargs.options({
verbose: {
type: 'boolean',
default: false,
description: 'Include additional diagnostic information',
},
'skip-api': {
type: 'boolean',
default: false,
description: 'Skip API calls (offline mode)',
},
'install-dir': {
type: 'string',
default: process.cwd(),
description: 'Project directory to analyze',
},
json: {
type: 'boolean',
default: false,
description: 'Output report as JSON',
},
copy: {
type: 'boolean',
default: false,
description: 'Copy report to clipboard',
},
}),
async (argv) => {
const { handleDoctor } = await import('./commands/doctor.js');
await handleDoctor(argv);
},
)
.command('env', 'Manage environment configurations', (yargs) =>
yargs
.options(insecureStorageOption)
.command(
'add [name] [apiKey]',
'Add an environment configuration',
(yargs) =>
yargs
.positional('name', { type: 'string', describe: 'Environment name' })
.positional('apiKey', { type: 'string', describe: 'WorkOS API key' })
.option('endpoint', { type: 'string', describe: 'Custom API endpoint' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { runEnvAdd } = await import('./commands/env.js');
await runEnvAdd({
name: argv.name,
apiKey: argv.apiKey,
endpoint: argv.endpoint,
});
},
)
.command(
'remove <name>',
'Remove an environment configuration',
(yargs) => yargs.positional('name', { type: 'string', demandOption: true, describe: 'Environment name' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { runEnvRemove } = await import('./commands/env.js');
await runEnvRemove(argv.name);
},
)
.command(
'switch [name]',
'Switch active environment',
(yargs) => yargs.positional('name', { type: 'string', describe: 'Environment name' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { runEnvSwitch } = await import('./commands/env.js');
await runEnvSwitch(argv.name);
},
)
.command('list', 'List configured environments', {}, async (argv) => {
await applyInsecureStorage((argv as any).insecureStorage);
const { runEnvList } = await import('./commands/env.js');
await runEnvList();
})
.demandCommand(1, 'Please specify an env subcommand')
.strict(),
)
.command('organization', 'Manage organizations', (yargs) =>
yargs
.options({
...insecureStorageOption,
'api-key': { type: 'string' as const, describe: 'WorkOS API key (overrides environment config)' },
})
.command(
'create <name> [domains..]',
'Create a new organization',
(yargs) =>
yargs
.positional('name', { type: 'string', demandOption: true, describe: 'Organization name' })
.positional('domains', { type: 'string', array: true, describe: 'Domains as domain:state' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runOrgCreate } = await import('./commands/organization.js');
const apiKey = resolveApiKey({ apiKey: argv.apiKey });
await runOrgCreate(argv.name, (argv.domains as string[]) || [], apiKey, resolveApiBaseUrl());
},
)
.command(
'update <orgId> <name> [domain] [state]',
'Update an organization',
(yargs) =>
yargs
.positional('orgId', { type: 'string', demandOption: true, describe: 'Organization ID' })
.positional('name', { type: 'string', demandOption: true, describe: 'Organization name' })
.positional('domain', { type: 'string', describe: 'Domain' })
.positional('state', { type: 'string', describe: 'Domain state (verified or pending)' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runOrgUpdate } = await import('./commands/organization.js');
const apiKey = resolveApiKey({ apiKey: argv.apiKey });
await runOrgUpdate(argv.orgId, argv.name, apiKey, argv.domain, argv.state, resolveApiBaseUrl());
},
)
.command(
'get <orgId>',
'Get an organization by ID',
(yargs) => yargs.positional('orgId', { type: 'string', demandOption: true, describe: 'Organization ID' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runOrgGet } = await import('./commands/organization.js');
const apiKey = resolveApiKey({ apiKey: argv.apiKey });
await runOrgGet(argv.orgId, apiKey, resolveApiBaseUrl());
},
)
.command(
'list',
'List organizations',
(yargs) =>
yargs.options({
domain: { type: 'string', describe: 'Filter by domain' },
limit: { type: 'number', describe: 'Limit number of results' },
before: { type: 'string', describe: 'Cursor for results before a specific item' },
after: { type: 'string', describe: 'Cursor for results after a specific item' },
order: { type: 'string', describe: 'Order of results (asc or desc)' },
}),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runOrgList } = await import('./commands/organization.js');
const apiKey = resolveApiKey({ apiKey: argv.apiKey });
await runOrgList(
{ domain: argv.domain, limit: argv.limit, before: argv.before, after: argv.after, order: argv.order },
apiKey,
resolveApiBaseUrl(),
);
},
)
.command(
'delete <orgId>',
'Delete an organization',
(yargs) => yargs.positional('orgId', { type: 'string', demandOption: true, describe: 'Organization ID' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runOrgDelete } = await import('./commands/organization.js');
const apiKey = resolveApiKey({ apiKey: argv.apiKey });
await runOrgDelete(argv.orgId, apiKey, resolveApiBaseUrl());
},
)
.demandCommand(1, 'Please specify an organization subcommand')
.strict(),
)
.command('user', 'Manage users', (yargs) =>
yargs
.options({
...insecureStorageOption,
'api-key': { type: 'string' as const, describe: 'WorkOS API key (overrides environment config)' },
})
.command(
'get <userId>',
'Get a user by ID',
(yargs) => yargs.positional('userId', { type: 'string', demandOption: true, describe: 'User ID' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runUserGet } = await import('./commands/user.js');
await runUserGet(argv.userId, resolveApiKey({ apiKey: argv.apiKey }), resolveApiBaseUrl());
},
)
.command(
'list',
'List users',
(yargs) =>
yargs.options({
email: { type: 'string', describe: 'Filter by email' },
organization: { type: 'string', describe: 'Filter by organization ID' },
limit: { type: 'number', describe: 'Limit number of results' },
before: { type: 'string', describe: 'Cursor for results before a specific item' },
after: { type: 'string', describe: 'Cursor for results after a specific item' },
order: { type: 'string', describe: 'Order of results (asc or desc)' },
}),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runUserList } = await import('./commands/user.js');
await runUserList(
{
email: argv.email,
organization: argv.organization,
limit: argv.limit,
before: argv.before,
after: argv.after,
order: argv.order,
},
resolveApiKey({ apiKey: argv.apiKey }),
resolveApiBaseUrl(),
);
},
)
.command(
'update <userId>',
'Update a user',
(yargs) =>
yargs.positional('userId', { type: 'string', demandOption: true, describe: 'User ID' }).options({
'first-name': { type: 'string', describe: 'First name' },
'last-name': { type: 'string', describe: 'Last name' },
'email-verified': { type: 'boolean', describe: 'Email verification status' },
password: { type: 'string', describe: 'New password' },
'external-id': { type: 'string', describe: 'External ID' },
}),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runUserUpdate } = await import('./commands/user.js');
await runUserUpdate(
argv.userId,
resolveApiKey({ apiKey: argv.apiKey }),
{
firstName: argv.firstName,
lastName: argv.lastName,
emailVerified: argv.emailVerified,
password: argv.password,
externalId: argv.externalId,
},
resolveApiBaseUrl(),
);
},
)
.command(
'delete <userId>',
'Delete a user',
(yargs) => yargs.positional('userId', { type: 'string', demandOption: true, describe: 'User ID' }),
async (argv) => {
await applyInsecureStorage(argv.insecureStorage);
const { resolveApiKey, resolveApiBaseUrl } = await import('./lib/api-key.js');
const { runUserDelete } = await import('./commands/user.js');
await runUserDelete(argv.userId, resolveApiKey({ apiKey: argv.apiKey }), resolveApiBaseUrl());
},
)
.demandCommand(1, 'Please specify a user subcommand')
.strict(),
)
.command(
'install',
'Install WorkOS AuthKit into your project',
(yargs) => yargs.options(installerOptions),
withAuth(async (argv) => {
const { handleInstall } = await import('./commands/install.js');
await handleInstall(argv);
}),
)
.command(
'dashboard',
false, // hidden from help
(yargs) => yargs.options(installerOptions),
withAuth(async (argv) => {
const { handleInstall } = await import('./commands/install.js');
await handleInstall({ ...argv, dashboard: true });
}),
)
.command(
['$0'],
'WorkOS AuthKit CLI',
(yargs) => yargs.options(insecureStorageOption),
async (argv) => {
// Non-TTY: show help
if (isNonInteractiveEnvironment()) {
yargs(hideBin(process.argv)).showHelp();
return;
}
// TTY: ask if user wants to run installer
const shouldInstall = await clack.confirm({
message: 'Run the AuthKit installer?',
});
if (clack.isCancel(shouldInstall) || !shouldInstall) {
process.exit(0);
}
// Auth check happens HERE, after user confirms
await applyInsecureStorage(argv.insecureStorage);
await ensureAuthenticated();
const { handleInstall } = await import('./commands/install.js');
await handleInstall({ dashboard: false } as any);
process.exit(0);
},
)
.strict()
.help()
.alias('help', 'h')
.version(getVersion())
.alias('version', 'v')
.wrap(process.stdout.isTTY && process.stdout.columns ? process.stdout.columns : 80).argv;