UNPKG

egnyte-resellers

Version:

Library for managing things against the undocumented egnyte resellers API.

110 lines (109 loc) 4.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Base = void 0; const axios_1 = __importDefault(require("axios")); const node_querystring_1 = require("node:querystring"); const cheerio_1 = require("cheerio"); const tough_cookie_1 = require("tough-cookie"); const http_1 = require("http-cookie-agent/http"); class Base { _extractTrailingNumericIdFromLocation(location) { var _a; const path = (() => { try { return new URL(location, 'https://resellers.egnyte.com').pathname; } catch { return location; } })(); const match = path.match(/\/(\d+)\/?$/); return (_a = match === null || match === void 0 ? void 0 : match[1]) !== null && _a !== void 0 ? _a : null; } constructor(config) { var _a; this.config = config; if (!config.username || !config.password) { throw new Error('missing config values username or password'); } const ms = (_a = this.config.timeoutMs) !== null && _a !== void 0 ? _a : -1; const n = parseInt(ms); const timeout = !isNaN(n) && Math.abs(n) > 1 ? n : 20000; const jar = new tough_cookie_1.CookieJar(); this.http = axios_1.default.create({ baseURL: 'https://resellers.egnyte.com', timeout, httpAgent: new http_1.HttpCookieAgent({ cookies: { jar } }), httpsAgent: new http_1.HttpsCookieAgent({ cookies: { jar } }), }); } async authenticate() { var _a; const { csrfMiddlewareToken, csrfToken } = await this._getCsrfTokens(); const auth = await this.http.post('/accounts/login/', `csrfmiddlewaretoken=${csrfMiddlewareToken}&username=${(0, node_querystring_1.escape)(this.config.username)}&password=${(0, node_querystring_1.escape)(this.config.password)}&this_is_the_login_form=1`, { params: { csrfmiddlewaretoken: csrfMiddlewareToken, username: (0, node_querystring_1.escape)(this.config.username), password: (0, node_querystring_1.escape)(this.config.password), this_is_the_login_form: 1, }, maxRedirects: 0, headers: { Referer: 'https://resellers.egnyte.com/accounts/login/', Cookie: `csrftoken=${csrfToken}`, }, validateStatus: (status) => status >= 200 && status <= 303, }); if (auth.status === 302) { const setCookieHeader = auth.headers['set-cookie']; if (!setCookieHeader) throw new Error('unable to find set-cookie header in response'); const authCookie = ((_a = setCookieHeader[0]) !== null && _a !== void 0 ? _a : '').split(';')[0]; await this.setResellerId(authCookie); return { authCookie, csrfToken }; } else { throw new Error('Authentication failed. Bad username or password.'); } } async setResellerId(authCookie) { if (!authCookie) throw new Error('missing authCookie'); const res = await this.http.get('/customer/browse/', { headers: { cookie: authCookie }, maxRedirects: 0, validateStatus: (status) => status >= 200 && status <= 303, }); if (res.status === 302) { const location = res.headers.location; if (!location) throw new Error('unable to find location header in response'); const resellerId = this._extractTrailingNumericIdFromLocation(location); if (!resellerId) { throw new Error(`unable to parse resellerId from location header: ${JSON.stringify(location)}`); } this.resellerId = resellerId; return resellerId; } else { throw new Error('an error occurred attempting to get the resellerId'); } } async _getCsrfTokens() { var _a; const res = await this.http.get('accounts/login/'); const html = (0, cheerio_1.load)(res.data); const csrfMiddlewareToken = html('[name=csrfmiddlewaretoken]').val(); const cookies = (_a = (res === null || res === void 0 ? void 0 : res.headers)['set-cookie']) !== null && _a !== void 0 ? _a : []; const csrfToken = cookies .find((e) => e.includes('csrftoken')) .match(/csrftoken=(.*); expires/)[1]; if (!csrfMiddlewareToken || !csrfToken) throw new Error('unable to find CSRF token in egnyte resellers login page'); return { csrfMiddlewareToken, csrfToken }; } } exports.Base = Base;