Skip to content

Commit

Permalink
release v9.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Nov 28, 2024
1 parent cbd281a commit 6c19fc9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [9.5.3] - (28.11.2024)

## Changes

- better handle cookie and own token
- config schema updated
- cleanup

## [9.5.2] - (28.11.2024)

## Changes
Expand Down
3 changes: 1 addition & 2 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"type": "string",
"placeholder": "IP Address or Hostname",
"format": "hostname",
"description": "Here set the envoy ip address/hostname or leave empty, will use default path envoy.local.",
"description": "Here set the envoy IP Address or Hostname, if not set default path envoy.local will be used. For firmware v7.x.x please set IP Address",
"required": false
},
"envoyFirmware7xx": {
Expand All @@ -40,7 +40,6 @@
"envoyFirmware7xxTokenGenerationMode": {
"title": "Token Generation Mode",
"type": "integer",
"default": 0,
"oneOf": [
{
"title": "Enlighten Credentials",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"displayName": "Enphase Envoy",
"name": "homebridge-enphase-envoy",
"version": "9.5.2",
"version": "9.5.3",
"description": "Homebridge plugin for Photovoltaic Energy System manufactured by Enphase.",
"license": "MIT",
"author": "grzegorz914",
Expand Down
33 changes: 23 additions & 10 deletions src/envoydevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,43 +1059,56 @@ class EnvoyDevice extends EventEmitter {
return true;
};

if (this.checkJwtTokenRunning) {
return false;
};
this.checkJwtTokenRunning = true;

//check cookie are valid
const cookieValid = this.cookie === this.oldCookie;

//validate own token
if (this.envoyFirmware7xxTokenGenerationMode === 1) {
if (cookieValid) {
return true;
};

//validate own JWT token
try {
this.emit('warn', `Cookie not valid, validating.`);
await this.validateJwtToken();

this.checkJwtTokenRunning = false;
return true;
} catch (error) {
this.checkJwtTokenRunning = false;
throw new Error(`Check own JWT token error: ${error.message || error}`);
};
};

if (this.checkJwtTokenRunning) {
return false;
};

//use token from enlighten
try {
this.checkJwtTokenRunning = true;

//read token from file
const data = await this.readData(this.envoyTokenFile);
const parsedData = JSON.parse(data);
parsedData.token ? parsedData : null;
const tokenValid = parsedData && parsedData.expires_at >= Math.floor(Date.now() / 1000) + 60;
const cookieValid = this.cookie === this.oldCookie;
const debug = this.enableDebugMode ? this.emit('warn', `JWT Token: ${tokenValid ? 'Valid' : 'Not valid'}, cookie: ${cookieValid ? 'Valid' : 'Not valid'}`) : false;

if (tokenValid && cookieValid) {
this.checkJwtTokenRunning = false;
return true;
}

//JWT token
//get new JWT token
const emit = !tokenValid ? this.emit('warn', `JWT Token expired, refreshing.`) : false;
const wait = !tokenValid ? await new Promise(resolve => setTimeout(resolve, 30000)) : false;
const getToken = await this.getJwtToken();

//Cookie
//validate JWT tokken
const wait1 = !cookieValid ? await new Promise(resolve => setTimeout(resolve, 2000)) : false;
const emit1 = !cookieValid ? this.emit('warn', `Cookie not valid, refreshing.`) : false;
const emit1 = !cookieValid ? this.emit('warn', `Cookie not valid, validating.`) : false;
const validateToken = getToken || !cookieValid ? await this.validateJwtToken() : false;

this.checkJwtTokenRunning = false;
Expand Down Expand Up @@ -1187,7 +1200,7 @@ class EnvoyDevice extends EventEmitter {
});

this.oldCookie = this.cookie;
this.emit('success', `Cookie refresh success.`);
this.emit('success', `Cookie validate success.`);

return true;
} catch (error) {
Expand Down

0 comments on commit 6c19fc9

Please sign in to comment.