Skip to content

Commit

Permalink
feat: implement promises for authenticate and static authenticate inc…
Browse files Browse the repository at this point in the history
…luding tests
  • Loading branch information
saintedlama committed Feb 27, 2018
1 parent 841c670 commit 6752e0c
Show file tree
Hide file tree
Showing 3 changed files with 440 additions and 31 deletions.
59 changes: 35 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = function(schema, options) {
throw new errors.MissingPasswordError(options.errorMessages.MissingPasswordError);
}
})
.then(() => this.authenticateAsync(oldPassword))
.then(() => this.authenticate(oldPassword))
.then(({ user }) => {
if (!user) {
throw new errors.IncorrectPasswordError(options.errorMessages.IncorrectPasswordError);
Expand All @@ -140,27 +140,30 @@ module.exports = function(schema, options) {
.catch(err => cb(err));
};

schema.methods.authenticateAsync = function(password) {
return new Promise((resolve, reject) => {
this.authenticate(password, (err, user, errorMessages) => err?reject(err):resolve({ user, errorMessages }))
});
}

schema.methods.authenticate = function(password, cb) {
// With hash/salt marked as "select: false" - load model including the salt/hash fields form db and authenticate
if (!this.get(options.saltField)) {
this.constructor.findByUsername(this.get(options.usernameField), true, (err, user) => {
if (err) { return cb(err); }

if (user) {
return authenticate(user, password, options, cb);
} else {
return cb(null, false, new errors.IncorrectUsernameError(options.errorMessages.IncorrectUsernameError));
const promise = Promise.resolve()
.then(() => {
if (this.get(options.saltField)) {
return authenticate(this, password, options);
}

return this.constructor.findByUsername(this.get(options.usernameField), true)
.then(user => {
if (user) {
return authenticate(user, password, options);
}

return { user: false, message: new errors.IncorrectUsernameError(options.errorMessages.IncorrectUsernameError) };
});
});
} else {
return authenticate(this, password, options, cb);

if (!cb) {
return promise;
}

promise
.then(({ user, message }) => cb(null, user, message))
.catch(err => cb(err));
};

if (options.limitAttempts) {
Expand All @@ -184,15 +187,23 @@ module.exports = function(schema, options) {
// Passport Local Interface
schema.statics.authenticate = function() {
return (username, password, cb) => {
this.findByUsername(username, true, (err, user) => {
if (err) { return cb(err); }

const promise = Promise.resolve()
.then(() => this.findByUsername(username, true))
.then((user) => {
if (user) {
return user.authenticate(password, cb);
} else {
return cb(null, false, new errors.IncorrectUsernameError(options.errorMessages.IncorrectUsernameError));
return user.authenticate(password);
}

return { user: false, message: new errors.IncorrectUsernameError(options.errorMessages.IncorrectUsernameError) };
});

if (!cb) {
return promise;
}

promise
.then(({ user, message }) => cb(null, user, message))
.catch(err => cb(err));
};
};

Expand Down
13 changes: 12 additions & 1 deletion lib/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ const scmp = require('scmp');
const pbkdf2 = require('./pbkdf2');
const errors = require('./errors');

module.exports = function authenticate(user, password, options, cb) {
// authenticate function needs refactoring - to avoid bugs we wrapped a bit dirty
module.exports = function(user, password, options, cb) {
if (cb) {
return authenticate(user, password, options, cb);
}

return new Promise((resolve, reject) => {
authenticate(user, password, options, (err, user, message) => err?reject(err):resolve({ user, message }));
});
}

function authenticate(user, password, options, cb) {
if (options.limitAttempts) {
const attemptsInterval = Math.pow(options.interval, Math.log(user.get(options.attemptsField) + 1));
const calculatedInterval = (attemptsInterval < options.maxInterval) ? attemptsInterval : options.maxInterval;
Expand Down
Loading

0 comments on commit 6752e0c

Please sign in to comment.