Skip to content

fix: add the organization header to the token request for http nodes #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions arduino-iot-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 !== "") {
Expand Down Expand Up @@ -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 !== "") {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -432,15 +432,15 @@ 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) {
str=RED._("arduino-iot-cloud.connection-error.no-cred-available");
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);
Expand Down
20 changes: 12 additions & 8 deletions utils/arduino-connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,12 +49,16 @@ async function getToken(connectionConfig) {
};

try {
var req = superagentsuperagent
.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) {
Expand Down Expand Up @@ -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.");
Expand All @@ -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);

Expand Down