Skip to content

Commit 8669d09

Browse files
reggiwraithgar
andauthored
fix: add otplease for enable-2fa, disable-2fa, access (#8228)
The `profile enable-2fa` and `profile disable-2fa` commands a broken. This fixes them by wrapping the calls in `otplease` which handles error headers / prompts with tfa urls. Closes: #8192 --------- Co-authored-by: Gar <gar+gh@danger.computer>
1 parent 57aa89f commit 8669d09

File tree

3 files changed

+129
-76
lines changed

3 files changed

+129
-76
lines changed

lib/commands/access.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ class Access extends BaseCommand {
116116
}
117117

118118
async #grant (permissions, scope, pkg) {
119-
await libnpmaccess.setPermissions(scope, pkg, permissions, this.npm.flatOptions)
119+
await otplease(this.npm, this.npm.flatOptions, async (opts) => {
120+
await libnpmaccess.setPermissions(scope, pkg, permissions, opts)
121+
})
120122
}
121123

122124
async #revoke (scope, pkg) {
123-
await libnpmaccess.removePermissions(scope, pkg, this.npm.flatOptions)
125+
await otplease(this.npm, this.npm.flatOptions, async (opts) => {
126+
await libnpmaccess.removePermissions(scope, pkg, opts)
127+
})
124128
}
125129

126130
async #listPackages (owner, pkg) {

lib/commands/profile.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class Profile extends BaseCommand {
222222
}
223223

224224
async enable2fa (args) {
225+
const conf = { ...this.npm.flatOptions }
226+
225227
if (args.length > 1) {
226228
throw new Error('npm profile enable-2fa [auth-and-writes|auth-only]')
227229
}
@@ -244,9 +246,16 @@ class Profile extends BaseCommand {
244246
)
245247
}
246248

249+
const userInfo = await get(conf)
250+
251+
if (!userInfo?.tfa?.pending && userInfo?.tfa?.mode === mode) {
252+
output.standard('Two factor authentication is already enabled and set to ' + mode)
253+
return
254+
}
255+
247256
const info = {
248257
tfa: {
249-
mode: mode,
258+
mode,
250259
},
251260
}
252261

@@ -296,25 +305,15 @@ class Profile extends BaseCommand {
296305
const password = await readUserInfo.password()
297306
info.tfa.password = password
298307

299-
log.info('profile', 'Determine if tfa is pending')
300-
const userInfo = await get({ ...this.npm.flatOptions })
301-
302-
const conf = { ...this.npm.flatOptions }
303308
if (userInfo && userInfo.tfa && userInfo.tfa.pending) {
304309
log.info('profile', 'Resetting two-factor authentication')
305310
await set({ tfa: { password, mode: 'disable' } }, conf)
306-
} else if (userInfo && userInfo.tfa) {
307-
if (!conf.otp) {
308-
conf.otp = await readUserInfo.otp(
309-
'Enter one-time password: '
310-
)
311-
}
312311
}
313312

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

317-
if (challenge.tfa === null) {
316+
if (challenge.tfa && challenge.tfa.mode) {
318317
output.standard('Two factor authentication mode changed to: ' + mode)
319318
return
320319
}
@@ -358,8 +357,8 @@ class Profile extends BaseCommand {
358357
}
359358

360359
async disable2fa () {
361-
const conf = { ...this.npm.flatOptions }
362-
const info = await get(conf)
360+
const opts = { ...this.npm.flatOptions }
361+
const info = await get(opts)
363362

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

369368
const password = await readUserInfo.password()
370369

371-
if (!conf.otp) {
372-
const msg = 'Enter one-time password: '
373-
conf.otp = await readUserInfo.otp(msg)
374-
}
375-
376370
log.info('profile', 'disabling tfa')
377-
378-
await set({ tfa: { password: password, mode: 'disable' } }, conf)
371+
await otplease(this.npm, opts, o => set({ tfa: { password: password, mode: 'disable' } }, o))
379372

380373
if (this.npm.config.get('json')) {
381374
output.buffer({ tfa: false })

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy