UNPKG

hilink-nodejs-sms

Version:

Node.js library to send sms using Huawei modems (hilink).

113 lines (112 loc) 5.04 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const common_1 = require("./common"); class CredentialsProvider { constructor(login, sha256password, _connector, _logger) { this._connector = _connector; this._logger = _logger; this._tokens = undefined; this._sessionId = undefined; this._auth = { login: login, password: sha256password }; this._tokens = []; } getCredentials() { return __awaiter(this, void 0, void 0, function* () { return yield this._getToken(); }); } destroy() { if (this._sessionTimeoutId !== undefined) { clearTimeout(this._sessionTimeoutId); } } _getToken(retry = 0) { return __awaiter(this, void 0, void 0, function* () { if (retry >= 5) { throw new Error('[SMS] Try to get token too many times'); } if (this._authPromise !== undefined) { yield this._authPromise; } const token = this._tokens.pop(); if (token === undefined) { yield this._authenticate(); return this._getToken(++retry); } return { token, sessionId: this._sessionId }; }); } _authenticate() { return __awaiter(this, void 0, void 0, function* () { if (this._authPromise !== undefined) { return this._authPromise; } clearTimeout(this._sessionTimeoutId); this._authPromise = (() => __awaiter(this, void 0, void 0, function* () { const { token, sessionId } = yield this._getSession(); const { tokens, sessionId: latestSessionId } = yield this._login(token, sessionId); this._tokens = tokens; this._sessionId = latestSessionId; }))(); yield this._authPromise; this._authPromise = undefined; }); } ; _login(token, sessionId) { return __awaiter(this, void 0, void 0, function* () { const response = yield this._connector.post('/api/user/login', utils_1.jsonToXmlString({ Username: this._auth.login, Password: utils_1.encodePassword(this._auth.login, this._auth.password, token), password_type: 4 //g_password_type - global variable from original Huawei code }), { cookie: sessionId, __RequestVerificationToken: token }); const responseJson = yield utils_1.xmlStringToJson(response.body); const { '__requestverificationtoken': verificationToken, 'set-cookie': cookies } = response.headers; if (!verificationToken || !cookies || responseJson.response !== 'OK') { this._clearSessionData(); throw new Error('[SMS] Failed to login.' + responseJson); } this._resetTimeout(); this._logger('LOGIN OK'); return { sessionId: utils_1.retrieveToken(cookies), tokens: verificationToken.split('#').slice(0, -1) // slice(0, -1) - last token is empty, remove it }; }); } ; _getSession() { return __awaiter(this, void 0, void 0, function* () { const sessionResponse = yield this._connector.get('/api/webserver/SesTokInfo'); const { response } = yield utils_1.xmlStringToJson(sessionResponse.body); if ((response === null || response === void 0 ? void 0 : response.SesInfo) === undefined || (response === null || response === void 0 ? void 0 : response.TokInfo) === undefined) { throw new Error('[SMS] Failed to fetch session data.'); } return { token: utils_1.retrieveToken(response.TokInfo), sessionId: utils_1.retrieveToken(response.SesInfo) }; }); } ; _clearSessionData() { this._tokens = []; this._sessionId = ''; } _resetTimeout() { this._logger('Auto clear session'); clearTimeout(this._sessionTimeoutId); this._sessionTimeoutId = setTimeout(this._clearSessionData.bind(this), common_1.DEFAULT_LOGOUT_TIME); } } exports.default = CredentialsProvider;