google-home: fix local auth

This commit is contained in:
Koushik Dutta
2022-10-21 13:35:09 -07:00
parent 8c148ba217
commit b014157deb
5 changed files with 38 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
{
"scrypted.debugHost": "127.0.0.1",
"scrypted.debugHost": "koushik-ubuntu",
}

View File

@@ -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);

View File

@@ -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",

View File

@@ -48,5 +48,5 @@
"@types/lodash": "^4.14.168",
"@types/url-parse": "^1.4.3"
},
"version": "0.0.50"
"version": "0.0.51"
}

View File

@@ -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<string>();
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;
}
}
}