2016-07-25 16:49:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2017-02-18 20:21:18 +00:00
|
|
|
const User = require('./user.js');
|
2016-07-25 16:49:41 +00:00
|
|
|
const Config = require('./config.js').config;
|
2017-03-15 02:21:23 +00:00
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
|
|
|
|
// deps
|
2017-05-20 03:20:19 +00:00
|
|
|
const fs = require('graceful-fs');
|
2015-12-11 04:44:21 +00:00
|
|
|
|
2015-12-12 22:52:56 +00:00
|
|
|
exports.validateNonEmpty = validateNonEmpty;
|
|
|
|
exports.validateMessageSubject = validateMessageSubject;
|
2015-12-11 04:44:21 +00:00
|
|
|
exports.validateUserNameAvail = validateUserNameAvail;
|
2016-07-25 07:01:14 +00:00
|
|
|
exports.validateUserNameExists = validateUserNameExists;
|
2015-12-11 04:44:21 +00:00
|
|
|
exports.validateEmailAvail = validateEmailAvail;
|
|
|
|
exports.validateBirthdate = validateBirthdate;
|
|
|
|
exports.validatePasswordSpec = validatePasswordSpec;
|
|
|
|
|
2015-12-12 22:52:56 +00:00
|
|
|
function validateNonEmpty(data, cb) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(data && data.length > 0 ? null : new Error('Field cannot be empty'));
|
2015-12-12 22:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateMessageSubject(data, cb) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(data && data.length > 1 ? null : new Error('Subject too short'));
|
2015-12-24 18:55:37 +00:00
|
|
|
}
|
2015-12-12 22:52:56 +00:00
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
function validateUserNameAvail(data, cb) {
|
2016-07-25 16:49:41 +00:00
|
|
|
if(!data || data.length < Config.users.usernameMin) {
|
2015-12-11 04:44:21 +00:00
|
|
|
cb(new Error('Username too short'));
|
|
|
|
} else if(data.length > Config.users.usernameMax) {
|
|
|
|
// generally should be unreached due to view restraints
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(new Error('Username too long'));
|
2015-12-11 04:44:21 +00:00
|
|
|
} else {
|
2016-07-25 16:49:41 +00:00
|
|
|
const usernameRegExp = new RegExp(Config.users.usernamePattern);
|
|
|
|
const invalidNames = Config.users.newUserNames + Config.users.badUserNames;
|
2015-12-11 04:44:21 +00:00
|
|
|
|
|
|
|
if(!usernameRegExp.test(data)) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(new Error('Username contains invalid characters'));
|
2015-12-11 04:44:21 +00:00
|
|
|
} else if(invalidNames.indexOf(data.toLowerCase()) > -1) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(new Error('Username is blacklisted'));
|
|
|
|
} else if(/^[0-9]+$/.test(data)) {
|
|
|
|
return cb(new Error('Username cannot be a number'));
|
2015-12-11 04:44:21 +00:00
|
|
|
} else {
|
2017-02-18 20:21:18 +00:00
|
|
|
User.getUserIdAndName(data, function userIdAndName(err) {
|
2015-12-11 04:44:21 +00:00
|
|
|
if(!err) { // err is null if we succeeded -- meaning this user exists already
|
2016-09-01 18:34:31 +00:00
|
|
|
return cb(new Error('Username unavailable'));
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
2016-07-25 16:49:41 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2015-12-11 04:44:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 07:01:14 +00:00
|
|
|
function validateUserNameExists(data, cb) {
|
|
|
|
const invalidUserNameError = new Error('Invalid username');
|
|
|
|
|
|
|
|
if(0 === data.length) {
|
|
|
|
return cb(invalidUserNameError);
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
User.getUserIdAndName(data, (err) => {
|
2016-07-25 07:01:14 +00:00
|
|
|
return cb(err ? invalidUserNameError : null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
function validateEmailAvail(data, cb) {
|
2015-12-12 22:52:56 +00:00
|
|
|
//
|
|
|
|
// This particular method allows empty data - e.g. no email entered
|
|
|
|
//
|
|
|
|
if(!data || 0 === data.length) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Otherwise, it must be a valid email. We'll be pretty lose here, like
|
|
|
|
// the HTML5 spec.
|
|
|
|
//
|
|
|
|
// See http://stackoverflow.com/questions/7786058/find-the-regex-used-by-html5-forms-for-validation
|
|
|
|
//
|
2016-07-25 16:49:41 +00:00
|
|
|
const emailRegExp = /[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9-]+(.[a-z0-9-]+)*/;
|
2015-12-12 22:52:56 +00:00
|
|
|
if(!emailRegExp.test(data)) {
|
|
|
|
return cb(new Error('Invalid email address'));
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
User.getUserIdsWithProperty('email_address', data, function userIdsWithEmail(err, uids) {
|
2015-12-11 04:44:21 +00:00
|
|
|
if(err) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(new Error('Internal system error'));
|
2015-12-11 04:44:21 +00:00
|
|
|
} else if(uids.length > 0) {
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(new Error('Email address not unique'));
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
2016-07-25 16:49:41 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2015-12-11 04:44:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function validateBirthdate(data, cb) {
|
|
|
|
// :TODO: check for dates in the future, or > reasonable values
|
2016-07-25 16:49:41 +00:00
|
|
|
return cb(isNaN(Date.parse(data)) ? new Error('Invalid birthdate') : null);
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validatePasswordSpec(data, cb) {
|
2017-03-15 02:21:23 +00:00
|
|
|
if(!data || data.length < Config.users.passwordMin) {
|
|
|
|
return cb(new Error('Password too short'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// check badpass, if avail
|
|
|
|
fs.readFile(Config.users.badPassFile, 'utf8', (err, passwords) => {
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { error : err.message }, 'Cannot read bad pass file');
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
passwords = passwords.toString().split(/\r\n|\n/g);
|
|
|
|
if(passwords.includes(data)) {
|
|
|
|
return cb(new Error('Password is too common'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|