-
-
Save roidrage/4319540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var program = require('commander'), | |
request = require('request'), | |
fs = require('fs'), | |
ursa = require('ursa'); | |
program.on('--help', function() { | |
console.log(' Alternative to https://gist.github.com/fbb99b638e9da27fe24d (generate secure environment variables)'); | |
console.log(' Example:'); | |
console.log(''); | |
console.log(' $ node index -r github-user/user-repo -v SECURE=value'); | |
console.log(' $ secure: "<travis-ci base64>"'); | |
console.log(''); | |
}); | |
program | |
.version('0.0.1') | |
.option('-r, --repository <string>', 'github username/repo') | |
.option('-v, --variable <string>', 'variable to protect') | |
.parse(process.argv); | |
program.prompt('Github username: ', function(username) { | |
program.password('Password: ', '*', function(password) { | |
process.stdin.destroy(); | |
githubAuthorization(username, password, function(err, token) { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
console.log('Getting travis-ci github auth'); | |
travisGithubAuth(token, function(err, accessToken) { | |
console.log('Getting travis-ci token'); | |
travisToken(accessToken, function(err, publicKey) { | |
var fixedKey = publicKey.replace(new RegExp('RSA PUBLIC KEY', 'gm'), 'PUBLIC KEY'); | |
var key = ursa.createPublicKey(fixedKey); | |
var encoded = key.encrypt(program.variable, 'utf8', 'base64', ursa.RSA_PKCS1_PADDING); | |
console.log('\nsecure: "' + encoded + '"'); | |
}); | |
}); | |
}); | |
}); | |
}); | |
var githubAuthorization = function(username, password, callback) { | |
var data = { | |
scopes: ['repo'], | |
note: 'temporary token to prove that I am me.' | |
}; | |
request({ | |
method: 'POST', | |
uri: 'https://' + username + ':' + password + '@api.github.com/authorizations', | |
json: data, | |
headers: { | |
'Accept': 'application/vnd.github.beta+json' | |
} | |
},function(err, response, body) { | |
if (err) { | |
return callback(new Error('Invalid username/password')); | |
} | |
return callback(null, body.token); | |
}); | |
}; | |
var travisGithubAuth = function(token, callback) { | |
request({ | |
method: 'POST', | |
uri: 'https://api.travis-ci.com/auth/github', | |
body: 'github_token=' + token | |
},function(err, response, body) { | |
callback(null, JSON.parse(body).access_token); | |
}); | |
}; | |
var travisToken = function(token, callback) { | |
request({ | |
method: 'GET', | |
uri: 'https://api.travis-ci.com/repos/' + program.repository + '/key', | |
headers: { | |
'Authorization': 'token ' + token | |
} | |
},function(err, response, body) { | |
callback(null, JSON.parse(body).key); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment