diff --git a/arduino-iot-cloud.html b/arduino-iot-cloud.html index b8025df..1d74d5b 100644 --- a/arduino-iot-cloud.html +++ b/arduino-iot-cloud.html @@ -128,13 +128,15 @@ $("#node-input-organization").change(() => { const connection = $("#node-input-connection").val(); const organization = $("#node-input-organization").val(); - this.organization = organization; + if (connection === "_ADD_") { $("#node-input-organization").empty(); str = this._("arduino-iot-cloud.config.connection.placeholders.no-conn-selected"); $("").appendTo("#node-input-thing"); $("#node-input-thing").trigger("change"); - } else { + } + if (this.organization != organization) { + this.organization = organization; $("select#node-input-thing").empty(); initThings(connection, this._, null, organization); } diff --git a/arduino-iot-cloud.js b/arduino-iot-cloud.js index b039aba..315fe51 100644 --- a/arduino-iot-cloud.js +++ b/arduino-iot-cloud.js @@ -75,7 +75,7 @@ module.exports = function (RED) { try { if (config.thing !== "" && config.property !== "") { - this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig); + this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization); if (this.arduinoRestClient){ this.arduinoRestClient.openConnections++; this.organization = config.organization; @@ -152,7 +152,7 @@ module.exports = function (RED) { this.timeWindowUnit = config.timeWindowUnit; if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") { try { - this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig); + this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization); if (this.arduinoRestClient){ this.arduinoRestClient.openConnections++; if (config.thing !== "" && config.property !== "") { @@ -251,7 +251,7 @@ module.exports = function (RED) { this.organization = config.organization; if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") { try { - this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig); + this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization); if (this.arduinoRestClient){ this.arduinoRestClient.openConnections++; if (config.thing !== "" && config.property !== "") { @@ -340,7 +340,7 @@ module.exports = function (RED) { try { if (config.thing !== "" && config.property !== "") { - this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig); + this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization); if (this.arduinoRestClient){ this.arduinoRestClient.openConnections++; this.organization = config.organization; @@ -432,7 +432,7 @@ module.exports = function (RED) { clientid: req.query.clientid, clientsecret: req.query.clientsecret } - }); + }, this.organization); } else if (req.query.connectionid) { const connectionConfig = RED.nodes.getNode(req.query.connectionid); if (!connectionConfig) { @@ -440,7 +440,7 @@ module.exports = function (RED) { console.log(str); return res.send(JSON.stringify({ error: str })); } - arduinoRestClient = await connectionManager.getClientHttp(connectionConfig); + arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization); } else { str=RED._("arduino-iot-cloud.connection-error.no-cred-available"); console.log(str); diff --git a/package-lock.json b/package-lock.json index b550bb9..7e78a84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.1.0", "license": "GNU AFFERO GENERAL PUBLIC LICENSE", "dependencies": { - "@arduino/arduino-iot-client": "github:arduino/iot-client-js", + "@arduino/arduino-iot-client": "^3.0.0", "@arduino/cbor-js": "github:arduino/cbor-js", "async-mutex": "^0.1.4", "jws": "^3.2.2", @@ -32,8 +32,8 @@ } }, "node_modules/@arduino/arduino-iot-client": { - "version": "2.0.5", - "resolved": "git+ssh://git@github.com/arduino/iot-client-js.git#bc4991edf2bbd64571e2b52f79d16d392c8d01f2", + "version": "3.0.0", + "resolved": "git+ssh://git@github.com/arduino/iot-client-js.git#ac903b60b4001cf7282144dc26a73d19f57a7cfa", "license": "GPLv3", "dependencies": { "@babel/cli": "^7.0.0", @@ -3321,8 +3321,8 @@ } }, "@arduino/arduino-iot-client": { - "version": "git+ssh://git@github.com/arduino/iot-client-js.git#bc4991edf2bbd64571e2b52f79d16d392c8d01f2", - "from": "@arduino/arduino-iot-client@arduino/iot-client-js", + "version": "git+ssh://git@github.com/arduino/iot-client-js.git#ac903b60b4001cf7282144dc26a73d19f57a7cfa", + "from": "@arduino/arduino-iot-client@^3.0.0", "requires": { "@babel/cli": "^7.0.0", "superagent": "^5.3.0" diff --git a/package.json b/package.json index 7127cfc..a520771 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ } }, "dependencies": { - "@arduino/arduino-iot-client": "github:arduino/iot-client-js", + "@arduino/arduino-iot-client": "^3.0.0", "@arduino/cbor-js": "github:arduino/cbor-js", "async-mutex": "^0.1.4", "jws": "^3.2.2", diff --git a/utils/arduino-connection-manager.js b/utils/arduino-connection-manager.js index e553778..de718f5 100644 --- a/utils/arduino-connection-manager.js +++ b/utils/arduino-connection-manager.js @@ -40,7 +40,7 @@ const getClientMutex = new Mutex(); var numRetry=0; -async function getToken(connectionConfig) { +async function getToken(connectionConfig, organizationID) { const dataToSend = { grant_type: 'client_credentials', client_id: connectionConfig.credentials.clientid, @@ -49,12 +49,16 @@ async function getToken(connectionConfig) { }; try { + var req = superagent + .post(accessTokenUri) + .set('content-type', 'application/x-www-form-urlencoded') + .set('accept', 'json') - var res = await superagent - .post(accessTokenUri) - .set('content-type', 'application/x-www-form-urlencoded') - .set('accept', 'json') - .send(dataToSend); + if (organizationID) { + req.set('X-Organization', organizationID) + } + + var res = await req.send(dataToSend); var token = res.body.access_token; var expires_in = res.body.expires_in * 0.8; // needed to change the token before it expires if (token !== undefined) { @@ -161,7 +165,7 @@ async function getClientMqtt(connectionConfig, RED) { } -async function getClientHttp(connectionConfig) { +async function getClientHttp(connectionConfig, organizationID) { if (!connectionConfig || !connectionConfig.credentials) { throw new Error("Cannot find cooonection config or credentials."); @@ -172,7 +176,7 @@ async function getClientHttp(connectionConfig) { var clientHttp; if (user === -1) { - var tokenInfo = await getToken(connectionConfig); + var tokenInfo = await getToken(connectionConfig, organizationID); if (tokenInfo !== undefined) { clientHttp = new ArduinoClientHttp.ArduinoClientHttp(tokenInfo.token);