This repository has been archived by the owner on Oct 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (61 loc) · 2.44 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var errors = require('restify-errors');
var CryptoJS = require('crypto-js');
//var options = {
// "PASSPHRASE": "thisisapassphrase",
// "excludeMethods": ['POST'],
// "excludeURLs":['Ping','Login'],
// "timeout":60
//};
module.exports = {
serverSide : function(req, res, next, options){
console.log(options);
if (!options || !options.PASSPHRASE)
return 'Check your options or passphrase';
var checkMethods = function () {
if (options.excludeMethods && options.excludeMethods.length > 0)
if (options.excludeMethods.indexOf(req.method) > -1) // Check if method in excluded HTTP methods
next();
else checkUrl();
else checkUrl();
};
var checkUrl = function () {
if (options.excludeURLs && options.excludeURLs.length > 0)
if (options.excludeURLs.indexOf(req.url) > -1) // Check if URL in excluded HTTP URLs
next();
else checkAuth();
else checkAuth();
};
var checkAuth = function () {
var timeout = (options.timeout) ? parseInt(options.timeout) : 60;
var authHeader = req.authorization.credentials;
// No Auth Token
if (!authHeader)
next(new errors.NotAuthorizedError());
else {
var decrypted = CryptoJS.TripleDES.decrypt(authHeader, options.PASSPHRASE).toString(CryptoJS.enc.Utf8);
if (!decrypted || decrypted === null) // Wrong PASSPHRASE
next(new errors.NotAuthorizedError());
else {
var requestDate = new Date(parseInt(decrypted));
var dateDiff = (requestDate - new Date()) / 1000; // Convert milliseconds to seconds.
if (dateDiff > timeout) // Auth timed-out.
next(new errors.NotAuthorizedError());
else { // Auth is fresh. Go on.
next();
}
}
}
};
checkMethods();
},
clientSide: function(method, passphrase){
var msTime = new Date().getTime().toString();
var encryptedTime = CryptoJS.TripleDES.encrypt(msTime, passphrase);
return {
method: method,
headers: {
Authorization: 'Bearer ' + encryptedTime.toString()
}
};
}
};