2019-09-28 01:14:24 +02:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2019-12-01 19:03:55 +01:00
|
|
|
class AccountController {
|
2019-09-28 01:14:24 +02:00
|
|
|
constructor (userModel) {
|
|
|
|
this.model = userModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hashPassword and validatePassword modified from https://stackoverflow.com/a/17201493
|
|
|
|
static hashPassword (password) {
|
|
|
|
var salt = crypto.randomBytes(128).toString('base64');
|
|
|
|
var iterations = 10000;
|
|
|
|
var hash = crypto.pbkdf2Sync(password, salt, iterations, 128, 'sha512').toString('base64');
|
|
|
|
|
|
|
|
return {
|
|
|
|
salt,
|
|
|
|
hash,
|
|
|
|
iterations,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static verifyPassword (savedHash, savedSalt, passwordAttempt) {
|
|
|
|
const attemptedHash = crypto.pbkdf2Sync(passwordAttempt, savedSalt, 10000, 128, 'sha512').toString('base64');
|
|
|
|
return savedHash == attemptedHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static createAccountDataIsValid (createAccountData) {
|
|
|
|
if (typeof createAccountData.email === 'undefined'
|
|
|
|
|| typeof createAccountData.username === 'undefined'
|
|
|
|
|| typeof createAccountData.password === 'undefined'
|
|
|
|
|| createAccountData.password === '') {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.create.required_data_missing',
|
2019-09-28 01:14:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (createAccountData.email.length < 5 || !/^.+@.+\..+$/.test(createAccountData.email)) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.create.invalid_email',
|
2019-09-28 01:14:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (createAccountData.username.length < 2 || !/^[a-z0-9_]+$/i.test(createAccountData.username)) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.create.invalid_username',
|
2019-09-28 01:14:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:38:09 +02:00
|
|
|
static loginDataIsValid (loginData) {
|
|
|
|
if (typeof loginData.email === 'undefined'
|
|
|
|
|| typeof loginData.password === 'undefined'
|
|
|
|
|| loginData.password === '') {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.login.required_data_missing',
|
2019-10-08 20:38:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (loginData.email.length < 5 || !/^.+@.+\..+$/.test(loginData.email)) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.login.invalid_email',
|
2019-10-08 20:38:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-28 01:14:24 +02:00
|
|
|
static cleanCreateAccountFormData (formData) {
|
2019-10-26 19:47:15 +02:00
|
|
|
var displayName = typeof formData.displayName !== 'undefined' ? formData.displayName.toString().trim() : '';
|
2019-09-28 01:14:24 +02:00
|
|
|
return {
|
|
|
|
email: formData.email.trim(),
|
|
|
|
username: formData.username.toString().trim(),
|
2019-10-26 19:47:15 +02:00
|
|
|
displayName: displayName.length > 0 ? displayName : 'A Bee',
|
2019-09-28 01:14:24 +02:00
|
|
|
password: formData.password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:17 +02:00
|
|
|
static confirmAccountDataIsValid(createAccountData) {
|
|
|
|
if (typeof createAccountData.id === 'undefined'
|
|
|
|
|| typeof createAccountData.confirm === 'undefined'
|
|
|
|
|| !createAccountData.confirm) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.confirm.required_data_missing',
|
2019-10-03 21:04:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-28 01:14:24 +02:00
|
|
|
async emailExists (email) {
|
2020-01-05 03:11:57 +01:00
|
|
|
const existingUser = await this.model.findOne({
|
2019-09-28 01:14:24 +02:00
|
|
|
attributes: ['id'],
|
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return existingUser != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async usernameExists (username) {
|
2020-01-05 03:11:57 +01:00
|
|
|
const existingUser = await this.model.findOne({
|
2019-09-28 01:14:24 +02:00
|
|
|
attributes: ['id'],
|
|
|
|
where: {
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return existingUser != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async canCreateUser (email, username) {
|
|
|
|
const emailExists = await this.emailExists(email);
|
|
|
|
const usernameExists = await this.usernameExists(username);
|
|
|
|
if (emailExists) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.email_exists',
|
2019-09-28 01:14:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (usernameExists) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.username_exists',
|
2019-09-28 01:14:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-28 02:40:39 +02:00
|
|
|
async createUser (email, username, displayName, password, needsConfirmation) {
|
2019-12-01 19:03:55 +01:00
|
|
|
const hashData = AccountController.hashPassword(password);
|
|
|
|
// The data should already have gone through AccountController.cleanCreateAccountFormData()
|
2019-12-01 02:02:24 +01:00
|
|
|
try {
|
|
|
|
return await this.model.create({
|
|
|
|
email,
|
|
|
|
username,
|
|
|
|
displayName,
|
|
|
|
passwordHash: hashData.hash,
|
|
|
|
passwordSalt: hashData.salt,
|
|
|
|
accountConfirm: needsConfirmation ? crypto.randomBytes(32).toString('hex') : null,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
}
|
2019-09-28 01:14:24 +02:00
|
|
|
}
|
2019-10-03 21:04:17 +02:00
|
|
|
|
|
|
|
async confirmUser (id, accountConfirm) {
|
|
|
|
const userToConfirm = await this.model.findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
accountConfirm,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!userToConfirm) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.confirm.invalid_code',
|
2019-10-03 21:04:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.model.update({
|
|
|
|
accountConfirm: null,
|
|
|
|
}, {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
accountConfirm,
|
|
|
|
},
|
|
|
|
}).then(success => {
|
|
|
|
if (success[0] < 1) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.confirm.update_fail',
|
2019-10-03 21:04:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return userToConfirm;
|
|
|
|
});
|
|
|
|
}
|
2019-10-08 20:38:09 +02:00
|
|
|
|
|
|
|
async validateLogin (email, password) {
|
2020-01-05 03:11:57 +01:00
|
|
|
const existingUser = await this.model.findOne({
|
2019-10-08 20:38:09 +02:00
|
|
|
attributes: ['id', 'passwordHash', 'passwordSalt', 'accountConfirm'],
|
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (existingUser == null) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.login.invalid_email',
|
2019-10-08 20:38:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingUser.accountConfirm !== null) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.login.not_confirmed',
|
2019-10-08 20:38:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-01 19:03:55 +01:00
|
|
|
const passwordIsValid = AccountController.verifyPassword(existingUser.passwordHash, existingUser.passwordSalt, password);
|
2019-10-08 20:38:09 +02:00
|
|
|
if (!passwordIsValid) {
|
|
|
|
return {
|
|
|
|
error: true,
|
2019-12-01 22:08:12 +01:00
|
|
|
message: 'api.account.login.invalid_password',
|
2019-10-08 20:38:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
error: false,
|
|
|
|
id: existingUser.id,
|
|
|
|
};
|
|
|
|
}
|
2019-12-01 02:02:24 +01:00
|
|
|
|
|
|
|
async deleteUser(user) {
|
|
|
|
return await user.destroy();
|
|
|
|
}
|
2019-09-28 01:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-01 19:03:55 +01:00
|
|
|
module.exports = AccountController;
|