-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlogin.ts
More file actions
402 lines (337 loc) · 12 KB
/
login.ts
File metadata and controls
402 lines (337 loc) · 12 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
import { intro, log, outro, select } from "@clack/prompts";
import { recordSpanException } from "@trigger.dev/core/v3/workers";
import { Command } from "commander";
import open from "open";
import pRetry, { AbortError } from "p-retry";
import { z } from "zod";
import { CliApiClient } from "../apiClient.js";
import {
CommonCommandOptions,
SkipLoggingError,
commonOptions,
handleTelemetry,
tracer,
wrapCommandAction,
} from "../cli/common.js";
import { chalkLink, prettyError } from "../utilities/cliOutput.js";
import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js";
import { printInitialBanner } from "../utilities/initialBanner.js";
import { LoginResult } from "../utilities/session.js";
import { whoAmI } from "./whoami.js";
import { logger } from "../utilities/logger.js";
import { spinner } from "../utilities/windows.js";
import { isLinuxServer } from "../utilities/linux.js";
import { VERSION } from "../version.js";
import { env, isCI } from "std-env";
import { CLOUD_API_URL } from "../consts.js";
import {
isPersonalAccessToken,
NotPersonalAccessTokenError,
} from "../utilities/isPersonalAccessToken.js";
import { links } from "@trigger.dev/core/v3";
export const LoginCommandOptions = CommonCommandOptions.extend({
apiUrl: z.string(),
});
export type LoginCommandOptions = z.infer<typeof LoginCommandOptions>;
export function configureLoginCommand(program: Command) {
return commonOptions(
program
.command("login")
.description("Login with Trigger.dev so you can perform authenticated actions")
)
.version(VERSION, "-v, --version", "Display the version number")
.action(async (options) => {
await handleTelemetry(async () => {
await printInitialBanner(false);
await loginCommand(options);
});
});
}
export async function loginCommand(options: unknown) {
return await wrapCommandAction("loginCommand", LoginCommandOptions, options, async (opts) => {
return await _loginCommand(opts);
});
}
async function _loginCommand(options: LoginCommandOptions) {
return login({ defaultApiUrl: options.apiUrl, embedded: false, profile: options.profile });
}
export type LoginOptions = {
defaultApiUrl?: string;
embedded?: boolean;
profile?: string;
silent?: boolean;
};
export async function login(options?: LoginOptions): Promise<LoginResult> {
return await tracer.startActiveSpan("login", async (span) => {
try {
const opts = {
defaultApiUrl: CLOUD_API_URL,
embedded: false,
silent: false,
...options,
};
span.setAttributes({
"cli.config.apiUrl": opts.defaultApiUrl,
"cli.options.profile": opts.profile,
});
if (!opts.embedded) {
intro("Logging in to Trigger.dev");
}
const accessTokenFromEnv = env.TRIGGER_ACCESS_TOKEN;
if (accessTokenFromEnv) {
if (!isPersonalAccessToken(accessTokenFromEnv)) {
throw new NotPersonalAccessTokenError(
"Your TRIGGER_ACCESS_TOKEN is not a Personal Access Token, they start with 'tr_pat_'. You can generate one here: https://cloud.trigger.dev/account/tokens"
);
}
const auth = {
accessToken: accessTokenFromEnv,
apiUrl: env.TRIGGER_API_URL ?? opts.defaultApiUrl ?? CLOUD_API_URL,
};
const apiClient = new CliApiClient(auth.apiUrl, auth.accessToken);
const userData = await apiClient.whoAmI();
if (!userData.success) {
throw new Error(userData.error);
}
return {
ok: true as const,
profile: options?.profile ?? "default",
userId: userData.data.userId,
email: userData.data.email,
dashboardUrl: userData.data.dashboardUrl,
auth: {
accessToken: auth.accessToken,
apiUrl: auth.apiUrl,
},
};
}
const authConfig = readAuthConfigProfile(options?.profile);
if (authConfig && authConfig.accessToken) {
const whoAmIResult = await whoAmI(
{
profile: options?.profile ?? "default",
skipTelemetry: !span.isRecording(),
logLevel: logger.loggerLevel,
},
true,
opts.silent
);
if (!whoAmIResult.success) {
prettyError("Unable to validate existing personal access token", whoAmIResult.error);
if (!opts.embedded) {
outro(
`Login failed using stored token. To fix, first logout using \`trigger.dev logout${
options?.profile ? ` --profile ${options.profile}` : ""
}\` and then try again.`
);
throw new SkipLoggingError(whoAmIResult.error);
} else {
throw new Error(whoAmIResult.error);
}
} else {
if (!opts.embedded) {
const continueOption = await select({
message: "You are already logged in.",
options: [
{
value: false,
label: "Exit",
},
{
value: true,
label: "Login with a different account",
},
],
initialValue: false,
});
if (continueOption !== true) {
outro("Already logged in");
span.setAttributes({
"cli.userId": whoAmIResult.data.userId,
"cli.email": whoAmIResult.data.email,
"cli.config.apiUrl": authConfig.apiUrl ?? opts.defaultApiUrl,
});
span.end();
return {
ok: true as const,
profile: options?.profile ?? "default",
userId: whoAmIResult.data.userId,
email: whoAmIResult.data.email,
dashboardUrl: whoAmIResult.data.dashboardUrl,
auth: {
accessToken: authConfig.accessToken,
apiUrl: authConfig.apiUrl ?? opts.defaultApiUrl,
},
};
}
} else {
span.setAttributes({
"cli.userId": whoAmIResult.data.userId,
"cli.email": whoAmIResult.data.email,
"cli.config.apiUrl": authConfig.apiUrl ?? opts.defaultApiUrl,
});
span.end();
return {
ok: true as const,
profile: options?.profile ?? "default",
userId: whoAmIResult.data.userId,
email: whoAmIResult.data.email,
dashboardUrl: whoAmIResult.data.dashboardUrl,
auth: {
accessToken: authConfig.accessToken,
apiUrl: authConfig.apiUrl ?? opts.defaultApiUrl,
},
};
}
}
}
if (isCI) {
const apiUrl =
env.TRIGGER_API_URL ?? authConfig?.apiUrl ?? opts.defaultApiUrl ?? CLOUD_API_URL;
const isSelfHosted = apiUrl !== CLOUD_API_URL;
// This is fine, as the api URL will generally be the same as the dashboard URL for self-hosted instances
const dashboardUrl = isSelfHosted ? apiUrl : "https://cloud.trigger.dev";
throw new Error(
`Authentication required in CI environment. Please set the TRIGGER_ACCESS_TOKEN environment variable with a Personal Access Token.
- You can generate one here: ${dashboardUrl}/account/tokens
- For more information, see: ${links.docs.gitHubActions.personalAccessToken}`
);
}
if (opts.embedded) {
log.step("You must login to continue.");
}
const apiClient = new CliApiClient(authConfig?.apiUrl ?? opts.defaultApiUrl);
//generate authorization code
const authorizationCodeResult = await createAuthorizationCode(apiClient);
//Link the user to the authorization code
log.step(
`Please visit the following URL to login:\n${chalkLink(authorizationCodeResult.url)}`
);
if (await isLinuxServer()) {
log.message("Please install `xdg-utils` to automatically open the login URL.");
} else {
await open(authorizationCodeResult.url);
}
//poll for personal access token (we need to poll for it)
const getPersonalAccessTokenSpinner = spinner();
getPersonalAccessTokenSpinner.start("Waiting for you to login");
try {
const indexResult = await pRetry(
() => getPersonalAccessToken(apiClient, authorizationCodeResult.authorizationCode),
{
//this means we're polling, same distance between each attempt
factor: 1,
retries: 60,
minTimeout: 1000,
}
);
getPersonalAccessTokenSpinner.stop(`Logged in with token ${indexResult.obfuscatedToken}`);
writeAuthConfigProfile(
{ accessToken: indexResult.token, apiUrl: opts.defaultApiUrl },
options?.profile
);
const whoAmIResult = await whoAmI(
{
profile: options?.profile ?? "default",
skipTelemetry: !span.isRecording(),
logLevel: logger.loggerLevel,
},
opts.embedded
);
if (!whoAmIResult.success) {
throw new Error(whoAmIResult.error);
}
if (opts.embedded) {
log.step("Logged in successfully");
} else {
outro("Logged in successfully");
}
span.end();
return {
ok: true as const,
profile: options?.profile ?? "default",
userId: whoAmIResult.data.userId,
email: whoAmIResult.data.email,
dashboardUrl: whoAmIResult.data.dashboardUrl,
auth: {
accessToken: indexResult.token,
apiUrl: authConfig?.apiUrl ?? opts.defaultApiUrl,
},
};
} catch (e) {
getPersonalAccessTokenSpinner.stop(`Failed to get access token`);
if (e instanceof AbortError) {
log.error(e.message);
}
recordSpanException(span, e);
span.end();
return {
ok: false as const,
error: e instanceof Error ? e.message : String(e),
};
}
} catch (e) {
recordSpanException(span, e);
span.end();
if (options?.embedded) {
if (e instanceof NotPersonalAccessTokenError) {
throw e;
}
return {
ok: false as const,
error: e instanceof Error ? e.message : String(e),
};
}
throw e;
}
});
}
async function getPersonalAccessToken(apiClient: CliApiClient, authorizationCode: string) {
return await tracer.startActiveSpan("getPersonalAccessToken", async (span) => {
try {
const token = await apiClient.getPersonalAccessToken(authorizationCode);
if (!token.success) {
throw new AbortError(token.error);
}
if (!token.data.token) {
throw new Error("No token found yet");
}
span.end();
return {
token: token.data.token.token,
obfuscatedToken: token.data.token.obfuscatedToken,
};
} catch (e) {
if (e instanceof AbortError) {
recordSpanException(span, e);
}
span.end();
throw e;
}
});
}
async function createAuthorizationCode(apiClient: CliApiClient) {
return await tracer.startActiveSpan("createAuthorizationCode", async (span) => {
try {
//generate authorization code
const createAuthCodeSpinner = spinner();
createAuthCodeSpinner.start("Creating authorization code");
const authorizationCodeResult = await apiClient.createAuthorizationCode();
if (!authorizationCodeResult.success) {
createAuthCodeSpinner.stop(
`Failed to create authorization code\n${authorizationCodeResult.error}`
);
throw new SkipLoggingError(
`Failed to create authorization code\n${authorizationCodeResult.error}`
);
}
createAuthCodeSpinner.stop("Created authorization code");
span.end();
return authorizationCodeResult.data;
} catch (e) {
recordSpanException(span, e);
span.end();
throw e;
}
});
}