@ant0/cli 0.0.0-stage → 0.3.22-beta.3
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/LICENSE +28 -0
- package/README.md +97 -2
- package/dist/api/generated/schema.d.ts +11600 -0
- package/dist/api/generated/schema.js +6 -0
- package/dist/api/index.d.ts +219 -0
- package/dist/api/index.js +342 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.js +12 -0
- package/dist/bulk.d.ts +38 -0
- package/dist/bulk.js +66 -0
- package/dist/commands/account.d.ts +48 -0
- package/dist/commands/account.js +337 -0
- package/dist/commands/api-keys.d.ts +5 -0
- package/dist/commands/api-keys.js +310 -0
- package/dist/commands/browsers.d.ts +4 -0
- package/dist/commands/browsers.js +165 -0
- package/dist/commands/daemon.d.ts +15 -0
- package/dist/commands/daemon.js +202 -0
- package/dist/commands/events.d.ts +8 -0
- package/dist/commands/events.js +81 -0
- package/dist/commands/extensions.d.ts +4 -0
- package/dist/commands/extensions.js +74 -0
- package/dist/commands/profiles.d.ts +36 -0
- package/dist/commands/profiles.js +552 -0
- package/dist/commands/proxies.d.ts +23 -0
- package/dist/commands/proxies.js +170 -0
- package/dist/commands/sync.d.ts +4 -0
- package/dist/commands/sync.js +435 -0
- package/dist/commands/team.d.ts +7 -0
- package/dist/commands/team.js +403 -0
- package/dist/commands/trash.d.ts +10 -0
- package/dist/commands/trash.js +54 -0
- package/dist/commands/workspace.d.ts +10 -0
- package/dist/commands/workspace.js +56 -0
- package/dist/context.d.ts +111 -0
- package/dist/context.js +300 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.js +96 -0
- package/dist/format.d.ts +23 -0
- package/dist/format.js +85 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +35 -0
- package/dist/inventory.d.ts +50 -0
- package/dist/inventory.js +202 -0
- package/dist/management-transport.d.ts +3 -0
- package/dist/management-transport.js +90 -0
- package/dist/native-terminal.d.ts +50 -0
- package/dist/native-terminal.js +228 -0
- package/dist/program.d.ts +10 -0
- package/dist/program.js +185 -0
- package/dist/runtime-types.d.ts +20 -0
- package/dist/runtime-types.js +2 -0
- package/dist/set.d.ts +15 -0
- package/dist/set.js +87 -0
- package/dist/shared/parse-error.d.ts +6 -0
- package/dist/shared/parse-error.js +8 -0
- package/dist/shared/proxy-parse.d.ts +42 -0
- package/dist/shared/proxy-parse.js +238 -0
- package/dist/shared/proxy-types.d.ts +21 -0
- package/dist/shared/proxy-types.js +2 -0
- package/dist/shared/recovery-code.d.ts +7 -0
- package/dist/shared/recovery-code.js +53 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +18 -0
- package/licenses/README.md +5 -0
- package/licenses/THIRD_PARTY_NOTICES +96 -0
- package/package.json +63 -4
package/dist/bulk.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-ref commands report the daemon's bulk envelopes. Existing operations use
|
|
3
|
+
* `{ updated, errors: [{ id, error, message }] }`; endpoint-free bulk run uses `code` instead.
|
|
4
|
+
* Successes are counted, every failure is printed on its own line, and any failure exits 1.
|
|
5
|
+
*/
|
|
6
|
+
import { isAnt0ApiError } from "./api/index.js";
|
|
7
|
+
import { CliError } from "./errors.js";
|
|
8
|
+
export const BULK_MAX_IDS = 50;
|
|
9
|
+
/** Drop repeated refs while keeping the order of first appearance. */
|
|
10
|
+
export function uniqueRefs(refs) {
|
|
11
|
+
return [...new Set(refs)];
|
|
12
|
+
}
|
|
13
|
+
/** Turn a thrown value into the per-id error shape (API errors keep their code). */
|
|
14
|
+
export function toBulkError(id, err) {
|
|
15
|
+
if (isAnt0ApiError(err))
|
|
16
|
+
return { id, error: err.code, message: err.message };
|
|
17
|
+
return { id, error: "error", message: err instanceof Error ? err.message : String(err) };
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Run `fn` once per ref, sequentially (the daemon serializes launches anyway), and collect the
|
|
21
|
+
* outcome. Used for routes that have no bulk twin (delete, restore).
|
|
22
|
+
*/
|
|
23
|
+
export async function forEachRef(refs, fn) {
|
|
24
|
+
const results = [];
|
|
25
|
+
const errors = [];
|
|
26
|
+
for (const ref of refs) {
|
|
27
|
+
try {
|
|
28
|
+
results.push(await fn(ref));
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
errors.push(toBulkError(ref, err));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { updated: results.length, errors, results };
|
|
35
|
+
}
|
|
36
|
+
/** Human rendering: `<verb> N profile(s)` then one indented line per failure. */
|
|
37
|
+
export function bulkText(verb, result, extra = []) {
|
|
38
|
+
const lines = [`${verb} ${result.updated} profile(s)`, ...extra];
|
|
39
|
+
if (result.errors.length > 0)
|
|
40
|
+
lines.push(`failed ${result.errors.length}`);
|
|
41
|
+
for (const e of result.errors)
|
|
42
|
+
lines.push(` ${e.id}: ${e.error}: ${e.message}`);
|
|
43
|
+
return lines.join("\n");
|
|
44
|
+
}
|
|
45
|
+
/** Emit the result (verbatim with `--json`) and exit 1 when any id failed. */
|
|
46
|
+
export function finishBulk(ctx, verb, result, extra = []) {
|
|
47
|
+
ctx.emit({ updated: result.updated, errors: result.errors }, () => bulkText(verb, result, extra));
|
|
48
|
+
if (result.errors.length > 0) {
|
|
49
|
+
throw new CliError(`${result.errors.length} of ${result.updated + result.errors.length} failed`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Keep the new bulk-run wire envelope intact while retaining old `{ error }` bulk envelopes. */
|
|
53
|
+
export function finishCodedBulk(ctx, verb, result) {
|
|
54
|
+
ctx.emit(result, () => {
|
|
55
|
+
const lines = [`${verb} ${result.updated} profile(s)`];
|
|
56
|
+
if (result.errors.length > 0)
|
|
57
|
+
lines.push(`failed ${result.errors.length}`);
|
|
58
|
+
for (const e of result.errors)
|
|
59
|
+
lines.push(` ${e.id}: ${e.code}: ${e.message}`);
|
|
60
|
+
return lines.join("\n");
|
|
61
|
+
});
|
|
62
|
+
if (result.errors.length > 0) {
|
|
63
|
+
throw new CliError(`${result.errors.length} of ${result.updated + result.errors.length} failed`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=bulk.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ant0 login [email] [--totp <code>] [--password-stdin] | logout | account | licence refresh`
|
|
3
|
+
*
|
|
4
|
+
* The daemon is the account holder for CLI users: `login` forwards the credentials to its
|
|
5
|
+
* `POST /v1/licence/sign-in`, which signs in at the cloud, registers this device and fetches the
|
|
6
|
+
* first licence. The password is read from the terminal without echo (or from stdin with
|
|
7
|
+
* `--password-stdin` for scripts); a 401 `auth.totp_required` on a terminal asks for the code and
|
|
8
|
+
* retries once. `account` and `licence refresh` print `GET /v1/licence` / `POST /v1/licence/refresh`
|
|
9
|
+
* as key/value lines (`--json`: the payload verbatim). Cloud codes become plain sentences
|
|
10
|
+
* (`describeAccountFailure`); everything exits 1 like any other API error, 3 without a daemon.
|
|
11
|
+
*/
|
|
12
|
+
import type { Command } from "commander";
|
|
13
|
+
import { type components } from "../api/index.js";
|
|
14
|
+
import type { Context } from "../context.js";
|
|
15
|
+
export type LicenceState = components["schemas"]["LicenceState"];
|
|
16
|
+
/** Plain sentences for the cloud / licence codes a CLI user can run into. */
|
|
17
|
+
export declare const ACCOUNT_MESSAGES: Readonly<Record<string, string>>;
|
|
18
|
+
/** One sentence for a failed account call; unknown codes keep the daemon's `code: message`. */
|
|
19
|
+
export declare function describeAccountFailure(err: unknown): string | undefined;
|
|
20
|
+
export declare function validateEmail(value: string): string;
|
|
21
|
+
/** A 6-digit TOTP (spaces tolerated) or an `xxxxx-xxxxx` recovery code, as the daemon accepts. */
|
|
22
|
+
export declare function normalizeSecondFactor(value: string): string;
|
|
23
|
+
/** The lines `ant0 account` prints. */
|
|
24
|
+
export declare function accountLines(s: LicenceState): string;
|
|
25
|
+
export declare function registerAccount(program: Command, ctx: Context): void;
|
|
26
|
+
/** Closed secret-free control DTO. The coordinator regenerates the public API client after the
|
|
27
|
+
* provider unit settles; no raw cloud response is ever passed through this boundary. */
|
|
28
|
+
export interface NativeLoginStatus {
|
|
29
|
+
protocol: "ant0-native-auth/v1";
|
|
30
|
+
status: "idle" | "awaiting_browser" | "install_pending" | "signed_in" | "denied" | "cancelled" | "expired" | "failed";
|
|
31
|
+
transaction_id: string | null;
|
|
32
|
+
expires_at: string | null;
|
|
33
|
+
verification_uri: "https://ant0.link/account/authorize" | null;
|
|
34
|
+
user_code: string | null;
|
|
35
|
+
account: {
|
|
36
|
+
id: string;
|
|
37
|
+
email: string;
|
|
38
|
+
} | null;
|
|
39
|
+
workspace: {
|
|
40
|
+
id: string;
|
|
41
|
+
name: string;
|
|
42
|
+
} | null;
|
|
43
|
+
error: string | null;
|
|
44
|
+
retry_after_s: number | null;
|
|
45
|
+
}
|
|
46
|
+
export declare function nativeStatusLines(status: NativeLoginStatus): string;
|
|
47
|
+
export declare const APP_INSTALLED_WARNING = "The Ant0 app is installed on this computer. Opening the app will ask you to sign in there and signs out this command-line session. If sync then asks for a key, use your recovery code.";
|
|
48
|
+
//# sourceMappingURL=account.d.ts.map
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { isAnt0ApiError, resolveHome, unwrap } from "../api/index.js";
|
|
5
|
+
import { CliError, UsageError } from "../errors.js";
|
|
6
|
+
import { keyValues } from "../format.js";
|
|
7
|
+
/** Plain sentences for the cloud / licence codes a CLI user can run into. */
|
|
8
|
+
export const ACCOUNT_MESSAGES = {
|
|
9
|
+
"auth.invalid_credentials": "wrong email or password",
|
|
10
|
+
"auth.totp_required": "this account uses two-factor authentication; pass --totp <code> (or a recovery code)",
|
|
11
|
+
"auth.totp_invalid": "the two-factor code is wrong or expired",
|
|
12
|
+
"auth.email_unverified": "confirm your email address first (a new code was sent); the desktop app or ant0.link can take it",
|
|
13
|
+
"auth.rate_limited": "too many attempts; wait 15 minutes and try again",
|
|
14
|
+
"auth.account_suspended": "this account is suspended — contact support",
|
|
15
|
+
"device.limit": "no free device slot on this account; remove a computer in the desktop app (Settings › Account) and retry",
|
|
16
|
+
"device.free_used": "this computer already carries a Free workspace; sign in to that account",
|
|
17
|
+
"device.kx_conflict": "this computer's sync keys differ from the ones registered with your account. Run `ant0 login` once more (this computer's device id may just have been updated). If it fails again, open the Ant0 app on this computer and sign in there, or use your sync recovery code if the keys were lost",
|
|
18
|
+
"device.unknown": "this computer is not registered with the account; sign in again",
|
|
19
|
+
"device.fingerprint_unavailable": "cannot identify this computer (fewer than two hardware identifiers readable); licensing is unavailable here",
|
|
20
|
+
"licence.required": "sign in first (`ant0 login`)",
|
|
21
|
+
"licence.offline": "the Ant0 cloud is unreachable right now; try again later",
|
|
22
|
+
"licence.profile_limit": "the plan's profile limit is reached",
|
|
23
|
+
"workspace.suspended": "account suspended — contact support",
|
|
24
|
+
"workspace.lapsed": "the subscription has lapsed; renew at ant0.link",
|
|
25
|
+
};
|
|
26
|
+
/** One sentence for a failed account call; unknown codes keep the daemon's `code: message`. */
|
|
27
|
+
export function describeAccountFailure(err) {
|
|
28
|
+
if (!isAnt0ApiError(err))
|
|
29
|
+
return undefined;
|
|
30
|
+
const known = ACCOUNT_MESSAGES[err.code];
|
|
31
|
+
if (known !== undefined)
|
|
32
|
+
return known;
|
|
33
|
+
if (err.status === 400)
|
|
34
|
+
return undefined; // validation details are more useful verbatim
|
|
35
|
+
return `${err.code}: ${err.message}`;
|
|
36
|
+
}
|
|
37
|
+
/** Wrap a cloud failure into a `CliError` with the plain sentence; rethrow anything else. */
|
|
38
|
+
function accountError(err) {
|
|
39
|
+
const sentence = describeAccountFailure(err);
|
|
40
|
+
if (sentence !== undefined)
|
|
41
|
+
throw new CliError(sentence);
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
45
|
+
export function validateEmail(value) {
|
|
46
|
+
const email = value.trim();
|
|
47
|
+
if (email.length === 0 || email.length > 254 || !EMAIL.test(email)) {
|
|
48
|
+
throw new UsageError(`not an email address: ${JSON.stringify(value)}`);
|
|
49
|
+
}
|
|
50
|
+
return email;
|
|
51
|
+
}
|
|
52
|
+
/** A 6-digit TOTP (spaces tolerated) or an `xxxxx-xxxxx` recovery code, as the daemon accepts. */
|
|
53
|
+
export function normalizeSecondFactor(value) {
|
|
54
|
+
const v = value.trim();
|
|
55
|
+
const digits = v.replace(/\s+/g, "");
|
|
56
|
+
if (/^\d{6}$/.test(digits))
|
|
57
|
+
return digits;
|
|
58
|
+
if (/^[A-Za-z0-9-]{8,16}$/.test(v))
|
|
59
|
+
return v;
|
|
60
|
+
throw new UsageError("--totp expects the 6-digit code from your authenticator or a recovery code");
|
|
61
|
+
}
|
|
62
|
+
/** The lines `ant0 account` prints. */
|
|
63
|
+
export function accountLines(s) {
|
|
64
|
+
// `used` counts claims other devices hold too; name this device's share
|
|
65
|
+
// when the two differ.
|
|
66
|
+
const local = s.used_local !== s.used ? ` (${s.used_local} on this device)` : "";
|
|
67
|
+
const used = s.limits === null
|
|
68
|
+
? `${s.used}${local}`
|
|
69
|
+
: `${s.used} of ${s.limits.profiles} (${s.plan ?? "-"})${local}`;
|
|
70
|
+
const pairs = [
|
|
71
|
+
["state", s.state],
|
|
72
|
+
["plan", s.plan],
|
|
73
|
+
["profiles", used],
|
|
74
|
+
["seats", s.limits?.seats ?? null],
|
|
75
|
+
["plan status", s.billing],
|
|
76
|
+
// `exp` is the licence lease (about the last refresh plus a day), not the end of the plan.
|
|
77
|
+
["licence refreshes by", s.expires_at],
|
|
78
|
+
["grace until", s.grace_until],
|
|
79
|
+
["device", s.device_id],
|
|
80
|
+
["workspace", s.workspace ? `${s.workspace.name ?? "-"} (${s.workspace.id})` : null],
|
|
81
|
+
["online", s.online],
|
|
82
|
+
["last refresh", s.last_refresh_at],
|
|
83
|
+
["last error", s.last_error],
|
|
84
|
+
];
|
|
85
|
+
if (s.mode === "off")
|
|
86
|
+
pairs.push(["mode", "off (nothing is enforced)"]);
|
|
87
|
+
if (s.keep.profile_ids.length > 0) {
|
|
88
|
+
pairs.push([
|
|
89
|
+
"kept profiles",
|
|
90
|
+
`${s.keep.profile_ids.length}${s.keep.explicit ? "" : " (default pick)"}`,
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
const body = keyValues(pairs);
|
|
94
|
+
return s.message ? `${body}\n${s.message}` : body;
|
|
95
|
+
}
|
|
96
|
+
export function registerAccount(program, ctx) {
|
|
97
|
+
program
|
|
98
|
+
.command("login")
|
|
99
|
+
.description("Sign in to your Ant0 account on this computer (the daemon holds the session). If the Ant0 app is installed here, sign in from the app instead")
|
|
100
|
+
.argument("[email-or-action]", "legacy account email, or status/cancel")
|
|
101
|
+
.option("--browser", "approve sign-in in the system browser (default)")
|
|
102
|
+
.option("--device-code", "approve sign-in with a code on another computer")
|
|
103
|
+
.option("--totp <code>", "6-digit authenticator code or a recovery code (2FA accounts)")
|
|
104
|
+
.option("--password-stdin", "read the password from stdin instead of the terminal")
|
|
105
|
+
.action(async (emailArg, opts) => {
|
|
106
|
+
const native = emailArg === "status" ||
|
|
107
|
+
emailArg === "cancel" ||
|
|
108
|
+
(emailArg === undefined && opts.totp === undefined && !opts.passwordStdin);
|
|
109
|
+
if ((opts.browser && opts.deviceCode) ||
|
|
110
|
+
((opts.browser || opts.deviceCode) && (!native || emailArg !== undefined)))
|
|
111
|
+
throw new UsageError("choose browser approval, device-code approval, or explicit legacy login");
|
|
112
|
+
if (native)
|
|
113
|
+
return nativeLogin(ctx, emailArg, opts.deviceCode === true);
|
|
114
|
+
const client = ctx.client();
|
|
115
|
+
const email = validateEmail(emailArg ?? (await ctx.prompt("Email: ")));
|
|
116
|
+
const password = opts.passwordStdin
|
|
117
|
+
? await ctx.readStdinLine()
|
|
118
|
+
: await ctx.promptSecret("Password: ");
|
|
119
|
+
if (password.length === 0)
|
|
120
|
+
throw new UsageError("the password is empty");
|
|
121
|
+
let totp = opts.totp !== undefined ? normalizeSecondFactor(opts.totp) : undefined;
|
|
122
|
+
const attempt = () => client.POST("/v1/licence/sign-in", {
|
|
123
|
+
body: { email, password, ...(totp !== undefined ? { totp } : {}) },
|
|
124
|
+
});
|
|
125
|
+
let result = await attempt();
|
|
126
|
+
if (result.error !== undefined &&
|
|
127
|
+
result.error.error === "auth.totp_required" &&
|
|
128
|
+
totp === undefined &&
|
|
129
|
+
ctx.interactive()) {
|
|
130
|
+
totp = normalizeSecondFactor(await ctx.prompt("Two-factor code: "));
|
|
131
|
+
result = await attempt();
|
|
132
|
+
}
|
|
133
|
+
let state;
|
|
134
|
+
try {
|
|
135
|
+
state = unwrap(result);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
accountError(err);
|
|
139
|
+
}
|
|
140
|
+
ctx.emit(state, () => `signed in as ${email}\n${accountLines(state)}`);
|
|
141
|
+
});
|
|
142
|
+
program
|
|
143
|
+
.command("logout")
|
|
144
|
+
.description("Sign this computer out (the licence and cloud session are forgotten)")
|
|
145
|
+
.action(async () => {
|
|
146
|
+
try {
|
|
147
|
+
unwrap(await ctx.client().POST("/v1/licence/sign-out"));
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
accountError(err);
|
|
151
|
+
}
|
|
152
|
+
ctx.emit({ signed_out: true }, () => "signed out");
|
|
153
|
+
});
|
|
154
|
+
program
|
|
155
|
+
.command("account")
|
|
156
|
+
.description("Show the account and licence state of this computer (GET /v1/licence)")
|
|
157
|
+
.action(async () => {
|
|
158
|
+
let state;
|
|
159
|
+
try {
|
|
160
|
+
state = unwrap(await ctx.client().GET("/v1/licence"));
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
accountError(err);
|
|
164
|
+
}
|
|
165
|
+
ctx.emit(state, () => accountLines(state));
|
|
166
|
+
});
|
|
167
|
+
const licence = program.command("licence").description("Licence of this computer");
|
|
168
|
+
licence
|
|
169
|
+
.command("refresh")
|
|
170
|
+
.description("Ask the Ant0 cloud for a fresh licence now (otherwise hourly)")
|
|
171
|
+
.action(async () => {
|
|
172
|
+
let state;
|
|
173
|
+
try {
|
|
174
|
+
state = unwrap(await ctx.client().POST("/v1/licence/refresh"));
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
accountError(err);
|
|
178
|
+
}
|
|
179
|
+
ctx.emit(state, () => accountLines(state));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
export function nativeStatusLines(status) {
|
|
183
|
+
if (status.status === "signed_in")
|
|
184
|
+
return `signed in${status.account ? ` as ${status.account.email}` : ""}`;
|
|
185
|
+
if (status.status === "awaiting_browser")
|
|
186
|
+
return status.user_code
|
|
187
|
+
? `awaiting browser approval\n${status.verification_uri}\nCode: ${status.user_code}`
|
|
188
|
+
: "awaiting browser approval; use `ant0 login status` to resume";
|
|
189
|
+
if (status.status === "install_pending")
|
|
190
|
+
return "approval received; session installation pending";
|
|
191
|
+
return `sign-in ${status.status}${status.error ? ` (${status.error})` : ""}`;
|
|
192
|
+
}
|
|
193
|
+
function nativeStatus(result) {
|
|
194
|
+
const status = unwrap(result);
|
|
195
|
+
const keys = [
|
|
196
|
+
"protocol",
|
|
197
|
+
"status",
|
|
198
|
+
"transaction_id",
|
|
199
|
+
"expires_at",
|
|
200
|
+
"verification_uri",
|
|
201
|
+
"user_code",
|
|
202
|
+
"account",
|
|
203
|
+
"workspace",
|
|
204
|
+
"error",
|
|
205
|
+
"retry_after_s",
|
|
206
|
+
];
|
|
207
|
+
if (!status ||
|
|
208
|
+
Object.keys(status).sort().join() !== keys.sort().join() ||
|
|
209
|
+
status.protocol !== "ant0-native-auth/v1" ||
|
|
210
|
+
![
|
|
211
|
+
"idle",
|
|
212
|
+
"awaiting_browser",
|
|
213
|
+
"install_pending",
|
|
214
|
+
"signed_in",
|
|
215
|
+
"denied",
|
|
216
|
+
"cancelled",
|
|
217
|
+
"expired",
|
|
218
|
+
"failed",
|
|
219
|
+
].includes(status.status) ||
|
|
220
|
+
(status.verification_uri !== null &&
|
|
221
|
+
status.verification_uri !== "https://ant0.link/account/authorize") ||
|
|
222
|
+
(status.user_code !== null &&
|
|
223
|
+
!/^[A-HJ-NP-Z2-9]{5}-[A-HJ-NP-Z2-9]{5}$/.test(status.user_code)) ||
|
|
224
|
+
(status.status !== "awaiting_browser" &&
|
|
225
|
+
(status.verification_uri !== null || status.user_code !== null)) ||
|
|
226
|
+
(status.account !== null &&
|
|
227
|
+
(Object.keys(status.account).sort().join() !== "email,id" ||
|
|
228
|
+
typeof status.account.email !== "string")) ||
|
|
229
|
+
(status.workspace !== null &&
|
|
230
|
+
(Object.keys(status.workspace).sort().join() !== "id,name" ||
|
|
231
|
+
typeof status.workspace.name !== "string")) ||
|
|
232
|
+
(status.error !== null &&
|
|
233
|
+
!/^(auth\.native\.[a-z_]+|device\.(key_conflict|limit)|workspace\.(suspended|lapsed))$/.test(status.error)) ||
|
|
234
|
+
(status.retry_after_s !== null &&
|
|
235
|
+
(!Number.isInteger(status.retry_after_s) ||
|
|
236
|
+
status.retry_after_s < 1 ||
|
|
237
|
+
status.retry_after_s > 86400)))
|
|
238
|
+
throw new CliError("invalid native authorization status");
|
|
239
|
+
return status;
|
|
240
|
+
}
|
|
241
|
+
export const APP_INSTALLED_WARNING = "The Ant0 app is installed on this computer. Opening the app will ask you to sign in there and signs out this command-line session. If sync then asks for a key, use your recovery code.";
|
|
242
|
+
function desktopAppInstalled(ctx) {
|
|
243
|
+
const device = join(resolveHome({
|
|
244
|
+
...(ctx.globals.home === undefined ? {} : { home: ctx.globals.home }),
|
|
245
|
+
env: ctx.io.env,
|
|
246
|
+
}), "device");
|
|
247
|
+
return existsSync(join(device, "keys.bin")) || existsSync(join(device, "auth-install.bin"));
|
|
248
|
+
}
|
|
249
|
+
async function nativeLogin(ctx, action, deviceCode) {
|
|
250
|
+
if (ctx.keyMode())
|
|
251
|
+
throw new UsageError("native login requires installation management authority");
|
|
252
|
+
const client = ctx.client();
|
|
253
|
+
if (action === undefined && desktopAppInstalled(ctx))
|
|
254
|
+
ctx.err(APP_INSTALLED_WARNING);
|
|
255
|
+
const get = async () => nativeStatus(await client.GET("/v1/licence/native/status"));
|
|
256
|
+
if (action === "status") {
|
|
257
|
+
const status = await get();
|
|
258
|
+
ctx.emit(status, () => nativeStatusLines(status));
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (action === "cancel") {
|
|
262
|
+
let status = await get();
|
|
263
|
+
if (status.transaction_id === null)
|
|
264
|
+
throw new CliError("there is no pending native authorization");
|
|
265
|
+
status = nativeStatus(await client.POST("/v1/licence/native/cancel", {
|
|
266
|
+
body: { transaction_id: status.transaction_id },
|
|
267
|
+
}));
|
|
268
|
+
ctx.emit(status, () => nativeStatusLines(status));
|
|
269
|
+
if (status.status !== "cancelled")
|
|
270
|
+
throw new CliError("authorization cancellation remains pending");
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
let status = nativeStatus(await client.POST("/v1/licence/native/begin", {
|
|
274
|
+
body: {
|
|
275
|
+
protocol: "ant0-native-auth/v1",
|
|
276
|
+
request_id: randomUUID(),
|
|
277
|
+
client: "cli",
|
|
278
|
+
delivery: deviceCode ? "device_code" : "loopback",
|
|
279
|
+
expected_account_id: null,
|
|
280
|
+
},
|
|
281
|
+
}));
|
|
282
|
+
const cancel = async () => {
|
|
283
|
+
if (status.transaction_id)
|
|
284
|
+
await client
|
|
285
|
+
.POST("/v1/licence/native/cancel", { body: { transaction_id: status.transaction_id } })
|
|
286
|
+
.catch(() => { });
|
|
287
|
+
};
|
|
288
|
+
if (ctx.json) {
|
|
289
|
+
ctx.emit(status, () => "");
|
|
290
|
+
if (status.status !== "signed_in")
|
|
291
|
+
throw new CliError("sign-in remains incomplete");
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
ctx.err(nativeStatusLines(status));
|
|
295
|
+
if (!deviceCode && status.verification_uri && status.user_code) {
|
|
296
|
+
try {
|
|
297
|
+
await ctx.openBrowser(`${status.verification_uri}#code=${status.user_code}`);
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
await cancel();
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
let announcedCode = status.user_code;
|
|
305
|
+
try {
|
|
306
|
+
while (status.status === "awaiting_browser" || status.status === "install_pending") {
|
|
307
|
+
if (ctx.io.signal?.aborted)
|
|
308
|
+
throw new CliError("sign-in cancelled");
|
|
309
|
+
await ctx.wait(Math.max(5, status.retry_after_s ?? 5) * 1000);
|
|
310
|
+
status = await get();
|
|
311
|
+
if (status.status === "awaiting_browser" &&
|
|
312
|
+
status.user_code &&
|
|
313
|
+
status.user_code !== announcedCode) {
|
|
314
|
+
announcedCode = status.user_code;
|
|
315
|
+
ctx.err(nativeStatusLines(status));
|
|
316
|
+
if (!deviceCode && status.verification_uri) {
|
|
317
|
+
try {
|
|
318
|
+
await ctx.openBrowser(`${status.verification_uri}#code=${status.user_code}`);
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
await cancel();
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
if (ctx.io.signal?.aborted)
|
|
330
|
+
await cancel();
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
ctx.emit(status, () => nativeStatusLines(status));
|
|
334
|
+
if (status.status !== "signed_in")
|
|
335
|
+
throw new CliError(`sign-in ${status.status}`);
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=account.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Command } from "commander";
|
|
2
|
+
import type { Context } from "../context.js";
|
|
3
|
+
export declare function provisionScopes(preset: string, selected?: readonly string[]): ("status:read" | "profiles:read" | "proxies:read" | "profiles:run" | "profiles:stop" | "profiles:cdp")[];
|
|
4
|
+
export declare function registerApiKeys(program: Command, ctx: Context): void;
|
|
5
|
+
//# sourceMappingURL=api-keys.d.ts.map
|