Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from zitsen/gitlab-provider
Add GitLab provider
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} | ||
}; | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"facebook", | ||
"foursquare", | ||
"github", | ||
"gitlab", | ||
"google", | ||
"instagram", | ||
"linkedin", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |