| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | const options = require('./options'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const my_scrypt = require('./my_scrypt'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-11-09 23:25:23 -05:00
										 |  |  | const password_encryption = require('./password_encryption'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 19:48:02 -05:00
										 |  |  | async function changePassword(currentPassword, newPassword, req) { | 
					
						
							| 
									
										
										
										
											2017-11-09 23:25:23 -05:00
										 |  |  |     if (!await password_encryption.verifyPassword(currentPassword)) { | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2017-11-09 23:25:23 -05:00
										 |  |  |             success: false, | 
					
						
							|  |  |  |             message: "Given current password doesn't match hash" | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newPasswordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(newPassword)); | 
					
						
							| 
									
										
										
										
											2017-11-18 12:53:17 -05:00
										 |  |  |     const decryptedDataKey = await password_encryption.getDataKey(currentPassword); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await password_encryption.setDataKey(newPassword, decryptedDataKey); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await options.setOption('password_verification_hash', newPasswordVerificationKey); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2017-11-22 20:36:07 -05:00
										 |  |  |         success: true | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     changePassword | 
					
						
							|  |  |  | }; |