Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit 9dae683

Browse files
committed
Add OAuth access token authentication support
Continuation of repo-utils#28
1 parent 762abed commit 9dae683

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

lib/gitlab.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ module.exports = Gitlab;
2727
* @param {Object} options
2828
* - {String} api, api root url, e.g.: 'http://gitlab.com/api/v3'
2929
* - {String} privateToken, You can find or reset your private token in your profile.
30+
* - {String} accessToken, Obtained via OAuth
3031
*/
3132
function Gitlab(options) {
3233
options = options || {};
3334
options.api = options.api || 'https://gitlab.com/api/v3';
3435
RESTFulClient.call(this, options);
3536
this.privateToken = options.privateToken;
37+
this.accessToken = options.accessToken;
3638

3739
this.addResources(resources);
3840

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

4749
Gitlab.prototype.setAuthentication = function (req) {
48-
req.params.data.private_token = req.params.data.private_token || this.privateToken;
50+
var accessToken = req.params.data.access_token || this.accessToken;
51+
if (accessToken) {
52+
req.params.data.access_token = accessToken;
53+
} else {
54+
req.params.data.private_token = req.params.data.private_token || this.privateToken;
55+
}
4956
return req;
5057
};
5158

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"test": "mocha --harmony -R spec -r co-mocha -t 40000 test/*.test.js",
88
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -r co-mocha -t 40000 test/*.test.js",
99
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -r co-mocha -t 40000 test/*.test.js",
10+
"mocha": "mocha",
1011
"jshint": "jshint .",
1112
"autod": "autod -w --prefix '~'",
1213
"cnpm": "npm install --registry=https://registry.npm.taobao.org",

test/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
api: process.env.NODE_GITLAB_API || 'https://gitlab.com/api/v3',
33
privateToken: process.env.NODE_GITLAB_TOKEN || 'enEWf516mA168tP6BiVe',
4+
accessToken: process.env.NODE_GITLAB_ACCESS_TOKEN || 'dbbf3e41770035b126fe203138c94007f7bc15c9ce2dd18766d243eda904dfb3',
45
requestTimeout: 30000,
56
};

test/gitlab.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,66 @@
1616

1717
var should = require('should');
1818
var client = require('./client');
19+
var gitlab = require('../');
1920

2021
describe('gitlab.test.js', function () {
22+
describe('setAuthentication', function () {
23+
var req = {};
24+
beforeEach(function () {
25+
req = {
26+
params: {
27+
data: {}
28+
}
29+
}
30+
});
31+
32+
it('should default to using a private token', function() {
33+
var privateToken = 'private';
34+
gitlab.prototype.setAuthentication.call({
35+
privateToken: privateToken
36+
}, req);
37+
38+
req.params.data.private_token.should.equal(privateToken);
39+
req.params.data.should.not.have.keys('access_token');
40+
});
41+
42+
it('should use access token if provided', function() {
43+
var accessToken = 'access';
44+
gitlab.prototype.setAuthentication.call({
45+
accessToken: accessToken
46+
}, req);
47+
48+
req.params.data.access_token.should.equal(accessToken);
49+
req.params.data.should.not.have.keys('private_token');
50+
});
51+
52+
it('should prefer already passed private token on the request object', function() {
53+
var privateToken = 'private';
54+
var existingPrivateToken = 'already-private';
55+
56+
req.params.data.private_token = existingPrivateToken;
57+
gitlab.prototype.setAuthentication.call({
58+
privateToken: privateToken
59+
}, req);
60+
61+
req.params.data.private_token.should.equal(existingPrivateToken);
62+
req.params.data.should.not.have.keys('access_token');
63+
});
64+
65+
it('should prefer already passed access token on the request object', function() {
66+
var accessToken = 'access';
67+
var existingAccessToken = 'already-access';
68+
69+
req.params.data.access_token = existingAccessToken;
70+
gitlab.prototype.setAuthentication.call({
71+
accessToken: accessToken
72+
}, req);
73+
74+
req.params.data.access_token.should.equal(existingAccessToken);
75+
req.params.data.should.not.have.keys('private_token');
76+
});
77+
});
78+
2179
describe('Client.request()', function () {
2280
it('should request success', function (done) {
2381
client.request('get', '/projects', {}, function (err, projects) {

0 commit comments

Comments
 (0)