From dd3b0ccb837464664248715b95ea6b39365c7a6b Mon Sep 17 00:00:00 2001 From: AlexanderC Date: Wed, 17 May 2017 10:42:43 +0300 Subject: [PATCH] Allow authentication to Travis Pro using GitHub access token --- README.md | 9 ++++++++ lib/travis-encrypt.js | 50 ++++++++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index aecd263..45f61c6 100644 --- a/README.md +++ b/README.md @@ -93,4 +93,13 @@ encrypt({ }, function (err, blob) { // do something with the encrypted data blob... }); + +// ...if you may want to use github token to login to travis pro. +encrypt({ + repo: 'pwmckenna/private-repo', + data: 'EXAMPLE_ENV_VARIABLE=asdf', + token: 'github-token' +}, function (err, blob) { + // do something with the encrypted data blob... +}); ``` diff --git a/lib/travis-encrypt.js b/lib/travis-encrypt.js index ea31978..1914a15 100644 --- a/lib/travis-encrypt.js +++ b/lib/travis-encrypt.js @@ -2,6 +2,9 @@ var rsa = require('ursa'); var merge = require('lodash.merge'); var Travis = require('travis-ci'); +var TOKEN = 'token'; +var CREDS = 'creds'; + function isUndefined (arg) { return typeof arg === 'undefined'; } @@ -28,19 +31,39 @@ function getRepoNotFoundError (options) { return new Error('repository ' + options.owner + '/' + options.repo + ' not found'); } -function encryptTravisProData (options, callback) { +function encryptTravisProData (options, callback, type) { var travis = getTravisClient(true); - travis.authenticate({ - username: options.username, - password: options.password - }, function onTravisAuthResponse (err) { - if (err) { - return callback(err); - } + if (type === CREDS) { + travis.authenticate({ + username: options.username, + password: options.password + }, function onTravisAuthResponse (err) { + if (err) { + return callback(err); + } - getRepoKeyAndEncrypt(travis, options, callback); - }); + getRepoKeyAndEncrypt(travis, options, callback); + }); + } else { + travis.auth.github.post({ + github_token: options.token + }, function (err, res) { + if (err) { + return callback(err); + } + + travis.authenticate({ + access_token: res.access_token + }, function (err) { + if (err) { + return callback(err); + } + + getRepoKeyAndEncrypt(travis, options, callback); + }); + }); + } } function encryptTravisData (options, callback) { @@ -49,7 +72,7 @@ function encryptTravisData (options, callback) { function getRepoKeyAndEncrypt (client, options, callback) { client.repos(options.owner, options.repo).key.get( - function onTravisRepoResponse (keyError, res) { + function onTravisRepoResponse (keyError, res) { if (!keyError) { return encryptData(options.data, res.key, callback); } @@ -76,6 +99,7 @@ function encrypt (options, callback) { var hasUser = !isUndefined(options.username); var hasPass = !isUndefined(options.password); + var hasToken = !isUndefined(options.token); if ((!hasUser && hasPass) || (hasUser && !hasPass)) { return callback(new Error('insufficient github credentials')); @@ -87,8 +111,8 @@ function encrypt (options, callback) { repo: repo[1] }); - if (hasUser && hasPass) { - encryptTravisProData(opts, callback); + if ((hasUser && hasPass) || hasToken) { + encryptTravisProData(opts, callback, hasToken ? TOKEN : CREDS); } else { encryptTravisData(opts, callback); }