@automattic/vip 4.0.10 → 4.0.13
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/bin/vip-defensive-mode-configure.js +216 -0
- package/dist/bin/vip-defensive-mode-disable.js +42 -0
- package/dist/bin/vip-defensive-mode-enable.js +45 -0
- package/dist/bin/vip-defensive-mode.js +21 -0
- package/dist/bin/vip-import-sql.js +4 -46
- package/dist/bin/vip.js +6 -1
- package/dist/lib/api/constants.js +7 -0
- package/dist/lib/api/feature-flags.js +17 -4
- package/dist/lib/api/http.js +3 -3
- package/dist/lib/api.js +23 -8
- package/dist/lib/cli/internal-bin-loader.js +4 -0
- package/dist/lib/defensive-mode/api.js +106 -0
- package/dist/lib/defensive-mode/cli-helpers.js +60 -0
- package/dist/lib/dev-environment/dev-environment-configuration-file.js +19 -14
- package/dist/lib/logout.js +9 -4
- package/dist/lib/rechallenge/client.js +55 -0
- package/dist/lib/rechallenge/errors.js +55 -0
- package/dist/lib/rechallenge/flow.js +145 -0
- package/dist/lib/rechallenge/index.js +27 -0
- package/dist/lib/rechallenge/link.js +167 -0
- package/dist/lib/rechallenge/open-browser.js +21 -0
- package/dist/lib/rechallenge/token-cache.js +113 -0
- package/dist/lib/rechallenge/types.js +15 -0
- package/dist/lib/token.js +3 -3
- package/docs/COMMANDER-MIGRATION.md +1 -1
- package/docs/SETUP.md +7 -6
- package/docs/decisions/0001-operation-scoped-elevated-token-reuse.md +87 -0
- package/helpers/generate-docs.js +17 -6
- package/package.json +7 -3
- package/npm-shrinkwrap.json +0 -15470
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.appQuery = void 0;
|
|
5
|
+
exports.updateDefensiveModeConfig = updateDefensiveModeConfig;
|
|
6
|
+
exports.updateDefensiveModeStatus = updateDefensiveModeStatus;
|
|
7
|
+
var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
|
|
8
|
+
var _api = _interopRequireDefault(require("../api"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
const appQuery = exports.appQuery = `
|
|
11
|
+
id
|
|
12
|
+
name
|
|
13
|
+
typeId
|
|
14
|
+
environments {
|
|
15
|
+
id
|
|
16
|
+
appId
|
|
17
|
+
name
|
|
18
|
+
primaryDomain {
|
|
19
|
+
name
|
|
20
|
+
}
|
|
21
|
+
type
|
|
22
|
+
defensiveMode {
|
|
23
|
+
config {
|
|
24
|
+
effective {
|
|
25
|
+
enabled
|
|
26
|
+
challengeType
|
|
27
|
+
connectionThresholdAbsolute
|
|
28
|
+
connectionThresholdPercentage
|
|
29
|
+
disableAtEpoch
|
|
30
|
+
keepEnabledUnderThresholdForSeconds
|
|
31
|
+
maxRequestRate
|
|
32
|
+
priorityBypass
|
|
33
|
+
}
|
|
34
|
+
stored {
|
|
35
|
+
enabled
|
|
36
|
+
challengeType
|
|
37
|
+
connectionThresholdAbsolute
|
|
38
|
+
connectionThresholdPercentage
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
organization {
|
|
44
|
+
id
|
|
45
|
+
name
|
|
46
|
+
salesforceId
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
const STATUS_MUTATION = (0, _graphqlTag.default)`
|
|
50
|
+
mutation UpdateDefensiveModeStatus($input: AppEnvironmentDefensiveModeUpdateStatusInput) {
|
|
51
|
+
updateDefensiveModeStatus(input: $input) {
|
|
52
|
+
success
|
|
53
|
+
message
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
const CONFIG_MUTATION = (0, _graphqlTag.default)`
|
|
58
|
+
mutation UpdateDefensiveModeConfig($input: AppEnvironmentDefensiveModeConfigInput) {
|
|
59
|
+
updateDefensiveModeConfig(input: $input) {
|
|
60
|
+
success
|
|
61
|
+
message
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
async function updateDefensiveModeStatus(input) {
|
|
66
|
+
const api = (0, _api.default)();
|
|
67
|
+
const result = await api.mutate({
|
|
68
|
+
mutation: STATUS_MUTATION,
|
|
69
|
+
variables: {
|
|
70
|
+
input: {
|
|
71
|
+
id: input.appId,
|
|
72
|
+
environmentId: input.envId,
|
|
73
|
+
enabled: input.enabled
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
if (!result.data) {
|
|
78
|
+
throw new Error('updateDefensiveModeStatus returned no data; the API may have rejected the request.');
|
|
79
|
+
}
|
|
80
|
+
return result.data.updateDefensiveModeStatus;
|
|
81
|
+
}
|
|
82
|
+
async function updateDefensiveModeConfig(input) {
|
|
83
|
+
const api = (0, _api.default)();
|
|
84
|
+
const mutationInput = {
|
|
85
|
+
id: input.appId,
|
|
86
|
+
environmentId: input.envId,
|
|
87
|
+
enabled: input.enabled,
|
|
88
|
+
challengeType: input.challengeType
|
|
89
|
+
};
|
|
90
|
+
if (input.connectionThresholdAbsolute !== undefined) {
|
|
91
|
+
mutationInput.connectionThresholdAbsolute = input.connectionThresholdAbsolute;
|
|
92
|
+
}
|
|
93
|
+
if (input.connectionThresholdPercentage !== undefined) {
|
|
94
|
+
mutationInput.connectionThresholdPercentage = input.connectionThresholdPercentage;
|
|
95
|
+
}
|
|
96
|
+
const result = await api.mutate({
|
|
97
|
+
mutation: CONFIG_MUTATION,
|
|
98
|
+
variables: {
|
|
99
|
+
input: mutationInput
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (!result.data) {
|
|
103
|
+
throw new Error('updateDefensiveModeConfig returned no data; the API may have rejected the request.');
|
|
104
|
+
}
|
|
105
|
+
return result.data.updateDefensiveModeConfig;
|
|
106
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.guardProductionMutation = guardProductionMutation;
|
|
5
|
+
exports.isInteractive = isInteractive;
|
|
6
|
+
exports.reportMutationResult = reportMutationResult;
|
|
7
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
function isInteractive(opt) {
|
|
10
|
+
if (process.env.VIP_NON_INTERACTIVE === '1') {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (opt.nonInteractive) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return Boolean(process.stdout.isTTY);
|
|
17
|
+
}
|
|
18
|
+
function capitalize(str) {
|
|
19
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Guards production mutations that require confirmation.
|
|
24
|
+
* Returns when the command should proceed. In non-interactive contexts without
|
|
25
|
+
* --skip-confirmation it emits an error and calls process.exit(1) directly.
|
|
26
|
+
* If the user declines the interactive prompt it calls process.exit() directly.
|
|
27
|
+
*/
|
|
28
|
+
async function guardProductionMutation(opt, action, trackingParams, confirmFn, trackEventFn, formatEnvironment) {
|
|
29
|
+
if (opt.skipConfirmation || opt.env.type !== 'production') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!isInteractive(opt)) {
|
|
33
|
+
console.error(_chalk.default.red(`Refusing to ${action} defensive mode on production without confirmation. ` + 'Pass --skip-confirmation to proceed non-interactively.'));
|
|
34
|
+
await trackEventFn(`defensive_mode_${action}_command_cancelled`, trackingParams);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
const yes = await confirmFn(`${capitalize(action)} defensive mode on ${formatEnvironment(opt.env.type)} for ${opt.app.name}?`);
|
|
38
|
+
if (!yes) {
|
|
39
|
+
await trackEventFn(`defensive_mode_${action}_command_cancelled`, trackingParams);
|
|
40
|
+
console.log('Command cancelled');
|
|
41
|
+
process.exit();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Handles success/failure reporting, telemetry, and log output after a
|
|
47
|
+
* defensive-mode mutation. Exits the process on failure.
|
|
48
|
+
*/
|
|
49
|
+
async function reportMutationResult(result, trackingParams, action, appName, envType, successVerb, failureVerb, trackEventFn) {
|
|
50
|
+
if (!result.success) {
|
|
51
|
+
await trackEventFn(`defensive_mode_${action}_command_error`, {
|
|
52
|
+
...trackingParams,
|
|
53
|
+
error: result.message
|
|
54
|
+
});
|
|
55
|
+
console.error(_chalk.default.red(`Failed to ${failureVerb}: ${result.message}`));
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
await trackEventFn(`defensive_mode_${action}_command_success`, trackingParams);
|
|
59
|
+
console.log(_chalk.default.green('✓'), `Defensive mode ${successVerb} for ${appName}.${envType} — ${result.message}`);
|
|
60
|
+
}
|
|
@@ -6,7 +6,6 @@ exports.getConfigurationFileOptions = getConfigurationFileOptions;
|
|
|
6
6
|
exports.mergeConfigurationFileOptions = mergeConfigurationFileOptions;
|
|
7
7
|
var _chalk = _interopRequireDefault(require("chalk"));
|
|
8
8
|
var _debug = _interopRequireDefault(require("debug"));
|
|
9
|
-
var _ejs = _interopRequireDefault(require("ejs"));
|
|
10
9
|
var _jsYaml = _interopRequireWildcard(require("js-yaml"));
|
|
11
10
|
var _promises = require("node:fs/promises");
|
|
12
11
|
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
@@ -17,6 +16,8 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
|
|
|
17
16
|
const debug = (0, _debug.default)('@automattic/vip:bin:dev-environment');
|
|
18
17
|
const CONFIGURATION_FILE_NAME = exports.CONFIGURATION_FILE_NAME = 'vip-dev-env.yml';
|
|
19
18
|
const CONFIGURATION_TEMPLATE_FILE_NAME = exports.CONFIGURATION_TEMPLATE_FILE_NAME = 'vip-dev-env.yml.ejs';
|
|
19
|
+
const ALLOWED_CONFIG_DIR_TEMPLATE = /<%=\s*configDir\s*%>/g;
|
|
20
|
+
const EJS_TEMPLATE_TAG = /<%[\s\S]*?%>/;
|
|
20
21
|
async function getConfigurationFileOptions() {
|
|
21
22
|
const configurationFile = await findConfigurationFile();
|
|
22
23
|
if (configurationFile === false) {
|
|
@@ -176,26 +177,30 @@ async function findConfigurationFile() {
|
|
|
176
177
|
file,
|
|
177
178
|
template
|
|
178
179
|
} of locations) {
|
|
180
|
+
let contents;
|
|
179
181
|
try {
|
|
180
182
|
// eslint-disable-next-line no-await-in-loop
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
183
|
+
contents = await (0, _promises.readFile)(file, 'utf8');
|
|
184
|
+
} catch (error) {
|
|
185
|
+
const err = error instanceof Error ? error : new Error('Unknown error');
|
|
186
|
+
debug(`Error reading file ${file}: ${err.message}`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (template) {
|
|
190
|
+
const stripped = contents.replace(ALLOWED_CONFIG_DIR_TEMPLATE, '');
|
|
191
|
+
if (EJS_TEMPLATE_TAG.test(stripped)) {
|
|
192
|
+
exit.withError(`EJS JavaScript is not supported in dev-env configuration file ${file}. ` + 'Use plain YAML, or only the supported <%= configDir %> placeholder.');
|
|
190
193
|
}
|
|
194
|
+
const rendered = contents.replace(ALLOWED_CONFIG_DIR_TEMPLATE, () => dir);
|
|
191
195
|
return {
|
|
192
196
|
configurationPath: file,
|
|
193
|
-
configurationContents:
|
|
197
|
+
configurationContents: rendered
|
|
194
198
|
};
|
|
195
|
-
} catch (error) {
|
|
196
|
-
const err = error instanceof Error ? error : new Error('Unknown error');
|
|
197
|
-
debug(`Error reading or rendering file ${file}: ${err.message}`);
|
|
198
199
|
}
|
|
200
|
+
return {
|
|
201
|
+
configurationPath: file,
|
|
202
|
+
configurationContents: contents
|
|
203
|
+
};
|
|
199
204
|
}
|
|
200
205
|
return false;
|
|
201
206
|
}
|
package/dist/lib/logout.js
CHANGED
|
@@ -3,14 +3,19 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.default = void 0;
|
|
5
5
|
var _http = _interopRequireDefault(require("../lib/api/http"));
|
|
6
|
+
var _tokenCache = _interopRequireDefault(require("../lib/rechallenge/token-cache"));
|
|
6
7
|
var _token = _interopRequireDefault(require("../lib/token"));
|
|
7
8
|
var _tracker = require("../lib/tracker");
|
|
8
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
10
|
var _default = async () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
try {
|
|
12
|
+
await (0, _http.default)('/logout', {
|
|
13
|
+
method: 'post'
|
|
14
|
+
});
|
|
15
|
+
} finally {
|
|
16
|
+
await _token.default.purge();
|
|
17
|
+
await _tokenCache.default.clearAll();
|
|
18
|
+
}
|
|
14
19
|
await (0, _tracker.trackEvent)('logout_command_execute');
|
|
15
20
|
};
|
|
16
21
|
exports.default = _default;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.createSession = createSession;
|
|
5
|
+
exports.exchange = exchange;
|
|
6
|
+
exports.getSessionStatus = getSessionStatus;
|
|
7
|
+
var _debug = _interopRequireDefault(require("debug"));
|
|
8
|
+
var _nodeCrypto = require("node:crypto");
|
|
9
|
+
var _errors = require("./errors");
|
|
10
|
+
var _types = require("./types");
|
|
11
|
+
var _http = _interopRequireDefault(require("../api/http"));
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
+
// Derived from http() rather than imported from a fetch library, so this module
|
|
14
|
+
// keeps compiling across the node-fetch -> undici migration (trunk #2837).
|
|
15
|
+
|
|
16
|
+
const debug = (0, _debug.default)('@automattic/vip:rechallenge:client');
|
|
17
|
+
function fillTemplate(template, challengeId) {
|
|
18
|
+
return template.replaceAll('{challengeId}', encodeURIComponent(challengeId));
|
|
19
|
+
}
|
|
20
|
+
async function parseOrThrow(response, scope) {
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
const text = await response.text();
|
|
23
|
+
throw new _errors.RechallengeHttpError(response.status, text, scope);
|
|
24
|
+
}
|
|
25
|
+
return await response.json();
|
|
26
|
+
}
|
|
27
|
+
async function createSession(opts) {
|
|
28
|
+
debug('createSession scope=%s', opts.requestedOperation);
|
|
29
|
+
const response = await (0, _http.default)(opts.path, {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
// New UUID per call — intent is a fresh session per invocation, not request deduplication.
|
|
33
|
+
'Idempotency-Key': (0, _nodeCrypto.randomUUID)()
|
|
34
|
+
},
|
|
35
|
+
body: {
|
|
36
|
+
clientType: _types.CLIENT_TYPE,
|
|
37
|
+
requestedOperation: opts.requestedOperation
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return parseOrThrow(response, opts.requestedOperation);
|
|
41
|
+
}
|
|
42
|
+
async function getSessionStatus(opts) {
|
|
43
|
+
const path = fillTemplate(opts.template, opts.challengeId);
|
|
44
|
+
const response = await (0, _http.default)(path, {
|
|
45
|
+
method: 'GET'
|
|
46
|
+
});
|
|
47
|
+
return parseOrThrow(response, opts.scope ?? '');
|
|
48
|
+
}
|
|
49
|
+
async function exchange(opts) {
|
|
50
|
+
const path = fillTemplate(opts.template, opts.challengeId);
|
|
51
|
+
const response = await (0, _http.default)(path, {
|
|
52
|
+
method: 'POST'
|
|
53
|
+
});
|
|
54
|
+
return parseOrThrow(response, opts.scope ?? '');
|
|
55
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.RechallengeUnsupportedVersionError = exports.RechallengeTerminalError = exports.RechallengeInteractionRequiredError = exports.RechallengeHttpError = exports.RechallengeError = exports.RechallengeAbortedError = void 0;
|
|
5
|
+
var _types = require("./types");
|
|
6
|
+
class RechallengeError extends Error {
|
|
7
|
+
scope;
|
|
8
|
+
constructor(message, scope) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'RechallengeError';
|
|
11
|
+
this.scope = scope;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.RechallengeError = RechallengeError;
|
|
15
|
+
class RechallengeUnsupportedVersionError extends RechallengeError {
|
|
16
|
+
constructor(version, scope) {
|
|
17
|
+
super(`Server requested rechallenge version "${version}" but this CLI only supports ${_types.RECHALLENGE_VERSION}. Update vip-cli.`, scope);
|
|
18
|
+
this.name = 'RechallengeUnsupportedVersionError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.RechallengeUnsupportedVersionError = RechallengeUnsupportedVersionError;
|
|
22
|
+
class RechallengeTerminalError extends RechallengeError {
|
|
23
|
+
status;
|
|
24
|
+
constructor(status, scope, detail) {
|
|
25
|
+
super(`Step-up verification did not complete (status=${status})${detail ? `: ${detail}` : ''}.`, scope);
|
|
26
|
+
this.name = 'RechallengeTerminalError';
|
|
27
|
+
this.status = status;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.RechallengeTerminalError = RechallengeTerminalError;
|
|
31
|
+
class RechallengeAbortedError extends RechallengeError {
|
|
32
|
+
constructor(scope) {
|
|
33
|
+
super('Step-up verification was cancelled.', scope);
|
|
34
|
+
this.name = 'RechallengeAbortedError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.RechallengeAbortedError = RechallengeAbortedError;
|
|
38
|
+
class RechallengeHttpError extends RechallengeError {
|
|
39
|
+
statusCode;
|
|
40
|
+
bodyText;
|
|
41
|
+
constructor(statusCode, bodyText, scope) {
|
|
42
|
+
super(`Step-up verification request failed (HTTP ${statusCode}): ${bodyText}`, scope);
|
|
43
|
+
this.name = 'RechallengeHttpError';
|
|
44
|
+
this.statusCode = statusCode;
|
|
45
|
+
this.bodyText = bodyText;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.RechallengeHttpError = RechallengeHttpError;
|
|
49
|
+
class RechallengeInteractionRequiredError extends RechallengeError {
|
|
50
|
+
constructor(scope) {
|
|
51
|
+
super(`Step-up verification is required for ${scope}, but this is a non-interactive session. ` + 'Re-run the command interactively, or pass --rechallenge-wait (or set VIP_RECHALLENGE_WAIT=1) ' + 'to print the verification URL and wait for you to complete it on another device.', scope);
|
|
52
|
+
this.name = 'RechallengeInteractionRequiredError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.RechallengeInteractionRequiredError = RechallengeInteractionRequiredError;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.isInteractiveContext = isInteractiveContext;
|
|
5
|
+
exports.runRechallenge = runRechallenge;
|
|
6
|
+
exports.shouldWaitForRechallenge = shouldWaitForRechallenge;
|
|
7
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
8
|
+
var _debug = _interopRequireDefault(require("debug"));
|
|
9
|
+
var _promises = require("node:timers/promises");
|
|
10
|
+
var _tracker = require("../tracker");
|
|
11
|
+
var client = _interopRequireWildcard(require("./client"));
|
|
12
|
+
var _errors = require("./errors");
|
|
13
|
+
var _openBrowser = require("./open-browser");
|
|
14
|
+
var _tokenCache = _interopRequireDefault(require("./token-cache"));
|
|
15
|
+
var _types = require("./types");
|
|
16
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
17
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
|
+
const debug = (0, _debug.default)('@automattic/vip:rechallenge:flow');
|
|
19
|
+
|
|
20
|
+
// Floor for the server-provided poll interval. Guards against a missing/0/NaN
|
|
21
|
+
// value (which would otherwise produce a tight status-poll loop) and clamps
|
|
22
|
+
// implausibly small values so a misbehaving server cannot make us hammer the API.
|
|
23
|
+
const MIN_POLL_INTERVAL_SECONDS = 2;
|
|
24
|
+
const TERMINAL = new Set(['verified', 'expired', 'failed', 'cancelled']);
|
|
25
|
+
async function runRechallenge(opts) {
|
|
26
|
+
const {
|
|
27
|
+
requestedOperation,
|
|
28
|
+
rechallenge,
|
|
29
|
+
interactive,
|
|
30
|
+
wait,
|
|
31
|
+
signal
|
|
32
|
+
} = opts;
|
|
33
|
+
if (rechallenge.version !== _types.RECHALLENGE_VERSION) {
|
|
34
|
+
throw new _errors.RechallengeUnsupportedVersionError(rechallenge.version, requestedOperation);
|
|
35
|
+
}
|
|
36
|
+
await (0, _tracker.trackEvent)('rechallenge_required', {
|
|
37
|
+
scope: requestedOperation,
|
|
38
|
+
clientType: _types.CLIENT_TYPE
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// In a non-interactive session with no opt-in to wait, fail fast instead of
|
|
42
|
+
// creating a verification session no human can complete — automation would
|
|
43
|
+
// otherwise block until the session expires.
|
|
44
|
+
if (!interactive && !wait) {
|
|
45
|
+
await (0, _tracker.trackEvent)('rechallenge_interaction_required', {
|
|
46
|
+
scope: requestedOperation
|
|
47
|
+
});
|
|
48
|
+
throw new _errors.RechallengeInteractionRequiredError(requestedOperation);
|
|
49
|
+
}
|
|
50
|
+
const session = await client.createSession({
|
|
51
|
+
path: rechallenge.createSessionPath,
|
|
52
|
+
requestedOperation
|
|
53
|
+
});
|
|
54
|
+
await (0, _tracker.trackEvent)('rechallenge_session_created', {
|
|
55
|
+
scope: requestedOperation
|
|
56
|
+
});
|
|
57
|
+
const verificationUrl = session.verificationUrl;
|
|
58
|
+
const expiresIso = session.expiresAt;
|
|
59
|
+
if (interactive) {
|
|
60
|
+
await (0, _openBrowser.openBrowser)(verificationUrl);
|
|
61
|
+
console.warn(_chalk.default.yellow('⚠'), `Step-up verification required for ${_chalk.default.bold(requestedOperation)}.`);
|
|
62
|
+
console.warn(` Open ${_chalk.default.cyan(verificationUrl)}`);
|
|
63
|
+
console.warn(` If your browser did not open, copy and paste the URL above. Expires at ${expiresIso}.`);
|
|
64
|
+
} else {
|
|
65
|
+
console.warn(`Step-up verification required for ${requestedOperation}. ` + `Complete it at: ${verificationUrl} (expires at ${expiresIso}).`);
|
|
66
|
+
}
|
|
67
|
+
const requestedInterval = Number(session.pollIntervalSeconds);
|
|
68
|
+
const interval = Math.max(Number.isFinite(requestedInterval) ? requestedInterval : MIN_POLL_INTERVAL_SECONDS, MIN_POLL_INTERVAL_SECONDS) * 1000;
|
|
69
|
+
const deadline = Date.parse(session.expiresAt);
|
|
70
|
+
if (Number.isNaN(deadline)) {
|
|
71
|
+
throw new _errors.RechallengeTerminalError('expired', requestedOperation, 'server returned unparseable expiresAt');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* eslint-disable no-await-in-loop -- polling loop; each iteration must complete before the next */
|
|
75
|
+
while (true) {
|
|
76
|
+
if (signal?.aborted) {
|
|
77
|
+
throw new _errors.RechallengeAbortedError(requestedOperation);
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
await (0, _promises.setTimeout)(interval, undefined, {
|
|
81
|
+
signal
|
|
82
|
+
});
|
|
83
|
+
} catch {
|
|
84
|
+
throw new _errors.RechallengeAbortedError(requestedOperation);
|
|
85
|
+
}
|
|
86
|
+
if (!Number.isNaN(deadline) && Date.now() > deadline) {
|
|
87
|
+
throw new _errors.RechallengeTerminalError('expired', requestedOperation, 'session window elapsed before completion');
|
|
88
|
+
}
|
|
89
|
+
const status = await client.getSessionStatus({
|
|
90
|
+
template: rechallenge.statusPathTemplate,
|
|
91
|
+
challengeId: session.challengeId,
|
|
92
|
+
scope: requestedOperation
|
|
93
|
+
});
|
|
94
|
+
if (!TERMINAL.has(status.status)) {
|
|
95
|
+
debug('still %s; polling again', status.status);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (status.status === 'verified') {
|
|
99
|
+
await (0, _tracker.trackEvent)('rechallenge_verified', {
|
|
100
|
+
scope: requestedOperation,
|
|
101
|
+
provider: status.provider ?? 'unknown'
|
|
102
|
+
});
|
|
103
|
+
const {
|
|
104
|
+
elevatedToken
|
|
105
|
+
} = await client.exchange({
|
|
106
|
+
template: rechallenge.exchangePathTemplate,
|
|
107
|
+
challengeId: session.challengeId,
|
|
108
|
+
scope: requestedOperation
|
|
109
|
+
});
|
|
110
|
+
await (0, _tracker.trackEvent)('rechallenge_exchanged', {
|
|
111
|
+
scope: requestedOperation
|
|
112
|
+
});
|
|
113
|
+
await _tokenCache.default.set(requestedOperation, {
|
|
114
|
+
...elevatedToken,
|
|
115
|
+
headerName: rechallenge.elevatedHeaderName
|
|
116
|
+
});
|
|
117
|
+
return elevatedToken;
|
|
118
|
+
}
|
|
119
|
+
await (0, _tracker.trackEvent)(`rechallenge_${status.status}`, {
|
|
120
|
+
scope: requestedOperation
|
|
121
|
+
});
|
|
122
|
+
throw new _errors.RechallengeTerminalError(status.status, requestedOperation, status.statusReason?.message);
|
|
123
|
+
}
|
|
124
|
+
/* eslint-enable no-await-in-loop */
|
|
125
|
+
}
|
|
126
|
+
function isInteractiveContext(argvOrFlags = process.argv) {
|
|
127
|
+
if (process.env.VIP_NON_INTERACTIVE === '1') {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (argvOrFlags.some(item => item === '--non-interactive' || item.startsWith('--non-interactive=') && !/^--non-interactive=(0|false|no|off)$/i.test(item))) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return Boolean(process.stdout.isTTY);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Opt-in for waiting on step-up in a non-interactive session: the operator can
|
|
137
|
+
// complete browser verification on another device while the command polls.
|
|
138
|
+
// Mirrors isInteractiveContext's argv/env detection because the rechallenge link
|
|
139
|
+
// is instantiated globally and cannot read a command's parsed options.
|
|
140
|
+
function shouldWaitForRechallenge(argvOrFlags = process.argv) {
|
|
141
|
+
if (process.env.VIP_RECHALLENGE_WAIT === '1') {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
return argvOrFlags.some(item => item === '--rechallenge-wait' || item.startsWith('--rechallenge-wait=') && !/^--rechallenge-wait=(0|false|no|off)$/i.test(item));
|
|
145
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
var _exportNames = {
|
|
5
|
+
rechallengeLink: true,
|
|
6
|
+
tokenCache: true
|
|
7
|
+
};
|
|
8
|
+
exports.tokenCache = exports.rechallengeLink = void 0;
|
|
9
|
+
var _link = _interopRequireDefault(require("./link"));
|
|
10
|
+
exports.rechallengeLink = _link.default;
|
|
11
|
+
var _tokenCache = _interopRequireDefault(require("./token-cache"));
|
|
12
|
+
exports.tokenCache = _tokenCache.default;
|
|
13
|
+
var _types = require("./types");
|
|
14
|
+
Object.keys(_types).forEach(function (key) {
|
|
15
|
+
if (key === "default" || key === "__esModule") return;
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
17
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
18
|
+
exports[key] = _types[key];
|
|
19
|
+
});
|
|
20
|
+
var _errors = require("./errors");
|
|
21
|
+
Object.keys(_errors).forEach(function (key) {
|
|
22
|
+
if (key === "default" || key === "__esModule") return;
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
24
|
+
if (key in exports && exports[key] === _errors[key]) return;
|
|
25
|
+
exports[key] = _errors[key];
|
|
26
|
+
});
|
|
27
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|