From ad67461dc3a73d5ae6569fdbee44c67e1daf86e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Mon, 20 Aug 2018 19:45:49 -0700 Subject: [PATCH] utils: add otplease util for otp prompting --- lib/utils/otplease.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/utils/otplease.js diff --git a/lib/utils/otplease.js b/lib/utils/otplease.js new file mode 100644 index 0000000000000..d0477a896d004 --- /dev/null +++ b/lib/utils/otplease.js @@ -0,0 +1,27 @@ +'use strict' + +const BB = require('bluebird') + +const optCheck = require('figgy-pudding')({ + prompt: {default: 'This operation requires a one-time password.\nEnter OTP:'}, + otp: {} +}) +const readUserInfo = require('./read-user-info.js') + +module.exports = otplease +function otplease (opts, fn) { + opts = opts.concat ? opts : optCheck(opts) + return BB.try(() => { + return fn(opts) + }).catch(err => { + if (err.code !== 'EOTP' && !(err.code === 'E401' && /one-time pass/.test(err.body))) { + throw err + } else if (!process.stdin.isTTY || !process.stdout.isTTY) { + throw err + } else { + return readUserInfo.otp( + optCheck(opts).prompt + ).then(otp => fn(opts.concat({otp}))) + } + }) +}