otplease.js 721 B

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