Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 9244b84

Browse files
author
Joseph Wilk
authored
Merge pull request #308 from Jeddf/profile-hashes
Profile password hashes
2 parents 08bded2 + 1cfc28b commit 9244b84

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

lib/profile-utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const bcrypt = require('bcryptjs');
2+
const saltRounds = process.env.NODE_ENV == 'test' ? 5 : 12;
3+
4+
const encodePassword = async input => {
5+
const salt = await bcrypt.genSalt(saltRounds);
6+
7+
return await bcrypt.hash(input, salt);
8+
};
9+
10+
module.exports = { encodePassword };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"dependencies": {
3030
"async": "0.9.0",
31+
"bcryptjs": "2.4.3",
3132
"camelcase": "^4.1.0",
3233
"cp-i18n-lib": "git+https://github.com/CoderDojo/cp-i18n-lib.git",
3334
"cp-logs-lib": "git://github.com/CoderDojo/cp-logs-lib#1.1.0",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DO $$
2+
BEGIN
3+
BEGIN
4+
ALTER TABLE sys_user ADD COLUMN profile_password character varying;
5+
EXCEPTION
6+
WHEN duplicate_column THEN RAISE NOTICE 'column profile_password already exists in sys_user.';
7+
END;
8+
END;
9+
$$

users.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var moment = require('moment');
77
var pg = require('pg');
88
var crypto = require('crypto');
99

10+
var profileUtils = require('./lib/profile-utils');
11+
1012
module.exports = function (options) {
1113
var seneca = this;
1214
var plugin = 'cd-users';
@@ -30,6 +32,7 @@ module.exports = function (options) {
3032
seneca.add({role: plugin, cmd: 'load_champions_for_user'}, cmd_load_champions_for_user);
3133
seneca.add({role: plugin, cmd: 'load_dojo_admins_for_user'}, cmd_load_dojo_admins_for_user);
3234
seneca.add({role: plugin, cmd: 'record_login'}, cmd_record_login);
35+
seneca.add({role: plugin, cmd: 'update_profile_password'}, cmd_update_profile_password);
3336
seneca.add({role: 'user', cmd: 'login'}, cmd_login);
3437
seneca.add({role: 'user', cmd: 'cdf_login'}, cmd_cdf_login);
3538
seneca.add({role: plugin, cmd: 'load_prev_founder'}, cmd_load_prev_founder);
@@ -65,6 +68,13 @@ module.exports = function (options) {
6568
});
6669
}
6770

71+
function cmd_update_profile_password (args, done) {
72+
profileUtils.encodePassword(args.password).then((profileHash) => {
73+
const updatedUser = Object.assign({}, args.user, {profilePassword: profileHash});
74+
seneca.act({role: plugin, cmd: 'update'}, { id: args.user.id, user: updatedUser }, done);
75+
});
76+
}
77+
6878
function cmd_load (args, done) {
6979
var seneca = this;
7080
var id = args.id;
@@ -130,6 +140,13 @@ module.exports = function (options) {
130140
}
131141
};
132142

143+
function addProfilePassword (data, done) {
144+
profileUtils.encodePassword(user.password).then((profileHash) => {
145+
user.profilePassword = profileHash;
146+
done(null, data);
147+
});
148+
}
149+
133150
function verifyCaptcha (done) {
134151
request.post(postData, function (err, response, body) {
135152
if (err) {
@@ -221,6 +238,7 @@ module.exports = function (options) {
221238
async.waterfall([
222239
verifyCaptcha,
223240
checkPermissions,
241+
addProfilePassword,
224242
registerUser,
225243
sendWelcomeEmail
226244
], function (err, results) {
@@ -428,6 +446,8 @@ module.exports = function (options) {
428446
out.reset = reset;
429447
if (!out.ok) { return done(null, out); }
430448

449+
seneca.act({role: plugin, cmd: 'update_profile_password'}, {password: args.password, user: user});
450+
431451
reset.active = false;
432452
reset.save$(function (err, reset) {
433453
if (err) { return done(err); }
@@ -503,17 +523,28 @@ module.exports = function (options) {
503523
if (err) return done(err);
504524
if (!loginResponse.ok || !loginResponse.user) return done(null, loginResponse);
505525

506-
async.series([
526+
const handlers = [
507527
verifyPermissions,
508528
recordLogin
509-
], function (err) {
529+
];
530+
531+
if (!loginResponse.user.profilePassword) {
532+
handlers.push(updateProfilePassword);
533+
}
534+
535+
async.series(handlers, function (err) {
510536
if (err) {
511537
return done(err);
512538
}
513539

514540
return done(null, loginResponse);
515541
});
516542

543+
function updateProfilePassword (next) {
544+
seneca.act({role: plugin, cmd: 'update_profile_password'}, {password: args.password, user: loginResponse.user});
545+
next();
546+
}
547+
517548
function verifyPermissions (next) {
518549
var userRole;
519550

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ bcrypt-pbkdf@^1.0.0:
479479
dependencies:
480480
tweetnacl "^0.14.3"
481481

482+
483+
version "2.4.3"
484+
resolved "https://registry.yarnpkg.com/bcryptjs/-/bcryptjs-2.4.3.tgz#9ab5627b93e60621ff7cdac5da9733027df1d0cb"
485+
integrity sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=
486+
482487
483488
version "0.3.1"
484489
resolved "https://registry.yarnpkg.com/big-number/-/big-number-0.3.1.tgz#ac73020c0a59bb79eb17c2ce2db77f77d974e013"

0 commit comments

Comments
 (0)