UNPKG

huawei-wingle-4g

Version:

This is a module NodeJS allowing to drive Huawei Wingle 4G. This module can :

82 lines 3.71 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Connection_1 = __importDefault(require("./Connection")); const log4js_1 = __importDefault(require("log4js")); const StringUtils_1 = require("../utils/StringUtils"); const EncodeUtils_1 = require("../utils/EncodeUtils"); class default_1 { constructor(username, password, connection) { this.username = username; this.password = password; this.connection = connection; this.logger = log4js_1.default.getLogger(StringUtils_1.substringAfter(__filename, 'huawei-wingle-4g')); } activeLog(activeLog) { this.logger.level = activeLog ? 'debug' : 'OFF'; this.connection.activeLog(activeLog); } login() { return __awaiter(this, void 0, void 0, function* () { if (yield this.isLogged()) { this.logger.debug(`Already logged, no need to login`); return; } const parameters = this.buildLoginParameters(); const response = yield this.connection.post('/api/user/login', parameters); if (Connection_1.default.isSuccess(response)) { this.logger.debug('Login success'); } else { throw new Error(`Login failed`); } }); } getConnnection() { return this.connection; } isLogged() { var _a; return __awaiter(this, void 0, void 0, function* () { const response = yield this.connection.get('/api/user/state-login'); const state = (_a = response.document.querySelector('State')) === null || _a === void 0 ? void 0 : _a.textContent; switch (state) { case '-1': return false; case '0': return true; default: throw new Error('Unable to derminate if logged or not'); } }); } buildLoginParameters() { const encryptedPassword = this.encryptPassword(this.connection.getToken()); return `<?xml version: "1.0" encoding="UTF-8"?><request><Username>${this.username}</Username><Password>${encryptedPassword}</Password><password_type>4</password_type></request>`; } encryptPassword(requestVerificationToken) { if (!requestVerificationToken) { throw new Error('Request verification token is blank'); } if (!this.username) { throw new Error('Username is blank'); } if (!this.password) { throw new Error('Password is blank'); } return EncodeUtils_1.encodeBase64(EncodeUtils_1.encodeSha256(this.username + EncodeUtils_1.encodeBase64(EncodeUtils_1.encodeSha256(this.password)) + requestVerificationToken)); } } exports.default = default_1; //# sourceMappingURL=Login.js.map