diff --git a/lib/utils/otplease.js b/lib/utils/otplease.js index 44f9aaa89b7a3..6e6eb961d0491 100644 --- a/lib/utils/otplease.js +++ b/lib/utils/otplease.js @@ -6,11 +6,7 @@ async function otplease (npm, opts, fn) { throw err } - const isClassicOTP = - err.code === 'EOTP' || (err.code === 'E401' && /one-time pass/.test(err.body)) - const isWebOTP = err.code === 'EOTP' && err.body?.authUrl && err.body?.doneUrl - - if (isWebOTP) { + if (isWebOTP(err)) { const webAuth = require('./web-auth') const openUrlPrompt = require('./open-url-prompt') @@ -26,7 +22,7 @@ async function otplease (npm, opts, fn) { return await fn({ ...opts, otp }) } - if (isClassicOTP) { + if (isClassicOTP(err)) { const readUserInfo = require('./read-user-info.js') const otp = await readUserInfo.otp('This operation requires a one-time password.\nEnter OTP:') return await fn({ ...opts, otp }) @@ -36,4 +32,15 @@ async function otplease (npm, opts, fn) { } } +function isWebOTP (err) { + if (!err.code === 'EOTP' || !err.body) { + return false + } + return err.body?.authUrl && err.body?.doneUrl +} + +function isClassicOTP (err) { + return err.code === 'EOTP' || (err.code === 'E401' && /one-time pass/.test(err.body)) +} + module.exports = otplease