diff --git a/lib/auth/sso.js b/lib/auth/sso.js index 9812a18cb99ca..621ead5d21b65 100644 --- a/lib/auth/sso.js +++ b/lib/auth/sso.js @@ -52,7 +52,7 @@ const login = async (npm, { creds, registry, scope }) => { authType: ssoType, } - const { token, sso } = await otplease(opts, + const { token, sso } = await otplease(npm, opts, opts => profile.loginCouch(auth.username, auth.password, opts) ) diff --git a/lib/commands/access.js b/lib/commands/access.js index bc8ce48bacdad..0a80da8ddd006 100644 --- a/lib/commands/access.js +++ b/lib/commands/access.js @@ -180,7 +180,7 @@ class Access extends BaseCommand { modifyPackage (pkg, opts, fn, requireScope = true) { return this.getPackage(pkg, requireScope) - .then(pkgName => otplease(opts, opts => fn(pkgName, opts))) + .then(pkgName => otplease(this.npm, opts, opts => fn(pkgName, opts))) } async getPackage (name, requireScope) { diff --git a/lib/commands/deprecate.js b/lib/commands/deprecate.js index 862c214dbe592..068bfdbcec717 100644 --- a/lib/commands/deprecate.js +++ b/lib/commands/deprecate.js @@ -60,7 +60,7 @@ class Deprecate extends BaseCommand { packument.versions[v].deprecated = msg }) - return otplease(this.npm.flatOptions, opts => fetch(uri, { + return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, { ...opts, spec: p, method: 'PUT', diff --git a/lib/commands/dist-tag.js b/lib/commands/dist-tag.js index e74a3f1d435c9..8052e4f7e4e38 100644 --- a/lib/commands/dist-tag.js +++ b/lib/commands/dist-tag.js @@ -116,7 +116,7 @@ class DistTag extends BaseCommand { }, spec, } - await otplease(reqOpts, reqOpts => regFetch(url, reqOpts)) + await otplease(this.npm, reqOpts, reqOpts => regFetch(url, reqOpts)) this.npm.output(`+${t}: ${spec.name}@${version}`) } @@ -142,7 +142,7 @@ class DistTag extends BaseCommand { method: 'DELETE', spec, } - await otplease(reqOpts, reqOpts => regFetch(url, reqOpts)) + await otplease(this.npm, reqOpts, reqOpts => regFetch(url, reqOpts)) this.npm.output(`-${tag}: ${spec.name}@${version}`) } diff --git a/lib/commands/hook.js b/lib/commands/hook.js index a4619802d8429..bb3a34b8d2d1b 100644 --- a/lib/commands/hook.js +++ b/lib/commands/hook.js @@ -22,7 +22,7 @@ class Hook extends BaseCommand { static ignoreImplicitWorkspace = true async exec (args) { - return otplease({ + return otplease(this.npm, { ...this.npm.flatOptions, }, (opts) => { switch (args[0]) { diff --git a/lib/commands/org.js b/lib/commands/org.js index e2202a9e9cf3b..599b4b9c8758a 100644 --- a/lib/commands/org.js +++ b/lib/commands/org.js @@ -33,7 +33,7 @@ class Org extends BaseCommand { } async exec ([cmd, orgname, username, role], cb) { - return otplease({ + return otplease(this.npm, { ...this.npm.flatOptions, }, opts => { switch (cmd) { diff --git a/lib/commands/owner.js b/lib/commands/owner.js index 732bb40a30050..824b64e044ecf 100644 --- a/lib/commands/owner.js +++ b/lib/commands/owner.js @@ -202,7 +202,7 @@ class Owner extends BaseCommand { const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}` try { - const res = await otplease(this.npm.flatOptions, opts => { + const res = await otplease(this.npm, this.npm.flatOptions, opts => { return npmFetch.json(dataPath, { ...opts, method: 'PUT', diff --git a/lib/commands/profile.js b/lib/commands/profile.js index fcf0eb7d53fa6..27060cf73a650 100644 --- a/lib/commands/profile.js +++ b/lib/commands/profile.js @@ -221,7 +221,7 @@ class Profile extends BaseCommand { newUser[prop] = value - const result = await otplease(conf, conf => npmProfile.set(newUser, conf)) + const result = await otplease(this.npm, conf, conf => npmProfile.set(newUser, conf)) if (this.npm.config.get('json')) { this.npm.output(JSON.stringify({ [prop]: result[prop] }, null, 2)) diff --git a/lib/commands/publish.js b/lib/commands/publish.js index 579f5d6e74e67..4310dba258d95 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -116,7 +116,7 @@ class Publish extends BaseCommand { log.notice('', `Publishing to ${outputRegistry}${dryRun ? ' (dry-run)' : ''}`) if (!dryRun) { - await otplease(opts, opts => libpub(manifest, tarballData, opts)) + await otplease(this.npm, opts, opts => libpub(manifest, tarballData, opts)) } if (spec.type === 'directory' && !ignoreScripts) { diff --git a/lib/commands/team.js b/lib/commands/team.js index 640002456acad..2d4fc663715e4 100644 --- a/lib/commands/team.js +++ b/lib/commands/team.js @@ -44,7 +44,7 @@ class Team extends BaseCommand { // XXX: "description" option to libnpmteam is used as a description of the // team, but in npm's options, this is a boolean meaning "show the // description in npm search output". Hence its being set to null here. - await otplease({ ...this.npm.flatOptions }, opts => { + await otplease(this.npm, { ...this.npm.flatOptions }, opts => { entity = entity.replace(/^@/, '') switch (cmd) { case 'create': return this.create(entity, opts) diff --git a/lib/commands/token.js b/lib/commands/token.js index edfb07b9d3a9a..cf3b8cbee53a4 100644 --- a/lib/commands/token.js +++ b/lib/commands/token.js @@ -121,7 +121,7 @@ class Token extends BaseCommand { }) await Promise.all( toRemove.map(key => { - return otplease(conf, conf => { + return otplease(this.npm, conf, conf => { return profile.removeToken(key, conf) }) }) @@ -146,7 +146,7 @@ class Token extends BaseCommand { const validCIDR = this.validateCIDRList(cidr) log.info('token', 'creating') return pulseTillDone.withPromise( - otplease(conf, conf => { + otplease(this.npm, conf, conf => { return profile.createToken(password, readonly, validCIDR, conf) }) ) diff --git a/lib/commands/unpublish.js b/lib/commands/unpublish.js index ab929d98cadfa..0e5ef3dc5e91d 100644 --- a/lib/commands/unpublish.js +++ b/lib/commands/unpublish.js @@ -130,7 +130,7 @@ class Unpublish extends BaseCommand { } if (!dryRun) { - await otplease(opts, opts => libunpub(spec, opts)) + await otplease(this.npm, opts, opts => libunpub(spec, opts)) } if (!silent) { this.npm.output(`- ${pkgName}${pkgVersion}`) diff --git a/lib/utils/otplease.js b/lib/utils/otplease.js index d8db8add9c4cc..790f74bd9704f 100644 --- a/lib/utils/otplease.js +++ b/lib/utils/otplease.js @@ -1,4 +1,4 @@ -async function otplease (opts, fn) { +async function otplease (npm, opts, fn) { try { return await fn(opts) } catch (err) { diff --git a/test/lib/utils/otplease.js b/test/lib/utils/otplease.js index 025084ab4f2c5..de85fc6c5914e 100644 --- a/test/lib/utils/otplease.js +++ b/test/lib/utils/otplease.js @@ -11,7 +11,7 @@ const otplease = t.mock('../../../lib/utils/otplease.js', { t.test('returns function results on success', async (t) => { const fn = () => 'test string' - const result = await otplease({}, fn) + const result = await otplease(null, {}, fn) t.equal('test string', result) }) @@ -26,7 +26,7 @@ t.test('returns function results on otp success', async (t) => { } throw Object.assign(new Error('nope'), { code: 'EOTP' }) } - const result = await otplease({}, fn) + const result = await otplease(null, {}, fn) t.equal('success', result) }) @@ -51,7 +51,7 @@ t.test('prompts for otp for EOTP', async (t) => { t.end() } - await otplease({ some: 'prop' }, fn) + await otplease(null, { some: 'prop' }, fn) }) t.test('prompts for otp for 401', async (t) => { @@ -78,7 +78,7 @@ t.test('prompts for otp for 401', async (t) => { t.end() } - await otplease({ some: 'prop' }, fn) + await otplease(null, { some: 'prop' }, fn) }) t.test('does not prompt for non-otp errors', async (t) => { @@ -95,7 +95,11 @@ t.test('does not prompt for non-otp errors', async (t) => { throw new Error('nope') } - t.rejects(otplease({ some: 'prop' }, fn), { message: 'nope' }, 'rejects with the original error') + t.rejects( + otplease(null, { some: 'prop' }, fn), + { message: 'nope' }, + 'rejects with the original error' + ) }) t.test('does not prompt if stdin or stdout is not a tty', async (t) => { @@ -112,5 +116,9 @@ t.test('does not prompt if stdin or stdout is not a tty', async (t) => { throw Object.assign(new Error('nope'), { code: 'EOTP' }) } - t.rejects(otplease({ some: 'prop' }, fn), { message: 'nope' }, 'rejects with the original error') + t.rejects( + otplease(null, { some: 'prop' }, fn), + { message: 'nope' }, + 'rejects with the original error' + ) })