From 296f8b3336ef851fa1e1bdff67c66640615ddb63 Mon Sep 17 00:00:00 2001 From: Tomek Kleszcz Date: Sun, 10 Mar 2024 15:32:46 +0100 Subject: [PATCH] fix: get api key and regional base url before logging in to gigya (#47) --- config.schema.json | 32 -------------------- src/const/apiKey.ts | 5 ++-- src/definitions/region.ts | 1 - src/platform.ts | 63 ++++++++++++++++++++++++++------------- 4 files changed, 45 insertions(+), 56 deletions(-) delete mode 100644 src/definitions/region.ts diff --git a/config.schema.json b/config.schema.json index 13003f1..c7b0b58 100644 --- a/config.schema.json +++ b/config.schema.json @@ -17,38 +17,6 @@ "required": true, "default": "" }, - "region": { - "title": "Region", - "type": "string", - "required": true, - "default": "eu", - "oneOf": [ - { - "title": "Europe", - "enum": ["eu"] - }, - { - "title": "United States", - "enum": ["us"] - }, - { - "title": "Australia", - "enum": ["au"] - }, - { - "title": "Russia", - "enum": ["ru"] - }, - { - "title": "China", - "enum": ["cn"] - }, - { - "title": "Israel", - "enum": ["il"] - } - ] - }, "pollingInterval": { "title": "Polling interval", "type": "number", diff --git a/src/const/apiKey.ts b/src/const/apiKey.ts index 8d4f48b..c34e52e 100644 --- a/src/const/apiKey.ts +++ b/src/const/apiKey.ts @@ -1,3 +1,4 @@ -export const ACCOUNTS_API_KEY = '4_JZvZObbVWc1YROHF9e6y8A'; - +export const CLIENT_ID = 'ElxOneApp'; +export const CLIENT_SECRET = + '8UKrsKD7jH9zvTV7rz5HeCLkit67Mmj68FvRVTlYygwJYy4dW6KF2cVLPKeWzUQUd6KJMtTifFf4NkDnjI7ZLdfnwcPtTSNtYvbP7OzEkmQD9IjhMOf5e1zeAQYtt2yN'; export const API_KEY = '2AMqwEV5MqVhTKrRCyYfVF8gmKrd2rAmp7cUsfky'; diff --git a/src/definitions/region.ts b/src/definitions/region.ts deleted file mode 100644 index 9ec3712..0000000 --- a/src/definitions/region.ts +++ /dev/null @@ -1 +0,0 @@ -export type Region = 'eu' | 'us' | 'au' | 'ru' | 'cn' | 'il'; diff --git a/src/platform.ts b/src/platform.ts index cabf3ac..74f36c1 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -12,14 +12,13 @@ import { PLATFORM_NAME, PLUGIN_NAME } from './settings'; import { axiosApi, axiosAppliance, axiosAuth } from './services/axios'; import { Appliances } from './definitions/appliances'; import { DEVICES } from './const/devices'; -import { ACCOUNTS_API_KEY } from './const/apiKey'; -import Gigya from 'gigya'; +import { CLIENT_ID, CLIENT_SECRET } from './const/apiKey'; +import Gigya, { DataCenter } from 'gigya'; import { TokenResponse } from './definitions/auth'; import { ElectroluxAccessoryController } from './accessories/controller'; import { ElectroluxAccessory } from './accessories/accessory'; import fs from 'fs'; import path from 'path'; -import { Region } from './definitions/region'; import { IdentityProvidersResponse } from './definitions/identityProviders'; import { API_URL } from './const/url'; @@ -102,9 +101,45 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin { } async signIn() { - const region: Region = this.config.region || 'eu'; + /* + Get the token from Electrolux API using CLIENT_ID and CLIENT_SECRET + to fetch the regional base URL and API key. + */ + const tokenResponse = await axiosAuth.post( + '/one-account-authorization/api/v1/token', + { + grantType: 'client_credentials', + clientId: CLIENT_ID, + clientSecret: CLIENT_SECRET, + scope: '' + }, + { + baseURL: API_URL + } + ); - this.gigya = new Gigya(ACCOUNTS_API_KEY, `${region}1`); + const regionResponse = await axiosApi.get( + '/one-account-user/api/v1/identity-providers', + { + headers: { + Authorization: `Bearer ${tokenResponse.data.accessToken}` + } + } + ); + + const regionData = regionResponse.data.find( + ({ brand }) => brand === 'electrolux' + ); + if (!regionData) { + throw new Error('Region not found'); + } + + this.regionalBaseUrl = regionData?.httpRegionalBaseUrl ?? null; + + this.gigya = new Gigya( + regionData?.apiKey, + regionData.domain.split('.')[0] as DataCenter + ); const storagePath = path.format({ dir: this.api.user.storagePath(), @@ -164,7 +199,7 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin { { grantType: 'urn:ietf:params:oauth:grant-type:token-exchange', - clientId: 'ElxOneApp', + clientId: CLIENT_ID, idToken: jwtResponse.id_token, scope: '' }, @@ -184,20 +219,6 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin { this.log.info('JWT token successfully fetched!'); } - const regionResponse = - await axiosApi.get( - '/one-account-user/api/v1/identity-providers', - { - headers: { - Authorization: `Bearer ${this.accessToken}` - } - } - ); - - this.regionalBaseUrl = - regionResponse.data.find(({ brand }) => brand === 'electrolux') - ?.httpRegionalBaseUrl ?? null; - const json = JSON.stringify({ uid: this.uid, oauthToken: this.oauthToken, @@ -237,7 +258,7 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin { '/token', { grantType: 'refresh_token', - clientId: 'ElxOneApp', + clientId: CLIENT_ID, refreshToken: this.refreshToken, scope: '' },