@p0security/cli 0.6.2 → 0.8.0

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.
@@ -8,19 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.scp = exports.ssm = void 0;
12
+ exports.sshOrScp = void 0;
13
+ const keys_1 = require("../../../common/keys");
16
14
  const stdio_1 = require("../../../drivers/stdio");
17
15
  const aws_1 = require("../../okta/aws");
16
+ const ssh_agent_1 = require("../../ssh-agent");
18
17
  const install_1 = require("./install");
19
- const lodash_1 = require("lodash");
20
18
  const node_child_process_1 = require("node:child_process");
21
- const node_stream_1 = require("node:stream");
22
- const ps_tree_1 = __importDefault(require("ps-tree"));
23
- const STARTING_SESSION_MESSAGE = /Starting session with SessionId: (.*)/;
24
19
  /** Matches the error message that AWS SSM print1 when access is not propagated */
25
20
  // Note that the resource will randomly be either the SSM document or the EC2 instance
26
21
  const UNPROVISIONED_ACCESS_MESSAGE = /An error occurred \(AccessDeniedException\) when calling the StartSession operation: User: arn:aws:sts::.*:assumed-role\/P0GrantsRole.* is not authorized to perform: ssm:StartSession on resource: arn:aws:.*:.*:.* because no identity-based policy allows the ssm:StartSession action/;
@@ -42,7 +37,6 @@ const UNPROVISIONED_ACCESS_VALIDATION_WINDOW_MS = 5e3;
42
37
  */
43
38
  const MAX_SSM_RETRIES = 30;
44
39
  /** The name of the SessionManager port forwarding document. This document is managed by AWS. */
45
- const LOCAL_PORT_FORWARDING_DOCUMENT_NAME = "AWS-StartPortForwardingSession";
46
40
  const START_SSH_SESSION_DOCUMENT_NAME = "AWS-StartSSHSession";
