-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.js
106 lines (99 loc) · 3.15 KB
/
middleware.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
exports = module.exports = function(app) {
var async = require('async'),
moment = require('moment'),
generateHmac = app.utility.hmac.generateHmac;
// RFC 2104 authentication
// Date restriction
app.use(function(req, res, next) {
var reqDate = req.get("Date");
if (moment().add(app.config.hmac.validFor.amount, app.config.hmac.validFor.type)
.utc().isAfter(moment(new Date(reqDate)))) {
req.hmac = {
date: reqDate
};
return next();
} else {
console.log("1");
res.status(404).json(app.utility.outcome.build());
}
});
// Id restriction
app.use(function(req, res, next) {
var reqId = req.get("X-API-Authentication-Id");
if (reqId) {
async.waterfall([
function(callback) {
app.db.mongoose.models.Client.update({
apiId: reqId
}, {
updatedOn: moment().utc().toDate()
}, {
upsert: true
},
function(error, numberAffected, raw) {
if (error) {
callback(error);
} else {
callback(null);
}
}
);
}
],
function(error) {
if (error) {
res.status(500).json(app.utility.outcome.build());
} else {
req.hmac.id = reqId;
return next();
}
});
} else {
console.log("2");
res.status(404).json(app.utility.outcome.build());
}
});
// Key calculation
app.use(function(req, res, next) {
async.waterfall([
function(callback) {
// This cache will speed up things and will offload mongodb
if (app.config.hmac.nextCheck &&
app.config.hmac.secret &&
moment().isBefore(app.config.hmac.nextCheck)) {
callback(null, app.config.hmac.secret);
} else {
app.db.mongoose.models.Secret.latest(function(error, record) {
if (error) {
callback(error);
}
app.config.hmac.secret = (record && record.secret) ?
record.secret : app.config.hmac.secret;
app.config.hmac.nextCheck = moment()
.add(app.config.hmac.check.amount, app.config.hmac.check.type);
callback(null, app.config.hmac.secret);
});
}
}
],
function(error, secret) {
if (error) {
res.status(500).json(app.utility.outcome.build());
} else {
var body = app.utility.json.isJSON(req.body) ? JSON.stringify(req.body) : "",
reqKey = req.get("X-API-Authentication-Secret"),
algorithm = app.config.hmac.algorithm,
encoding = app.config.hmac.encoding,
calcKey = (body !== "{}") ?
generateHmac(body, secret, algorithm, encoding) :
generateHmac(req.hmac.id, secret, algorithm, encoding);
if (reqKey === calcKey) {
req.hmac.key = reqKey;
return next();
}
res.status(404).json(app.utility.outcome.build());
}
});
});
};