Skip to content

Commit

Permalink
fix: Fix root API_SECRET returning 401
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 30, 2023
1 parent d746da5 commit 80f123a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ class Nightscout {
this.context = context;
this.url = "";
this.request = { headers: {} };
this.settings = settings;
this.setSettings(settings);
nightscoutMap[context] = this;
}

set settings(settings) {
this._settings = settings;
async setSettings(settings) {
this.settings = settings;
if (this.settings.nightscoutUrl) {
this.url = new URL(this.settings.nightscoutUrl);
this.url.pathname += "api/v2/properties";
if (this.settings.token) {
this.request.headers["Api-Secret"] = this.settings.token;
try {
const buffer = new TextEncoder().encode(this.settings.token);
const hashBuffer = await crypto.subtle.digest("SHA-1", buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
this.request.headers["Api-Secret"] = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
} catch (err) {
console.error(err);
this.request.headers["Api-Secret"] = this.settings.token;
$SD.showAlert(this.context);
}
}
this.url = new URL(this.settings.nightscoutUrl);
this.url.pathname += "api/v2/properties";
this.beginTick();
}
}

get settings() {
return this._settings;
}

async tick() {
if (!this.url) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/com.gabe565.nightscout.sdPlugin/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

const nightscoutAction = new Action("com.gabe565.nightscout.action");

nightscoutAction.onDidReceiveSettings(
(data) => (new Nightscout(data).settings = data.payload.settings),
nightscoutAction.onDidReceiveSettings((data) =>
new Nightscout(data).setSettings(data.payload.settings),
);

nightscoutAction.onKeyDown((data) => new Nightscout(data).beginTick());
Expand Down

0 comments on commit 80f123a

Please sign in to comment.