otplease.js 612 B

1234567891011121314151617181920
  1. const prompt = 'This operation requires a one-time password.\nEnter OTP:'
  2. const readUserInfo = require('./read-user-info.js')
  3. const isOtpError = err =>
  4. err.code === 'EOTP' || (err.code === 'E401' && /one-time pass/.test(err.body))
  5. module.exports = otplease
  6. function otplease (opts, fn) {
  7. opts = { prompt, ...opts }
  8. return Promise.resolve().then(() => fn(opts)).catch(err => {
  9. if (!isOtpError(err))
  10. throw err
  11. else if (!process.stdin.isTTY || !process.stdout.isTTY)
  12. throw err
  13. else {
  14. return readUserInfo.otp(opts.prompt)
  15. .then(otp => fn({ ...opts, otp }))
  16. }
  17. })
  18. }