From 5a939d3da378970d5d66807bba2fa6adad93d98e Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Wed, 5 Dec 2018 19:49:50 -0800 Subject: [PATCH 1/8] Update index.js --- packages/authentication-oauth1/lib/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/authentication-oauth1/lib/index.js b/packages/authentication-oauth1/lib/index.js index b029d9d7c3..4d76c3e6fa 100644 --- a/packages/authentication-oauth1/lib/index.js +++ b/packages/authentication-oauth1/lib/index.js @@ -54,6 +54,9 @@ function init (options = {}) { // Set callback defaults based on provided path oauth1Settings.callbackPath = oauth1Settings.callbackPath || `${oauth1Settings.path}/callback`; oauth1Settings.callbackURL = oauth1Settings.callbackURL || makeUrl(oauth1Settings.callbackPath, app); + oauth1Settings.makeQuery = oauth1Settings.makeQuery || function(profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; if (!oauth1Settings.consumerKey) { throw new Error(`You must provide a 'consumerKey' in your authentication configuration or pass one explicitly`); From 5717e1ced92090a9b71c832337c63a83f295aafc Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Wed, 5 Dec 2018 19:51:24 -0800 Subject: [PATCH 2/8] Update verifier.js --- packages/authentication-oauth1/lib/verifier.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/authentication-oauth1/lib/verifier.js b/packages/authentication-oauth1/lib/verifier.js index a53d5a2bc4..e12a087d5e 100644 --- a/packages/authentication-oauth1/lib/verifier.js +++ b/packages/authentication-oauth1/lib/verifier.js @@ -71,10 +71,7 @@ class OAuth1Vierifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const query = { - [options.idField]: profile.id, // facebookId: profile.id - $limit: 1 - }; + const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; From e85c9b73ba75a26eb1fa806946a41f2d9fb01cc5 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Wed, 5 Dec 2018 19:52:37 -0800 Subject: [PATCH 3/8] Update index.js --- packages/authentication-oauth2/lib/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/authentication-oauth2/lib/index.js b/packages/authentication-oauth2/lib/index.js index 86008fc433..7100b73eff 100644 --- a/packages/authentication-oauth2/lib/index.js +++ b/packages/authentication-oauth2/lib/index.js @@ -68,6 +68,9 @@ function init (options = {}) { // Set callback defaults based on provided path oauth2Settings.callbackPath = oauth2Settings.callbackPath || `${oauth2Settings.path}/callback`; oauth2Settings.callbackURL = oauth2Settings.callbackURL || makeUrl(oauth2Settings.callbackPath, app); + oauth2Settings.makeQuery = oauth2Settings.makeQuery || function(profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; if (!oauth2Settings.clientID) { throw new Error(`You must provide a 'clientID' in your authentication configuration or pass one explicitly`); From 58cc9ffa039b6b74099e61204c87703386efea4b Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Wed, 5 Dec 2018 19:53:22 -0800 Subject: [PATCH 4/8] Update verifier.js --- packages/authentication-oauth2/lib/verifier.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/authentication-oauth2/lib/verifier.js b/packages/authentication-oauth2/lib/verifier.js index 81a2bd22bd..bda146aaa4 100644 --- a/packages/authentication-oauth2/lib/verifier.js +++ b/packages/authentication-oauth2/lib/verifier.js @@ -77,10 +77,7 @@ class OAuth2Verifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const query = { - [options.idField]: profile.id, // facebookId: profile.id - $limit: 1 - }; + const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; From e7aef20c55363049040c091b2a2cb965bf5f7055 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Mon, 17 Dec 2018 18:01:26 -0800 Subject: [PATCH 5/8] Fix breaking tests --- packages/authentication-oauth1/lib/index.js | 3 --- packages/authentication-oauth1/lib/verifier.js | 5 ++++- packages/authentication-oauth2/lib/index.js | 3 --- packages/authentication-oauth2/lib/verifier.js | 5 ++++- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/authentication-oauth1/lib/index.js b/packages/authentication-oauth1/lib/index.js index 4d76c3e6fa..b029d9d7c3 100644 --- a/packages/authentication-oauth1/lib/index.js +++ b/packages/authentication-oauth1/lib/index.js @@ -54,9 +54,6 @@ function init (options = {}) { // Set callback defaults based on provided path oauth1Settings.callbackPath = oauth1Settings.callbackPath || `${oauth1Settings.path}/callback`; oauth1Settings.callbackURL = oauth1Settings.callbackURL || makeUrl(oauth1Settings.callbackPath, app); - oauth1Settings.makeQuery = oauth1Settings.makeQuery || function(profile, options) { - return { [options.idField]: profile.id }; // facebookId: profile.id - }; if (!oauth1Settings.consumerKey) { throw new Error(`You must provide a 'consumerKey' in your authentication configuration or pass one explicitly`); diff --git a/packages/authentication-oauth1/lib/verifier.js b/packages/authentication-oauth1/lib/verifier.js index e12a087d5e..9932bb1981 100644 --- a/packages/authentication-oauth1/lib/verifier.js +++ b/packages/authentication-oauth1/lib/verifier.js @@ -71,7 +71,10 @@ class OAuth1Vierifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); + const makeQuery = options.makeQuery || function (profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; + const query = Object.assign({}, makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; diff --git a/packages/authentication-oauth2/lib/index.js b/packages/authentication-oauth2/lib/index.js index 7100b73eff..86008fc433 100644 --- a/packages/authentication-oauth2/lib/index.js +++ b/packages/authentication-oauth2/lib/index.js @@ -68,9 +68,6 @@ function init (options = {}) { // Set callback defaults based on provided path oauth2Settings.callbackPath = oauth2Settings.callbackPath || `${oauth2Settings.path}/callback`; oauth2Settings.callbackURL = oauth2Settings.callbackURL || makeUrl(oauth2Settings.callbackPath, app); - oauth2Settings.makeQuery = oauth2Settings.makeQuery || function(profile, options) { - return { [options.idField]: profile.id }; // facebookId: profile.id - }; if (!oauth2Settings.clientID) { throw new Error(`You must provide a 'clientID' in your authentication configuration or pass one explicitly`); diff --git a/packages/authentication-oauth2/lib/verifier.js b/packages/authentication-oauth2/lib/verifier.js index bda146aaa4..47c8974e44 100644 --- a/packages/authentication-oauth2/lib/verifier.js +++ b/packages/authentication-oauth2/lib/verifier.js @@ -77,7 +77,10 @@ class OAuth2Verifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); + const makeQuery = options.makeQuery || function (profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; + const query = Object.assign({}, makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; From 353979f1184eaf4ccdf9bf790bdb19c13d794988 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Mon, 17 Dec 2018 18:07:00 -0800 Subject: [PATCH 6/8] Fix codeclimate cognitive code complexity failure (hopefully) --- packages/authentication-oauth1/lib/verifier.js | 9 +++++---- packages/authentication-oauth2/lib/verifier.js | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/authentication-oauth1/lib/verifier.js b/packages/authentication-oauth1/lib/verifier.js index 9932bb1981..df9c490b88 100644 --- a/packages/authentication-oauth1/lib/verifier.js +++ b/packages/authentication-oauth1/lib/verifier.js @@ -8,6 +8,10 @@ class OAuth1Vierifier { this.options = options; this.service = typeof options.service === 'string' ? app.service(options.service) : options.service; + options.makeQuery = options.makeQuery || function (profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; + if (!this.service) { throw new Error(`options.service does not exist.\n\tMake sure you are passing a valid service path or service instance and it is initialized before @feathersjs/authentication-oauth1.`); } @@ -71,10 +75,7 @@ class OAuth1Vierifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const makeQuery = options.makeQuery || function (profile, options) { - return { [options.idField]: profile.id }; // facebookId: profile.id - }; - const query = Object.assign({}, makeQuery(profile, options), { $limit: 1 }); + const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; diff --git a/packages/authentication-oauth2/lib/verifier.js b/packages/authentication-oauth2/lib/verifier.js index 47c8974e44..710cf7651a 100644 --- a/packages/authentication-oauth2/lib/verifier.js +++ b/packages/authentication-oauth2/lib/verifier.js @@ -8,6 +8,10 @@ class OAuth2Verifier { this.options = options; this.service = typeof options.service === 'string' ? app.service(options.service) : options.service; + options.makeQuery = options.makeQuery || function (profile, options) { + return { [options.idField]: profile.id }; // facebookId: profile.id + }; + if (!this.service) { throw new Error(`options.service does not exist.\n\tMake sure you are passing a valid service path or service instance and it is initialized before @feathersjs/authentication-oauth2.`); } @@ -77,10 +81,7 @@ class OAuth2Verifier { verify (req, accessToken, refreshToken, profile, done) { debug('Checking credentials'); const options = this.options; - const makeQuery = options.makeQuery || function (profile, options) { - return { [options.idField]: profile.id }; // facebookId: profile.id - }; - const query = Object.assign({}, makeQuery(profile, options), { $limit: 1 }); + const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 }); const data = { profile, accessToken, refreshToken }; let existing; From 435e7eb806221451d45e21f4be1f4dcc88c64c61 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Sat, 5 Jan 2019 15:49:31 -0800 Subject: [PATCH 7/8] Tests for makeQuery --- packages/authentication-oauth1/test/verifier.test.js | 11 +++++++++++ packages/authentication-oauth2/test/verifier.test.js | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/authentication-oauth1/test/verifier.test.js b/packages/authentication-oauth1/test/verifier.test.js index 720327970a..8ef9ced738 100644 --- a/packages/authentication-oauth1/test/verifier.test.js +++ b/packages/authentication-oauth1/test/verifier.test.js @@ -199,6 +199,17 @@ describe('Verifier', () => { }); }); + it('calls with query from makeQuery', done => { + options = { ...options, makeQuery: sinon.stub().returns({ key: 'value' }) }; + verifier = new Verifier(app, options); + verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { + const query = { key: 'value', $limit: 1 }; + expect(options.makeQuery).to.have.been.calledOnce; + expect(service.find).to.have.been.calledWith({ query }); + done(); + }); + }); + it('calls _normalizeResult', done => { sinon.spy(verifier, '_normalizeResult'); verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { diff --git a/packages/authentication-oauth2/test/verifier.test.js b/packages/authentication-oauth2/test/verifier.test.js index fe0198429e..8935900804 100644 --- a/packages/authentication-oauth2/test/verifier.test.js +++ b/packages/authentication-oauth2/test/verifier.test.js @@ -199,6 +199,17 @@ describe('Verifier', () => { }); }); + it('calls with query from makeQuery', done => { + options = { ...options, makeQuery: sinon.stub().returns({ key: 'value' }) }; + verifier = new Verifier(app, options); + verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { + const query = { key: 'value', $limit: 1 }; + expect(options.makeQuery).to.have.been.calledOnce; + expect(service.find).to.have.been.calledWith({ query }); + done(); + }); + }); + it('calls _normalizeResult', done => { sinon.spy(verifier, '_normalizeResult'); verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { From f070b7aadbef411275ef410eddc856925d120bc1 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Sat, 5 Jan 2019 16:00:13 -0800 Subject: [PATCH 8/8] Fix tests for node 6 --- packages/authentication-oauth1/test/verifier.test.js | 2 +- packages/authentication-oauth2/test/verifier.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/authentication-oauth1/test/verifier.test.js b/packages/authentication-oauth1/test/verifier.test.js index 8ef9ced738..e096835cb0 100644 --- a/packages/authentication-oauth1/test/verifier.test.js +++ b/packages/authentication-oauth1/test/verifier.test.js @@ -200,7 +200,7 @@ describe('Verifier', () => { }); it('calls with query from makeQuery', done => { - options = { ...options, makeQuery: sinon.stub().returns({ key: 'value' }) }; + options = Object.assign({}, options, { makeQuery: sinon.stub().returns({ key: 'value' }) }); verifier = new Verifier(app, options); verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { const query = { key: 'value', $limit: 1 }; diff --git a/packages/authentication-oauth2/test/verifier.test.js b/packages/authentication-oauth2/test/verifier.test.js index 8935900804..102ede1ba7 100644 --- a/packages/authentication-oauth2/test/verifier.test.js +++ b/packages/authentication-oauth2/test/verifier.test.js @@ -200,7 +200,7 @@ describe('Verifier', () => { }); it('calls with query from makeQuery', done => { - options = { ...options, makeQuery: sinon.stub().returns({ key: 'value' }) }; + options = Object.assign({}, options, { makeQuery: sinon.stub().returns({ key: 'value' }) }); verifier = new Verifier(app, options); verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => { const query = { key: 'value', $limit: 1 };