2015-07-09 23:01:43 +01:00
|
|
|
import crypto from 'crypto';
|
2016-06-19 10:14:47 -07:00
|
|
|
import { join } from 'path';
|
2015-07-09 23:01:43 +01:00
|
|
|
import passwords from 'passwords';
|
|
|
|
import uuid from 'node-uuid';
|
|
|
|
import views from 'co-views';
|
2018-06-02 15:50:39 +00:00
|
|
|
import debugname from 'debug';
|
|
|
|
import sendgridInit from 'sendgrid';
|
2016-06-19 10:14:47 -07:00
|
|
|
import models from '../../models';
|
2018-06-02 15:50:39 +00:00
|
|
|
|
2016-06-06 15:37:00 +01:00
|
|
|
const render = views(join(__dirname, '..', 'views'), { default: 'ejs' });
|
2015-07-09 23:01:43 +01:00
|
|
|
const debug = debugname('hostr-web:auth');
|
2016-05-01 12:30:46 +01:00
|
|
|
const sendgrid = sendgridInit(process.env.SENDGRID_KEY);
|
2015-07-09 23:01:43 +01:00
|
|
|
|
2016-06-06 15:37:00 +01:00
|
|
|
const from = process.env.EMAIL_FROM;
|
|
|
|
const fromname = process.env.EMAIL_NAME;
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function authenticate(email, password) {
|
2016-08-07 20:41:21 +01:00
|
|
|
const remoteIp = this.headers['x-forwarded-for'] || this.ip;
|
2015-07-09 23:01:43 +01:00
|
|
|
|
2015-08-23 22:12:32 +01:00
|
|
|
if (!password || password.length < 6) {
|
2015-07-09 23:01:43 +01:00
|
|
|
debug('No password, or password too short');
|
|
|
|
return new Error('Invalid login details');
|
|
|
|
}
|
2018-06-02 15:50:39 +00:00
|
|
|
const count = await models.login.count({
|
2016-06-19 10:14:47 -07:00
|
|
|
where: {
|
|
|
|
ip: remoteIp,
|
|
|
|
successful: false,
|
|
|
|
createdAt: {
|
2016-08-07 14:38:05 +01:00
|
|
|
$gt: Math.ceil(Date.now()) - 600000,
|
2016-06-19 10:14:47 -07:00
|
|
|
},
|
|
|
|
},
|
2016-06-06 15:37:00 +01:00
|
|
|
});
|
2016-06-19 10:14:47 -07:00
|
|
|
|
2015-07-09 23:01:43 +01:00
|
|
|
if (count > 25) {
|
|
|
|
debug('Throttling brute force');
|
|
|
|
return new Error('Invalid login details');
|
|
|
|
}
|
2018-06-02 15:50:39 +00:00
|
|
|
const user = await models.user.findOne({
|
2016-08-07 14:38:05 +01:00
|
|
|
where: {
|
|
|
|
email: email.toLowerCase(),
|
|
|
|
activated: true,
|
|
|
|
},
|
2016-06-19 10:14:47 -07:00
|
|
|
});
|
2016-08-07 14:38:05 +01:00
|
|
|
debug(user);
|
2018-06-02 15:50:39 +00:00
|
|
|
const login = await models.login.create({
|
2016-06-19 10:14:47 -07:00
|
|
|
ip: remoteIp,
|
|
|
|
successful: false,
|
2016-06-06 15:37:00 +01:00
|
|
|
});
|
2016-06-19 10:14:47 -07:00
|
|
|
|
2016-08-07 14:38:05 +01:00
|
|
|
if (user && user.password) {
|
2018-06-02 15:50:39 +00:00
|
|
|
if (await passwords.verify(password, user.password)) {
|
2015-07-09 23:01:43 +01:00
|
|
|
debug('Password verified');
|
|
|
|
login.successful = true;
|
2018-06-02 15:50:39 +00:00
|
|
|
await login.save();
|
2016-08-07 14:38:05 +01:00
|
|
|
debug(user);
|
2015-07-09 23:01:43 +01:00
|
|
|
return user;
|
|
|
|
}
|
2015-08-23 22:12:32 +01:00
|
|
|
debug('Password invalid');
|
2016-06-19 10:14:47 -07:00
|
|
|
login.userId = user.id;
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
2018-06-02 15:50:39 +00:00
|
|
|
await login.save();
|
2016-08-07 14:38:05 +01:00
|
|
|
return false;
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function setupSession(user) {
|
2015-07-09 23:01:43 +01:00
|
|
|
debug('Setting up session');
|
|
|
|
const token = uuid.v4();
|
2018-06-02 15:50:39 +00:00
|
|
|
await this.redis.set(token, user.id, 'EX', 604800);
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
const sessionUser = {
|
2016-06-19 10:14:47 -07:00
|
|
|
id: user.id,
|
2016-06-06 15:37:00 +01:00
|
|
|
email: user.email,
|
|
|
|
dailyUploadAllowance: 15,
|
|
|
|
maxFileSize: 20971520,
|
2016-06-19 10:14:47 -07:00
|
|
|
joined: user.createdAt,
|
|
|
|
plan: user.plan,
|
2018-06-02 15:50:39 +00:00
|
|
|
uploadsToday: await models.file.count({ userId: user.id }),
|
2016-06-06 15:37:00 +01:00
|
|
|
md5: crypto.createHash('md5').update(user.email).digest('hex'),
|
|
|
|
token,
|
2015-07-09 23:01:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (sessionUser.plan === 'Pro') {
|
|
|
|
sessionUser.maxFileSize = 524288000;
|
|
|
|
sessionUser.dailyUploadAllowance = 'unlimited';
|
|
|
|
}
|
|
|
|
|
2015-08-22 16:16:15 +01:00
|
|
|
this.session.user = sessionUser;
|
|
|
|
if (this.request.body.remember && this.request.body.remember === 'on') {
|
2018-06-02 15:50:39 +00:00
|
|
|
const remember = await models.remember.create({
|
2016-06-19 10:14:47 -07:00
|
|
|
id: uuid(),
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
this.cookies.set('r', remember.id, { maxAge: 1209600000, httpOnly: true });
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
debug('Session set up');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function signup(email, password, ip) {
|
|
|
|
const existingUser = await models.user.findOne({
|
2016-08-07 14:38:05 +01:00
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
activated: true,
|
|
|
|
},
|
|
|
|
});
|
2015-07-09 23:01:43 +01:00
|
|
|
if (existingUser) {
|
|
|
|
debug('Email already in use.');
|
2015-08-23 16:50:40 +01:00
|
|
|
throw new Error('Email already in use.');
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
2018-06-02 15:50:39 +00:00
|
|
|
const cryptedPassword = await passwords.crypt(password);
|
|
|
|
const user = await models.user.create({
|
2016-06-06 15:37:00 +01:00
|
|
|
email,
|
2016-06-19 10:14:47 -07:00
|
|
|
password: cryptedPassword,
|
|
|
|
ip,
|
2016-08-07 14:38:05 +01:00
|
|
|
plan: 'Free',
|
2016-06-19 10:14:47 -07:00
|
|
|
activation: {
|
|
|
|
id: uuid(),
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
include: [models.activation],
|
|
|
|
});
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
await user.save();
|
2015-07-09 23:01:43 +01:00
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
const html = await render('email/inlined/activate', {
|
2016-06-19 10:14:47 -07:00
|
|
|
activationUrl: `${process.env.WEB_BASE_URL}/activate/${user.activation.id}`,
|
2016-06-06 15:37:00 +01:00
|
|
|
});
|
2015-07-09 23:01:43 +01:00
|
|
|
const text = `Thanks for signing up to Hostr!
|
|
|
|
Please confirm your email address by clicking the link below.
|
|
|
|
|
2016-06-19 10:14:47 -07:00
|
|
|
${process.env.WEB_BASE_URL}/activate/${user.activation.id}
|
2015-07-09 23:01:43 +01:00
|
|
|
|
|
|
|
— Jonathan Cremin, Hostr Founder
|
|
|
|
`;
|
2016-05-01 12:30:46 +01:00
|
|
|
const mail = new sendgrid.Email({
|
|
|
|
to: user.email,
|
2015-07-09 23:01:43 +01:00
|
|
|
subject: 'Welcome to Hostr',
|
2016-06-06 15:37:00 +01:00
|
|
|
from,
|
|
|
|
fromname,
|
|
|
|
html,
|
|
|
|
text,
|
2016-05-01 12:30:46 +01:00
|
|
|
});
|
|
|
|
mail.addCategory('activate');
|
|
|
|
sendgrid.send(mail);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function sendResetToken(email) {
|
|
|
|
const user = await models.user.findOne({
|
2016-08-07 14:38:05 +01:00
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
});
|
2015-07-09 23:01:43 +01:00
|
|
|
if (user) {
|
2018-06-02 15:50:39 +00:00
|
|
|
const reset = await models.reset.create({
|
2016-06-19 10:14:47 -07:00
|
|
|
id: uuid.v4(),
|
|
|
|
userId: user.id,
|
2016-06-06 15:37:00 +01:00
|
|
|
});
|
2018-06-02 15:50:39 +00:00
|
|
|
const html = await render('email/inlined/forgot', {
|
2016-06-19 10:14:47 -07:00
|
|
|
forgotUrl: `${process.env.WEB_BASE_URL}/forgot/${reset.id}`,
|
2015-07-09 23:01:43 +01:00
|
|
|
});
|
|
|
|
const text = `It seems you've forgotten your password :(
|
2016-06-19 10:14:47 -07:00
|
|
|
Visit ${process.env.WEB_BASE_URL}/forgot/${reset.id} to set a new one.
|
2015-07-09 23:01:43 +01:00
|
|
|
`;
|
2016-05-01 12:30:46 +01:00
|
|
|
const mail = new sendgrid.Email({
|
|
|
|
to: user.email,
|
|
|
|
from: 'jonathan@hostr.co',
|
|
|
|
fromname: 'Jonathan from Hostr',
|
2015-07-09 23:01:43 +01:00
|
|
|
subject: 'Hostr Password Reset',
|
2016-06-06 15:37:00 +01:00
|
|
|
html,
|
|
|
|
text,
|
2016-05-01 12:30:46 +01:00
|
|
|
});
|
|
|
|
mail.addCategory('password-reset');
|
|
|
|
sendgrid.send(mail);
|
2015-07-09 23:01:43 +01:00
|
|
|
} else {
|
2015-08-23 16:50:40 +01:00
|
|
|
throw new Error('There was an error looking up your email address.');
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function fromToken(token) {
|
|
|
|
const userId = await this.redis.get(token);
|
|
|
|
return await models.user.findById(userId);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function fromCookie(rememberId) {
|
|
|
|
const userId = await models.remember.findById(rememberId);
|
|
|
|
return await models.user.findById(userId);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function validateResetToken(resetId) {
|
|
|
|
return await models.reset.findById(resetId);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function updatePassword(userId, password) {
|
|
|
|
const cryptedPassword = await passwords.crypt(password);
|
|
|
|
const user = await models.user.findById(userId);
|
2016-06-19 10:14:47 -07:00
|
|
|
user.password = cryptedPassword;
|
2018-06-02 15:50:39 +00:00
|
|
|
await user.save();
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
export async function activateUser(code) {
|
2016-06-19 10:14:47 -07:00
|
|
|
debug(code);
|
2018-06-02 15:50:39 +00:00
|
|
|
const activation = await models.activation.findOne({
|
2016-06-19 10:14:47 -07:00
|
|
|
where: {
|
|
|
|
id: code,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (activation.updatedAt.getTime() === activation.createdAt.getTime()) {
|
|
|
|
activation.activated = true;
|
2018-06-02 15:50:39 +00:00
|
|
|
await activation.save();
|
|
|
|
const user = await activation.getUser();
|
2016-06-19 10:14:47 -07:00
|
|
|
user.activated = true;
|
2018-06-02 15:50:39 +00:00
|
|
|
await user.save();
|
|
|
|
await setupSession.call(this, user);
|
2015-09-04 00:04:48 +02:00
|
|
|
return true;
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
2015-09-04 00:04:48 +02:00
|
|
|
return false;
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|