Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/commands/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,15 @@ class Access extends BaseCommand {
}

async #grant (permissions, scope, pkg) {
await libnpmaccess.setPermissions(scope, pkg, permissions, this.npm.flatOptions)
await otplease(this.npm, this.npm.flatOptions, async (opts) => {
await libnpmaccess.setPermissions(scope, pkg, permissions, opts)
})
}

async #revoke (scope, pkg) {
await libnpmaccess.removePermissions(scope, pkg, this.npm.flatOptions)
await otplease(this.npm, this.npm.flatOptions, async (opts) => {
await libnpmaccess.removePermissions(scope, pkg, opts)
})
}

async #listPackages (owner, pkg) {
Expand Down
38 changes: 16 additions & 22 deletions lib/commands/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class Profile extends BaseCommand {
}

async enable2fa (args) {
const conf = { ...this.npm.flatOptions }

if (args.length > 1) {
throw new Error('npm profile enable-2fa [auth-and-writes|auth-only]')
}
Expand All @@ -244,9 +246,17 @@ class Profile extends BaseCommand {
)
}

log.info('profile', 'Determine if tfa is pending')
const userInfo = await get(conf)

if (!userInfo?.tfa?.pending && userInfo?.tfa?.mode === mode) {
output.standard('Two factor authentication is already enabled and set to ' + mode)
return
}

const info = {
tfa: {
mode: mode,
mode,
},
}

Expand Down Expand Up @@ -296,25 +306,15 @@ class Profile extends BaseCommand {
const password = await readUserInfo.password()
info.tfa.password = password

log.info('profile', 'Determine if tfa is pending')
const userInfo = await get({ ...this.npm.flatOptions })

const conf = { ...this.npm.flatOptions }
if (userInfo && userInfo.tfa && userInfo.tfa.pending) {
log.info('profile', 'Resetting two-factor authentication')
await set({ tfa: { password, mode: 'disable' } }, conf)
} else if (userInfo && userInfo.tfa) {
if (!conf.otp) {
conf.otp = await readUserInfo.otp(
'Enter one-time password: '
)
}
}

log.info('profile', 'Setting two-factor authentication to ' + mode)
const challenge = await set(info, conf)
const challenge = await otplease(this.npm, conf, c => set(info, c))

if (challenge.tfa === null) {
if (challenge.tfa && challenge.tfa.mode) {
output.standard('Two factor authentication mode changed to: ' + mode)
return
}
Expand Down Expand Up @@ -358,8 +358,8 @@ class Profile extends BaseCommand {
}

async disable2fa () {
const conf = { ...this.npm.flatOptions }
const info = await get(conf)
const opts = { ...this.npm.flatOptions }
const info = await get(opts)

if (!info.tfa || info.tfa.pending) {
output.standard('Two factor authentication not enabled.')
Expand All @@ -368,14 +368,8 @@ class Profile extends BaseCommand {

const password = await readUserInfo.password()

if (!conf.otp) {
const msg = 'Enter one-time password: '
conf.otp = await readUserInfo.otp(msg)
}

log.info('profile', 'disabling tfa')

await set({ tfa: { password: password, mode: 'disable' } }, conf)
await otplease(this.npm, opts, o => set({ tfa: { password: password, mode: 'disable' } }, o))

if (this.npm.config.get('json')) {
output.buffer({ tfa: false })
Expand Down
1 change: 0 additions & 1 deletion lib/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const otplease = async (npm, opts, fn) => {
if (!process.stdin.isTTY || !process.stdout.isTTY) {
throw err
}

// web otp
if (err.code === 'EOTP' && err.body?.authUrl && err.body?.doneUrl) {
const { token: otp } = await webAuthOpener(
Expand Down
15 changes: 9 additions & 6 deletions node_modules/npm-registry-fetch/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,25 @@ function regFetch (uri, /* istanbul ignore next */ opts_ = {}) {
noProxy: opts.noProxy,
proxy: opts.httpsProxy || opts.proxy,
retry: opts.retry ? opts.retry : {
retries: opts.fetchRetries,
factor: opts.fetchRetryFactor,
minTimeout: opts.fetchRetryMintimeout,
maxTimeout: opts.fetchRetryMaxtimeout,
retries: opts.fetchRetries,
factor: opts.fetchRetryFactor,
minTimeout: opts.fetchRetryMintimeout,
maxTimeout: opts.fetchRetryMaxtimeout,
},
strictSSL: opts.strictSSL,
timeout: opts.timeout || 30 * 1000,
}).then(res => checkResponse({
}).then(res => {
console.log('Response Headers:', res.headers.raw());
return checkResponse({
method,
uri,
res,
registry,
startTime,
auth,
opts,
}))
});
})

if (typeof opts.otpPrompt === 'function') {
return p.catch(async er => {
Expand Down
Loading
Loading