From 1ec2a7bd65f30489e4b7d0d9e3c2aad205cc1ea6 Mon Sep 17 00:00:00 2001 From: sergiu Date: Thu, 5 Dec 2019 10:59:13 +0200 Subject: [PATCH] Update to use secured routes --- src/asset.js | 33 +++++++++++++++++++++++---------- src/simulator.js | 30 +++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/asset.js b/src/asset.js index 2659f63..c17adf7 100644 --- a/src/asset.js +++ b/src/asset.js @@ -18,9 +18,10 @@ const log = require('./utils').log class Asset { - constructor(id, configs) { + constructor(id, configs, token) { this.id = id; + this.token = token; this.configs = configs this._processConfigs(); this.startTime = +new Date(); @@ -71,13 +72,16 @@ class Asset { } _getDataFromApi(callback) { + utils.writeLog(`Get informations about asset ${this.id}`) Request.get( this.apiEndpoint + '/asset/' + this.id, - {}, + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, (error, response, body) => { - utils.writeLog(`Get informations about asset ${this.id}`) if (!error && (response.statusCode === 200 || response.statusCode === 201)) { - let bodyObj = JSON.parse(body); // Here are the asset fields. @@ -97,7 +101,11 @@ class Asset { Request.get( this.apiEndpoint + '/asset/ ' + this.id + '/groups', - {}, + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, (error, response, body) => { if (!error && (response.statusCode === 200 || response.statusCode === 201)) { @@ -203,10 +211,13 @@ class Asset { } _register(callback) { - Request.post( this.apiEndpoint + '/audio/register/' + this.id, - {}, + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, (error, response, body) => { if (!error && (response.statusCode === 200 || response.statusCode === 201)) { utils.writeLog(`Asset ${this.id} audio registered`); @@ -221,13 +232,15 @@ class Asset { } _moveToChannel(callback) { - Request.post( this.apiEndpoint + '/audio/enter-group/' + this.id + '/' + this.groupId, - {}, + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, (error, response, body) => { if (!error && (response.statusCode === 200 || response.statusCode === 201)) { - let hub = this.hub; if (hub && hub.connected) { diff --git a/src/simulator.js b/src/simulator.js index 1b994ff..9351244 100644 --- a/src/simulator.js +++ b/src/simulator.js @@ -10,7 +10,8 @@ class Simulator { constructor(configs) { this.configs = configs; - + this.token; + // Read assets ids from configs this.assetIds = configs.assets.ids; @@ -19,20 +20,22 @@ class Simulator { async.waterfall([ this._manageLogs.bind(this), - this._unregisterAsstes.bind(this) + this._unregisterAsstes.bind(this), + this._getToken.bind(this), ], (err, result) => { if(err) { console.log(err) return; } + utils.writeLog(`Using token: ${this.token}`); this._start(); }); } _start() { this.assetIds.forEach(id => { - new Asset(id, this.configs); + new Asset(id, this.configs, this.token); }); } @@ -73,6 +76,27 @@ class Simulator { ); } + _getToken(callback) { + let url = this.apiEndpoint + '/generate-token'; + Request.get( + url, + {timeout: 15000}, + (error, response, body) => { + if(error) { + utils.writeLog(`ERROR can't get token`, error); + utils.writeErrorLog(`ERROR_API`); + return callback(error); + } + else { + let res = JSON.parse(response.body) + this.token = res.data.token; + utils.writeLog(`API TOKEN: ${this.token}`) + return callback(); + } + } + ); + } + } module.exports = Simulator;