Skip to content

Commit

Permalink
Merge pull request #207 from zitsen/gitlab-provider
Browse files Browse the repository at this point in the history
Add GitLab provider
  • Loading branch information
ldesplat committed Apr 20, 2016
2 parents cd305e9 + 6366a85 commit 3ae0283
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Providers.md
Expand Up @@ -173,6 +173,22 @@ credentials.profile = {
};
```

### GitLab

[Provider Documentation](https://gitlab.com/help/api/oauth2.md)

- `scope`: No default scope.
- `config`:
- `uri`: Point to your gitlab uri. Defaults to `https://gitlab.com`.
- `auth`: /oauth/authorize
- `token`: /oauth/token

The default profile response will look like this:

```javascript
// Defaults to gitlab response (https://gitlab.com/help/api/users.md#current-user)
```

### Google

[Provider Documentation](https://developers.google.com/identity/protocols/OpenIDConnect)
Expand Down
24 changes: 24 additions & 0 deletions lib/providers/gitlab.js
@@ -0,0 +1,24 @@
'use strict';

exports = module.exports = function (options) {

options = options || {};

const uri = options.uri || 'https://gitlab.com';
const user = uri + '/api/v3/user';

return {
protocol: 'oauth2',
auth: uri + '/oauth/authorize',
token: uri + '/oauth/token',
profile: function (credentials, params, get, callback) {

get(user, null, (profile) => {

credentials.profile = profile;

return callback();
});
}
};
};
1 change: 1 addition & 0 deletions lib/providers/index.js
Expand Up @@ -7,6 +7,7 @@ exports = module.exports = {
facebook: require('./facebook'),
foursquare: require('./foursquare'),
github: require('./github'),
gitlab: require('./gitlab'),
google: require('./google'),
instagram: require('./instagram'),
linkedin: require('./linkedin'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"facebook",
"foursquare",
"github",
"gitlab",
"google",
"instagram",
"linkedin",
Expand Down
159 changes: 159 additions & 0 deletions test/providers/gitlab.js
@@ -0,0 +1,159 @@
'use strict';

// Load modules

const Bell = require('../../');
const Code = require('code');
const Hapi = require('hapi');
const Hoek = require('hoek');
const Lab = require('lab');
const Mock = require('../mock');


// Test shortcuts

const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('gitlab', () => {

it('authenticates with mock', { parallel: false }, (done) => {

const mock = new Mock.V2();
mock.start((provider) => {

const server = new Hapi.Server();
server.connection({ host: 'localhost', port: 80 });
server.register(Bell, (err) => {

expect(err).to.not.exist();

const custom = Bell.providers.gitlab();
Hoek.merge(custom, provider);

const profile = {
id: '1234567890',
username: 'steve',
name: 'steve',
email: 'steve@example.com',
state: 'active'
};

Mock.override('https://gitlab.com/api/v3/user', profile);

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'gitlab',
clientSecret: 'secret',
provider: custom
});

server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {

reply(request.auth.credentials);
}
}
});

server.inject('/login', (res) => {

const cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
mock.server.inject(res.headers.location, (mockRes) => {

server.inject({ url: mockRes.headers.location, headers: { cookie: cookie } }, (response) => {

Mock.clear();
expect(response.result).to.deep.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: profile
});
mock.stop(done);
});
});
});
});
});
});

it('authenticates with mock and custom uri', { parallel: false }, (done) => {

const mock = new Mock.V2();
mock.start((provider) => {

const server = new Hapi.Server();
server.connection({ host: 'localhost', port: 80 });
server.register(Bell, (err) => {

expect(err).to.not.exist();

const custom = Bell.providers.gitlab({ uri: 'http://example.com' });
Hoek.merge(custom, provider);

const profile = {
id: '1234567890',
username: 'steve',
name: 'steve',
email: 'steve@example.com',
state: 'active'
};

Mock.override('http://example.com/api/v3/user', profile);

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'gitlab',
clientSecret: 'secret',
provider: custom
});

server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {

reply(request.auth.credentials);
}
}
});

server.inject('/login', (res) => {

const cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
mock.server.inject(res.headers.location, (mockRes) => {

server.inject({ url: mockRes.headers.location, headers: { cookie: cookie } }, (response) => {

Mock.clear();
expect(response.result).to.deep.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: profile
});

mock.stop(done);
});
});
});
});
});
});
});

0 comments on commit 3ae0283

Please sign in to comment.