Skip to content
This repository was archived by the owner on Feb 17, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = function(store) {

return res.success(token);
} catch (e) {
if (e.Error === 'LockError') {
res.set('Retry-After', e.retry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the retry date as an ISO 8601 string, if that is not already the case.

return res.failure('To many attempts.', 429);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message should be changed to:

'Login have been locked for a short cooldown period due to failed login attempts.'

}
return res.failure('Invalid credentials.', 401);
}
};
Expand Down
3 changes: 2 additions & 1 deletion store/createModels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module.exports = function (db) {
role: String,
scope: String,
}],
data: {},
failedAttempts: { type: Number, default: 0 },
locked: { type: Date, default: null },
}, {
toJSON: {
virtuals: true,
Expand Down
20 changes: 16 additions & 4 deletions store/query/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const passwordHash = require('../util/passwordHash');
const moment = require('moment');

module.exports = async function(email, password) {
const hash = passwordHash(password);
const user = await this.findOne({ email });// the user has to be checked for locked whether the password was correct or not.

const users = await this.find({ email, password: hash });
if (users.length === 0) throw new Error('Wrong e-mail or password');
if (user.locked && moment().isBefore(user.locked)) {
throw { Error: 'LockError', retry: user.locked };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the key to name instead of Error. A true error object has { name: 'ErrorType', message: 'details' }. Sticking to this convention will allow us to replace this object literal with an error instance.

}

const user = users[0];
return user.toJSON();
if (user.password === hash) {
user.failedAttempts = 0;
user.locked = null;
await user.save();
return user.toJSON();
}

const time = 250 * user.failedAttempts;
const lockedTill = moment().add(time, 'ms');
await this.findOneAndUpdate({ email }, { $set: { failedAttempts: user.failedAttempts + 1, locked: lockedTill } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with user.save() in this instance? Why not set the user.locked field directly?

throw new Error('Wrong e-mail or password');
};