2014-02-19 17:57:26 +04:00
|
|
|
var _ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
when = require('when'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-06-16 07:33:25 +04:00
|
|
|
nodefn = require('when/node'),
|
2013-10-23 17:00:28 +04:00
|
|
|
bcrypt = require('bcryptjs'),
|
2013-09-24 14:46:30 +04:00
|
|
|
Posts = require('./post').Posts,
|
2013-09-23 02:20:08 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
2013-09-24 14:46:30 +04:00
|
|
|
Role = require('./role').Role,
|
2013-11-11 23:55:22 +04:00
|
|
|
Permission = require('./permission').Permission,
|
|
|
|
http = require('http'),
|
2014-01-30 16:27:29 +04:00
|
|
|
crypto = require('crypto'),
|
2014-02-19 21:32:23 +04:00
|
|
|
validator = require('validator'),
|
2014-01-30 16:27:29 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
tokenSecurity = {},
|
|
|
|
User,
|
|
|
|
Users;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-08-20 22:52:44 +04:00
|
|
|
function validatePasswordLength(password) {
|
|
|
|
try {
|
2014-02-28 10:51:52 +04:00
|
|
|
if (!validator.isLength(password, 8)) {
|
|
|
|
throw new Error('Your password must be at least 8 characters long.');
|
|
|
|
}
|
2013-08-20 22:52:44 +04:00
|
|
|
} catch (error) {
|
|
|
|
return when.reject(error);
|
|
|
|
}
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
2013-11-22 07:17:38 +04:00
|
|
|
function generatePasswordHash(password) {
|
|
|
|
// Generate a new salt
|
|
|
|
return nodefn.call(bcrypt.genSalt).then(function (salt) {
|
|
|
|
// Hash the provided password with bcrypt
|
|
|
|
return nodefn.call(bcrypt.hash, password, salt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
User = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
tableName: 'users',
|
|
|
|
|
2014-03-25 14:59:15 +04:00
|
|
|
saving: function (newPage, attr, options) {
|
2014-07-08 20:00:59 +04:00
|
|
|
/*jshint unused:false*/
|
2014-03-25 14:59:15 +04:00
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
var self = this;
|
2014-02-19 17:57:26 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
|
|
|
// this.set('name', this.sanitize('name'));
|
|
|
|
// this.set('email', this.sanitize('email'));
|
|
|
|
// this.set('location', this.sanitize('location'));
|
|
|
|
// this.set('website', this.sanitize('website'));
|
|
|
|
// this.set('bio', this.sanitize('bio'));
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
ghostBookshelf.Model.prototype.saving.apply(this, arguments);
|
2013-09-14 23:01:46 +04:00
|
|
|
|
2014-03-25 14:59:15 +04:00
|
|
|
if (this.hasChanged('slug') || !this.get('slug')) {
|
2013-09-14 23:01:46 +04:00
|
|
|
// Generating a slug requires a db call to look for conflicting slugs
|
2014-03-25 14:59:15 +04:00
|
|
|
return ghostBookshelf.Model.generateSlug(User, this.get('slug') || this.get('name'),
|
|
|
|
{transacting: options.transacting})
|
2013-09-14 23:01:46 +04:00
|
|
|
.then(function (slug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
2013-10-07 21:02:57 +04:00
|
|
|
},
|
|
|
|
|
2014-05-06 14:14:58 +04:00
|
|
|
toJSON: function (options) {
|
|
|
|
var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
|
|
|
|
// remove password hash for security reasons
|
|
|
|
delete attrs.password;
|
2014-05-06 05:45:08 +04:00
|
|
|
|
2014-05-06 14:14:58 +04:00
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
posts: function () {
|
|
|
|
return this.hasMany(Posts, 'created_by');
|
|
|
|
},
|
|
|
|
|
|
|
|
roles: function () {
|
|
|
|
return this.belongsToMany(Role);
|
|
|
|
},
|
|
|
|
|
|
|
|
permissions: function () {
|
|
|
|
return this.belongsToMany(Permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, {
|
2014-05-06 05:45:08 +04:00
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function (methodName) {
|
|
|
|
var options = ghostBookshelf.Model.permittedOptions(),
|
|
|
|
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
|
|
validOptions = {
|
|
|
|
findOne: ['withRelated'],
|
2014-07-08 20:00:59 +04:00
|
|
|
findAll: ['withRelated'],
|
2014-05-06 05:45:08 +04:00
|
|
|
add: ['user'],
|
2014-07-08 20:00:59 +04:00
|
|
|
edit: ['user', 'withRelated']
|
2014-05-06 05:45:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-07-08 20:00:59 +04:00
|
|
|
/**
|
|
|
|
* ### Find All
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
|
|
|
options.withRelated = _.union([ 'roles' ], options.include);
|
|
|
|
return ghostBookshelf.Model.findAll.call(this, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find One
|
|
|
|
* @extends ghostBookshelf.Model.findOne to include roles
|
|
|
|
* **See:** [ghostBookshelf.Model.findOne](base.js.html#Find%20One)
|
|
|
|
*/
|
|
|
|
findOne: function (data, options) {
|
|
|
|
options = options || {};
|
|
|
|
options.withRelated = _.union([ 'roles' ], options.include);
|
|
|
|
|
|
|
|
return ghostBookshelf.Model.findOne.call(this, data, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Edit
|
|
|
|
* @extends ghostBookshelf.Model.edit to handle returning the full object
|
|
|
|
* **See:** [ghostBookshelf.Model.edit](base.js.html#edit)
|
|
|
|
*/
|
|
|
|
edit: function (data, options) {
|
|
|
|
options = options || {};
|
|
|
|
options.withRelated = _.union([ 'roles' ], options.include);
|
|
|
|
|
|
|
|
return ghostBookshelf.Model.edit.call(this, data, options);
|
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* ## Add
|
2013-06-25 15:43:15 +04:00
|
|
|
* Naive user add
|
|
|
|
* Hashes the password provided before saving to the database.
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
* @param {object} options
|
|
|
|
* @extends ghostBookshelf.Model.add to manage all aspects of user signup
|
|
|
|
* **See:** [ghostBookshelf.Model.add](base.js.html#Add)
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
add: function (data, options) {
|
2013-08-16 03:22:08 +04:00
|
|
|
var self = this,
|
|
|
|
// Clone the _user so we don't expose the hashed password unnecessarily
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
userData = this.filterData(data);
|
2014-05-06 05:45:08 +04:00
|
|
|
|
|
|
|
options = this.filterOptions(options, 'add');
|
2014-07-08 20:00:59 +04:00
|
|
|
options.withRelated = _.union([ 'roles' ], options.include);
|
2014-05-06 05:45:08 +04:00
|
|
|
|
2013-06-01 18:47:41 +04:00
|
|
|
/**
|
2013-06-25 15:43:15 +04:00
|
|
|
* This only allows one user to be added to the database, otherwise fails.
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {object} user
|
2013-06-25 15:43:15 +04:00
|
|
|
* @author javorszky
|
2013-06-01 18:47:41 +04:00
|
|
|
*/
|
2013-08-20 22:52:44 +04:00
|
|
|
return validatePasswordLength(userData.password).then(function () {
|
|
|
|
return self.forge().fetch();
|
|
|
|
}).then(function () {
|
2013-11-22 07:17:38 +04:00
|
|
|
// Generate a new password hash
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return generatePasswordHash(data.password);
|
2013-08-16 03:22:08 +04:00
|
|
|
}).then(function (hash) {
|
|
|
|
// Assign the hashed password
|
|
|
|
userData.password = hash;
|
2013-11-11 23:55:22 +04:00
|
|
|
// LookupGravatar
|
|
|
|
return self.gravatarLookup(userData);
|
|
|
|
}).then(function (userData) {
|
2013-08-16 03:22:08 +04:00
|
|
|
// Save the user with the hashed password
|
2014-04-03 17:03:09 +04:00
|
|
|
return ghostBookshelf.Model.add.call(self, userData, options);
|
2013-08-16 03:22:08 +04:00
|
|
|
}).then(function (addedUser) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
// Assign the userData to our created user so we can pass it back
|
|
|
|
userData = addedUser;
|
2014-07-02 18:22:18 +04:00
|
|
|
if (!data.role) {
|
|
|
|
// TODO: needs change when owner role is introduced and setup is changed
|
|
|
|
data.role = 1;
|
|
|
|
}
|
|
|
|
return userData.roles().attach(data.role);
|
2013-08-16 03:22:08 +04:00
|
|
|
}).then(function (addedUserRole) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2014-04-29 00:42:38 +04:00
|
|
|
// find and return the added user
|
|
|
|
return self.findOne({id: userData.id}, options);
|
2013-08-19 01:50:42 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
2014-05-14 05:49:07 +04:00
|
|
|
permissable: function (userModelOrId, context, loadedPermissions, hasUserPermission, hasAppPermission) {
|
2014-04-08 17:40:33 +04:00
|
|
|
var self = this,
|
2014-05-14 05:49:07 +04:00
|
|
|
userModel = userModelOrId,
|
|
|
|
origArgs;
|
2014-04-08 17:40:33 +04:00
|
|
|
|
|
|
|
// If we passed in an id instead of a model, get the model
|
|
|
|
// then check the permissions
|
|
|
|
if (_.isNumber(userModelOrId) || _.isString(userModelOrId)) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Grab the original args without the first one
|
|
|
|
origArgs = _.toArray(arguments).slice(1);
|
|
|
|
// Get the actual post model
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
return this.findOne({id: userModelOrId}).then(function (foundUserModel) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Build up the original args but substitute with actual model
|
|
|
|
var newArgs = [foundUserModel].concat(origArgs);
|
|
|
|
|
|
|
|
return self.permissable.apply(self, newArgs);
|
2014-04-08 17:40:33 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
|
2014-05-14 05:49:07 +04:00
|
|
|
if (userModel) {
|
|
|
|
// If this is the same user that requests the operation allow it.
|
|
|
|
hasUserPermission = hasUserPermission || context.user === userModel.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUserPermission && hasAppPermission) {
|
2014-04-08 17:40:33 +04:00
|
|
|
return when.resolve();
|
|
|
|
}
|
2014-05-14 05:49:07 +04:00
|
|
|
|
2014-04-08 17:40:33 +04:00
|
|
|
return when.reject();
|
|
|
|
},
|
|
|
|
|
2013-11-29 04:28:01 +04:00
|
|
|
setWarning: function (user) {
|
|
|
|
var status = user.get('status'),
|
|
|
|
regexp = /warn-(\d+)/i,
|
|
|
|
level;
|
|
|
|
|
|
|
|
if (status === 'active') {
|
|
|
|
user.set('status', 'warn-1');
|
|
|
|
level = 1;
|
|
|
|
} else {
|
|
|
|
level = parseInt(status.match(regexp)[1], 10) + 1;
|
|
|
|
if (level > 3) {
|
|
|
|
user.set('status', 'locked');
|
|
|
|
} else {
|
|
|
|
user.set('status', 'warn-' + level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return when(user.save()).then(function () {
|
|
|
|
return 5 - level;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Finds the user by email, and checks the password
|
2014-06-30 16:58:10 +04:00
|
|
|
check: function (object) {
|
2013-11-29 04:28:01 +04:00
|
|
|
var self = this,
|
|
|
|
s;
|
2014-06-30 16:58:10 +04:00
|
|
|
return this.getByEmail(object.email).then(function (user) {
|
2014-07-10 14:00:51 +04:00
|
|
|
if (!user || user.get('status') === 'invited' || user.get('status') === 'inactive') {
|
2014-07-02 18:22:18 +04:00
|
|
|
return when.reject(new Error('NotFound'));
|
|
|
|
}
|
2013-11-29 04:28:01 +04:00
|
|
|
if (user.get('status') !== 'locked') {
|
2014-06-30 16:58:10 +04:00
|
|
|
return nodefn.call(bcrypt.compare, object.password, user.get('password')).then(function (matched) {
|
2013-11-29 04:28:01 +04:00
|
|
|
if (!matched) {
|
|
|
|
return when(self.setWarning(user)).then(function (remaining) {
|
|
|
|
s = (remaining > 1) ? 's' : '';
|
|
|
|
return when.reject(new Error('Your password is incorrect.<br>' +
|
|
|
|
remaining + ' attempt' + s + ' remaining!'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-27 19:28:34 +04:00
|
|
|
return when(user.set({status : 'active', last_login : new Date()}).save()).then(function (user) {
|
2013-11-29 04:28:01 +04:00
|
|
|
return user;
|
|
|
|
});
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
return when.reject(new Error('Your account is locked due to too many ' +
|
|
|
|
'login attempts. Please reset your password to log in again by clicking ' +
|
|
|
|
'the "Forgotten password?" link!'));
|
|
|
|
|
2013-08-09 05:22:49 +04:00
|
|
|
}, function (error) {
|
2014-01-15 02:47:17 +04:00
|
|
|
if (error.message === 'NotFound' || error.message === 'EmptyResponse') {
|
|
|
|
return when.reject(new Error('There is no user with that email address.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return when.reject(error);
|
2013-08-09 05:22:49 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
2013-08-06 03:49:06 +04:00
|
|
|
/**
|
|
|
|
* Naive change password method
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {object} _userdata email, old pw, new pw, new pw2
|
2013-08-06 03:49:06 +04:00
|
|
|
*/
|
2014-06-20 13:15:01 +04:00
|
|
|
changePassword: function (oldPassword, newPassword, ne2Password, options) {
|
2013-08-20 22:52:44 +04:00
|
|
|
var self = this,
|
2014-06-20 13:15:01 +04:00
|
|
|
userid = options.context.user,
|
2013-09-01 02:20:12 +04:00
|
|
|
user = null;
|
|
|
|
|
2013-08-06 03:49:06 +04:00
|
|
|
if (newPassword !== ne2Password) {
|
2013-08-20 22:52:44 +04:00
|
|
|
return when.reject(new Error('Your new passwords do not match'));
|
2013-08-06 03:49:06 +04:00
|
|
|
}
|
|
|
|
|
2013-08-20 22:52:44 +04:00
|
|
|
return validatePasswordLength(newPassword).then(function () {
|
|
|
|
return self.forge({id: userid}).fetch({require: true});
|
2013-09-01 02:20:12 +04:00
|
|
|
}).then(function (_user) {
|
|
|
|
user = _user;
|
|
|
|
return nodefn.call(bcrypt.compare, oldPassword, user.get('password'));
|
|
|
|
}).then(function (matched) {
|
|
|
|
if (!matched) {
|
|
|
|
return when.reject(new Error('Your password is incorrect'));
|
|
|
|
}
|
2013-10-23 17:00:28 +04:00
|
|
|
return nodefn.call(bcrypt.genSalt);
|
|
|
|
}).then(function (salt) {
|
|
|
|
return nodefn.call(bcrypt.hash, newPassword, salt);
|
2013-09-01 02:20:12 +04:00
|
|
|
}).then(function (hash) {
|
|
|
|
user.save({password: hash});
|
|
|
|
return user;
|
2013-08-06 03:49:06 +04:00
|
|
|
});
|
2013-09-01 02:20:12 +04:00
|
|
|
},
|
|
|
|
|
2013-11-22 07:17:38 +04:00
|
|
|
generateResetToken: function (email, expires, dbHash) {
|
2014-01-15 02:47:17 +04:00
|
|
|
return this.getByEmail(email).then(function (foundUser) {
|
2014-07-02 18:22:18 +04:00
|
|
|
if (!foundUser) {
|
|
|
|
return when.reject(new Error('NotFound'));
|
|
|
|
}
|
|
|
|
|
2013-11-22 07:17:38 +04:00
|
|
|
var hash = crypto.createHash('sha256'),
|
|
|
|
text = "";
|
2013-08-20 22:52:44 +04:00
|
|
|
|
2013-11-22 07:17:38 +04:00
|
|
|
// Token:
|
|
|
|
// BASE64(TIMESTAMP + email + HASH(TIMESTAMP + email + oldPasswordHash + dbHash ))
|
|
|
|
|
|
|
|
hash.update(String(expires));
|
|
|
|
hash.update(email.toLocaleLowerCase());
|
|
|
|
hash.update(foundUser.get('password'));
|
|
|
|
hash.update(String(dbHash));
|
|
|
|
|
|
|
|
text += [expires, email, hash.digest('base64')].join('|');
|
|
|
|
|
|
|
|
return new Buffer(text).toString('base64');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validateToken: function (token, dbHash) {
|
2014-01-30 16:27:29 +04:00
|
|
|
/*jslint bitwise:true*/
|
2013-11-22 07:17:38 +04:00
|
|
|
// TODO: Is there a chance the use of ascii here will cause problems if oldPassword has weird characters?
|
|
|
|
var tokenText = new Buffer(token, 'base64').toString('ascii'),
|
|
|
|
parts,
|
|
|
|
expires,
|
|
|
|
email;
|
|
|
|
|
|
|
|
parts = tokenText.split('|');
|
|
|
|
|
|
|
|
// Check if invalid structure
|
|
|
|
if (!parts || parts.length !== 3) {
|
|
|
|
return when.reject(new Error("Invalid token structure"));
|
|
|
|
}
|
|
|
|
|
|
|
|
expires = parseInt(parts[0], 10);
|
|
|
|
email = parts[1];
|
|
|
|
|
|
|
|
if (isNaN(expires)) {
|
|
|
|
return when.reject(new Error("Invalid token expiration"));
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:27:29 +04:00
|
|
|
// Check if token is expired to prevent replay attacks
|
2013-11-22 07:17:38 +04:00
|
|
|
if (expires < Date.now()) {
|
|
|
|
return when.reject(new Error("Expired token"));
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:27:29 +04:00
|
|
|
// to prevent brute force attempts to reset the password the combination of email+expires is only allowed for 10 attempts
|
|
|
|
if (tokenSecurity[email + '+' + expires] && tokenSecurity[email + '+' + expires].count >= 10) {
|
|
|
|
return when.reject(new Error("Token locked"));
|
|
|
|
}
|
|
|
|
|
2013-11-22 07:17:38 +04:00
|
|
|
return this.generateResetToken(email, expires, dbHash).then(function (generatedToken) {
|
2014-01-30 16:27:29 +04:00
|
|
|
// Check for matching tokens with timing independent comparison
|
|
|
|
var diff = 0,
|
|
|
|
i;
|
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
// check if the token length is correct
|
2014-01-30 16:27:29 +04:00
|
|
|
if (token.length !== generatedToken.length) {
|
|
|
|
diff = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = token.length - 1; i >= 0; i = i - 1) {
|
|
|
|
diff |= token.charCodeAt(i) ^ generatedToken.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff === 0) {
|
2013-11-22 07:17:38 +04:00
|
|
|
return when.resolve(email);
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:27:29 +04:00
|
|
|
// increase the count for email+expires for each failed attempt
|
|
|
|
tokenSecurity[email + '+' + expires] = {count: tokenSecurity[email + '+' + expires] ? tokenSecurity[email + '+' + expires].count + 1 : 1};
|
2013-11-22 07:17:38 +04:00
|
|
|
return when.reject(new Error("Invalid token"));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPassword: function (token, newPassword, ne2Password, dbHash) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (newPassword !== ne2Password) {
|
|
|
|
return when.reject(new Error("Your new passwords do not match"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return validatePasswordLength(newPassword).then(function () {
|
|
|
|
// Validate the token; returns the email address from token
|
|
|
|
return self.validateToken(token, dbHash);
|
|
|
|
}).then(function (email) {
|
|
|
|
// Fetch the user by email, and hash the password at the same time.
|
|
|
|
return when.join(
|
|
|
|
self.forge({email: email.toLocaleLowerCase()}).fetch({require: true}),
|
|
|
|
generatePasswordHash(newPassword)
|
|
|
|
);
|
|
|
|
}).then(function (results) {
|
|
|
|
// Update the user with the new password hash
|
|
|
|
var foundUser = results[0],
|
|
|
|
passwordHash = results[1];
|
|
|
|
|
2014-07-03 19:06:07 +04:00
|
|
|
return foundUser.save({password: passwordHash, status: 'active'});
|
2013-09-01 02:20:12 +04:00
|
|
|
});
|
2013-08-06 03:49:06 +04:00
|
|
|
},
|
|
|
|
|
2013-11-11 23:55:22 +04:00
|
|
|
gravatarLookup: function (userData) {
|
2013-12-17 20:21:00 +04:00
|
|
|
var gravatarUrl = '//www.gravatar.com/avatar/' +
|
2014-07-02 18:22:18 +04:00
|
|
|
crypto.createHash('md5').update(userData.email.toLowerCase().trim()).digest('hex') +
|
|
|
|
"?d=404",
|
2013-11-11 23:55:22 +04:00
|
|
|
checkPromise = when.defer();
|
|
|
|
|
2014-01-01 21:47:12 +04:00
|
|
|
http.get('http:' + gravatarUrl, function (res) {
|
2013-11-11 23:55:22 +04:00
|
|
|
if (res.statusCode !== 404) {
|
|
|
|
userData.image = gravatarUrl;
|
|
|
|
}
|
|
|
|
checkPromise.resolve(userData);
|
|
|
|
}).on('error', function () {
|
|
|
|
//Error making request just continue.
|
|
|
|
checkPromise.resolve(userData);
|
|
|
|
});
|
|
|
|
|
|
|
|
return checkPromise.promise;
|
2014-01-15 02:47:17 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get the user by email address, enforces case insensitivity rejects if the user is not found
|
|
|
|
// When multi-user support is added, email addresses must be deduplicated with case insensitivity, so that
|
|
|
|
// joe@bloggs.com and JOE@BLOGGS.COM cannot be created as two separate users.
|
|
|
|
getByEmail: function (email) {
|
|
|
|
// We fetch all users and process them in JS as there is no easy way to make this query across all DBs
|
|
|
|
// Although they all support `lower()`, sqlite can't case transform unicode characters
|
|
|
|
// This is somewhat mute, as validator.isEmail() also doesn't support unicode, but this is much easier / more
|
|
|
|
// likely to be fixed in the near future.
|
|
|
|
return Users.forge().fetch({require: true}).then(function (users) {
|
|
|
|
var userWithEmail = users.find(function (user) {
|
|
|
|
return user.get('email').toLowerCase() === email.toLowerCase();
|
|
|
|
});
|
|
|
|
if (userWithEmail) {
|
|
|
|
return when.resolve(userWithEmail);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Users = ghostBookshelf.Collection.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
model: User
|
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
User: User,
|
|
|
|
Users: Users
|
2013-08-06 03:49:06 +04:00
|
|
|
};
|