Skip to content

Commit

Permalink
Add OAuth access token authentication support
Browse files Browse the repository at this point in the history
Continuation of repo-utils#28
  • Loading branch information
MadLittleMods committed Aug 29, 2017
1 parent 762abed commit a70f983
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ module.exports = Gitlab;
* @param {Object} options
* - {String} api, api root url, e.g.: 'http://gitlab.com/api/v3'
* - {String} privateToken, You can find or reset your private token in your profile.
* - {String} accessToken, Obtained via OAuth
*/
function Gitlab(options) {
options = options || {};
options.api = options.api || 'https://gitlab.com/api/v3';
RESTFulClient.call(this, options);
this.privateToken = options.privateToken;
this.accessToken = options.accessToken;

this.addResources(resources);

Expand All @@ -45,7 +47,12 @@ function Gitlab(options) {
util.inherits(Gitlab, RESTFulClient);

Gitlab.prototype.setAuthentication = function (req) {
req.params.data.private_token = req.params.data.private_token || this.privateToken;
var accessToken = req.params.data.access_token || this.accessToken;
if (accessToken) {
req.params.data.access_token = accessToken;
} else {
req.params.data.private_token = req.params.data.private_token || this.privateToken;
}
return req;
};

Expand Down
1 change: 1 addition & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
api: process.env.NODE_GITLAB_API || 'https://gitlab.com/api/v3',
privateToken: process.env.NODE_GITLAB_TOKEN || 'enEWf516mA168tP6BiVe',
accessToken: process.env.NODE_GITLAB_ACCESS_TOKEN || 'dbbf3e41770035b126fe203138c94007f7bc15c9ce2dd18766d243eda904dfb3',
requestTimeout: 30000,
};
58 changes: 58 additions & 0 deletions test/gitlab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,66 @@

var should = require('should');
var client = require('./client');
var gitlab = require('../');

describe('gitlab.test.js', function () {
describe('setAuthentication', function () {
var req = {};
beforeEach(function () {
req = {
params: {
data: {}
}
}
});

it('should default to using a private token', function() {
var privateToken = 'private';
gitlab.prototype.setAuthentication.call({
privateToken: privateToken
}, req);

req.params.data.private_token.should.equal(privateToken);
req.params.data.should.not.have.keys('access_token');
});

it('should use access token if provided', function() {
var accessToken = 'access';
gitlab.prototype.setAuthentication.call({
accessToken: accessToken
}, req);

req.params.data.access_token.should.equal(accessToken);
req.params.data.should.not.have.keys('private_token');
});

it('should prefer already passed private token on the request object', function() {
var privateToken = 'private';
var existingPrivateToken = 'already-private';

req.params.data.private_token = existingPrivateToken;
gitlab.prototype.setAuthentication.call({
privateToken: privateToken
}, req);

req.params.data.private_token.should.equal(existingPrivateToken);
req.params.data.should.not.have.keys('access_token');
});

it('should prefer already passed access token on the request object', function() {
var accessToken = 'access';
var existingAccessToken = 'already-access';

req.params.data.access_token = existingAccessToken;
gitlab.prototype.setAuthentication.call({
accessToken: accessToken
}, req);

req.params.data.access_token.should.equal(existingAccessToken);
req.params.data.should.not.have.keys('private_token');
});
});

describe('Client.request()', function () {
it('should request success', function (done) {
client.request('get', '/projects', {}, function (err, projects) {
Expand Down

0 comments on commit a70f983

Please sign in to comment.