Skip to content

Commit

Permalink
Add support for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
shweelan authored and zaidka committed Apr 5, 2019
1 parent 1b11b84 commit c2cd574
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let http = null;
let requestOptions = null;
let device = null;
let httpAgent = null;
let basicAuth;


function createSoapDocument(id) {
Expand All @@ -35,7 +36,6 @@ function createSoapDocument(id) {
return xml;
}


function sendRequest(xml, callback) {
let headers = {};
let body = "";
Expand All @@ -45,6 +45,7 @@ function sendRequest(xml, callback) {

headers["Content-Length"] = body.length;
headers["Content-Type"] = "text/xml; charset=\"utf-8\"";
headers["Authorization"]= basicAuth;

if (device._cookie)
headers["Cookie"] = device._cookie;
Expand Down Expand Up @@ -75,6 +76,12 @@ function sendRequest(xml, callback) {
return offset += chunk.length;
});

if (Math.floor(response.statusCode / 100) !== 2) {
throw new Error(
`Unexpected response Code ${response.statusCode}: ${body}`
);
}

if (+response.headers["Content-Length"] > 0 || body.length > 0)
xml = libxmljs.parseXml(body);
else
Expand Down Expand Up @@ -186,6 +193,18 @@ function start(dataModel, serialNumber, acsUrl) {
else if (device["InternetGatewayDevice.DeviceInfo.SerialNumber"])
device["InternetGatewayDevice.DeviceInfo.SerialNumber"][1] = serialNumber;

let username = "";
let password = "";
if (device["Device.ManagementServer.Username"]) {
username = device["Device.ManagementServer.Username"][1];
password = device["Device.ManagementServer.Password"][1];
} else if (device["InternetGatewayDevice.ManagementServer.Username"]) {
username = device["InternetGatewayDevice.ManagementServer.Username"][1];
password = device["InternetGatewayDevice.ManagementServer.Password"][1];
}

basicAuth = "Basic " + Buffer.from(`${username}:${password}`).toString("base64");

requestOptions = require("url").parse(acsUrl);
http = require(requestOptions.protocol.slice(0, -1));
httpAgent = new http.Agent({keepAlive: true, maxSockets: 1});
Expand Down

0 comments on commit c2cd574

Please sign in to comment.