Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Add support for dot notation, fix some whitespace (#8)
Browse files Browse the repository at this point in the history
* Add support for dot notation, fix some whitespace

* Update package.json

* Remove oldPassword from tests

Storing the password and resetting the object wasn't needed because Mocha gives us a clean slate for every test it performs

* Remove usernameField dot notation "support"

Lots of edge-cases that weren't being handled with this implementation. Better to remove these changes for now and come back to this later.

For now, if you need dot notation for your usernameField you're better off writing a custom verifier function.
  • Loading branch information
elfey authored and daffl committed Jan 27, 2017
1 parent 4f75217 commit 400b1dc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -53,6 +53,7 @@
"bcryptjs": "^2.3.0",
"debug": "^2.2.0",
"feathers-errors": "^2.4.0",
"lodash.get": "^4.4.2",
"lodash.merge": "^4.6.0",
"lodash.omit": "^4.5.0",
"lodash.pick": "^4.4.0",
Expand Down
8 changes: 5 additions & 3 deletions src/verifier.js
@@ -1,6 +1,7 @@
import Debug from 'debug';
import errors from 'feathers-errors';
import bcrypt from 'bcryptjs';
import get from 'lodash.get';

const debug = Debug('feathers-authentication-local:verify');

Expand All @@ -20,14 +21,15 @@ class LocalVerifier {
}

_comparePassword(entity, password) {
const hash = entity[this.options.passwordField];
// find password in entity, this allows for dot notation
const hash = get(entity, this.options.passwordField);

if (!hash) {
return Promise.reject(new Error(`'${this.options.entity}' record in the database is missing a '${this.options.passwordField}'`));
}

debug('Verifying password');

return new Promise((resolve, reject) => {
bcrypt.compare(password, hash, function(error, result) {
// Handle 500 server error.
Expand Down Expand Up @@ -80,4 +82,4 @@ class LocalVerifier {
}
}

export default LocalVerifier;
export default LocalVerifier;
12 changes: 6 additions & 6 deletions test/index.test.js
Expand Up @@ -36,7 +36,7 @@ describe('feathers-authentication-local', () => {
app.configure(authentication({ secret: 'supersecret' }));
});

it('throws an error if passport has not been registered', () => {
it('throws an error if passport has not been registered', () => {
expect(() => {
feathers().configure(local());
}).to.throw();
Expand All @@ -50,7 +50,7 @@ describe('feathers-authentication-local', () => {

expect(passportLocal.Strategy).to.have.been.calledOnce;
expect(app.passport.use).to.have.been.calledWith('local');

app.passport.use.restore();
passportLocal.Strategy.restore();
});
Expand All @@ -61,7 +61,7 @@ describe('feathers-authentication-local', () => {
app.setup();

expect(app.passport.options).to.have.been.calledOnce;

app.passport.options.restore();
});

Expand Down Expand Up @@ -116,7 +116,7 @@ describe('feathers-authentication-local', () => {
app.setup();

expect(passportLocal.Strategy.getCall(0).args[0].usernameField).to.equal('username');

passportLocal.Strategy.restore();
});

Expand All @@ -131,7 +131,7 @@ describe('feathers-authentication-local', () => {

expect(passportLocal.Strategy.getCall(0).args[0].usernameField).to.equal('username');
expect(passportLocal.Strategy.getCall(0).args[0].passwordField).to.equal('password');

passportLocal.Strategy.restore();
});

Expand All @@ -146,7 +146,7 @@ describe('feathers-authentication-local', () => {

expect(passportLocal.Strategy.getCall(0).args[0].usernameField).to.equal('username');
expect(passportLocal.Strategy.getCall(0).args[0].passwordField).to.equal('password');

passportLocal.Strategy.restore();
});

Expand Down
24 changes: 19 additions & 5 deletions test/verifier.test.js
Expand Up @@ -21,7 +21,7 @@ describe('Verifier', () => {
return hasher('admin').then(password => {
user = {
email: 'admin@feathersjs.com',
password
password
};

service = {
Expand All @@ -45,6 +45,8 @@ describe('Verifier', () => {
expect(typeof Verifier).to.equal('function');
});



describe('constructor', () => {
it('retains an app reference', () => {
expect(verifier.app).to.deep.equal(app);
Expand Down Expand Up @@ -95,32 +97,44 @@ describe('Verifier', () => {
expect(result).to.deep.equal(user);
});
});

it('allows dot notation for password field', () => {
user.password = {
value: user.password
};

verifier.options.passwordField = 'password.value';

return verifier._comparePassword(user, 'admin').then(result => {
expect(result).to.deep.equal(user);
});
});
});
});

describe('_normalizeResult', () => {
describe('when has results', () => {
it('returns entity when paginated', () => {
it('returns entity when paginated', () => {
return verifier._normalizeResult({ data: [user] }).then(result => {
expect(result).to.deep.equal(user);
});
});

it('returns entity when not paginated', () => {
it('returns entity when not paginated', () => {
return verifier._normalizeResult([user]).then(result => {
expect(result).to.deep.equal(user);
});
});
});

describe('when no results', () => {
it('rejects with false when paginated', () => {
it('rejects with false when paginated', () => {
return verifier._normalizeResult({ data: [] }).catch(error => {
expect(error).to.equal(false);
});
});

it('rejects with false when not paginated', () => {
it('rejects with false when not paginated', () => {
return verifier._normalizeResult([]).catch(error => {
expect(error).to.equal(false);
});
Expand Down

0 comments on commit 400b1dc

Please sign in to comment.