-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (69 loc) · 2.51 KB
/
index.js
File metadata and controls
75 lines (69 loc) · 2.51 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
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
"use strict";
var crypt = require('./crypto');
var util = require('util');
var crypto = require('crypto');
module.exports.ChecksumUtils = {
genChecksum:function(data, key, cb) {
crypt.gen_salt(8, function (err, salt) {
var sha256 = crypto.createHash('sha256').update(salt+data).digest('hex');
var check_sum = sha256+salt;
cb(undefined, crypt.encrypt(check_sum, key));
});
},
verifyChecksum:function(data, key,checksumhash) {
try{
var checksum = crypt.decrypt(checksumhash, key);
var salt = checksum.slice(checksum.length-8, checksum.length);
var sha256 = checksum.slice(0, checksum.length - 8);
var hash = crypto.createHash('sha256').update(salt+data).digest("hex");
return (hash === sha256);
}catch(err){
return false;
}
}
}
var AppInstance = function (config) {
this.config = config;
return this;
}
AppInstance.prototype.exec = function(micromodule_id, intent, payload, userID){
var userID = userID || "generic";
return new Promise(function(resolve, reject){
var body = {
intent:intent,
data:payload
}
var payload = JSON.stringify(body) + "|" + micromodule_id + "|" + this.config.appKey + "|" + userID;
module.exports.ChecksumUtils.genChecksum(payload, this.config.secret, function (err, checksum) {
if(!err){
request.post({
url:this.config.executorUrl + "/executor/exec",
method:"POST",
json:body,
headers:{
"X-Module-Handle":micromodule_id,
"X-App-Key":this.config.appKey,
"X-Checksum":checksum,
"X-UUID":userID
}
}, function (err, resp, body) {
if(err){
reject(err);
}
else{
if(module.exports.ChecksumUtils.verifyChecksum(body, this.config.secret, resp.headers["X-Checksum"])){
resolve(JSON.parse(body));
}
else{
reject({message:"Checksum Validation Failed"});
}
}
});
}
else{
reject(err);
}
})
});
}
module.exports.AppInstance = AppInstance;