47
41
  /** Checks if access has propagated through AWS to the SSM agent
48
42
  *
@@ -88,53 +82,11 @@ const createBaseSsmCommand = (args) => {
88
82
  args.instance,
89
83
  ];
90
84
  };
91
- const createInteractiveShellCommand = (args) => {
92
- var _a;
93
- const ssmCommand = [
94
- ...createBaseSsmCommand(args),
95
- "--document-name",
96
- args.documentName,
97
- ];
98
- const command = (_a = args.command) === null || _a === void 0 ? void 0 : _a.trim();
99
- if (command) {
100
- ssmCommand.push("--parameters", `command='${command}'`);
101
- }
102
- return ssmCommand;
103
- };
104
- const createPortForwardingCommand = (args) => {
105
- const [localPort, remotePort] = args.forwardPortAddress
106
- .split(":")
107
- .map(Number);
108
- return [
109
- ...createBaseSsmCommand(args),
110
- "--document-name",
111
- // Port forwarding is a special case that uses an AWS-managed document, not the user-generated document we use for an SSH session
112
- LOCAL_PORT_FORWARDING_DOCUMENT_NAME,
113
- "--parameters",
114
- `localPortNumber=${localPort},portNumber=${remotePort}`,
115
- ];
116
- };
117
- const createSsmCommands = (args) => {
118
- const interactiveShellCommand = createInteractiveShellCommand(args);
119
- const forwardPortAddress = args.forwardPortAddress;
120
- if (!forwardPortAddress) {
121
- return { shellCommand: interactiveShellCommand };
122
- }
123
- const portForwardingCommand = createPortForwardingCommand(Object.assign(Object.assign({}, args), { forwardPortAddress }));
124
- if (args.noRemoteCommands) {
125
- return { shellCommand: portForwardingCommand };
126
- }
127
- return {
128
- shellCommand: interactiveShellCommand,
129
- subCommand: portForwardingCommand,
130
- };
131
- };
132
- function spawnChildProcess(credential, command, args, stdio) {
133
- return (0, node_child_process_1.spawn)(command, args, {
134
- env: Object.assign(Object.assign({}, process.env), credential),
135
- stdio,
136
- });
137
- }
85
+ const spawnChildProcess = (credential, command, args, stdio) => (0, node_child_process_1.spawn)(command, args, {
86
+ env: Object.assign(Object.assign({}, process.env), credential),
87
+ stdio,
88
+ shell: false,
89
+ });
138
90
  /** Starts an SSM session in the terminal by spawning `aws ssm` as a subprocess
139
91
  *
140
92
  * Requires `aws ssm` to be installed on the client machine.
@@ -142,12 +94,7 @@ function spawnChildProcess(credential, command, args, stdio) {
142
94
  function spawnSsmNode(options) {
143
95
  return __awaiter(this, void 0, void 0, function* () {
144
96
  return new Promise((resolve, reject) => {
145
- var _a, _b;
146
97
  const child = spawnChildProcess(options.credential, options.command, options.args, options.stdio);
147
- // optionally buffer content into stdin for the child process
148
- for (const command of (_a = options.writeStdin) !== null && _a !== void 0 ? _a : []) {
149
- (_b = child.stdin) === null || _b === void 0 ? void 0 : _b.write(`${command}\n`);
150
- }
151
98
  const { isAccessPropagated } = accessPropagationGuard(child);
152
99
  const exitListener = child.on("exit", (code) => {
153
100
  var _a, _b;
@@ -172,114 +119,10 @@ function spawnSsmNode(options) {
172
119
  });
173
120
  });
174
121
  }
175
- /**
176
- * A subprocess SSM session redirects its output through a proxy that filters certain messages reducing the verbosity of the output.
177
- * The subprocess also makes sure to terminate any grandchild processes that might spawn during the session.
178
- *
179
- * This process should be used when multiple SSM sessions need to be spawned in parallel.
180
- */
181
- const spawnSubprocessSsmNode = (options) => __awaiter(void 0, void 0, void 0, function* () {
182
- return new Promise((resolve, reject) => {
183
- const child = spawnChildProcess(options.credential, "/usr/bin/env", options.command, ["ignore", "pipe", "pipe"]);
184
- // Captures the starting session message and filters it from the output
185
- const proxyStream = new node_stream_1.Transform({
186
- transform(chunk, _, end) {
187
- const message = chunk.toString("utf-8");
188
- const match = message.match(STARTING_SESSION_MESSAGE);
189
- if (!match) {
190
- this.push(chunk);
191
- }
192
- end();
193
- },
194
- });
195
- // Ensures that content from the child process is printed to the terminal and the proxy stream
196
- child.stdout.pipe(proxyStream).pipe(process.stdout);
197
- const { isAccessPropagated } = accessPropagationGuard(child);
198
- const abortListener = (code) => {
199
- options.abortController.signal.removeEventListener("abort", abortListener);
200
- // AWS CLI typically will spawn a grandchild process for the SSM session. Using `ps-tree` will allow us
201
- // to identify and terminate the grandchild process as well.
202
- (0, ps_tree_1.default)(child.pid, function (_, children) {
203
- // kill the original child process first so that messages from grandchildren are not printed to stdout
204
- child.kill();
205
- // Send a SIGTERM because other signals (e.g. SIGKILL) will not propagate to the grandchildren
206
- (0, node_child_process_1.exec)(`kill -15 ${children.map((p) => p.PID).join(" ")}`);
207
- });
208
- resolve(code);
209
- };
210
- child.on("spawn", () => {
211
- options.abortController.signal.addEventListener("abort", abortListener);
212
- });
213
- const exitListener = child.on("exit", (code) => {
214
- var _a;
215
- exitListener.unref();
216
- // In the case of ephemeral AccessDenied exceptions due to unpropagated
217
- // permissions, continually retry access until success
218
- if (!isAccessPropagated()) {
219
- options.abortController.signal.removeEventListener("abort", abortListener);
220
- const attemptsRemaining = (_a = options === null || options === void 0 ? void 0 : options.attemptsRemaining) !== null && _a !== void 0 ? _a : MAX_SSM_RETRIES;
221
- if (attemptsRemaining <= 0) {
222
- reject("Access did not propagate through AWS before max retry attempts were exceeded. Please contact support@p0.dev for assistance.");
223
- return;
224
- }
225
- spawnSubprocessSsmNode(Object.assign(Object.assign({}, options), { attemptsRemaining: attemptsRemaining - 1 }))
226
- .then((code) => resolve(code))
227
- .catch(reject);
228
- return;
229
- }
230
- options.abortController.abort(code);
231
- });
232
- });
233
- });
234
- /** Convert an SshCommandArgs into an SSM document "command" parameter */
235
- const commandParameter = (args) => args.command
236
- ? `${args.command} ${args.arguments
237
- .map((argument) =>
238
- // escape all double quotes (") in commands such as `p0 ssh <instance>> echo 'hello; "world"'` because we
239
- // need to encapsulate command arguments in double quotes as we pass them along to the remote shell
240
- `"${String(argument).replace(/"/g, '\\"')}"`)
241
- .join(" ")}`.trim()
242
- : undefined;
243
- /** Connect to an SSH backend using AWS Systems Manager (SSM) */
244
- const ssm = (authn, request, args) => __awaiter(void 0, void 0, void 0, function* () {
245
- const isInstalled = yield (0, install_1.ensureSsmInstall)();
246
- if (!isInstalled)
247
- throw "Please try again after installing the required AWS utilities";
248
- const credential = yield (0, aws_1.assumeRoleWithOktaSaml)(authn, {
249
- account: request.instance.accountId,
250
- role: request.role,
251
- });
252
- const ssmArgs = {
253
- instance: request.instance.id,
254
- region: request.instance.region,
255
- documentName: request.documentName,
256
- forwardPortAddress: args.L,
257
- noRemoteCommands: args.N,
258
- command: commandParameter(args),
259
- };
260
- const ssmCommands = createSsmCommands(ssmArgs);
261
- yield startSsmProcesses(credential, ssmCommands);
262
- });
263
- exports.ssm = ssm;
264
- /**
265
- * Starts the SSM session and any additional processes that are requested for the session to function properly.
266
- */
267
- const startSsmProcesses = (credential, commands) => __awaiter(void 0, void 0, void 0, function* () {
268
- /** The AbortController is responsible for sending a shared signal to all spawned processes ({@link spawnSsmNode}) when the parent process is terminated unexpectedly. This is necessary because the spawned processes are detached and would otherwise continue running after the parent process is terminated. */
269
- const abortController = new AbortController();
270
- const args = { credential, abortController };
271
- const processes = [
272
- spawnSsmNode(Object.assign(Object.assign({}, args), { command: "/usr/bin/env", args: commands.shellCommand, stdio: ["inherit", "inherit", "pipe"] })),
273
- ];
274
- if (commands.subCommand) {
275
- processes.push(spawnSubprocessSsmNode(Object.assign(Object.assign({}, args), { command: commands.subCommand })));
276
- }
277
- yield Promise.all(processes);
278
- });
279
122
  const createProxyCommands = (data, args, debug) => {
280
123
  const ssmCommand = [
281
124
  ...createBaseSsmCommand({
282
- region: data.instance.region,
125
+ region: data.region,
283
126
  instance: "%h",
284
127
  }),
285
128
  "--document-name",
@@ -287,36 +130,56 @@ const createProxyCommands = (data, args, debug) => {
287
130
  "--parameters",
288
131
  '"portNumber=%p"',
289
132
  ];
133
+ const commonArgs = [
134
+ ...(debug ? ["-v"] : []),
135
+ "-o",
136
+ `ProxyCommand=${ssmCommand.join(" ")}`,
137
+ ];
138
+ if ("source" in args) {
139
+ return {
140
+ command: "scp",
141
+ args: [
142
+ ...commonArgs,
143
+ // if a response is not received after three 5 minute attempts,
144
+ // the connection will be closed.
145
+ "-o",
146
+ "ServerAliveCountMax=3",
147
+ `-o`,
148
+ "ServerAliveInterval=300",
149
+ ...(args.recursive ? ["-r"] : []),
150
+ args.source,
151
+ args.destination,
152
+ ],
153
+ };
154
+ }
290
155
  return {
291
- scp: [
292
- "scp",
293
- ...(debug ? ["-v"] : []),
294
- // ignore any overrides in the user's config file, we only want to use the ssh-agent we've set up for the session
295
- "-o",
296
- "IdentityAgent=$SSH_AUTH_SOCK",
297
- "-o",
298
- `ProxyCommand='${ssmCommand.join(" ")}'`,
299
- // if a response is not received after three 5 minute attempts,
300
- // the connection will be closed.
301
- "-o",
302
- "ServerAliveCountMax=3",
303
- `-o`,
304
- "ServerAliveInterval=300",
305
- ...(args.recursive ? ["-r"] : []),
306
- args.source,
307
- args.destination,
308
- ],
309
- ssh: [
310
- "ssh",
311
- ...(debug ? ["-v"] : []),
312
- "-o",
313
- `ProxyCommand='${ssmCommand.join(" ")}'`,
314
- `${data.linuxUserName}@${data.instance.id}`,
156
+ command: "ssh",
157
+ args: [
158
+ ...commonArgs,
159
+ ...(args.A ? ["-A"] : []),
160
+ ...(args.L ? ["-L", args.L] : []),
161
+ ...(args.N ? ["-N"] : []),
162
+ `${data.linuxUserName}@${data.id}`,
163
+ ...(args.command ? [args.command] : []),
164
+ ...args.arguments.map((argument) =>
165
+ // escape all double quotes (") in commands such as `p0 ssh <instance>> echo 'hello; "world"'` because we
166
+ // need to encapsulate command arguments in double quotes as we pass them along to the remote shell
167
+ `"${String(argument).replace(/"/g, '\\"')}"`),
315
168
  ],
