@@ -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