Skip to content

Commit 5d43e3c

Browse files
saiichihashimotodaffl
authored andcommitted
feat: Make custom query for oAuth authentication (#1124)
1 parent 8c3a740 commit 5d43e3c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

packages/authentication-oauth1/lib/verifier.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class OAuth1Vierifier {
88
this.options = options;
99
this.service = typeof options.service === 'string' ? app.service(options.service) : options.service;
1010

11+
options.makeQuery = options.makeQuery || function (profile, options) {
12+
return { [options.idField]: profile.id }; // facebookId: profile.id
13+
};
14+
1115
if (!this.service) {
1216
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.`);
1317
}
@@ -71,10 +75,7 @@ class OAuth1Vierifier {
7175
verify (req, accessToken, refreshToken, profile, done) {
7276
debug('Checking credentials');
7377
const options = this.options;
74-
const query = {
75-
[options.idField]: profile.id, // facebookId: profile.id
76-
$limit: 1
77-
};
78+
const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 });
7879
const data = { profile, accessToken, refreshToken };
7980
let existing;
8081

packages/authentication-oauth1/test/verifier.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ describe('Verifier', () => {
199199
});
200200
});
201201

202+
it('calls with query from makeQuery', done => {
203+
options = Object.assign({}, options, { makeQuery: sinon.stub().returns({ key: 'value' }) });
204+
verifier = new Verifier(app, options);
205+
verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => {
206+
const query = { key: 'value', $limit: 1 };
207+
expect(options.makeQuery).to.have.been.calledOnce;
208+
expect(service.find).to.have.been.calledWith({ query });
209+
done();
210+
});
211+
});
212+
202213
it('calls _normalizeResult', done => {
203214
sinon.spy(verifier, '_normalizeResult');
204215
verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => {

packages/authentication-oauth2/lib/verifier.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class OAuth2Verifier {
88
this.options = options;
99
this.service = typeof options.service === 'string' ? app.service(options.service) : options.service;
1010

11+
options.makeQuery = options.makeQuery || function (profile, options) {
12+
return { [options.idField]: profile.id }; // facebookId: profile.id
13+
};
14+
1115
if (!this.service) {
1216
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.`);
1317
}
@@ -77,10 +81,7 @@ class OAuth2Verifier {
7781
verify (req, accessToken, refreshToken, profile, done) {
7882
debug('Checking credentials');
7983
const options = this.options;
80-
const query = {
81-
[options.idField]: profile.id, // facebookId: profile.id
82-
$limit: 1
83-
};
84+
const query = Object.assign({}, options.makeQuery(profile, options), { $limit: 1 });
8485
const data = { profile, accessToken, refreshToken };
8586
let existing;
8687

packages/authentication-oauth2/test/verifier.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ describe('Verifier', () => {
199199
});
200200
});
201201

202+
it('calls with query from makeQuery', done => {
203+
options = Object.assign({}, options, { makeQuery: sinon.stub().returns({ key: 'value' }) });
204+
verifier = new Verifier(app, options);
205+
verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => {
206+
const query = { key: 'value', $limit: 1 };
207+
expect(options.makeQuery).to.have.been.calledOnce;
208+
expect(service.find).to.have.been.calledWith({ query });
209+
done();
210+
});
211+
});
212+
202213
it('calls _normalizeResult', done => {
203214
sinon.spy(verifier, '_normalizeResult');
204215
verifier.verify({}, 'access', 'refresh', { id: 1234 }, () => {

0 commit comments

Comments
 (0)