From 12b10bad5c86f51cc198addf2168feadf9f37b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=B8ller=20Ellehauge?= Date: Mon, 27 Jun 2022 11:08:06 +0200 Subject: [PATCH] refactor: change flow of otplease --- lib/utils/otplease.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/utils/otplease.js b/lib/utils/otplease.js index 83985b6bc170d..b3f355ea340df 100644 --- a/lib/utils/otplease.js +++ b/lib/utils/otplease.js @@ -1,16 +1,23 @@ +const jsonParse = require('json-parse-even-better-errors') + async function otplease (opts, fn) { try { return await fn(opts) } catch (err) { - const readUserInfo = require('./read-user-info.js') - if (err.code !== 'EOTP' && (err.code !== 'E401' || !/one-time pass/.test(err.body))) { - throw err - } else if (!process.stdin.isTTY || !process.stdout.isTTY) { + if (!process.stdin.isTTY || !process.stdout.isTTY) { throw err - } else { + } + + const isBackwardsCompatibleOTP = err.code === 'E401' && /one-time pass/.test(err.body) + const isClassicOTP = err.code === 'EOTP' && /one-time pass/.test(err.body?.error) + + if (isClassicOTP || isBackwardsCompatibleOTP) { + 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 }) } + + throw err } }