Skip to content

Commit

Permalink
Clean up MFA.
Browse files Browse the repository at this point in the history
Clean up MFA and run pretty code from VS
  • Loading branch information
jfarmer08 committed Feb 8, 2024
1 parent 2a56055 commit 463197a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules/*
example/node_modules/*
example/scratch/*
.env
.env
example/.env
8 changes: 8 additions & 0 deletions example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
USERNAME="[email protected]"
PASSWORD="Honda!@5125"
KEY_ID="9e842a21-8f2e-4895-852b-ca8d301eae12"
API_KEY="6yANO0rh8bDTNSTjotSrmOl6oDIM2XbbQAF1qjodmpl4GBaAFLDp72DfVS1I"
PERSIST_PATH="./scratch"
LOG_LEVEL="debug"
API_LOG_ENABLED=true
LOCAL_DEV=true
4 changes: 3 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

let WyzeAPI = null;
if (process.env.LOCAL_DEV) {
WyzeAPI = require('../src/index'); // Local Debug
Expand Down Expand Up @@ -35,6 +37,6 @@ async function deviceListCheck() {


(async () => {
// await deviceListCheck();
await deviceListCheck();
// await loginCheck(4);
})()
41 changes: 34 additions & 7 deletions example/package-lock.json

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

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"author": "Allen Farmer",
"license": "MIT",
"dependencies": {
"axios": "^0.27.2",
"@ptkdev/logger": "1.8.0",
"axios": "^0.27.2",
"dotenv": "^16.4.1",
"wyze-api": "^1.1.1"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wyze-api",
"version": "1.1.2",
"version": "1.1.3",
"description": "An unoficial API wrapper for Wyze products.",
"homepage": "https://github.com/jfarmer08/wyze-api",
"main": "./src/index.js",
Expand Down
101 changes: 62 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fs = require("fs").promises;
const path = require("path");
const getUuid = require("uuid-by-string");

const payloadFactory = require('./payloadFactory');
const crypto = require('./crypto');
const constants = require('./constants');
const util = require('./util');
const { time } = require('console');
const payloadFactory = require("./payloadFactory");
const crypto = require("./crypto");
const constants = require("./constants");
const util = require("./util");
const { time } = require("console");

module.exports = class WyzeAPI {
constructor(options, log) {
Expand Down Expand Up @@ -145,13 +145,19 @@ module.exports = class WyzeAPI {
throw e;
}
if (result.data.msg) {
if (result.data.msg == "DeviceIsOffline" || result.data.msg == "SUCCESS") { return result } else
throw new Error(result.data.msg)
} else { return result }
if (
result.data.msg == "DeviceIsOffline" ||
result.data.msg == "SUCCESS"
) {
return result;
} else throw new Error(result.data.msg);
} else {
return result;
}
}

_performLoginRequest(data = {}) {
let url = "user/login";
let url = "api/user/login";
data = {
email: this.username,
password: util.createPassword(this.password),
Expand All @@ -160,44 +166,43 @@ module.exports = class WyzeAPI {

const config = {
baseURL: this.authBaseUrl,
headers: { "x-api-key": this.authApiKey, "User-Agent": this.userAgent },
};

if (this.apiKey && this.keyId) {
url = "api/user/login";
config.headers = {
headers: {
"x-api-key": this.authApiKey,
apikey: this.apiKey,
keyid: this.keyId,
"User-Agent": this.userAgent,
};
}
},
};

return this._performRequest(url, data, config);
}

async login() {
let result = await this._performLoginRequest();

// Do we need to perform a 2-factor login?
if (!result.data.access_token && result.data.mfa_details) {
if (!this.mfaCode) {
let result;
// Do we need apiKey or keyId?
if (this.apiKey == null) {
throw new Error(
'ApiKey Required, Please provide the "apiKey" parameter in config.json'
);
} else if (this.keyId == null) {
throw new Error(
'KeyId Required, Please provide the "keyid" parameter in config.json'
);
} else {
result = await this._performLoginRequest();
if (
result.data.description ==
"Invalid credentials, please check username, password, keyid or apikey"
) {
throw new Error(
'Your account has 2-factor auth enabled. Please provide the "mfaCode" parameter in config.json.'
"Invalid credentials, please check username, password, keyid or apikey"
);
} else {
if (this.apiLogEnabled)
this.log.debug("Successfully logged into Wyze API");
await this._updateTokens(result.data);
}

const data = {
mfa_type: "TotpVerificationCode",
verification_id: result.data.mfa_details.totp_apps[0].app_id,
verification_code: this.mfaCode,
};

result = await this._performLoginRequest(data);
}

await this._updateTokens(result.data);

if (this.apiLogEnabled) this.log.debug("Successfully logged into Wyze API");
}

async maybeLogin() {
Expand All @@ -208,20 +213,35 @@ module.exports = class WyzeAPI {
if (!this.access_token) {
let now = new Date().getTime();
// check if the last login attempt occurred too recently
if (this.apiLogEnabled) this.log.debug("Last login " + this.lastLoginAttempt + " debounce " + this.loginAttemptDebounceMilliseconds + " now " + now);
if (this.apiLogEnabled)
this.log.debug(
"Last login " +
this.lastLoginAttempt +
" debounce " +
this.loginAttemptDebounceMilliseconds +
" now " +
now
);
if (this.lastLoginAttempt + this.loginAttemptDebounceMilliseconds < now) {
// reset loginAttemptDebounceMilliseconds if last attempted login occurred more than 12 hours ago
if (this.lastLoginAttempt - now > 60 * 1000 * 60 * 12) {
this.loginAttemptDebounceMilliseconds = 1000;
} else {
// max debounce of 5 minutes
this.loginAttemptDebounceMilliseconds = Math.min(this.loginAttemptDebounceMilliseconds * 2, 1000 * 60 * 5);
this.loginAttemptDebounceMilliseconds = Math.min(
this.loginAttemptDebounceMilliseconds * 2,
1000 * 60 * 5
);
}

this.lastLoginAttempt = now;
await this.login();
} else {
this.log.warning("Attempting to login before debounce has cleared, waiting " + this.loginAttemptDebounceMilliseconds / 1000 + " seconds");
this.log.warning(
"Attempting to login before debounce has cleared, waiting " +
this.loginAttemptDebounceMilliseconds / 1000 +
" seconds"
);

var waitTime = 0;
while (waitTime < this.loginAttemptDebounceMilliseconds) {
Expand All @@ -234,7 +254,10 @@ module.exports = class WyzeAPI {

if (!this.access_token) {
this.lastLoginAttempt = now;
this.loginAttemptDebounceMilliseconds = Math.min(this.loginAttemptDebounceMilliseconds * 2, 1000 * 60 * 5);
this.loginAttemptDebounceMilliseconds = Math.min(
this.loginAttemptDebounceMilliseconds * 2,
1000 * 60 * 5
);
await this.login();
}
}
Expand Down

0 comments on commit 463197a

Please sign in to comment.