From b014157debd8971ff3b429a0cec59772128d0567 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 21 Oct 2022 13:35:09 -0700 Subject: [PATCH] google-home: fix local auth --- plugins/google-home/.vscode/settings.json | 2 +- plugins/google-home/local-sdk-app/index.ts | 8 +++++ plugins/google-home/package-lock.json | 4 +-- plugins/google-home/package.json | 2 +- plugins/google-home/src/main.ts | 41 ++++++++++++++-------- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/plugins/google-home/.vscode/settings.json b/plugins/google-home/.vscode/settings.json index 77ccdbd6d..44d4d203f 100644 --- a/plugins/google-home/.vscode/settings.json +++ b/plugins/google-home/.vscode/settings.json @@ -1,4 +1,4 @@ { - "scrypted.debugHost": "127.0.0.1", + "scrypted.debugHost": "koushik-ubuntu", } \ No newline at end of file diff --git a/plugins/google-home/local-sdk-app/index.ts b/plugins/google-home/local-sdk-app/index.ts index 20cd43eba..1ccf3bd8f 100644 --- a/plugins/google-home/local-sdk-app/index.ts +++ b/plugins/google-home/local-sdk-app/index.ts @@ -71,6 +71,10 @@ app command.data = JSON.stringify(request); + command.additionalHeaders = { + 'Authorization': (request.inputs?.[0]?.payload?.devices?.[0]?.customData as any)?.localAuthorization, + } + try { const result = await app.getDeviceManager() .send(command); @@ -109,6 +113,10 @@ app command.data = JSON.stringify(request); + command.additionalHeaders = { + 'Authorization': (request.inputs?.[0]?.payload?.commands?.[0].devices?.[0]?.customData as any)?.localAuthorization, + } + try { const result = await app.getDeviceManager() .send(command); diff --git a/plugins/google-home/package-lock.json b/plugins/google-home/package-lock.json index e6b434fea..30bd0476e 100644 --- a/plugins/google-home/package-lock.json +++ b/plugins/google-home/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/google-home", - "version": "0.0.50", + "version": "0.0.51", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/google-home", - "version": "0.0.50", + "version": "0.0.51", "dependencies": { "@googleapis/homegraph": "^2.0.0", "@homebridge/ciao": "^1.1.5", diff --git a/plugins/google-home/package.json b/plugins/google-home/package.json index 613ee2c86..0905cb317 100644 --- a/plugins/google-home/package.json +++ b/plugins/google-home/package.json @@ -48,5 +48,5 @@ "@types/lodash": "^4.14.168", "@types/url-parse": "^1.4.3" }, - "version": "0.0.50" + "version": "0.0.51" } diff --git a/plugins/google-home/src/main.ts b/plugins/google-home/src/main.ts index 70a68d3ae..5a495a306 100644 --- a/plugins/google-home/src/main.ts +++ b/plugins/google-home/src/main.ts @@ -48,6 +48,7 @@ const includeToken = 3; class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, EngineIOHandler, MixinProvider { linkTracker = localStorage.getItem('linkTracker'); agentUserId = localStorage.getItem('agentUserId'); + localAuthorization = localStorage.getItem('localAuthorization'); reportQueue = new Set(); reportStateThrottled = throttle(() => this.reportState(), 2000); throttleSync = throttle(() => this.requestSync(), 15000, { @@ -83,6 +84,11 @@ class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, Engin localStorage.setItem('agentUserId', this.agentUserId); } + if (!this.localAuthorization) { + this.localAuthorization = uuidv4(); + localStorage.setItem('localAuthorization', this.localAuthorization); + } + try { this.defaultIncluded = JSON.parse(localStorage.getItem('defaultIncluded')); } @@ -247,6 +253,9 @@ class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, Engin const probe = await supportedType.getSyncResponse(device); + probe.customData = { + 'localAuthorization': this.localAuthorization, + }; probe.roomHint = device.room; probe.notificationSupportedByAgent = true; ret.payload.devices.push(probe); @@ -507,21 +516,23 @@ class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, Engin } const { authorization } = request.headers; - if (!this.validAuths.has(authorization)) { - try { - await axios.get('https://home.scrypted.app/_punch/getcookie', { - headers: { - 'Authorization': authorization, - } - }); - this.validAuths.add(authorization); - } - catch (e) { - this.console.error(`request failed due to invalid authorization`, e); - response.send(e.message, { - code: 500, - }); - return; + if (authorization !== this.localAuthorization) { + if (!this.validAuths.has(authorization)) { + try { + await axios.get('https://home.scrypted.app/_punch/getcookie', { + headers: { + 'Authorization': authorization, + } + }); + this.validAuths.add(authorization); + } + catch (e) { + this.console.error(`request failed due to invalid authorization`, e); + response.send(e.message, { + code: 500, + }); + return; + } } }