"use strict"; import * as btoa from "btoa"; import * as CryptoJS from "crypto-js"; import { isDefined, isDefinedAndNotEmpty, isNotDefined, isNotDefinedOrEmpty, makeId, stackTrace } from "../common/Utils.js"; import {Logger} from "../common/Logger.js"; import {LevelLogs} from "../common/LevelLogs.js"; let packageVersion = require("../../package.json"); class GenericRESTService extends LevelLogs{ protected _token: any; protected _decodedtokenRest: any; protected _credentials: any; protected _application: any; protected _auth: any; protected _logger: Logger; protected _logId: string; protected startingInfos : { constructorDate: Date, startDate: Date, startedDate: Date, initilizedDate: Date readyDate: Date } = { constructorDate: new Date(), startDate: new Date(), startedDate: new Date(), initilizedDate: new Date(), readyDate: new Date() }; constructor( _logger : Logger, logId : string = "UNDF/SVCE - ") { super(); this.setLogLevels(this); let that = this; that._logger = _logger; if (logId) { that._logId = logId; } // that._logger.log("debug", that._logId + "(GenericService::constructor) " ); that.setConstructed(); } setConstructed () { let that = this; that.startingInfos.constructorDate = new Date(); that._logger.log(that.INFO, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`); } set p_token(value: any) { this._token = value; } set p_credentials(value: any) { this._credentials = value; } set p_application(value: any) { this._application = value; } set p_auth(value: any) { this._auth = value; } get token(): any { return this._token; } get credentials(): any { return this._credentials; } get application(): any { return this._application; } get auth(): any { return this._auth; } isUserCredentialsLogin(): boolean { return (isDefinedAndNotEmpty(this._credentials.login) && isDefinedAndNotEmpty(this._credentials.password) && isNotDefinedOrEmpty(this._credentials.apikey)); } isAPIKeyCredentialsLogin(): boolean { return (isNotDefinedOrEmpty(this._credentials.login) && isNotDefinedOrEmpty(this._credentials.password) && isDefinedAndNotEmpty(this._credentials.apikey)); } get p_decodedtokenRest(): any { return this._decodedtokenRest; } set p_decodedtokenRest(value: any) { this._decodedtokenRest = value; } getRequestHeader (accept : string = undefined) { let that = this; let headers = { // "Authorization": "Bearer " + that._token, "Accept": accept || "application/json", "Range": undefined, "x-rainbow-client": "sdk_node", "x-rainbow-client-version": packageVersion.version, "x-rainbow-client-id": that.application?that.application.appID:"", "x-rainbow-correlation-id" : makeId(16) //"x-rainbow-request-node-id" : makeId(9) }; //let caller = arguments.callee.caller.toString(); // let caller = arguments.callee.caller.prototype; //console.log("caller : ", caller); if (that._credentials.apikey) { /*var uuid = require('uuid'); var btoa = require('btoa'); var cryptojs = require('crypto-js'); //*/ headers['x-rainbow-api-key'] = that._credentials.apikey; let challenge = makeId(22); //console.log('challenge: ' + challenge); headers['x-rainbow-challenge'] = challenge; let xapp = "XXX"; // Basic (Concatenation of appSecret and provided challenge, hashed with sha256). let appIdNet = that.application.appID; //console.log('appIdNet: ' + appIdNet); let appSecretNet = that.application.appSecret; //console.log('appSecretNet: ' + appSecretNet); let toEncrypt = appSecretNet + challenge; //console.log('appSecretNet + challenge : ' + toEncrypt); let encrypted = CryptoJS.SHA256(toEncrypt).toString(); //console.log('appSecretNet + challenge encrypted : ' + encrypted); let appCode = appIdNet + ':' + encrypted //console.log('appIdNet:encrypted : ' + appCode); let base64 = btoa(appCode); //console.log('base64: ' + base64); xapp = base64; headers["x-rainbow-app-auth"] = "Basic " + xapp; } else { headers["Authorization"] = "Bearer " + that._token; } return headers; } getRequestHeaderLowercaseAccept (accept : string = undefined) { let that = this; let headers = { //"Authorization": "Bearer " + that._token, "accept": accept || "application/json", "x-rainbow-client": "sdk_node", "x-rainbow-client-version": packageVersion.version, "x-rainbow-client-id": that.application?that.application.appID:"", "x-rainbow-correlation-id" : makeId(16) //"x-rainbow-request-node-id" : makeId(9) }; if (that._credentials.apikey) { /*var uuid = require('uuid'); var btoa = require('btoa'); var cryptojs = require('crypto-js'); //*/ headers['x-rainbow-api-key'] = that._credentials.apikey; let challenge = makeId(22); // console.log('challenge: ' + challenge); headers['x-rainbow-challenge'] = challenge; let xapp = "XXX"; // Basic (Concatenation of appSecret and provided challenge, hashed with sha256). let appIdNet = that.application.appID; // console.log('appIdNet: ' + appIdNet); let appSecretNet = that.application.appSecret; //console.log('appSecretNet: ' + appSecretNet); let toEncrypt = appSecretNet + challenge; // console.log('appSecretNet + challenge : ' + toEncrypt); let encrypted = CryptoJS.SHA256(toEncrypt).toString(); // console.log('appSecretNet + challenge encrypted : ' + encrypted); let appCode = appIdNet + ':' + encrypted // console.log('appIdNet:encrypted : ' + appCode); let base64 = btoa(appCode); // console.log('base64: ' + base64); xapp = base64; headers["x-rainbow-app-auth"] = "Basic " + xapp; } else { headers["Authorization"] = "Bearer " + that._token; } return headers; } getRequestHeaderWithRange (accept: string = undefined, range: string = undefined) { let that = this; let header = that.getRequestHeader(accept); header.Range = range; return header; } getPostHeader (contentType : string = undefined) { let that = this; let header = that.getRequestHeader(undefined); let type = contentType || "application/json"; header["Content-Type"] = type; return header; }; getPostHeaderWithRange (accept: string = undefined, initialSize: string = undefined, minRange: string = undefined, maxRange: string = undefined) { let that = this; let header = this.getRequestHeader(accept); // Content-Range: bytes 0-1048575/2960156 //header["Content-Range"] = "bytes " + minRange + "-" + maxRange + "/" + initialSize; return header; }; getLoginHeader (auth : string = undefined, password : string = undefined) { let that = this; let headers = { "Accept": "application/json", "Content-Type": "application/json", "x-rainbow-client": "sdk_node", "x-rainbow-client-version": packageVersion.version, "x-rainbow-client-id": that.application?that.application.appID:"", "x-rainbow-correlation-id" : makeId(16) //"x-rainbow-request-node-id" : makeId(9) }; if (that._credentials.apikey) { /*var uuid = require('uuid'); var btoa = require('btoa'); var cryptojs = require('crypto-js'); //*/ headers['x-rainbow-api-key'] = that._credentials.apikey; let challenge = makeId(22); // console.log('challenge: ' + challenge); headers['x-rainbow-challenge'] = challenge; let xapp = "XXX"; // Basic (Concatenation of appSecret and provided challenge, hashed with sha256). let appIdNet = that.application.appID; // console.log('appIdNet: ' + appIdNet); let appSecretNet = that.application.appSecret; // console.log('appSecretNet: ' + appSecretNet); let toEncrypt = appSecretNet + challenge; // console.log('appSecretNet + challenge : ' + toEncrypt); let encrypted = CryptoJS.SHA256(toEncrypt).toString(); // console.log('appSecretNet + challenge encrypted : ' + encrypted); let appCode = appIdNet + ':' + encrypted // console.log('appIdNet:encrypted : ' + appCode); let base64 = btoa(appCode); // console.log('base64: ' + base64); xapp = base64; headers["x-rainbow-app-auth"] = "Basic " + xapp; } else { headers["Authorization"] = "Basic " + (auth || that._auth); let toEncrypt = that._application.appSecret + (password || that._credentials.password); //that._logger.log(that.DEBUG, LOG_ID + "toEncrypt : " + toEncrypt); let encrypted = CryptoJS.SHA256(toEncrypt).toString(); //that._logger.log(that.DEBUG, LOG_ID + "encrypted : " + encrypted); let base64 = btoa(that._application.appID + ':' + encrypted); //that._logger.log(that.DEBUG, LOG_ID + "base64 : " + base64); if (that._application.appSecret && base64 && base64.length) { headers["x-rainbow-app-auth"] = "Basic " + base64 || ""; } } return headers; }; getDefaultHeader () { let that = this; return { "Accept": "application/json", "Content-Type": "application/json", "x-rainbow-client": "sdk_node", "x-rainbow-client-version": packageVersion.version, "x-rainbow-client-id": that.application?that.application.appID:"", "x-rainbow-correlation-id" : makeId(16) //"x-rainbow-request-node-id" : makeId(9) }; }; } module.exports = {'GenericRESTService' : GenericRESTService}; export {GenericRESTService as GenericRESTService};