UNPKG

@owlnext/heimdall-js-native

Version:

Heimdall API client & utils for react native technologies

102 lines 4.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 axios_1 = __importDefault(require("axios")); const fingerprint_utils_1 = __importDefault(require("../../crypto/fingerprint-utils")); const react_native_uuid_1 = __importDefault(require("react-native-uuid")); const js_sha512_1 = require("js-sha512"); const Routes = __importStar(require("../../api/routing/routes")); const react_native_base64_1 = __importDefault(require("react-native-base64")); const config_loader_1 = __importDefault(require("../../config/config-loader")); class HttpClient { constructor() { this.setServerEnvironment = (server_env) => { this._server_environment = server_env; }; this.doRequest = (props) => __awaiter(this, void 0, void 0, function* () { let uri = this.getAPIEntrypoint() + props.uri; const payload = { method: props.method, url: uri }; if (null !== props.query_params && undefined !== props.query_params) { payload.params = props.query_params; } if (null !== props.body && undefined !== props.body) { payload.data = props.body; } return this.generateSecurityHeader().then(secHeader => { payload.headers = { 'Content-Type': 'application/json', 'X-HEIMDALL': secHeader }; if (props.uri === Routes.AUTHENTICATE || props.uri === Routes.REFRESH_AUTHENTICATION) { payload.data.fingerprint = this._fingerprint; } else { payload.headers = Object.assign(payload.headers, { 'Authorization': `Bearer ${props.jwt_token}` }); } return axios_1.default.request(payload) .then(res => { return Promise.resolve(res); }) .catch(); }); }); this.getAPIEntrypoint = () => { let entrypoint = this._config.config.PRODUCTION_API_ENTRYPOINT; if (this._server_environment === 'integration') { entrypoint = this._config.config.INTEGRATION_API_ENTRYPOINT; } return entrypoint; }; this.generateSecurityHeader = () => __awaiter(this, void 0, void 0, function* () { return this._fingerprintUtils.getFingerprint().then(fingerprint => { this._fingerprint = fingerprint; return Promise.resolve(this.makeHeimdallHeader(fingerprint)); }); }); this.makeHeimdallHeader = (fingerprint) => { const nonce = react_native_uuid_1.default.v4(); const cipher = (0, js_sha512_1.sha512)(fingerprint + '' + nonce); const nonce64 = react_native_base64_1.default.encode(nonce); return `cipher="${cipher}"; nonce="${nonce64}";`; }; this._config = new config_loader_1.default(); this._fingerprintUtils = new fingerprint_utils_1.default(); this._fingerprint = ''; this._server_environment = 'production'; } } exports.default = HttpClient; //# sourceMappingURL=http-client.js.map