diff --git a/packages/authentication-oauth1/lib/verifier.js b/packages/authentication-oauth1/lib/verifier.js index a53d5a2bc4..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 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; diff --git a/packages/authentication-oauth1/test/verifier.test.js b/packages/authentication-oauth1/test/verifier.test.js index 720327970a..e096835cb0 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 = 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 }; + 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/lib/verifier.js b/packages/authentication-oauth2/lib/verifier.js index 81a2bd22bd..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 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; diff --git a/packages/authentication-oauth2/test/verifier.test.js b/packages/authentication-oauth2/test/verifier.test.js index fe0198429e..102ede1ba7 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 = 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 }; + 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 }, () => {