hostr/api/lib/auth.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import passwords from 'passwords';
import auth from 'basic-auth';
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-07-09 23:01:43 +01:00
import debugname from 'debug';
const debug = debugname('hostr-api:auth');
const badLoginMsg = '{"error": {"message": "Incorrect login details.", "code": 607}}';
2015-08-23 22:12:32 +01:00
export default function* (next) {
2015-07-09 23:01:43 +01:00
let user = false;
2016-06-19 10:14:47 -07:00
const remoteIp = this.req.headers['x-real-ip'] || this.req.connection.remoteAddress;
const login = yield models.login.create({
ip: remoteIp,
successful: false,
});
2015-07-09 23:01:43 +01:00
if (this.req.headers.authorization && this.req.headers.authorization[0] === ':') {
debug('Logging in with token');
const userToken = yield this.redis.get(this.req.headers.authorization.substr(1));
this.assert(userToken, 401, '{"error": {"message": "Invalid token.", "code": 606}}');
debug('Token found');
2016-06-19 10:14:47 -07:00
user = yield models.user.findById(userToken);
if (!user) {
login.save();
return;
}
2015-07-09 23:01:43 +01:00
} else {
const authUser = auth(this);
this.assert(authUser, 401, badLoginMsg);
2016-06-19 10:14:47 -07:00
const count = yield models.login.count({
where: {
ip: remoteIp,
successful: false,
createdAt: {
2016-08-07 14:38:05 +01:00
$gt: new Date(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
2016-06-06 15:37:00 +01:00
this.assert(count < 25, 401,
'{"error": {"message": "Too many incorrect logins.", "code": 608}}');
2015-07-09 23:01:43 +01:00
2016-06-19 10:14:47 -07:00
user = yield models.user.findOne({
where: {
email: authUser.name,
activated: true,
},
2016-06-06 15:37:00 +01:00
});
2016-06-19 10:14:47 -07:00
if (!user || !(yield passwords.match(authUser.pass, user.password))) {
login.save();
this.throw(401, badLoginMsg);
return;
}
2015-07-09 23:01:43 +01:00
}
debug('Checking user');
this.assert(user, 401, badLoginMsg);
debug('Checking user is activated');
2016-06-19 10:14:47 -07:00
debug(user.activated);
this.assert(user.activated === true, 401,
2016-06-06 15:37:00 +01:00
'{"error": {"message": "Account has not been activated.", "code": 603}}');
2015-07-09 23:01:43 +01:00
2016-06-19 10:14:47 -07:00
login.successful = true;
yield login.save();
const uploadedTotal = yield models.file.count({
where: {
userId: user.id,
},
});
const uploadedToday = yield models.file.count({
where: {
userId: user.id,
createdAt: {
2016-08-07 14:38:05 +01:00
$gt: Date.now() - 86400000,
2016-06-19 10:14:47 -07:00
},
},
2016-06-06 15:37:00 +01:00
});
2015-07-09 23:01:43 +01:00
const normalisedUser = {
2016-06-19 10:14:47 -07:00
id: user.id,
2016-06-06 15:37:00 +01:00
email: user.email,
2016-06-19 10:14:47 -07:00
daily_upload_allowance: user.plan === 'Pro' ? 'unlimited' : 15,
2016-06-06 15:37:00 +01:00
file_count: uploadedTotal,
2016-06-19 10:14:47 -07:00
max_filesize: user.plan === 'Pro' ? 524288000 : 20971520,
plan: user.plan,
2016-06-06 15:37:00 +01:00
uploads_today: uploadedToday,
2015-07-09 23:01:43 +01:00
};
2016-06-06 15:37:00 +01:00
this.response.set('Daily-Uploads-Remaining',
user.type === 'Pro' ? 'unlimited' : 15 - uploadedToday);
2015-07-09 23:01:43 +01:00
this.user = normalisedUser;
2016-06-06 15:37:00 +01:00
debug('Authenticated user: ', this.user.email);
2015-07-09 23:01:43 +01:00
yield next;
2015-08-23 22:12:32 +01:00
}