-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurityPolicy.js
More file actions
40 lines (38 loc) · 1.43 KB
/
securityPolicy.js
File metadata and controls
40 lines (38 loc) · 1.43 KB
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
/**
* Created by kevin.salim on 3-4-2016.
*/
var SecurityToken = require('./infrastructure/securityToken');
var logger = require('./utils/logger');
function authorise(req, res, next) {
var apiAccessToken = req.body.apiAccessToken || null;
var userId = req.params.userId || req.body.userId || null;
if (apiAccessToken && userId) {
SecurityToken.authorise(apiAccessToken, userId)
.then(function(authorised) {
if (authorised) {
next();
}
else {
logger.log('info', 'User ' + userId + ' is not authorised. Request from address ' + req.connection.remoteAddress + '.');
res.json(401, {
error: "User is not authorised"
});
}
}, function(err) {
logger.log('error', 'An error has occurred while processing a request ' +
' from ' +
req.connection.remoteAddress + '. Stack trace: ' + err.stack);
res.json(500, {
error: err.message
});
});
}
else {
logger.log('info', 'Bad request from ' +
req.connection.remoteAddress + '. Api access token and user id are mandatory.');
res.json(400, {
error: 'Api access token and user id are mandatory.'
});
}
}
exports.authorise = authorise;