From 34ee7f5b6733ced44d9033b33f5b2a1377869970 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Sun, 21 Jan 2024 19:58:41 -0600 Subject: [PATCH] Update server.ts --- src/homebridge-ui/server.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/homebridge-ui/server.ts b/src/homebridge-ui/server.ts index 5721f7d0..e4ed632c 100644 --- a/src/homebridge-ui/server.ts +++ b/src/homebridge-ui/server.ts @@ -5,6 +5,7 @@ import { request } from 'undici'; import { createServer } from 'http'; import fs from 'fs'; import url from 'node:url'; +import { exec } from 'child_process'; class PluginUiServer extends HomebridgePluginUiServer { public key!: string; @@ -32,7 +33,7 @@ class PluginUiServer extends HomebridgePluginUiServer { } case 'auth': { if (query.code) { - try { + /*try { const code = query.code; const auth = Buffer.from(this.key + ':' + this.secret).toString('base64'); const { body, statusCode } = await request(TokenURL, { @@ -49,7 +50,23 @@ class PluginUiServer extends HomebridgePluginUiServer { method: 'POST', }); const response: any = await body.json(); - console.log(`(Token) ${response}: ${JSON.stringify(response)}, statusCode: ${statusCode}`); + console.log(`(Token) ${response}: ${JSON.stringify(response)}, statusCode: ${statusCode}`);*/ + const code = query.code; + const auth = Buffer.from(this.key + ':' + this.secret).toString('base64'); + let curlString = ''; + curlString += 'curl -X POST '; + curlString += '--header "Authorization: Basic ' + auth + '" '; + curlString += '--header "Accept: application/json" '; + curlString += '--header "Content-Type: application/x-www-form-urlencoded" '; + curlString += '-d "'; + curlString += 'grant_type=authorization_code&'; + curlString += 'code=' + code + '&'; + curlString += 'redirect_uri=' + encodeURI('http://' + this.hostname + ':8585/auth'); + curlString += '" '; + curlString += '"https://api.honeywell.com/oauth2/token"'; + try { + const { stdout } = await exec(curlString); + const response = JSON.parse(String(stdout)); if (response.access_token) { this.pushEvent('creds-received', { access: response.access_token,