Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): Support 2FA during publish (WIP)
TODO: unit tests (direct + high-level) refs #1091
- Loading branch information
Showing
12 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
commands/publish/lib/__mocks__/get-two-factor-auth-required.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
// to mock user modules, you _must_ call `jest.mock('./path/to/module')` | ||
module.exports = jest.fn(() => Promise.resolve(false)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
// to mock user modules, you _must_ call `jest.mock('./path/to/module')` | ||
module.exports = jest.fn(() => Promise.resolve("MOCK_OTP")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use strict"; | ||
|
||
const profile = require("libnpm/profile"); | ||
const ValidationError = require("@lerna/validation-error"); | ||
const FetchConfig = require("./fetch-config"); | ||
|
||
module.exports = getTwoFactorAuthRequired; | ||
|
||
function getTwoFactorAuthRequired(_opts) { | ||
const opts = FetchConfig(_opts, { | ||
// don't wait forever for third-party failures to be dealt with | ||
"fetch-retries": 0, | ||
}); | ||
|
||
opts.log.silly("getTwoFactorAuthRequired", opts.registry); | ||
|
||
return profile.get(opts).then(success, failure); | ||
|
||
function success(result) { | ||
opts.log.silly("2FA", result.tfa); | ||
|
||
if (result.tfa.pending) { | ||
// if 2FA is pending, it is disabled | ||
return false; | ||
} | ||
|
||
return result.tfa.mode === "auth-and-writes"; | ||
} | ||
|
||
function failure(err) { | ||
// pass if registry does not support profile endpoint | ||
if (err.code === "E500" || err.code === "E404") { | ||
// most likely a private registry (npm Enterprise, verdaccio, etc) | ||
opts.log.warn( | ||
"EREGISTRY", | ||
"Registry %j does not support `npm profile get`, skipping two-factor auth check...", | ||
// registry | ||
opts.registry | ||
); | ||
|
||
// don't log redundant errors | ||
return false; | ||
} | ||
|
||
// Log the error cleanly to stderr | ||
opts.log.pause(); | ||
console.error(err.message); // eslint-disable-line no-console | ||
opts.log.resume(); | ||
|
||
throw new ValidationError("ETWOFACTOR", "Unable to obtain two-factor auth mode"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
const PromptUtilities = require("@lerna/prompt"); | ||
|
||
module.exports = promptOneTimePassword; | ||
|
||
function promptOneTimePassword() { | ||
// Logic taken from npm internals: https://git.io/fNoMe | ||
return PromptUtilities.input("Enter OTP", { | ||
filter: otp => otp.replace(/\s+/g, ""), | ||
validate: otp => | ||
(otp && /^[\d ]+$|^[A-Fa-f0-9]{64,64}$/.test(otp)) || | ||
"Must be a valid one-time-password. " + | ||
"See https://docs.npmjs.com/getting-started/using-two-factor-authentication", | ||
}); | ||
} |