-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): Eager prompt for OTP when account-level 2FA is enabled
- Loading branch information
Showing
15 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
commands/publish/__tests__/get-two-factor-auth-required.test.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,113 @@ | ||
"use strict"; | ||
|
||
jest.mock("../lib/get-profile-data"); | ||
|
||
const loggingOutput = require("@lerna-test/logging-output"); | ||
const getProfileData = require("../lib/get-profile-data"); | ||
const getTwoFactorAuthRequired = require("../lib/get-two-factor-auth-required"); | ||
|
||
getProfileData.mockImplementation(() => Promise.resolve({ tfa: {} })); | ||
|
||
expect.extend(require("@lerna-test/figgy-pudding-matchers")); | ||
|
||
describe("getTwoFactorAuthRequired", () => { | ||
const origConsoleError = console.error; | ||
|
||
beforeEach(() => { | ||
console.error = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
console.error = origConsoleError; | ||
}); | ||
|
||
it("resolves true if tfa.mode === 'auth-and-writes'", async () => { | ||
getProfileData.mockResolvedValueOnce({ | ||
tfa: { | ||
mode: "auth-and-writes", | ||
}, | ||
}); | ||
|
||
const result = await getTwoFactorAuthRequired(); | ||
expect(result).toBe(true); | ||
expect(getProfileData).toHaveBeenLastCalledWith(expect.figgyPudding({ "fetch-retries": 0 })); | ||
}); | ||
|
||
it("resolves false if tfa.mode !== 'auth-and-writes'", async () => { | ||
getProfileData.mockResolvedValueOnce({ | ||
tfa: { | ||
mode: "auth-only", | ||
}, | ||
}); | ||
|
||
const result = await getTwoFactorAuthRequired(); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("resolves false if tfa.pending === true", async () => { | ||
getProfileData.mockResolvedValueOnce({ | ||
tfa: { | ||
pending: true, | ||
mode: "ignored", | ||
}, | ||
}); | ||
|
||
const result = await getTwoFactorAuthRequired(); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("resolves false after profile 404", async () => { | ||
getProfileData.mockImplementationOnce(() => { | ||
const err = new Error("third-party profile fail"); | ||
|
||
err.code = "E404"; | ||
|
||
return Promise.reject(err); | ||
}); | ||
|
||
const result = await getTwoFactorAuthRequired(); | ||
|
||
expect(result).toBe(false); | ||
expect(console.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("resolves false after profile 500", async () => { | ||
getProfileData.mockImplementationOnce(() => { | ||
const err = new Error("legacy npm Enterprise profile fail"); | ||
|
||
err.code = "E500"; | ||
|
||
return Promise.reject(err); | ||
}); | ||
|
||
const opts = new Map([["registry", "such-registry-wow"]]); | ||
const result = await getTwoFactorAuthRequired(opts); | ||
|
||
expect(result).toBe(false); | ||
expect(loggingOutput("warn")).toContain( | ||
`Registry "${opts.get( | ||
"registry" | ||
)}" does not support 'npm profile get', skipping two-factor auth check...` | ||
); | ||
}); | ||
|
||
it("logs unexpected failure message before throwing validation error", async () => { | ||
getProfileData.mockImplementationOnce(() => { | ||
const err = new Error("zomg explosions"); | ||
|
||
err.code = "E401"; | ||
|
||
return Promise.reject(err); | ||
}); | ||
|
||
try { | ||
await getTwoFactorAuthRequired(); | ||
} catch (err) { | ||
expect(err.prefix).toBe("ETWOFACTOR"); | ||
expect(err.message).toBe("Unable to obtain two-factor auth mode"); | ||
expect(console.error).toHaveBeenCalledWith("zomg explosions"); | ||
} | ||
|
||
expect.assertions(3); | ||
}); | ||
}); |
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
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
6 changes: 6 additions & 0 deletions
6
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,6 @@ | ||
"use strict"; | ||
|
||
// to mock user modules, you _must_ call `jest.mock('./path/to/module')` | ||
const mockGetTwoFactorAuthRequired = jest.fn(() => Promise.resolve(false)); | ||
|
||
module.exports = mockGetTwoFactorAuthRequired; |
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,50 @@ | ||
"use strict"; | ||
|
||
const ValidationError = require("@lerna/validation-error"); | ||
const FetchConfig = require("./fetch-config"); | ||
const getProfileData = require("./get-profile-data"); | ||
|
||
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.info("", "Checking two-factor auth mode"); | ||
|
||
return getProfileData(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 "${opts.registry}" does not support 'npm profile get', skipping two-factor auth check...` | ||
); | ||
|
||
// 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.