Skip to content

Commit 573ca50

Browse files
committed
access: Add support for requiring per-package 2fa
Credit: @iarna PR-URL: #11
1 parent 1a2ba28 commit 573ca50

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

doc/cli/npm-access.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ npm-access(1) -- Set access level on published packages
66
npm access public [<package>]
77
npm access restricted [<package>]
88

9+
npm access 2fa-required [<package>]
10+
npm access 2fa-not-required [<package>]
11+
912
npm access grant <read-only|read-write> <scope:team> [<package>]
1013
npm access revoke <scope:team> [<package>]
1114

@@ -24,6 +27,10 @@ subcommand.
2427
* public / restricted:
2528
Set a package to be either publicly accessible or restricted.
2629

30+
* 2fa-required / 2fa-not-required:
31+
Set a package as requiring or not requiring that the publisher have two
32+
factor authentication enabled on their account.
33+
2734
* grant / revoke:
2835
Add or remove the ability of users and teams to have read-only or read-write
2936
access to a package.

lib/access.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var readPackageJson = require('read-package-json')
77
var mapToRegistry = require('./utils/map-to-registry.js')
88
var npm = require('./npm.js')
99
var output = require('./utils/output.js')
10+
const readUserInfo = require('./utils/read-user-info.js')
11+
const Bluebird = require('bluebird')
12+
const registryAccess = Bluebird.promisify(npm.registry.access.bind(npm.registry))
1013

1114
var whoami = require('./whoami')
1215

@@ -17,6 +20,8 @@ access.usage =
1720
'npm access restricted [<package>]\n' +
1821
'npm access grant <read-only|read-write> <scope:team> [<package>]\n' +
1922
'npm access revoke <scope:team> [<package>]\n' +
23+
'npm access 2fa-required <package>\n' +
24+
'npm access 2fa-not-required <package>\n' +
2025
'npm access ls-packages [<user>|<scope>|<scope:team>]\n' +
2126
'npm access ls-collaborators [<package> [<user>]]\n' +
2227
'npm access edit [<package>]'
@@ -61,17 +66,22 @@ function access (args, cb) {
6166

6267
function invokeCmd (err, uri, auth, base) {
6368
if (err) { return cb(err) }
64-
params.auth = auth
65-
try {
66-
return npm.registry.access(cmd, uri, params, function (err, data) {
67-
if (!err && data) {
68-
output(JSON.stringify(data, undefined, 2))
69-
}
70-
cb(err, data)
69+
return Bluebird.try(() => {
70+
params.auth = auth
71+
try {
72+
return registryAccess(cmd, uri, params)
73+
} catch (err) {
74+
throw err.message + '\n\nUsage:\n' + access.usage
75+
}
76+
}).catch(err => {
77+
if (err.code !== 'EOTP') throw err
78+
return readUserInfo.otp('Enter OTP: ').then(otp => {
79+
params.auth.otp = otp
80+
return registryAccess(cmd, uri, params)
7181
})
72-
} catch (e) {
73-
cb(e.message + '\n\nUsage:\n' + access.usage)
74-
}
82+
}).then(data => {
83+
return output(data ? JSON.stringify(data, undefined, 2) : '"ok"')
84+
}).asCallback(cb)
7585
}
7686
}
7787

0 commit comments

Comments
 (0)