Code maintenance, small refactors (#3811)
* Removes promise wrapping in AccountLockoutPolicy * Use bcrypt promises as globally available
This commit is contained in:
@@ -26,24 +26,19 @@ export class AccountLockout {
|
||||
* check if the _failed_login_count field has been set
|
||||
*/
|
||||
_isFailedLoginCountSet() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_failed_login_count: { $exists: true }
|
||||
};
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_failed_login_count: { $exists: true }
|
||||
};
|
||||
|
||||
this._config.database.find('_User', query)
|
||||
return this._config.database.find('_User', query)
|
||||
.then(users => {
|
||||
if (Array.isArray(users) && users.length > 0) {
|
||||
resolve(true);
|
||||
return true;
|
||||
} else {
|
||||
resolve(false);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,23 +46,12 @@ export class AccountLockout {
|
||||
* else do nothing
|
||||
*/
|
||||
_initFailedLoginCount() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
this._isFailedLoginCountSet()
|
||||
return this._isFailedLoginCountSet()
|
||||
.then(failedLoginCountIsSet => {
|
||||
if (!failedLoginCountIsSet) {
|
||||
return this._setFailedLoginCount(0);
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,30 +73,25 @@ export class AccountLockout {
|
||||
* else do nothing
|
||||
*/
|
||||
_setLockoutExpiration() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_failed_login_count: { $gte: this._config.accountLockout.threshold }
|
||||
};
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_failed_login_count: { $gte: this._config.accountLockout.threshold }
|
||||
};
|
||||
|
||||
const now = new Date();
|
||||
const now = new Date();
|
||||
|
||||
const updateFields = {
|
||||
_account_lockout_expires_at: Parse._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
|
||||
};
|
||||
const updateFields = {
|
||||
_account_lockout_expires_at: Parse._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
|
||||
};
|
||||
|
||||
this._config.database.update('_User', query, updateFields)
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
return this._config.database.update('_User', query, updateFields)
|
||||
.catch(err => {
|
||||
if (err && err.code && err.message && err.code === 101 && err.message === 'Object not found.') {
|
||||
resolve(); // nothing to update so we are good
|
||||
return; // nothing to update so we are good
|
||||
} else {
|
||||
reject(err); // unknown error
|
||||
throw err; // unknown error
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,25 +101,18 @@ export class AccountLockout {
|
||||
* resolve
|
||||
*/
|
||||
_notLocked() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_account_lockout_expires_at: { $gt: Parse._encode(new Date()) },
|
||||
_failed_login_count: {$gte: this._config.accountLockout.threshold}
|
||||
};
|
||||
const query = {
|
||||
username: this._user.username,
|
||||
_account_lockout_expires_at: { $gt: Parse._encode(new Date()) },
|
||||
_failed_login_count: {$gte: this._config.accountLockout.threshold}
|
||||
};
|
||||
|
||||
this._config.database.find('_User', query)
|
||||
return this._config.database.find('_User', query)
|
||||
.then(users => {
|
||||
if (Array.isArray(users) && users.length > 0) {
|
||||
reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Your account is locked due to multiple failed login attempts. Please try again after ' + this._config.accountLockout.duration + ' minute(s)'));
|
||||
} else {
|
||||
resolve();
|
||||
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Your account is locked due to multiple failed login attempts. Please try again after ' + this._config.accountLockout.duration + ' minute(s)');
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,21 +123,13 @@ export class AccountLockout {
|
||||
* do nothing
|
||||
*/
|
||||
_handleFailedLoginAttempt() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._initFailedLoginCount()
|
||||
return this._initFailedLoginCount()
|
||||
.then(() => {
|
||||
return this._incrementFailedLoginCount();
|
||||
})
|
||||
.then(() => {
|
||||
return this._setLockoutExpiration();
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,23 +139,14 @@ export class AccountLockout {
|
||||
if (!this._config.accountLockout) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._notLocked()
|
||||
return this._notLocked()
|
||||
.then(() => {
|
||||
if (loginSuccessful) {
|
||||
return this._setFailedLoginCount(0);
|
||||
} else {
|
||||
return this._handleFailedLoginAttempt();
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,33 +8,17 @@ try {
|
||||
|
||||
// Returns a promise for a hashed password string.
|
||||
function hash(password) {
|
||||
return new Promise(function(fulfill, reject) {
|
||||
bcrypt.hash(password, 10, function(err, hashedPassword) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
fulfill(hashedPassword);
|
||||
}
|
||||
});
|
||||
});
|
||||
return bcrypt.hash(password, 10);
|
||||
}
|
||||
|
||||
// Returns a promise for whether this password compares to equal this
|
||||
// hashed password.
|
||||
function compare(password, hashedPassword) {
|
||||
return new Promise(function(fulfill, reject) {
|
||||
// Cannot bcrypt compare when one is undefined
|
||||
if (!password || !hashedPassword) {
|
||||
return fulfill(false);
|
||||
}
|
||||
bcrypt.compare(password, hashedPassword, function(err, success) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
fulfill(success);
|
||||
}
|
||||
});
|
||||
});
|
||||
// Cannot bcrypt compare when one is undefined
|
||||
if (!password || !hashedPassword) {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
return bcrypt.compare(password, hashedPassword);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user