-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
policies.js
55 lines (42 loc) · 1.21 KB
/
policies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// app - policies
var connectEnsureLogin = require('connect-ensure-login');
var auth = require('basic-auth');
var _ = require('underscore');
exports = module.exports = function(IoC, User) {
// policy/middleware helpers
var ensureLoggedIn = connectEnsureLogin.ensureLoggedIn;
var ensureLoggedOut = connectEnsureLogin.ensureLoggedOut;
// since there are issues with `passport-http` right now
// this is implemented as a temporary solution
function ensureApiToken(req, res, next) {
var creds = auth(req);
if (!creds || !_.isString(creds.name)) {
res.statusCode = 401;
return next({
message: 'API token missing',
param: 'username'
});
}
User.findOne({
api_token: creds.name
}, function(err, user) {
if (err) return next(err);
if (!user) {
return next({
message: 'Invalid API token provided',
param: 'username'
});
}
req.user = user;
next();
});
}
var policies = {
ensureLoggedIn: ensureLoggedIn,
ensureLoggedOut: ensureLoggedOut,
ensureApiToken: ensureApiToken
};
return policies;
};
exports['@singleton'] = true;
exports['@require'] = [ '$container', 'models/user' ];