Skip to content

Commit d71683a

Browse files
authored
Code maintenance, small refactors (#3811)
* Removes promise wrapping in AccountLockoutPolicy * Use bcrypt promises as globally available
1 parent ab5b759 commit d71683a

File tree

2 files changed

+35
-96
lines changed

2 files changed

+35
-96
lines changed

src/AccountLockout.js

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,32 @@ export class AccountLockout {
2626
* check if the _failed_login_count field has been set
2727
*/
2828
_isFailedLoginCountSet() {
29-
return new Promise((resolve, reject) => {
30-
const query = {
31-
username: this._user.username,
32-
_failed_login_count: { $exists: true }
33-
};
29+
const query = {
30+
username: this._user.username,
31+
_failed_login_count: { $exists: true }
32+
};
3433

35-
this._config.database.find('_User', query)
34+
return this._config.database.find('_User', query)
3635
.then(users => {
3736
if (Array.isArray(users) && users.length > 0) {
38-
resolve(true);
37+
return true;
3938
} else {
40-
resolve(false);
39+
return false;
4140
}
42-
})
43-
.catch(err => {
44-
reject(err);
4541
});
46-
});
4742
}
4843

4944
/**
5045
* if _failed_login_count is NOT set then set it to 0
5146
* else do nothing
5247
*/
5348
_initFailedLoginCount() {
54-
return new Promise((resolve, reject) => {
55-
56-
this._isFailedLoginCountSet()
49+
return this._isFailedLoginCountSet()
5750
.then(failedLoginCountIsSet => {
5851
if (!failedLoginCountIsSet) {
5952
return this._setFailedLoginCount(0);
60-
} else {
61-
return Promise.resolve();
6253
}
63-
})
64-
.then(() => {
65-
resolve();
66-
})
67-
.catch(err => {
68-
reject(err);
6954
});
70-
});
7155
}
7256

7357
/**
@@ -89,30 +73,25 @@ export class AccountLockout {
8973
* else do nothing
9074
*/
9175
_setLockoutExpiration() {
92-
return new Promise((resolve, reject) => {
93-
const query = {
94-
username: this._user.username,
95-
_failed_login_count: { $gte: this._config.accountLockout.threshold }
96-
};
76+
const query = {
77+
username: this._user.username,
78+
_failed_login_count: { $gte: this._config.accountLockout.threshold }
79+
};
9780

98-
const now = new Date();
81+
const now = new Date();
9982

100-
const updateFields = {
101-
_account_lockout_expires_at: Parse._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
102-
};
83+
const updateFields = {
84+
_account_lockout_expires_at: Parse._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
85+
};
10386

104-
this._config.database.update('_User', query, updateFields)
105-
.then(() => {
106-
resolve();
107-
})
87+
return this._config.database.update('_User', query, updateFields)
10888
.catch(err => {
10989
if (err && err.code && err.message && err.code === 101 && err.message === 'Object not found.') {
110-
resolve(); // nothing to update so we are good
90+
return; // nothing to update so we are good
11191
} else {
112-
reject(err); // unknown error
92+
throw err; // unknown error
11393
}
11494
});
115-
});
11695
}
11796

11897
/**
@@ -122,25 +101,18 @@ export class AccountLockout {
122101
* resolve
123102
*/
124103
_notLocked() {
125-
return new Promise((resolve, reject) => {
126-
const query = {
127-
username: this._user.username,
128-
_account_lockout_expires_at: { $gt: Parse._encode(new Date()) },
129-
_failed_login_count: {$gte: this._config.accountLockout.threshold}
130-
};
131-
132-
this._config.database.find('_User', query)
104+
const query = {
105+
username: this._user.username,
106+
_account_lockout_expires_at: { $gt: Parse._encode(new Date()) },
107+
_failed_login_count: {$gte: this._config.accountLockout.threshold}
108+
};
109+
110+
return this._config.database.find('_User', query)
133111
.then(users => {
134112
if (Array.isArray(users) && users.length > 0) {
135-
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)'));
136-
} else {
137-
resolve();
113+
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)');
138114
}
139-
})
140-
.catch(err => {
141-
reject(err);
142115
});
143-
});
144116
}
145117

146118
/**
@@ -151,21 +123,13 @@ export class AccountLockout {
151123
* do nothing
152124
*/
153125
_handleFailedLoginAttempt() {
154-
return new Promise((resolve, reject) => {
155-
this._initFailedLoginCount()
126+
return this._initFailedLoginCount()
156127
.then(() => {
157128
return this._incrementFailedLoginCount();
158129
})
159130
.then(() => {
160131
return this._setLockoutExpiration();
161-
})
162-
.then(() => {
163-
resolve();
164-
})
165-
.catch(err => {
166-
reject(err);
167132
});
168-
});
169133
}
170134

171135
/**
@@ -175,23 +139,14 @@ export class AccountLockout {
175139
if (!this._config.accountLockout) {
176140
return Promise.resolve();
177141
}
178-
179-
return new Promise((resolve, reject) => {
180-
this._notLocked()
142+
return this._notLocked()
181143
.then(() => {
182144
if (loginSuccessful) {
183145
return this._setFailedLoginCount(0);
184146
} else {
185147
return this._handleFailedLoginAttempt();
186148
}
187-
})
188-
.then(() => {
189-
resolve();
190-
})
191-
.catch(err => {
192-
reject(err);
193149
});
194-
});
195150
}
196151

197152
}

src/password.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,17 @@ try {
88

99
// Returns a promise for a hashed password string.
1010
function hash(password) {
11-
return new Promise(function(fulfill, reject) {
12-
bcrypt.hash(password, 10, function(err, hashedPassword) {
13-
if (err) {
14-
reject(err);
15-
} else {
16-
fulfill(hashedPassword);
17-
}
18-
});
19-
});
11+
return bcrypt.hash(password, 10);
2012
}
2113

2214
// Returns a promise for whether this password compares to equal this
2315
// hashed password.
2416
function compare(password, hashedPassword) {
25-
return new Promise(function(fulfill, reject) {
26-
// Cannot bcrypt compare when one is undefined
27-
if (!password || !hashedPassword) {
28-
return fulfill(false);
29-
}
30-
bcrypt.compare(password, hashedPassword, function(err, success) {
31-
if (err) {
32-
reject(err);
33-
} else {
34-
fulfill(success);
35-
}
36-
});
37-
});
17+
// Cannot bcrypt compare when one is undefined
18+
if (!password || !hashedPassword) {
19+
return Promise.resolve(false);
20+
}
21+
return bcrypt.compare(password, hashedPassword);
3822
}
3923

4024
module.exports = {

0 commit comments

Comments
 (0)