Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable 2FA when publishing a new package #346

Merged
merged 9 commits into from
Feb 22, 2019
Merged

Enable 2FA when publishing a new package #346

merged 9 commits into from
Feb 22, 2019

Conversation

SamVerschueren
Copy link
Collaborator

This PR fixes #288 by adding a new step only if the package is new and the package is not private.

I also moved npm specific parts to an npm directory. We might want to do the same for git? I can revert it but to me it looks cleaner this way.

I also noticed that almost every time, the OTP of the publish package will be the same as for the 2FA. So maybe we should just try to re-use the first OTP, if that fails, it will still just ask for a new OTP. But in 99% of the use cases, this would only require to enter the OTP once. What do you think?

@@ -79,6 +79,7 @@ process.on('SIGINT', () => {
{
...cli.flags,
confirm: true,
exists: !isAvailable,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need this, and it is returned from the ui() step, so makes sense to add it here as well.

@@ -15,7 +15,8 @@ const pkgDir = require('pkg-dir');
const hostedGitInfo = require('hosted-git-info');
const prerequisiteTasks = require('./prerequisite-tasks');
const gitTasks = require('./git-tasks');
const publishTaskHelper = require('./publish-task-helper');
const publish = require('./npm/publish');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could merge these two into

const {publish, enable2fa} = require('./npm');

I don't have a strong opinion, this might be a bit cleaner.

const {throwError} = require('rxjs');
const {catchError} = require('rxjs/operators');

const handleNpmError = (error, task, message, executor) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted this out of the publish-task-helper as it's necessary for every npm command which uses OTP.

const versions = JSON.parse(await execa.stdout('npm', ['version', '--json']));

if (version.satisfies(versions.npm, '<6.5.0')) {
throw new Error(`npm@${versions.npm} does not support enabling two-factor authentication on new repositories`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the error messages. Should we help the user and tell which version he should install? Currently, only npm@6.5.0 works. npm@6.8.0 will probably work as well, but it's not released yet.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm@6.8.0 will probably work as well, but it's not released yet.

It is: https://github.com/npm/cli/releases/tag/v6.8.0

I think we should just drop this error message and require npm@6.8.0 for everyone.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, good idea. When I pushed this yesterday, v6.8 wasn't released yet so I had to add this because otherwise it was untestable :).

@sindresorhus
Copy link
Owner

So maybe we should just try to re-use the first OTP, if that fails, it will still just ask for a new OTP. But in 99% of the use cases, this would only require to enter the OTP once. What do you think?

👍

@sindresorhus sindresorhus changed the title Enable 2FA when publishing a new package - fixes #288 Enable 2FA when publishing a new package Feb 14, 2019
@SamVerschueren
Copy link
Collaborator Author

Because of the npm version check, the tests fail on Travis. Should we do a npm i -g npm@6.5.0 in the travis.yml file as pretest script?

@sindresorhus
Copy link
Owner

Should we do a npm i -g npm@6.5.0 in the travis.yml file as pretest script?

npm install --global npm@6.8.0

const versions = JSON.parse(await execa.stdout('npm', ['version', '--json']));

if (version.satisfies(versions.npm, '<6.8.0')) {
throw new Error('Please upgrade to npm@6.8.0 or newer');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the error message here...

@SamVerschueren
Copy link
Collaborator Author

  • Dropped the npm@6.5.0 check
  • Added re-use of the OTP provided in the npm publish step
  • Made sure npm prerelease tags are correctly handled in the npm version check

@sindresorhus sindresorhus merged commit d10ffcf into master Feb 22, 2019
@sindresorhus sindresorhus deleted the iss288 branch February 22, 2019 17:49
@sindresorhus
Copy link
Owner

Tested this when publishing a new package now and it worked perfectly 👌🎉

@sindresorhus
Copy link
Owner

@SamVerschueren You need to submit the PR URL to IssueHunt to claim the bounty.

@itaisteinherz
Copy link
Collaborator

Why are source/npm/util.js and source/npm-util.js separate files? I can't see a reason why the latter shouldn't be merged into the former.

@sindresorhus
Copy link
Owner

@itaisteinherz I agree, it's weird to have it like that. Let's rename npm-util.js to handle-npm-error.js and move npm-util.js into the npm directory.

@mehulkar
Copy link

mehulkar commented Jun 5, 2019

Hi all, I'm using np against an internal, private NPM registry that does not support 2FA. How do I disable this feature? I just updated to np 5.x and am unable to publish packages now as it fails the 2FA check

@itaisteinherz
Copy link
Collaborator

@mehulkar See #398 (and #424, #427). If you need this ASAP, feel free to open a PR that addresses #398 (comment) and we'll gladly review it and try to get it landed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable 2FA when publishing a new package
4 participants