asyncified setting and verifying the password

Signed-off-by: Claudius <opensource@amenthes.de>
This commit is contained in:
Claudius 2019-05-13 11:51:05 +02:00
parent df666dd214
commit 1d403e183d
5 changed files with 48 additions and 18 deletions

View file

@ -1,7 +1,7 @@
'use strict'
// external modules
var Sequelize = require('sequelize')
var scrypt = require('@mlink/scrypt')
var scrypt = require('scrypt-kdf')
// core
var logger = require('../logger')
@ -46,11 +46,7 @@ module.exports = function (sequelize, DataTypes) {
}, {
instanceMethods: {
verifyPassword: function (attempt) {
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) {
return this
} else {
return false
}
return scrypt.verify(Buffer.from(this.password, 'hex'), attempt)
}
},
classMethods: {
@ -153,9 +149,11 @@ module.exports = function (sequelize, DataTypes) {
// suggested way to hash passwords to be able to do this asynchronously:
// @see https://github.com/sequelize/sequelize/issues/1821#issuecomment-44265819
if (!user.changed('password')) { return done() }
const hash = scrypt.kdfSync(user.get('password'), scrypt.paramsSync(0.1)).toString('hex')
user.setDataValue('password', hash)
done()
scrypt.kdf(user.getDataValue('password'), { logN: 15 }).then(keyBuf => {
user.setDataValue('password', keyBuf.toString('hex'))
done()
})
}
User.beforeCreate(updatePasswordHashHook)