Skip to content

Commit

Permalink
feat: Make custom query for oAuth authentication (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
saiichihashimoto authored and daffl committed Jan 6, 2019
1 parent 8c3a740 commit 5d43e3c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/authentication-oauth1/lib/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions packages/authentication-oauth1/test/verifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/authentication-oauth2/lib/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions packages/authentication-oauth2/test/verifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, () => {
Expand Down

0 comments on commit 5d43e3c

Please # to comment.