@@ -7,6 +7,7 @@ const { Op } = require("sequelize");
7
7
const logger = require ( "../config/logger" ) ;
8
8
const model = require ( "../models" ) ;
9
9
const { generateToken } = require ( "../middlewares/csrfMiddleware" ) ;
10
+ const bcrypt = require ( "bcryptjs" ) ;
10
11
11
12
// Constants
12
13
const User = model . user ;
@@ -131,18 +132,22 @@ const setPasswordExpiry = (user) => {
131
132
return user ;
132
133
} ;
133
134
134
-
135
135
// Get Support Notification Recipient's Emails
136
136
const getSupportContactEmails = async ( ) => {
137
137
// Get Instance Setting
138
138
const instanceSetting = await InstanceSettings . findOne ( { raw : true } ) ;
139
139
140
- let supportEmailRecipientsEmail = instanceSetting . metaData . supportEmailRecipients || [ ] ;
141
- let supportEmailRecipientsRoles = instanceSetting . metaData . supportEmailRecipientsRoles || [ ] ;
140
+ let supportEmailRecipientsEmail =
141
+ instanceSetting . metaData . supportEmailRecipients || [ ] ;
142
+ let supportEmailRecipientsRoles =
143
+ instanceSetting . metaData . supportEmailRecipientsRoles || [ ] ;
142
144
const supportRolesEmail = [ ] ;
143
145
144
146
// If support email recipients exist and no support email recipients roles
145
- if ( supportEmailRecipientsEmail . length > 0 && supportEmailRecipientsRoles . length === 0 ) {
147
+ if (
148
+ supportEmailRecipientsEmail . length > 0 &&
149
+ supportEmailRecipientsRoles . length === 0
150
+ ) {
146
151
return supportEmailRecipientsEmail ;
147
152
}
148
153
@@ -166,8 +171,6 @@ const getSupportContactEmails = async () => {
166
171
] ,
167
172
} ) ;
168
173
169
-
170
-
171
174
// Get the e-mail addresses
172
175
ownerAndAdminEmails . forEach ( ( user ) => {
173
176
supportRolesEmail . push ( user . email ) ;
@@ -182,12 +185,17 @@ const getAccessRequestContactEmails = async () => {
182
185
// Get Instance Setting
183
186
const instanceSetting = await InstanceSettings . findOne ( { raw : true } ) ;
184
187
185
- let accessRequestEmailRecipientsEmail = instanceSetting . metaData . accessRequestEmailRecipientsEmail || [ ] ;
186
- let accessRequestEmailRecipientsRoles = instanceSetting . metaData . accessRequestEmailRecipientsRoles || [ ] ;
188
+ let accessRequestEmailRecipientsEmail =
189
+ instanceSetting . metaData . accessRequestEmailRecipientsEmail || [ ] ;
190
+ let accessRequestEmailRecipientsRoles =
191
+ instanceSetting . metaData . accessRequestEmailRecipientsRoles || [ ] ;
187
192
const accessRequestRolesEmail = [ ] ;
188
193
189
194
// If access email recipients exist and no support email recipients roles
190
- if ( accessRequestEmailRecipientsEmail . length > 0 && accessRequestEmailRecipientsRoles . length === 0 ) {
195
+ if (
196
+ accessRequestEmailRecipientsEmail . length > 0 &&
197
+ accessRequestEmailRecipientsRoles . length === 0
198
+ ) {
191
199
return accessRequestEmailRecipientsEmail ;
192
200
}
193
201
@@ -219,7 +227,6 @@ const getAccessRequestContactEmails = async () => {
219
227
return [ ...accessRequestEmailRecipientsEmail , ...accessRequestRolesEmail ] ;
220
228
} ;
221
229
222
-
223
230
const setAndSendPasswordExpiredEmail = async ( user ) => {
224
231
//if the forcePasswordReset flag isn't set but the password has expired, set the flag
225
232
if ( user . passwordExpiresAt < new Date ( ) && ! user . forcePasswordReset ) {
@@ -286,6 +293,7 @@ const checkPasswordSecurityViolations = ({ password, user }) => {
286
293
const email = user . email ;
287
294
const firstName = user . firstName ;
288
295
const lastName = user . lastName ;
296
+ const previousPasswords = user . metaData . previousPasswords || [ ] ;
289
297
290
298
if ( password . includes ( email ) ) {
291
299
passwordViolations . push ( "Password contains email address" ) ;
@@ -302,10 +310,34 @@ const checkPasswordSecurityViolations = ({ password, user }) => {
302
310
}
303
311
304
312
//TODO -- check if password contains any of previous 12 passwords
313
+ previousPasswords . forEach ( ( oldPassword ) => {
314
+ if ( bcrypt . compareSync ( password , oldPassword ) ) {
315
+ passwordViolations . push (
316
+ "Password cannot be the same as one of the previous passwords"
317
+ ) ;
318
+ }
319
+ } ) ;
305
320
306
321
return passwordViolations ;
307
322
} ;
308
323
324
+ const setPreviousPasswords = async ( user ) => {
325
+ //get existing previous passwords
326
+ let previousPasswords = user . metaData . previousPasswords || [ ] ;
327
+
328
+ //add current password to the list
329
+ previousPasswords . push ( user . hash ) ;
330
+
331
+ //if there are more than 12 previous passwords, remove the oldest one
332
+ if ( previousPasswords . length > 12 ) {
333
+ previousPasswords . shift ( ) ;
334
+ }
335
+
336
+ user . metaData . previousPasswords = previousPasswords ;
337
+
338
+ return user ;
339
+ } ;
340
+
309
341
//Exports
310
342
module . exports = {
311
343
generateAccessToken,
@@ -321,4 +353,5 @@ module.exports = {
321
353
checkPasswordSecurityViolations,
322
354
getSupportContactEmails,
323
355
getAccessRequestContactEmails,
356
+ setPreviousPasswords,
324
357
} ;
0 commit comments