heroku-cli-util
Advanced tools
Comparing version 5.7.1 to 5.7.2
@@ -7,2 +7,4 @@ 'use strict'; | ||
let url = require('url'); | ||
let spawn = require('child_process').spawn; | ||
let exec = require('child_process').exec; | ||
@@ -33,2 +35,5 @@ function apiMiddleware (response, cb) { | ||
} | ||
if (context.reason) { | ||
opts.headers = cli.extend(opts.headers, {'X-Heroku-Sudo-Reason': context.reason}); | ||
} | ||
return new Heroku(opts); | ||
@@ -61,2 +66,54 @@ } | ||
function login () { | ||
return new Promise(function (fulfill, error) { | ||
spawn('heroku', ['login'], {stdio: 'inherit'}) | ||
.on('close', function (e) { | ||
if (e === 0) fulfill(); | ||
else error(new Error('Authorization failed.')); | ||
}); | ||
}); | ||
} | ||
function getNewAPIKey () { | ||
return new Promise(function (fulfill, reject) { | ||
exec('heroku auth:token', function (error, stdout, stderr) { | ||
if (stderr) console.error(stderr); | ||
if (error) reject(error); | ||
fulfill(stdout.trim()); | ||
}); | ||
}); | ||
} | ||
function relogin () { | ||
if (process.env.HEROKU_API_KEY) { | ||
cli.error(`API key is currently set by the HEROKU_API_KEY environment variable. | ||
Ensure this is set to a correct value or unset it to use the netrc file.`); | ||
process.exit(1); | ||
} | ||
return login().then(getNewAPIKey); | ||
} | ||
function twoFactorPrompt(options, context) { | ||
cli.yubikey.enable(); | ||
return cli.prompt('Two-factor code', {mask: true}) | ||
.then(function (secondFactor) { | ||
cli.yubikey.disable(); | ||
return secondFactor; | ||
}) | ||
.then(function (secondFactor) { | ||
if (options.preauth) { | ||
return cli.preauth(context.app, heroku(context), secondFactor); | ||
} else { | ||
context.secondFactor = secondFactor; | ||
} | ||
}); | ||
} | ||
function reasonPrompt(context) { | ||
return cli.prompt('Reason') | ||
.then(function (reason) { | ||
context.reason = reason; | ||
}); | ||
} | ||
module.exports = function command (options, fn) { | ||
@@ -74,21 +131,16 @@ return function (context) { | ||
.catch(function (err) { | ||
if (err && err.body && err.body.id === 'two_factor') { | ||
cli.yubikey.enable(); | ||
cli.prompt('Two-factor code', {mask: true}) | ||
.then(function (secondFactor) { | ||
cli.yubikey.disable(); | ||
return secondFactor; | ||
}) | ||
.then(function (secondFactor) { | ||
if (options.preauth) { | ||
return cli.preauth(context.app, heroku(context), secondFactor); | ||
} else { | ||
context.secondFactor = secondFactor; | ||
} | ||
}) | ||
if (err && err.body && err.body.id === 'unauthorized') { | ||
cli.error(err.body.message || 'Unauthorized'); | ||
relogin() | ||
.then(apiKey => context.auth = {password: apiKey}) | ||
.then(run) | ||
.catch(handleErr); | ||
return; | ||
} | ||
throw err; | ||
else if (err && err.body && err.body.id === 'sudo_reason_required') { | ||
cli.warn(err.body.message); | ||
reasonPrompt(context).then(run).catch(handleErr); | ||
} | ||
else if (err && err.body && err.body.id === 'two_factor') { | ||
twoFactorPrompt(options, context).then(run).catch(handleErr); | ||
} else throw err; | ||
}).catch(handleErr); | ||
@@ -95,0 +147,0 @@ }; |
{ | ||
"name": "heroku-cli-util", | ||
"version": "5.7.1", | ||
"version": "5.7.2", | ||
"description": "Set of helpful CLI utilities", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75349
1835
11
4