-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
319 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ lib-cov | |
*.gz | ||
.vscode | ||
.yarn-cache | ||
.DS_Store | ||
|
||
pids | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const bcrypt = require('bcrypt'); | ||
const crypto = require('crypto'); | ||
|
||
const ModelError = require('../modelError'); | ||
|
||
const { min_length: password_min_length, salt_factor: password_salt_factor } = require('config').get('openSenseMap-API-models.password'); | ||
|
||
const preparePasswordHash = function preparePasswordHash (plaintextPassword) { | ||
// first round: hash plaintextPassword with sha512 | ||
const hash = crypto.createHash('sha512'); | ||
hash.update(plaintextPassword.toString(), 'utf8'); | ||
const hashed = hash.digest('base64'); // base64 for more entropy than hex | ||
|
||
return hashed; | ||
}; | ||
|
||
const checkPassword = function checkPassword ( | ||
plaintextPassword, | ||
hashedPassword | ||
) { | ||
return bcrypt | ||
.compare(preparePasswordHash(plaintextPassword), hashedPassword.hash) | ||
.then(function (passwordIsCorrect) { | ||
if (passwordIsCorrect === false) { | ||
throw new ModelError('Password incorrect', { type: 'ForbiddenError' }); | ||
} | ||
|
||
return true; | ||
}); | ||
}; | ||
|
||
const validatePassword = function validatePassword (newPassword) { | ||
return newPassword.length >= Number(password_min_length); | ||
}; | ||
|
||
module.exports = { | ||
checkPassword, | ||
validatePassword | ||
}; |
Oops, something went wrong.