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

Make custom query for authentication #1124

Merged
merged 12 commits into from
Jan 6, 2019
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