Skip to content

Commit ad67461

Browse files
committed
utils: add otplease util for otp prompting
1 parent 4cca9cb commit ad67461

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/utils/otplease.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const BB = require('bluebird')
4+
5+
const optCheck = require('figgy-pudding')({
6+
prompt: {default: 'This operation requires a one-time password.\nEnter OTP:'},
7+
otp: {}
8+
})
9+
const readUserInfo = require('./read-user-info.js')
10+
11+
module.exports = otplease
12+
function otplease (opts, fn) {
13+
opts = opts.concat ? opts : optCheck(opts)
14+
return BB.try(() => {
15+
return fn(opts)
16+
}).catch(err => {
17+
if (err.code !== 'EOTP' && !(err.code === 'E401' && /one-time pass/.test(err.body))) {
18+
throw err
19+
} else if (!process.stdin.isTTY || !process.stdout.isTTY) {
20+
throw err
21+
} else {
22+
return readUserInfo.otp(
23+
optCheck(opts).prompt
24+
).then(otp => fn(opts.concat({otp})))
25+
}
26+
})
27+
}

0 commit comments

Comments
 (0)