Skip to content

Commit

Permalink
Merge pull request hapijs#249 from bbatliner/master
Browse files Browse the repository at this point in the history
Twitter additional GET parameters on extended profile request
  • Loading branch information
ldesplat authored Aug 20, 2016
2 parents e5e816a + 1e9790f commit 7293af0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions Providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ credentials.profile = {
- `config`:
- `extendedProfile`: Request for more profile information
- `getMethod`: [Twitter API](https://dev.twitter.com/rest/public) GET method to call when `extendedProfile` is enabled. Defaults to `'users/show'`
- `getParams`: Additional parameters to pass to the GET method. For example, the `include_email` parameter for the [`account/verify` route](https://dev.twitter.com/rest/reference/get/account/verify_credentials)
- `temporary`: 'https://api.twitter.com/oauth/request_token'
- `auth`: https://api.twitter.com/oauth/authenticate
- `token`: https://api.twitter.com/oauth/access_token
Expand Down
7 changes: 6 additions & 1 deletion lib/providers/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ exports = module.exports = function (options) {
return callback();
}

get(`https://api.twitter.com/1.1/${settings.getMethod}.json`, { user_id: params.user_id }, (profile) => {
const paramDefaults = {
user_id: params.user_id
};
const getParams = Hoek.applyToDefaults(paramDefaults, settings.getParams || {});

get(`https://api.twitter.com/1.1/${settings.getMethod}.json`, getParams, (profile) => {

credentials.profile.displayName = profile.name;
credentials.profile.raw = profile;
Expand Down
75 changes: 75 additions & 0 deletions test/providers/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,81 @@ describe('twitter', () => {
});
});

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

const mock = new Mock.V1();
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.twitter({
getMethod: 'custom/method',
getParams: {
param1: 'custom',
param2: 'params'
}
});
Hoek.merge(custom, provider);

Mock.override('https://api.twitter.com/1.1/custom/method.json', {
property: 'something'
});

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'twitter',
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.equal({
provider: 'custom',
token: 'final',
secret: 'secret',
query: {},
profile: {
id: '1234567890',
username: 'Steve Stevens',
displayName: undefined,
raw: {
property: 'something'
}
}
});

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

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

const mock = new Mock.V1();
Expand Down

0 comments on commit 7293af0

Please sign in to comment.