Skip to content
This repository was archived by the owner on Feb 17, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 0.6.0
- Option to assign new roles to the user scope during creation #69
- Fix mercutio middleware injection
- Fixes mercutio middleware injection
- Added password salt #55
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"chalk": "^1.1.3",
"commander": "^2.9.0",
"copy-paste": "^1.3.0",
"crypto-random-string": "^1.0.0",
"express": "^4.14.0",
"handlebars": "^4.0.6",
"jsonwebtoken": "^7.1.9",
Expand Down
2 changes: 2 additions & 0 deletions store/createModels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function (db) {
email: { type: String, required: true, index: { unique: true }, validate: value => validator.isEmail(value) },
firstname: { type: String, required: false },
lastname: { type: String, required: false },
salt: { type: String, required: false },
password: { type: String, required: true, set: passwordHash },
created: { type: Date, default: Date.now },
updated: { type: Date, default: null },
Expand All @@ -49,6 +50,7 @@ module.exports = function (db) {
virtuals: true,
transform(doc, ret) {
delete ret.password;
delete ret.salt;
delete ret.__t;
delete ret.__v;
delete ret.confirmationToken;
Expand Down
3 changes: 2 additions & 1 deletion store/query/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const _ = require('lodash');
const passwordHash = require('../util/passwordHash');

module.exports = async function(email, password) {
const hash = passwordHash(password);
const body = await this.find({ email });
const hash = passwordHash(password + body[0].salt);

const users = await this.find({ email, password: hash });
if (users.length === 0) throw new Error('Wrong e-mail or password');
Expand Down
6 changes: 4 additions & 2 deletions store/query/change-password.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const passwordHash = require('../util/passwordHash');
const salt = require('../util/salt');

module.exports = async function(id, password, repeated, newPassword) {
const user = await this.findById(id);
if (passwordHash(password) === user.password) {
if (passwordHash(password + user.salt) === user.password) {
if (password === repeated) {
user.password = newPassword;
user.salt = salt();
user.password = newPassword + user.salt;
const err = await user.validateSync();
if (err) throw new Error(err);
await user.save();
Expand Down
3 changes: 3 additions & 0 deletions store/query/insert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const passwordGenerator = require('password-generator');
const _ = require('lodash');
const salt = require('../util/salt');

function sortRoles(roles) {
const rolesWithoutScope = [];
Expand All @@ -26,7 +27,9 @@ function sortRoles(roles) {
module.exports = async function(body) {
let rolesWithoutScope;
let rolesWithScope;
body.salt = salt();
body.password = (body.password) ? body.password : passwordGenerator(8);
body.password += body.salt;
if (body.roles) {
const sortedRoles = sortRoles(body.roles);
rolesWithoutScope = sortedRoles.rolesWithoutScope;
Expand Down
4 changes: 4 additions & 0 deletions store/query/register.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const salt = require('../util/salt');

module.exports = async function(user) {
user.salt = salt();
user.password += user.salt;
const rawUser = await this.create(user);
await this.configureDefaultRoles(rawUser._id);
const result = await this.get(rawUser._id);
Expand Down
5 changes: 5 additions & 0 deletions store/util/salt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const cryptoRandomString = require('crypto-random-string');

module.exports = function() {
return cryptoRandomString(30);
};