-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSF.js
More file actions
81 lines (72 loc) · 1.72 KB
/
Copy pathMSF.js
File metadata and controls
81 lines (72 loc) · 1.72 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
75
76
77
78
79
80
81
var https = require('https');
function MSF() {
}
MSF.prototype.MSF_URL = "amers1.msf2.cp.reutest.com";
MSF.prototype.LOGIN_PATH = "/msf/auth/login";
MSF.prototype.SERVICE_PATH = "/msf";
MSF.prototype._cookie = null;
MSF.prototype.login = function(username, password, callback) {
var buffer = new Buffer(username+":"+password);
var b64 = buffer.toString('base64');
var authValue = "Basic "+b64;
var headers = {
"Authorization": authValue
};
var options = {
hostname: this.MSF_URL,
port: 443,
path: this.LOGIN_PATH,
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
res.on('data', function(data) {
//Ignore data here
});
if (res.statusCode == 200) {
var cookie = res.headers['set-cookie'];
this._cookie = cookie;
callback(null);
} else {
callback("Unable to login - HTTP STATUS "+res.statusCode);
}
}.bind(this));
req.on('error', function(e) {
callback(e)
}.bind(this));
req.end();
}
MSF.prototype.getData = function(param, callback) {
if (this._cookie == null) {
callback(new Error("Error: Not login yet"));
return;
}
var headers = {
"Cookie": this._cookie,
"Content-Type": "application/json"
};
var options = {
hostname: this.MSF_URL,
port: 443,
path: this.SERVICE_PATH,
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var resultObject = JSON.parse(responseString);
callback(null, resultObject);
}.bind(this));
}.bind(this));
req.on('error', function(e) {
callback(e)
});
req.write(JSON.stringify(param));
req.end();
}
module.exports = MSF;