Skip to content

Commit

Permalink
Fixed regression in authentication flow introduced when fixing issue #…
Browse files Browse the repository at this point in the history
…116

Updated versions of the package dependencies
  • Loading branch information
bropat committed Feb 8, 2022
1 parent 49b8138 commit 234613e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Please use GitHub issues for this.

## Changelog

### 1.6.5 (2022-02-08)

* (bropat) Fixed regression in authentication flow introduced when fixing issue #116
* (bropat) Updated versions of the package dependencies

### 1.6.4 (2022-02-07)

* (bropat) Fixed issue #116 (choosing the correct country as in the Eufy App is esentially)
Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![logo](_media/eufy-security-client.png)

# eufy-security-client <small>1.6.4</small>
# eufy-security-client <small>1.6.5</small>

> This shared library allows to control [Eufy security devices](https://us.eufylife.com/collections/security) by connecting to the Eufy cloud servers and local/remote stations over p2p
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eufy-security-client",
"version": "1.6.4",
"version": "1.6.5",
"description": "Client to comunicate with Eufy-Security devices",
"author": {
"name": "bropat",
Expand Down Expand Up @@ -47,7 +47,7 @@
"@cospired/i18n-iso-languages": "^3.1.1",
"fs-extra": "^10.0.0",
"sweet-collections": "^1.1.0",
"mqtt": "^4.3.4"
"mqtt": "^4.3.5"
},
"devDependencies": {
"@types/node": "^16.11.14",
Expand Down
5 changes: 4 additions & 1 deletion src/eufysecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {

public async connect(verifyCodeOrCaptcha?: string | null, captchaId?: string | null): Promise<boolean> {
let retries = 0;
await this.api.loadApiBase().catch((error) => {
this.log.error("Load Api base Error", error);
});
while (true) {
switch (await this.api.authenticate(verifyCodeOrCaptcha, captchaId)) {
case AuthResult.CAPTCHA_NEEDED:
Expand All @@ -570,7 +573,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
}
return true;
}
if (retries > 2) {
if (retries > 4) {
this.log.error("Max connect attempts reached, interrupt");
return false;
} else {
Expand Down
29 changes: 9 additions & 20 deletions src/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export class HTTPApi extends TypedEmitter<HTTPApiEvents> {
this.headers.timezone = getTimezoneGMTString();
}

public async loadApiBase(): Promise<void> {
const apiBase = await this.getApiBaseFromCloud(this.getCountry());
if (apiBase !== this.apiBase) {
this.log.debug(`Detected correct API_BASE: ${apiBase} (before: ${this.apiBase})`);
this.apiBase = apiBase;
}
}

private invalidateToken(): void {
this.token = null;
this.tokenExpiration = null;
Expand Down Expand Up @@ -140,12 +148,6 @@ export class HTTPApi extends TypedEmitter<HTTPApiEvents> {
public async authenticate(verifyCodeOrCaptcha: string | null = null, captchaId: string | null = null): Promise<AuthResult> {
//Authenticate and get an access token
this.log.debug("Authenticate and get an access token", { token: this.token, tokenExpiration: this.tokenExpiration });
const apiBase = await this.getApiBaseFromCloud(this.getCountry());
if (apiBase !== this.apiBase) {
this.log.debug(`Found wrong API_BASE (${this.apiBase}), switching to correct one (${apiBase})`);
this.apiBase = apiBase;
this.invalidateToken();
}
if (!this.token || (this.tokenExpiration && (new Date()).getTime() >= this.tokenExpiration.getTime()) || verifyCodeOrCaptcha) {
try {
this.ecdh.generateKeys();
Expand Down Expand Up @@ -450,20 +452,7 @@ export class HTTPApi extends TypedEmitter<HTTPApiEvents> {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public async request(request: HTTPApiRequest): Promise<ApiResponse> {

if (!this.token && request.endpoint != "v2/passport/login") {
//No token get one
switch (await this.authenticate()) {
case AuthResult.RENEW:
this.log.debug("Renew token", { method: request.method, endpoint: request.endpoint });
await this.authenticate();
break;
case AuthResult.ERROR:
this.log.error("Token error", { method: request.method, endpoint: request.endpoint });
break;
default: break;
}
}
if (this.tokenExpiration && (new Date()).getTime() >= this.tokenExpiration.getTime()) {
if (this.token && this.tokenExpiration && (new Date()).getTime() >= this.tokenExpiration.getTime()) {
this.log.info("Access token expired; fetching a new one")
this.invalidateToken();
if (request.endpoint != "v2/passport/login")
Expand Down

0 comments on commit 234613e

Please sign in to comment.