Skip to content

Commit 594fd64

Browse files
committed
Use credential API for API key
1 parent 85c9a02 commit 594fd64

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

arduino-cloud.html

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,38 +116,21 @@
116116

117117
<script type="text/javascript">
118118
function initThings(connection, thing_id) {
119-
var clientid;
120-
var clientsecret;
121-
RED.nodes.eachConfig((c) => {
122-
if (c.type === "arduino-connection" && c.id === connection) {
123-
clientid = c.clientid;
124-
clientsecret = c.clientsecret;
125-
$.getJSON(`things?clientid=${clientid}&clientsecret=${clientsecret}` , things => {
126-
$("<option value='" + 0 + "'> " + "Select a thing" + "</option>").appendTo("#node-input-thing");
127-
for (const t of things) {
128-
$("<option value='" + t.id + "'>" + t.name + "</option>").appendTo("#node-input-thing");
129-
}
130-
if (thing_id) {
131-
$("#node-input-thing").val(thing_id);
132-
}
133-
});
119+
$.getJSON(`things?connectionid=${connection}` , things => {
120+
$("<option value='" + 0 + "'> " + "Select a thing" + "</option>").appendTo("#node-input-thing");
121+
for (const t of things) {
122+
$("<option value='" + t.id + "'>" + t.name + "</option>").appendTo("#node-input-thing");
123+
}
124+
if (thing_id) {
125+
$("#node-input-thing").val(thing_id);
134126
}
135127
});
136128
}
137129
function initProperties(connection, thing_id, property_id, outs) {
138130
if (thing_id === "" || thing_id === "0")
139131
return;
140-
141132
$("#node-input-property").html("");
142-
var clientid = "";
143-
var clientsecret = "";
144-
RED.nodes.eachConfig((c) => {
145-
if (c.type === "arduino-connection" && c.id === connection) {
146-
clientid = c.clientid;
147-
clientsecret = c.clientsecret;
148-
}
149-
});
150-
$.getJSON(`properties?clientid=${clientid}&clientsecret=${clientsecret}&thing_id=${thing_id}` , properties => {
133+
$.getJSON(`properties?connectionid=${connection}&thing_id=${thing_id}` , properties => {
151134
$("<option value='" + 0 + "'> " + "Select a property" + "</option>").appendTo("#node-input-property");
152135
for (const p of properties) {
153136
if (outs > 0 || p.permission === "READ_WRITE")
@@ -165,8 +148,10 @@
165148
category: 'config',
166149
defaults: {
167150
applicationname: {value:"",required:true},
168-
clientid: {value:"",required:true},
169-
clientsecret: {value:"",required:true}
151+
},
152+
credentials: {
153+
clientid: {type: "password"},
154+
clientsecret: {type: "password"}
170155
},
171156
label: function() {
172157
return this.applicationname || "";
@@ -176,8 +161,6 @@
176161
},
177162
oneditsave: function() {
178163
console.log("Application Name: " + this.applicationname);
179-
console.log("Client ID: " + this.clientid);
180-
console.log("Client Secret: " + this.clientsecret);
181164
}
182165
});
183166
</script>
@@ -188,11 +171,11 @@
188171
</div>
189172
<div class="form-row">
190173
<label for="node-config-input-clientid"><i class="icon-tag"></i> Client ID</label>
191-
<input type="text" id="node-config-input-clientid" placeholder="Client ID">
174+
<input type="password" id="node-config-input-clientid" placeholder="Client ID">
192175
</div>
193176
<div class="form-row">
194177
<label for="node-config-input-clientsecret"><i class="icon-tag"></i> Client secret</label>
195-
<input type="text" id="node-config-input-clientsecret" placeholder="Client secret">
178+
<input type="password" id="node-config-input-clientsecret" placeholder="Client secret">
196179
</div>
197180
</script>
198181
<script type="text/x-red" data-help-name="arduino-connection">

arduino-cloud.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,36 +235,37 @@ module.exports = function (RED) {
235235
this.clientid = config.clientid;
236236
this.clientsecret = config.clientsecret;
237237
}
238-
RED.nodes.registerType("arduino-connection", ArduinoConnectionNode);
238+
RED.nodes.registerType("arduino-connection", ArduinoConnectionNode, {
239+
credentials: {
240+
clientid: { type: "password" },
241+
clientsecret: { type: "password" }
242+
}
243+
});
239244

240245
RED.httpAdmin.get("/things", RED.auth.needsPermission('Property-in.read'), async function (req, res) {
246+
const connectionConfig = RED.nodes.getNode(req.query.connectionid);
241247
try {
242-
const connectionConfig = {
243-
clientid: req.query.clientid,
244-
clientsecret: req.query.clientsecret
245-
}
246248
await connectionManager.connect(connectionConfig);
247249
const arduinoRestClient = connectionManager.apiRest;
248250
const things = await arduinoRestClient.getThings();
249251
return res.send(JSON.stringify(things));
250252
} catch (err) {
251253
console.log(err);
254+
return res.send(JSON.stringify({}));
252255
}
253256
});
254257

255258
RED.httpAdmin.get("/properties", RED.auth.needsPermission('Property-in.read'), async function (req, res) {
259+
const connectionConfig = RED.nodes.getNode(req.query.connectionid);
256260
try {
257-
const connectionConfig = {
258-
clientid: req.query.clientid,
259-
clientsecret: req.query.clientsecret
260-
}
261261
await connectionManager.connect(connectionConfig);
262262
const ArduinoRestClient = connectionManager.apiRest;
263263
const thing_id = req.query.thing_id;
264264
const properties = await ArduinoRestClient.getProperties(thing_id);
265265
return res.send(JSON.stringify(properties));
266266
} catch (err) {
267267
console.log(err);
268+
return res.send(JSON.stringify({}));
268269
}
269270
});
270271

arduino-connection-manager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var ids=[];
1212
async function connect(connectionConfig) {
1313
var date = new Date();
1414
var timestamp = date.getTime();
15-
var user = findUser(connectionConfig.clientid);
15+
var user = findUser(connectionConfig.credentials.clientid);
1616
var requiretoken=false;
1717
var token;
1818
if(user!==-1){
@@ -29,8 +29,8 @@ async function connect(connectionConfig) {
2929
headers: {'content-type': 'application/x-www-form-urlencoded'},
3030
data: {
3131
grant_type: 'client_credentials',
32-
client_id: connectionConfig.clientid,
33-
client_secret: connectionConfig.clientsecret,
32+
client_id: connectionConfig.credentials.clientid,
33+
client_secret: connectionConfig.credentials.clientsecret,
3434
audience: accessTokenAudience
3535
}
3636
};
@@ -45,7 +45,7 @@ async function connect(connectionConfig) {
4545
if(token!==undefined){
4646
if(user===-1){
4747
const newIds={
48-
clientId: connectionConfig.clientid,
48+
clientId: connectionConfig.credentials.clientid,
4949
token: token,
5050
expires_token_ts: timestamp+expires_in
5151
};

0 commit comments

Comments
 (0)