Skip to content

Commit

Permalink
fix: get api key and regional base url before logging in to gigya (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekkleszcz authored Mar 10, 2024
1 parent 69da355 commit 296f8b3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 56 deletions.
32 changes: 0 additions & 32 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/const/apiKey.ts
Original file line number Diff line number Diff line change
@@ -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';
1 change: 0 additions & 1 deletion src/definitions/region.ts

This file was deleted.

63 changes: 42 additions & 21 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<TokenResponse>(
'/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<IdentityProvidersResponse>(
'/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(),
Expand Down Expand Up @@ -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: ''
},
Expand All @@ -184,20 +219,6 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin {
this.log.info('JWT token successfully fetched!');
}

const regionResponse =
await axiosApi.get<IdentityProvidersResponse>(
'/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,
Expand Down Expand Up @@ -237,7 +258,7 @@ export class ElectroluxDevicesPlatform implements DynamicPlatformPlugin {
'/token',
{
grantType: 'refresh_token',
clientId: 'ElxOneApp',
clientId: CLIENT_ID,
refreshToken: this.refreshToken,
scope: ''
},
Expand Down

0 comments on commit 296f8b3

Please sign in to comment.