316
- ssm: ssmCommand,
317
169
  };
318
170
  };
319
- const scp = (authn, data, args, privateKey) => __awaiter(void 0, void 0, void 0, function* () {
171
+ /** Converts arguments for manual execution - arguments may have to be quoted or certain characters escaped when executing the commands from a shell */
172
+ const transformForShell = (args) => {
173
+ return args.map((arg) => {
174
+ // The ProxyCommand option must be surrounded by single quotes
175
+ if (arg.startsWith("ProxyCommand=")) {
176
+ const [name, ...value] = arg.split("="); // contains the '=' character in the parameters option: ProxyCommand=aws ssm start-session ... --parameters "portNumber=%p"
177
+ return `${name}='${value.join("=")}'`;
178
+ }
179
+ return arg;
180
+ });
181
+ };
182
+ const sshOrScp = (authn, data, cmdArgs, privateKey) => __awaiter(void 0, void 0, void 0, function* () {
320
183
  if (!(yield (0, install_1.ensureSsmInstall)())) {
321
184
  throw "Please try again after installing the required AWS utilities";
322
185
  }
@@ -324,58 +187,27 @@ const scp = (authn, data, args, privateKey) => __awaiter(void 0, void 0, void 0,
324
187
  throw "Failed to load a private key for this request. Please contact support@p0.dev for assistance.";
325
188
  }
326
189
  const credential = yield (0, aws_1.assumeRoleWithOktaSaml)(authn, {
327
- account: data.instance.accountId,
190
+ account: data.accountId,
328
191
  role: data.role,
329
192
  });
330
- const commands = createProxyCommands(data, args, args.debug);
331
- const scpCommand = commands.scp.join(" ");
332
- const sshCommand = commands.ssh.join(" ");
333
- const debug = [
334
- `echo "SSH_AUTH_SOCK: $SSH_AUTH_SOCK"`,
335
- `echo "SSH_AGENT_PID: $SSH_AGENT_PID"`,
336
- `echo '$(p0 aws role assume ${data.role})'`,
337
- `echo "${sshCommand}"`,
338
- `echo "${scpCommand}"`,
339
- `echo "SSH Agent Keys:"`,
340
- `ssh-add -l`,
341
- ];
342
- /**
343
- * Spawns a child process to add a private key to the ssh-agent. The SSH agent is included in the OpenSSH suite
344
- * of tools and is used to hold private keys during a session. The SSH agent typically does not persist keys
345
- * across system reboots or logout/login cycles. Once you log out or restart your system, any keys added to
346
- * the SSH agent during that session will need to be added again in subsequent sessions.
347
- */
348
- const writeStdin = [
349
- // This might be overkill because we are already spawning a subprocess that will run the commands for us
350
- // but just in case someone enters that subprocess we're also disabling the history of commands run.
351
- `unset HISTFILE`,
352
- // in debug mode, we want to see the pid of the ssh-agent and compare it to the environment variable
353
- `eval $(ssh-agent)${args.debug ? "" : " >/dev/null 2>&1"}`,
354
- `trap 'kill $SSH_AGENT_PID' EXIT`,
355
- `ssh-add -q - <<< '${privateKey}'`,
356
- // in debug mode, we'll see the keys that were added to the agent and more information about the agent
357
- ...(args.debug ? debug : []),
358
- scpCommand,
359
- `SCP_EXIT_CODE=$?`,
360
- `exit $SCP_EXIT_CODE`,
361
- ];
362
- if (args.debug) {
363
- // Print commands that can be individually executed to reproduce behavior
364
- // Remove the debug information - can be executed manually between steps
365
- const reproCommands = (0, lodash_1.without)([
366
- "bash",
367
- ...Object.entries(process.env).map(([key, value]) => `export ${key}='${value}'`),
368
- ...Object.entries(credential).map(([key, value]) => `export ${key}='${value}'`),
369
- ...writeStdin,
370
- ], ...debug);
371
- (0, stdio_1.print2)(`Execute the following commands to create a similar SCP session:\n *** COMMANDS BEGIN ***\n${reproCommands.join("\n")}\n *** COMMANDS END ***`);
372
- }
373
- return spawnSsmNode({
374
- credential,
375
- writeStdin,
376
- stdio: ["pipe", "inherit", "pipe"],
377
- command: "bash",
378
- args: [],
379
- });
193
+ return (0, ssh_agent_1.withSshAgent)(cmdArgs, () => __awaiter(void 0, void 0, void 0, function* () {
194
+ const { command, args } = createProxyCommands(data, cmdArgs, cmdArgs.debug);
195
+ if (cmdArgs.debug) {
196
+ const reproCommands = [
197
+ `eval $(ssh-agent)`,
198
+ `ssh-add "${keys_1.PRIVATE_KEY_PATH}"`,
199
+ `eval $(p0 aws role assume ${data.role} --account ${data.accountId})`,
200
+ `${command} ${transformForShell(args).join(" ")}`,
201
+ ];
202
+ (0, stdio_1.print2)(`Execute the following commands to create a similar SSH/SCP session:\n*** COMMANDS BEGIN ***\n${reproCommands.join("\n")}\n*** COMMANDS END ***"\n`);
203
+ }
204
+ return spawnSsmNode({
205
+ credential,
206
+ abortController: new AbortController(),
207
+ command,
208
+ args,
209
+ stdio: ["inherit", "inherit", "pipe"],
210
+ });
211
+ }));
380
212
  });
381
- exports.scp = scp;
213
+ exports.sshOrScp = sshOrScp;
@@ -53,16 +53,18 @@ export declare type AwsConfig = {
53
53
  export declare type AwsSsh = {
54
54
  permission: {
55
55
  spec: {
56
- awsResourcePermission: {
57
- resource: {
58
- arn: string;
59
- };
60
- };
56
+ instanceId: string;
57
+ accountId: string;
58
+ region: string;
61
59
  };
62
60
  type: "session";
63
61
  };
64
62
  generated: {
65
- documentName: string;
63
+ name: string;
64
+ ssh: {
65
+ linuxUserName: string;
66
+ publicKey: string;
67
+ };
66
68
  };
67
69
  };
68
70
  export {};
@@ -12,8 +12,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.pluginLoginMap = void 0;
13
13
  const login_1 = require("./google/login");
14
14
  const login_2 = require("./okta/login");
15
+ const login_3 = require("./ping/login");
15
16
  exports.pluginLoginMap = {
16
17
  google: login_1.googleLogin,
17
18
  okta: login_2.oktaLogin,
19
+ ping: login_3.pingLogin,
18
20
  "oidc-pkce": (org) => __awaiter(void 0, void 0, void 0, function* () { return yield exports.pluginLoginMap[org.providerType](org); }),
19
21
  };
@@ -0,0 +1,5 @@
1
+ import { TokenResponse } from "../../types/oidc";
2
+ import { OrgData } from "../../types/org";
3
+ export declare const validateProviderDomain: (org: OrgData) => void;
4
+ /** Logs in to an Identity Provider via OIDC */
5
+ export declare const oidcLogin: (org: OrgData, scope: string) => Promise<TokenResponse>;
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.oidcLogin = exports.validateProviderDomain = void 0;
16
+ /** Copyright © 2024-present P0 Security
17
+
18
+ This file is part of @p0security/cli
19
+
20
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
21
+
22
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
25
+ **/
26
+ const oidc_1 = require("../../common/auth/oidc");
27
+ const fetch_1 = require("../../common/fetch");
28
+ const stdio_1 = require("../../drivers/stdio");
29
+ const util_1 = require("../../util");
30
+ const lodash_1 = require("lodash");
31
+ const open_1 = __importDefault(require("open"));
32
+ const DEVICE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
33
+ const validateProviderDomain = (org) => {
34
+ if (!org.providerDomain)
35
+ throw "Login requires a configured provider domain.";
36
+ };
37
+ exports.validateProviderDomain = validateProviderDomain;
38
+ /** Executes the first step of a device-authorization grant flow */
39
+ // cf. https://developer.okta.com/docs/guides/device-authorization-grant/main/
40
+ const authorize = (org, scope) => __awaiter(void 0, void 0, void 0, function* () {
41
+ if (org.providerType === undefined) {
42
+ throw "Login requires a configured provider type.";
43
+ }
44
+ const init = {
45
+ method: "POST",
46
+ headers: oidc_1.OIDC_HEADERS,
47
+ body: (0, fetch_1.urlEncode)({
48
+ client_id: org.clientId,
49
+ scope,
50
+ }),
51
+ };
52
+ (0, exports.validateProviderDomain)(org);
53
+ // This is the "org" authorization server; the okta.apps.* scopes are not
54
+ // available with custom authorization servers
55
+ const url = org.providerType === "okta"
56
+ ? `https:${org.providerDomain}/oauth2/v1/device/authorize`
57
+ : org.providerType === "ping"
58
+ ? `https://${org.providerDomain}/${org.environmentId}/as/device_authorization`
59
+ : (0, util_1.throwAssertNever)(org.providerType);
60
+ const response = yield fetch(url, init);
61
+ yield (0, fetch_1.validateResponse)(response);
62
+ return (yield response.json());
63
+ });
64
+ /** Attempts to fetch this device's OIDC token
65
+ *
66
+ * The authorization may or may not be granted at this stage. If it is not, the
67
+ * authorization server will return "authorization_pending", in which case this
68
+ * function will return undefined.
69
+ */
70
+ const fetchOidcToken = (org, authorize) => __awaiter(void 0, void 0, void 0, function* () {
71
+ if (org.providerType === undefined) {
72
+ throw "Login requires a configured provider type.";
73
+ }
74
+ const init = {
75
+ method: "POST",
76
+ headers: oidc_1.OIDC_HEADERS,
77
+ body: (0, fetch_1.urlEncode)({
78
+ client_id: org.clientId,
79
+ device_code: authorize.device_code,
80
+ grant_type: DEVICE_GRANT_TYPE,
81
+ }),
82
+ };
83
+ (0, exports.validateProviderDomain)(org);
84
+ const url = org.providerType === "okta"
85
+ ? `https:${org.providerDomain}/oauth2/v1/token`
86
+ : org.providerType === "ping"
87
+ ? `https://${org.providerDomain}/${org.environmentId}/as/token`
88
+ : (0, util_1.throwAssertNever)(org.providerType);
89
+ const response = yield fetch(url, init);
90
+ if (!response.ok) {
91
+ if (response.status === 400) {
92
+ const data = yield response.json();
93
+ if (data.error === "authorization_pending")
94
+ return undefined;
95
+ }
96
+ yield (0, fetch_1.validateResponse)(response);
97
+ }
98
+ return (yield response.json());
99
+ });
100
+ /** Waits until user device authorization is complete
101
+ *
102
+ * Returns the OIDC token after completion.
103
+ */
104
+ const waitForActivation = (org, authorize) => __awaiter(void 0, void 0, void 0, function* () {
105
+ const start = Date.now();
106
+ while (Date.now() - start <= authorize.expires_in * 1e3) {
107
+ const response = yield fetchOidcToken(org, authorize);
108
+ if (!response)
109
+ yield (0, util_1.sleep)(authorize.interval * 1e3);
110
+ else
111
+ return response;
112
+ }
113
+ throw "Expired awaiting in-browser authorization.";
114
+ });
115
+ /** Logs in to an Identity Provider via OIDC */
116
+ const oidcLogin = (org, scope) => __awaiter(void 0, void 0, void 0, function* () {
117
+ if (org.providerType === undefined) {
118
+ throw "Login requires a configured provider type.";
119
+ }
120
+ const authorizeResponse = yield authorize(org, scope);
121
+ (0, stdio_1.print2)(`Please use the opened browser window to continue your P0 login.
122
+
123
+ When prompted, confirm that ${(0, lodash_1.capitalize)(org.providerType)} displays this code:
124
+
125
+ ${authorizeResponse.user_code}
126
+
127
+ Waiting for authorization...
128
+ `);
129
+ void (0, open_1.default)(authorizeResponse.verification_uri_complete);
130
+ const oidcResponse = yield waitForActivation(org, authorizeResponse);
131
+ return oidcResponse;
132
+ });
133
+ exports.oidcLogin = oidcLogin;