11cb0ef41Sopenharmony_ciconst log = require('./log-shim') 21cb0ef41Sopenharmony_ciasync function otplease (npm, opts, fn) { 31cb0ef41Sopenharmony_ci try { 41cb0ef41Sopenharmony_ci return await fn(opts) 51cb0ef41Sopenharmony_ci } catch (err) { 61cb0ef41Sopenharmony_ci if (!process.stdin.isTTY || !process.stdout.isTTY) { 71cb0ef41Sopenharmony_ci throw err 81cb0ef41Sopenharmony_ci } 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci if (isWebOTP(err)) { 111cb0ef41Sopenharmony_ci log.disableProgress() 121cb0ef41Sopenharmony_ci const webAuth = require('./web-auth') 131cb0ef41Sopenharmony_ci const openUrlPrompt = require('./open-url-prompt') 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci const openerPromise = (url, emitter) => 161cb0ef41Sopenharmony_ci openUrlPrompt( 171cb0ef41Sopenharmony_ci npm, 181cb0ef41Sopenharmony_ci url, 191cb0ef41Sopenharmony_ci 'Authenticate your account at', 201cb0ef41Sopenharmony_ci 'Press ENTER to open in the browser...', 211cb0ef41Sopenharmony_ci emitter 221cb0ef41Sopenharmony_ci ) 231cb0ef41Sopenharmony_ci const otp = await webAuth(openerPromise, err.body.authUrl, err.body.doneUrl, opts) 241cb0ef41Sopenharmony_ci return await fn({ ...opts, otp }) 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci if (isClassicOTP(err)) { 281cb0ef41Sopenharmony_ci const readUserInfo = require('./read-user-info.js') 291cb0ef41Sopenharmony_ci const otp = await readUserInfo.otp('This operation requires a one-time password.\nEnter OTP:') 301cb0ef41Sopenharmony_ci return await fn({ ...opts, otp }) 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci throw err 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cifunction isWebOTP (err) { 381cb0ef41Sopenharmony_ci if (err.code === 'EOTP' && err.body) { 391cb0ef41Sopenharmony_ci return err.body.authUrl && err.body.doneUrl 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci return false 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cifunction isClassicOTP (err) { 451cb0ef41Sopenharmony_ci return err.code === 'EOTP' || (err.code === 'E401' && /one-time pass/.test(err.body)) 461cb0ef41Sopenharmony_ci} 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_cimodule.exports = otplease 49