From c2cd574d807bfa7008f0e7e495ed83dddf92076e Mon Sep 17 00:00:00 2001 From: Shweelan Samson Date: Tue, 2 Apr 2019 12:38:44 +0300 Subject: [PATCH] Add support for authentication --- simulator.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/simulator.js b/simulator.js index 7387c8f..54e26c1 100644 --- a/simulator.js +++ b/simulator.js @@ -15,6 +15,7 @@ let http = null; let requestOptions = null; let device = null; let httpAgent = null; +let basicAuth; function createSoapDocument(id) { @@ -35,7 +36,6 @@ function createSoapDocument(id) { return xml; } - function sendRequest(xml, callback) { let headers = {}; let body = ""; @@ -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; @@ -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 @@ -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});