Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New dropbox-v2 provider which uses Dropbox V2 API #244

Merged
merged 4 commits into from
Sep 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ The `server.auth.strategy()` method requires the following strategy options:
Defaults to space (Facebook and GitHub default to comma).
- `headers` - a headers object with additional headers required by the provider (e.g. GitHub required the 'User-Agent' header which is
set by default).
- `profileMethod` - `get` or `post` for obtaining user profile by `profile` function. Default is `get`.
- `profile` - a function used to obtain user profile information and normalize it. The function signature is
`function(credentials, params, get, callback)` where:
- `credentials` - the credentials object. Change the object directly within the function (profile information is typically stored
Expand All @@ -94,6 +95,7 @@ The `server.auth.strategy()` method requires the following strategy options:
- `params` - any URI query parameters (cannot include them in the URI due to signature requirements).
- `callback` - request callback with signature `function(response)` where `response` is the parsed payload (any errors are
handled internally).
If `profileMethod` is set to `post` the helper function sends a POST request for obtaining the user profile.
- `callback` - the callback function which much be called once profile processing is complete.
- `password` - the cookie encryption password. Used to encrypt the temporary state cookie used by the module in between the authorization
protocol steps.
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internals.schema = Joi.object({
token: Joi.string().required(),
headers: Joi.object(),
profile: Joi.func(),
profileMethod: Joi.string().valid('get', 'post').default('get'),
scope: Joi.alternatives().try(
Joi.array().items(Joi.string()),
Joi.func()
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ exports.v2 = function (settings) {
}

const getQuery = (params ? '?' + internals.queryString(params) : '');
Wreck.get(uri + getQuery, getOptions, (err, res, response) => {
Wreck[settings.provider.profileMethod](uri + getQuery, getOptions, (err, res, response) => {

if (err ||
res.statusCode !== 200) {
Expand Down
20 changes: 20 additions & 0 deletions lib/providers/dropbox-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

exports = module.exports = function (options) {

return {
protocol: 'oauth2',
useParamsAuth: true,
auth: 'https://www.dropbox.com/oauth2/authorize',
token: 'https://api.dropboxapi.com/oauth2/token',
profileMethod: 'post',
profile: function (credentials, params, post, callback) {

post('https://api.dropboxapi.com/2/users/get_current_account', null, (profile) => {

credentials.profile = profile;
return callback();
});
}
};
};
1 change: 1 addition & 0 deletions lib/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports = module.exports = {
auth0: require('./auth0'),
bitbucket: require('./bitbucket'),
dropbox: require('./dropbox'),
dropboxV2: require('./dropbox-v2'),
facebook: require('./facebook'),
fitbit: require('./fitbit'),
foursquare: require('./foursquare'),
Expand Down
92 changes: 92 additions & 0 deletions test/providers/dropbox-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'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('dropbox-v2', () => {

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.dropboxV2();
Hoek.merge(custom, provider);

const profile = {
display_name: '1234567890',
username: 'steve',
name: 'steve',
first_name: 'steve',
last_name: 'smith',
email: 'steve@example.com'
};

Mock.override('https://api.dropboxapi.com/2/users/get_current_account', profile);

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'dropbox',
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
});

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