2016-07-25 16:49:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2023-02-04 20:44:55 +00:00
|
|
|
const User = require('./user');
|
|
|
|
const Config = require('./config').get;
|
|
|
|
const Log = require('./logger').log;
|
|
|
|
const { getAddressedToInfo } = require('./mail_util');
|
|
|
|
const Message = require('./message');
|
|
|
|
const { Errors, ErrorReasons } = require('./enig_error'); // note: Only use ValidationFailed in this module!
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const fs = require('graceful-fs');
|
|
|
|
|
|
|
|
exports.validateNonEmpty = validateNonEmpty;
|
|
|
|
exports.validateMessageSubject = validateMessageSubject;
|
|
|
|
exports.validateUserNameAvail = validateUserNameAvail;
|
|
|
|
exports.validateUserNameExists = validateUserNameExists;
|
|
|
|
exports.validateUserNameOrRealNameExists = validateUserNameOrRealNameExists;
|
|
|
|
exports.validateGeneralMailAddressedTo = validateGeneralMailAddressedTo;
|
|
|
|
exports.validateEmailAvail = validateEmailAvail;
|
|
|
|
exports.validateBirthdate = validateBirthdate;
|
|
|
|
exports.validatePasswordSpec = validatePasswordSpec;
|
2015-12-11 04:44:21 +00:00
|
|
|
|
2015-12-12 22:52:56 +00:00
|
|
|
function validateNonEmpty(data, cb) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
data && data.length > 0
|
|
|
|
? null
|
|
|
|
: Errors.ValidationFailed('Field cannot be empty', ErrorReasons.ValueTooShort)
|
|
|
|
);
|
2015-12-12 22:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateMessageSubject(data, cb) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
data && data.length > 1
|
|
|
|
? null
|
|
|
|
: Errors.ValidationFailed('Subject too short', ErrorReasons.ValueTooShort)
|
|
|
|
);
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!data || data.length < config.users.usernameMin) {
|
2023-02-04 20:44:55 +00:00
|
|
|
cb(Errors.ValidationFailed('Username too short', ErrorReasons.ValueTooShort));
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (data.length > config.users.usernameMax) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// generally should be unreached due to view restraints
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Username too long', ErrorReasons.ValueTooLong)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
const usernameRegExp = new RegExp(config.users.usernamePattern);
|
|
|
|
const invalidNames = config.users.newUserNames + config.users.badUserNames;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!usernameRegExp.test(data)) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
'Username contains invalid characters',
|
|
|
|
ErrorReasons.ValueInvalid
|
|
|
|
)
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (invalidNames.indexOf(data.toLowerCase()) > -1) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
'Username is blacklisted',
|
|
|
|
ErrorReasons.NotAllowed
|
|
|
|
)
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (/^[0-9]+$/.test(data)) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
'Username cannot be a number',
|
|
|
|
ErrorReasons.ValueInvalid
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// a new user name cannot be an existing user name or an existing real name
|
2018-06-22 05:15:04 +00:00
|
|
|
User.getUserIdAndNameByLookup(data, function userIdAndName(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
|
|
|
// err is null if we succeeded -- meaning this user exists already
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
'Username unavailable',
|
|
|
|
ErrorReasons.NotAvailable
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 15:58:28 +00:00
|
|
|
function validateUserNameExists(data, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === data.length) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Invalid username', ErrorReasons.ValueTooShort)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-07-25 07:01:14 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
User.getUserIdAndName(data, err => {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
err
|
|
|
|
? Errors.ValidationFailed(
|
|
|
|
'Failed to find username',
|
|
|
|
err.reasonCode || ErrorReasons.DoesNotExist
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2016-07-25 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 05:03:33 +00:00
|
|
|
function validateUserNameOrRealNameExists(data, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === data.length) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Invalid username', ErrorReasons.ValueTooShort)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-06 05:03:33 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
User.getUserIdAndNameByLookup(data, err => {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
err
|
|
|
|
? Errors.ValidationFailed(
|
|
|
|
'Failed to find user',
|
|
|
|
err.reasonCode || ErrorReasons.DoesNotExist
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2018-01-06 05:03:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 15:58:28 +00:00
|
|
|
function validateGeneralMailAddressedTo(data, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Allow any supported addressing:
|
|
|
|
// - Local username or real name
|
|
|
|
// - Supported remote flavors such as FTN, email, ...
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const addressedToInfo = getAddressedToInfo(data);
|
2023-01-12 05:37:09 +00:00
|
|
|
if (Message.AddressFlavor.Local !== addressedToInfo.flavor) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateUserNameOrRealNameExists(data, cb);
|
2018-01-13 15:58:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
function validateEmailAvail(data, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This particular method allows empty data - e.g. no email entered
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!data || 0 === data.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Otherwise, it must be a valid email. We'll be pretty lose here, like
|
|
|
|
// the HTML5 spec.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See http://stackoverflow.com/questions/7786058/find-the-regex-used-by-html5-forms-for-validation
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const emailRegExp = /[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(.[a-z0-9-]+)*/;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!emailRegExp.test(data)) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Invalid email address', ErrorReasons.ValueInvalid)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
User.getUserIdsWithProperty(
|
|
|
|
'email_address',
|
|
|
|
data,
|
|
|
|
function userIdsWithEmail(err, uids) {
|
|
|
|
if (err) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
err.message,
|
|
|
|
err.reasonCode || ErrorReasons.DoesNotExist
|
|
|
|
)
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (uids.length > 0) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed(
|
|
|
|
'Email address not unique',
|
|
|
|
ErrorReasons.NotAvailable
|
|
|
|
)
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateBirthdate(data, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: check for dates in the future, or > reasonable values
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
isNaN(Date.parse(data))
|
|
|
|
? Errors.ValidationFailed('Invalid birthdate', ErrorReasons.ValueInvalid)
|
|
|
|
: null
|
|
|
|
);
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validatePasswordSpec(data, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!data || data.length < config.users.passwordMin) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Password too short', ErrorReasons.ValueTooShort)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// check badpass, if avail
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.readFile(config.users.badPassFile, 'utf8', (err, passwords) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2023-02-04 20:44:55 +00:00
|
|
|
Log.warn(
|
|
|
|
{ error: err.message, path: config.users.badPassFile },
|
|
|
|
'Cannot read bad pass file'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
passwords = passwords.toString().split(/\r\n|\n/g);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (passwords.includes(data)) {
|
2023-02-04 20:44:55 +00:00
|
|
|
return cb(
|
|
|
|
Errors.ValidationFailed('Password is too common', ErrorReasons.NotAllowed)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
2015-12-11 04:44:21 +00:00
|
|
|
}
|