@argos-ci/cli 6.3.0 → 6.4.1
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/index.mjs +194 -40
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -6,8 +6,8 @@ import { deploy, finalize, skip, upload } from "@argos-ci/core";
|
|
|
6
6
|
import ora from "ora";
|
|
7
7
|
import { APIError, createClient, throwAPIError } from "@argos-ci/api-client";
|
|
8
8
|
import { homedir } from "node:os";
|
|
9
|
-
import { createServer } from "node:http";
|
|
10
9
|
import { createHash, randomBytes } from "node:crypto";
|
|
10
|
+
import { createServer } from "node:http";
|
|
11
11
|
import open from "open";
|
|
12
12
|
//#region src/options.ts
|
|
13
13
|
const parallelNonceOption = new Option("--parallel-nonce <string>", "A unique ID for this parallel build");
|
|
@@ -368,6 +368,118 @@ function fail(message) {
|
|
|
368
368
|
throw new CliError(message);
|
|
369
369
|
}
|
|
370
370
|
//#endregion
|
|
371
|
+
//#region src/lib/oauth.ts
|
|
372
|
+
/**
|
|
373
|
+
* OAuth 2.1 client configuration and helpers for the `argos login` flow
|
|
374
|
+
* (Authorization Code + PKCE with a loopback redirect, RFC 8252).
|
|
375
|
+
*
|
|
376
|
+
* The authorization server is the Argos app origin; endpoints and token
|
|
377
|
+
* validation are implemented in the main Argos repo (`apps/backend/src/oauth`).
|
|
378
|
+
*/
|
|
379
|
+
/** First-party public client id, seeded in the Argos database. */
|
|
380
|
+
const OAUTH_CLIENT_ID = "argos-cli";
|
|
381
|
+
/**
|
|
382
|
+
* Scopes requested by `argos login`, covering the CLI's user actions (identity,
|
|
383
|
+
* projects, reviews, comments). Build upload keeps using a project token.
|
|
384
|
+
*/
|
|
385
|
+
const OAUTH_SCOPES = [
|
|
386
|
+
"profile",
|
|
387
|
+
"projects:read",
|
|
388
|
+
"projects:write",
|
|
389
|
+
"reviews:write",
|
|
390
|
+
"comments:read",
|
|
391
|
+
"comments:write"
|
|
392
|
+
];
|
|
393
|
+
/** Refresh a bit early so a token never expires mid-request. */
|
|
394
|
+
const EXPIRY_SKEW_MS = 60 * 1e3;
|
|
395
|
+
function getAppBaseUrl() {
|
|
396
|
+
return process.env["ARGOS_APP_BASE_URL"] ?? "https://app.argos-ci.com/";
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* A token endpoint request that reached the server and was rejected by it
|
|
400
|
+
* (non-2xx / OAuth error), as opposed to a transient network failure. Lets
|
|
401
|
+
* callers tell "your session is gone, log in again" from "the network is down".
|
|
402
|
+
*/
|
|
403
|
+
var OAuthTokenError = class extends Error {
|
|
404
|
+
code;
|
|
405
|
+
constructor(message, code) {
|
|
406
|
+
super(message);
|
|
407
|
+
this.code = code;
|
|
408
|
+
this.name = "OAuthTokenError";
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
function toTokenSet(data) {
|
|
412
|
+
return {
|
|
413
|
+
accessToken: data.access_token,
|
|
414
|
+
refreshToken: data.refresh_token,
|
|
415
|
+
expiresAt: Date.now() + data.expires_in * 1e3 - EXPIRY_SKEW_MS,
|
|
416
|
+
scope: data.scope ?? ""
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/** Generate a PKCE `code_verifier` and its S256 `code_challenge`. */
|
|
420
|
+
function generatePkce() {
|
|
421
|
+
const codeVerifier = randomBytes(32).toString("base64url");
|
|
422
|
+
return {
|
|
423
|
+
codeVerifier,
|
|
424
|
+
codeChallenge: createHash("sha256").update(codeVerifier).digest("base64url")
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/** Build the `/oauth/authorize` URL to open in the browser. */
|
|
428
|
+
function buildAuthorizeUrl(params) {
|
|
429
|
+
const url = new URL("/oauth/authorize", getAppBaseUrl());
|
|
430
|
+
url.searchParams.set("response_type", "code");
|
|
431
|
+
url.searchParams.set("client_id", OAUTH_CLIENT_ID);
|
|
432
|
+
url.searchParams.set("redirect_uri", params.redirectUri);
|
|
433
|
+
url.searchParams.set("scope", OAUTH_SCOPES.join(" "));
|
|
434
|
+
url.searchParams.set("state", params.state);
|
|
435
|
+
url.searchParams.set("code_challenge", params.codeChallenge);
|
|
436
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
437
|
+
return url.href;
|
|
438
|
+
}
|
|
439
|
+
async function postToken(body) {
|
|
440
|
+
const url = new URL("/oauth/token", getAppBaseUrl());
|
|
441
|
+
const response = await fetch(url, {
|
|
442
|
+
method: "POST",
|
|
443
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
444
|
+
body: new URLSearchParams(body).toString()
|
|
445
|
+
});
|
|
446
|
+
const data = await response.json().catch(() => null);
|
|
447
|
+
if (!response.ok || !data?.access_token) throw new OAuthTokenError(data?.error_description ?? data?.error ?? `HTTP ${response.status}`, data?.error);
|
|
448
|
+
if (typeof data.refresh_token !== "string" || data.refresh_token === "") throw new Error("Token endpoint response is missing a refresh token.");
|
|
449
|
+
if (typeof data.expires_in !== "number" || !Number.isFinite(data.expires_in)) throw new Error("Token endpoint response is missing a valid expiry.");
|
|
450
|
+
return data;
|
|
451
|
+
}
|
|
452
|
+
/** Exchange an authorization code for a token set (`grant_type=authorization_code`). */
|
|
453
|
+
async function exchangeAuthorizationCode(params) {
|
|
454
|
+
return toTokenSet(await postToken({
|
|
455
|
+
grant_type: "authorization_code",
|
|
456
|
+
code: params.code,
|
|
457
|
+
redirect_uri: params.redirectUri,
|
|
458
|
+
client_id: OAUTH_CLIENT_ID,
|
|
459
|
+
code_verifier: params.codeVerifier
|
|
460
|
+
}));
|
|
461
|
+
}
|
|
462
|
+
/** Rotate a refresh token for a new token set (`grant_type=refresh_token`). */
|
|
463
|
+
async function refreshTokenSet(refreshToken) {
|
|
464
|
+
return toTokenSet(await postToken({
|
|
465
|
+
grant_type: "refresh_token",
|
|
466
|
+
refresh_token: refreshToken,
|
|
467
|
+
client_id: OAUTH_CLIENT_ID
|
|
468
|
+
}));
|
|
469
|
+
}
|
|
470
|
+
/** Best-effort token revocation (RFC 7009); never throws. */
|
|
471
|
+
async function revokeToken(token) {
|
|
472
|
+
const url = new URL("/oauth/revoke", getAppBaseUrl());
|
|
473
|
+
await fetch(url, {
|
|
474
|
+
method: "POST",
|
|
475
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
476
|
+
body: new URLSearchParams({
|
|
477
|
+
token,
|
|
478
|
+
client_id: OAUTH_CLIENT_ID
|
|
479
|
+
}).toString()
|
|
480
|
+
}).catch(() => {});
|
|
481
|
+
}
|
|
482
|
+
//#endregion
|
|
371
483
|
//#region src/auth.ts
|
|
372
484
|
function getConfigPaths() {
|
|
373
485
|
const configDir = resolve(homedir(), ".config", "argos-ci");
|
|
@@ -376,6 +488,16 @@ function getConfigPaths() {
|
|
|
376
488
|
configPath: resolve(configDir, "config.json")
|
|
377
489
|
};
|
|
378
490
|
}
|
|
491
|
+
function parseOAuthTokenSet(value) {
|
|
492
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return;
|
|
493
|
+
const record = value;
|
|
494
|
+
if (typeof record.accessToken === "string" && typeof record.refreshToken === "string" && typeof record.expiresAt === "number") return {
|
|
495
|
+
accessToken: record.accessToken,
|
|
496
|
+
refreshToken: record.refreshToken,
|
|
497
|
+
expiresAt: record.expiresAt,
|
|
498
|
+
scope: typeof record.scope === "string" ? record.scope : ""
|
|
499
|
+
};
|
|
500
|
+
}
|
|
379
501
|
function parseConfig(raw) {
|
|
380
502
|
let parsed;
|
|
381
503
|
try {
|
|
@@ -385,9 +507,12 @@ function parseConfig(raw) {
|
|
|
385
507
|
}
|
|
386
508
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
387
509
|
const record = parsed;
|
|
388
|
-
if (
|
|
389
|
-
|
|
390
|
-
|
|
510
|
+
if ("token" in record && record.token !== void 0 && typeof record.token !== "string") return null;
|
|
511
|
+
const config = {};
|
|
512
|
+
if (typeof record.token === "string") config.token = record.token;
|
|
513
|
+
const oauth = parseOAuthTokenSet(record.oauth);
|
|
514
|
+
if (oauth) config.oauth = oauth;
|
|
515
|
+
return config;
|
|
391
516
|
}
|
|
392
517
|
async function clearConfig() {
|
|
393
518
|
const { configPath } = getConfigPaths();
|
|
@@ -417,16 +542,43 @@ async function writeConfig(config) {
|
|
|
417
542
|
await writeFile(tmpPath, JSON.stringify(config, null, 2), { mode: 384 });
|
|
418
543
|
await rename(tmpPath, configPath);
|
|
419
544
|
}
|
|
420
|
-
|
|
421
|
-
|
|
545
|
+
/** Persist the OAuth token set from a successful login or refresh. */
|
|
546
|
+
async function saveOAuthTokens(tokens) {
|
|
547
|
+
await writeConfig({ oauth: tokens });
|
|
422
548
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
549
|
+
/**
|
|
550
|
+
* Return a valid access token for API calls, or `undefined` when not logged in.
|
|
551
|
+
* Transparently refreshes (and persists) an expired OAuth access token, and
|
|
552
|
+
* falls back to a legacy personal access token when present.
|
|
553
|
+
*/
|
|
554
|
+
async function getAccessToken() {
|
|
555
|
+
const config = await readConfig();
|
|
556
|
+
if (!config) return;
|
|
557
|
+
if (config.oauth) {
|
|
558
|
+
if (Date.now() < config.oauth.expiresAt) return config.oauth.accessToken;
|
|
559
|
+
try {
|
|
560
|
+
const tokens = await refreshTokenSet(config.oauth.refreshToken);
|
|
561
|
+
await saveOAuthTokens(tokens);
|
|
562
|
+
return tokens.accessToken;
|
|
563
|
+
} catch (err) {
|
|
564
|
+
if (config.token) return config.token;
|
|
565
|
+
if (err instanceof OAuthTokenError) console.warn("Warning: Your Argos session has expired. Run `argos login` again.");
|
|
566
|
+
else console.warn("Warning: Could not reach Argos to refresh your session. Check your connection and try again.");
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return config.token;
|
|
428
571
|
}
|
|
429
|
-
|
|
572
|
+
/** Read the stored credentials without refreshing (used by `logout`). */
|
|
573
|
+
async function getStoredCredentials() {
|
|
574
|
+
const config = await readConfig();
|
|
575
|
+
return {
|
|
576
|
+
legacyToken: config?.token,
|
|
577
|
+
oauth: config?.oauth
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
/** Remove all stored credentials. */
|
|
581
|
+
async function clearStoredCredentials() {
|
|
430
582
|
await writeConfig({});
|
|
431
583
|
}
|
|
432
584
|
//#endregion
|
|
@@ -485,7 +637,7 @@ function parseProjectPathOrFail(slug) {
|
|
|
485
637
|
* `ARGOS_TOKEN`) over the one stored by `argos login`.
|
|
486
638
|
*/
|
|
487
639
|
async function resolveToken(options) {
|
|
488
|
-
const token = options.token || process.env["ARGOS_TOKEN"] || await
|
|
640
|
+
const token = options.token || process.env["ARGOS_TOKEN"] || await getAccessToken();
|
|
489
641
|
if (!token) fail("No Argos token found. Use --token, set ARGOS_TOKEN, or run `argos login`.");
|
|
490
642
|
return token;
|
|
491
643
|
}
|
|
@@ -1004,8 +1156,6 @@ function commentCommand(program) {
|
|
|
1004
1156
|
}
|
|
1005
1157
|
//#endregion
|
|
1006
1158
|
//#region src/commands/login.ts
|
|
1007
|
-
const APP_BASE_URL = process.env["ARGOS_APP_BASE_URL"] ?? "https://app.argos-ci.com/";
|
|
1008
|
-
const API_BASE_URL = process.env["ARGOS_API_BASE_URL"] ?? "https://api.argos-ci.com/v2/";
|
|
1009
1159
|
const LOGIN_CLI_SUCCESS_ROUTE = `/auth/cli/success`;
|
|
1010
1160
|
const LOGIN_TIMEOUT_MS = 300 * 1e3;
|
|
1011
1161
|
const CALLBACK_ERROR_HTML = `<!DOCTYPE html><html lang="en">
|
|
@@ -1017,13 +1167,13 @@ const CALLBACK_ERROR_HTML = `<!DOCTYPE html><html lang="en">
|
|
|
1017
1167
|
<p>Authorization failed. Please run <code>argos login</code> again.</p>
|
|
1018
1168
|
</body>
|
|
1019
1169
|
</html>`;
|
|
1020
|
-
function color(text, code) {
|
|
1021
|
-
if (!
|
|
1022
|
-
return `\
|
|
1170
|
+
function color(text, code, isTTY) {
|
|
1171
|
+
if (!isTTY || process.env["NO_COLOR"]) return text;
|
|
1172
|
+
return `\x1b[${code}m${text}\x1b[0m`;
|
|
1023
1173
|
}
|
|
1024
|
-
const successColor = (text) => color(text, 32);
|
|
1025
|
-
const warningColor = (text) => color(text, 33);
|
|
1026
|
-
const errorColor = (text) => color(text, 31);
|
|
1174
|
+
const successColor = (text) => color(text, 32, process.stdout.isTTY);
|
|
1175
|
+
const warningColor = (text) => color(text, 33, process.stderr.isTTY);
|
|
1176
|
+
const errorColor = (text) => color(text, 31, process.stderr.isTTY);
|
|
1027
1177
|
function startCallbackServer() {
|
|
1028
1178
|
return new Promise((resolve, reject) => {
|
|
1029
1179
|
let resolveCallback = () => {};
|
|
@@ -1039,7 +1189,7 @@ function startCallbackServer() {
|
|
|
1039
1189
|
server.closeAllConnections();
|
|
1040
1190
|
};
|
|
1041
1191
|
const server = createServer((req, res) => {
|
|
1042
|
-
const url = new URL(req.url ?? "/", "http://
|
|
1192
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
1043
1193
|
if (url.pathname !== "/callback") {
|
|
1044
1194
|
res.writeHead(404, { Connection: "close" });
|
|
1045
1195
|
res.end();
|
|
@@ -1073,7 +1223,7 @@ function startCallbackServer() {
|
|
|
1073
1223
|
return;
|
|
1074
1224
|
}
|
|
1075
1225
|
res.writeHead(302, {
|
|
1076
|
-
Location: new URL(LOGIN_CLI_SUCCESS_ROUTE,
|
|
1226
|
+
Location: new URL(LOGIN_CLI_SUCCESS_ROUTE, getAppBaseUrl()).href,
|
|
1077
1227
|
Connection: "close"
|
|
1078
1228
|
});
|
|
1079
1229
|
res.end(() => {
|
|
@@ -1102,8 +1252,7 @@ function startCallbackServer() {
|
|
|
1102
1252
|
function loginCommand(program) {
|
|
1103
1253
|
program.command("login").description("Log in to Argos by opening your browser").action(async () => {
|
|
1104
1254
|
const state = randomBytes(16).toString("hex");
|
|
1105
|
-
const codeVerifier =
|
|
1106
|
-
const codeChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
|
|
1255
|
+
const { codeVerifier, codeChallenge } = generatePkce();
|
|
1107
1256
|
let port;
|
|
1108
1257
|
let waitForCallback;
|
|
1109
1258
|
try {
|
|
@@ -1113,13 +1262,15 @@ function loginCommand(program) {
|
|
|
1113
1262
|
console.error(err);
|
|
1114
1263
|
process.exit(1);
|
|
1115
1264
|
}
|
|
1116
|
-
const
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1265
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`;
|
|
1266
|
+
const authorizeUrl = buildAuthorizeUrl({
|
|
1267
|
+
redirectUri,
|
|
1268
|
+
state,
|
|
1269
|
+
codeChallenge
|
|
1270
|
+
});
|
|
1120
1271
|
console.log("\nOpening browser for authentication…");
|
|
1121
|
-
console.log(`If the browser doesn't open, visit:\n ${
|
|
1122
|
-
if (process.env["ARGOS_CLI_DISABLE_BROWSER"] !== "1") await open(
|
|
1272
|
+
console.log(`If the browser doesn't open, visit:\n ${authorizeUrl}\n`);
|
|
1273
|
+
if (process.env["ARGOS_CLI_DISABLE_BROWSER"] !== "1") await open(authorizeUrl).catch((err) => {
|
|
1123
1274
|
console.warn(warningColor(`Warning: Failed to open browser — ${err instanceof Error ? err.message : String(err)}`));
|
|
1124
1275
|
console.log("Hint: Open the URL above manually to continue authentication.");
|
|
1125
1276
|
});
|
|
@@ -1134,15 +1285,16 @@ function loginCommand(program) {
|
|
|
1134
1285
|
console.error(errorColor("Error: State mismatch. Try logging in again."));
|
|
1135
1286
|
process.exit(1);
|
|
1136
1287
|
}
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1288
|
+
try {
|
|
1289
|
+
await saveOAuthTokens(await exchangeAuthorizationCode({
|
|
1290
|
+
code: result.code,
|
|
1291
|
+
codeVerifier,
|
|
1292
|
+
redirectUri
|
|
1293
|
+
}));
|
|
1294
|
+
} catch (err) {
|
|
1295
|
+
console.error(errorColor(`Error: Authentication failed${err instanceof Error ? ` — ${err.message}` : ""}. Please run \`argos login\` again.`));
|
|
1143
1296
|
process.exit(1);
|
|
1144
1297
|
}
|
|
1145
|
-
await saveToken(data.token);
|
|
1146
1298
|
console.log(successColor("Logged in to Argos successfully."));
|
|
1147
1299
|
});
|
|
1148
1300
|
}
|
|
@@ -1150,11 +1302,13 @@ function loginCommand(program) {
|
|
|
1150
1302
|
//#region src/commands/logout.ts
|
|
1151
1303
|
function logoutCommand(program) {
|
|
1152
1304
|
program.command("logout").description("Log out from Argos").action(async () => {
|
|
1153
|
-
|
|
1305
|
+
const { legacyToken, oauth } = await getStoredCredentials();
|
|
1306
|
+
if (!legacyToken && !oauth) {
|
|
1154
1307
|
console.log("No token found. You are already logged out.");
|
|
1155
1308
|
return;
|
|
1156
1309
|
}
|
|
1157
|
-
await
|
|
1310
|
+
if (oauth) await revokeToken(oauth.refreshToken);
|
|
1311
|
+
await clearStoredCredentials();
|
|
1158
1312
|
console.log("Logged out from Argos.");
|
|
1159
1313
|
});
|
|
1160
1314
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@argos-ci/cli",
|
|
3
3
|
"description": "Command-line (CLI) for visual testing with Argos.",
|
|
4
|
-
"version": "6.
|
|
4
|
+
"version": "6.4.1",
|
|
5
5
|
"bin": {
|
|
6
6
|
"argos": "./bin/argos-cli.js"
|
|
7
7
|
},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@argos-ci/api-client": "0.26.0",
|
|
38
|
-
"@argos-ci/core": "6.6.
|
|
38
|
+
"@argos-ci/core": "6.6.2",
|
|
39
39
|
"commander": "^15.0.0",
|
|
40
40
|
"open": "^11.0.0",
|
|
41
41
|
"ora": "^9.4.0",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"check-format": "prettier --check --ignore-unknown --ignore-path=../../.gitignore --ignore-path=../../.prettierignore .",
|
|
55
55
|
"lint": "eslint ."
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "a86f2dc1a243591e064a2d5c4e03d712c8f03b86"
|
|
58
58
|
}
|