Skip to content

Commit d7cc951

Browse files
authored
Merge pull request #1474 from binotaliu/switch-scrypt-kdf
Replace scrypt with scrypt-kdf
2 parents 4a748cb + d4d0120 commit d7cc951

File tree

5 files changed

+131
-117
lines changed

5 files changed

+131
-117
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: node_js
22

33
node_js:
4-
- "lts/carbon"
54
- "lts/dubnium"
65
- "11"
76
- "12"
@@ -12,7 +11,6 @@ cache: npm
1211
matrix:
1312
fast_finish: true
1413
include:
15-
- node_js: lts/carbon
1614
- node_js: lts/dubnium
1715
allow_failures:
1816
- node_js: "11"

lib/auth/email/index.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,56 @@ const emailAuth = module.exports = Router()
1515

1616
passport.use(new LocalStrategy({
1717
usernameField: 'email'
18-
}, function (email, password, done) {
18+
}, async function (email, password, done) {
1919
if (!validator.isEmail(email)) return done(null, false)
20-
models.User.findOne({
21-
where: {
22-
email: email
23-
}
24-
}).then(function (user) {
20+
21+
try {
22+
const user = await models.User.findOne({
23+
where: {
24+
email: email
25+
}
26+
})
27+
2528
if (!user) return done(null, false)
26-
if (!user.verifyPassword(password)) return done(null, false)
29+
if (!await user.verifyPassword(password)) return done(null, false)
2730
return done(null, user)
28-
}).catch(function (err) {
31+
} catch (err) {
2932
logger.error(err)
3033
return done(err)
31-
})
34+
}
3235
}))
3336

3437
if (config.allowEmailRegister) {
35-
emailAuth.post('/register', urlencodedParser, function (req, res, next) {
38+
emailAuth.post('/register', urlencodedParser, async function (req, res, next) {
3639
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
3740
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
38-
models.User.findOrCreate({
39-
where: {
40-
email: req.body.email
41-
},
42-
defaults: {
43-
password: req.body.password
44-
}
45-
}).spread(function (user, created) {
46-
if (user) {
47-
if (created) {
48-
logger.debug('user registered: ' + user.id)
49-
req.flash('info', "You've successfully registered, please signin.")
50-
} else {
51-
logger.debug('user found: ' + user.id)
52-
req.flash('error', 'This email has been used, please try another one.')
41+
try {
42+
const [user, created] = await models.User.findOrCreate({
43+
where: {
44+
email: req.body.email
45+
},
46+
defaults: {
47+
password: req.body.password
5348
}
49+
})
50+
51+
if (!user) {
52+
req.flash('error', 'Failed to register your account, please try again.')
5453
return res.redirect(config.serverURL + '/')
5554
}
56-
req.flash('error', 'Failed to register your account, please try again.')
55+
56+
if (created) {
57+
logger.debug('user registered: ' + user.id)
58+
req.flash('info', "You've successfully registered, please signin.")
59+
} else {
60+
logger.debug('user found: ' + user.id)
61+
req.flash('error', 'This email has been used, please try another one.')
62+
}
5763
return res.redirect(config.serverURL + '/')
58-
}).catch(function (err) {
64+
} catch (err) {
5965
logger.error('auth callback failed: ' + err)
6066
return response.errorInternalError(req, res)
61-
})
67+
}
6268
})
6369
}
6470

lib/models/user.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
// external modules
33
var Sequelize = require('sequelize')
4-
var scrypt = require('scrypt')
4+
var Scrypt = require('scrypt-kdf')
55

66
// core
77
var logger = require('../logger')
@@ -41,22 +41,34 @@ module.exports = function (sequelize, DataTypes) {
4141
}
4242
},
4343
password: {
44-
type: Sequelize.TEXT,
45-
set: function (value) {
46-
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
47-
this.setDataValue('password', hash)
48-
}
44+
type: Sequelize.TEXT
4945
}
5046
})
5147

52-
User.prototype.verifyPassword = function (attempt) {
53-
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) {
48+
User.hashPassword = async function (plain) {
49+
return (await Scrypt.kdf(plain, await Scrypt.pickParams(0.1))).toString('hex')
50+
}
51+
52+
User.prototype.verifyPassword = async function (attempt) {
53+
if (await Scrypt.verify(Buffer.from(this.password, 'hex'), attempt)) {
5454
return this
55-
} else {
56-
return false
5755
}
56+
57+
return false
5858
}
5959

60+
User.addHook('beforeCreate', async function (user) {
61+
// only do hash when password is presented
62+
if (user.password) {
63+
user.password = await User.hashPassword(user.password)
64+
}
65+
})
66+
User.addHook('beforeUpdate', async function (user) {
67+
if (user.changed('password')) {
68+
user.password = await User.hashPassword(user.password)
69+
}
70+
})
71+
6072
User.associate = function (models) {
6173
User.hasMany(models.Note, {
6274
foreignKey: 'ownerId',

0 commit comments

Comments
 (0)