forked from hapijs/bell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
139268b
commit bd3702d
Showing
9 changed files
with
205 additions
and
15 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
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,28 @@ | ||
'use strict'; | ||
|
||
exports = module.exports = function (options) { | ||
|
||
return { | ||
protocol: 'oauth2', | ||
useParamsAuth: true, | ||
auth: 'https://medium.com/m/oauth/authorize', | ||
token: 'https://medium.com/v1/tokens', | ||
scope: ['basicProfile'], | ||
scopeSeparator: ',', | ||
headers: { 'User-Agent': 'hapi-bell-medium' }, | ||
profile: function (credentials, params, get, callback) { | ||
|
||
get('https://api.medium.com/v1/me', null, (profile) => { | ||
|
||
credentials.profile = { | ||
id: profile.data.id, | ||
username: profile.data.username, | ||
displayName: profile.data.name, | ||
raw: profile.data | ||
}; | ||
|
||
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
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
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,104 @@ | ||
'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('medium', () => { | ||
|
||
it('authenticates with mock', { parallel: false }, (done) => { | ||
|
||
const mock = new Mock.V2({ code: 201 }); | ||
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.medium(); | ||
Hoek.merge(custom, provider); | ||
|
||
const profile = { | ||
data: { | ||
id: '5303d74c64f66366f00cb9b2a94f3251bf5', | ||
username: 'majelbstoat', | ||
name: 'Jamie Talbot', | ||
url: 'https://medium.com/@majelbstoat', | ||
imageUrl: 'https://images.medium.com/0*fkfQiTzT7TlUGGyI.png' | ||
} | ||
}; | ||
|
||
Mock.override('https://api.medium.com/v1/me', profile); | ||
|
||
server.auth.strategy('custom', 'bell', { | ||
password: 'cookie_encryption_password_secure', | ||
isSecure: false, | ||
clientId: 'medium', | ||
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 } }, (response) => { | ||
|
||
Mock.clear(); | ||
expect(response.result).to.equal({ | ||
provider: 'custom', | ||
token: '456', | ||
expiresIn: 3600, | ||
refreshToken: undefined, | ||
query: {}, | ||
profile: { | ||
id: '5303d74c64f66366f00cb9b2a94f3251bf5', | ||
username: 'majelbstoat', | ||
displayName: 'Jamie Talbot', | ||
raw: { | ||
id: '5303d74c64f66366f00cb9b2a94f3251bf5', | ||
username: 'majelbstoat', | ||
name: 'Jamie Talbot', | ||
url: 'https://medium.com/@majelbstoat', | ||
imageUrl: 'https://images.medium.com/0*fkfQiTzT7TlUGGyI.png' | ||
} | ||
} | ||
}); | ||
|
||
mock.stop(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |