"use strict";
/*
 * nodejs-license-file
 * https://github.com/bushev/nodejs-license-file
 *
 * Copyright (c) 2016 Yury Bushev
 * Licensed under the MIT license.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicenseFile = void 0;
var fs = require("fs");
var crypto = require("crypto");
var stringify = require("json-stable-stringify");
var Config_1 = require("./Config");
var openflow_api_1 = require("@openiap/openflow-api");
/**
 * LicenseFile Class
 */
var LicenseFile = /** @class */ (function () {
    function LicenseFile() {
        this.template_v1 = [
            '====BEGIN LICENSE====',
            '{{&licenseVersion}}',
            '{{&email}}',
            '{{&expirationDate}}',
            '{{&serial}}',
            '=====END LICENSE====='
        ].join('\n');
        this.template_v2 = [
            '====BEGIN LICENSE====',
            '{{&licenseVersion}}',
            '{{&email}}',
            '{{&expirationDate}}',
            '{{&domain}}',
            '{{&serial}}',
            '=====END LICENSE====='
        ].join('\n');
        this.license_public_key = Config_1.Config.getEnv("license_public_key", "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFyVVJncVpLcHpmbTJISEUxTUJ3YwpmZW5oMk1PZVZQbEJMS2dUM3l2TXIvQlpTSHVPb3pkaFprZ3JoNVVOSVlXZ1JYNVR1SDVoeVZ1ODJzOFhoR2RLCnZ5ajFGWGt2SHVubWp4ZFpGWEhDcjJCVE12Y09VTnZsTk5MWFpTOXh0YXE1cHdBemh0SlpjbmZLdTVHcjZNR1MKbVFxcHpVeDRWRVIyN3hSaVo0QlhXUkQ4MGNVaHAvTHF3UGswdHYwU0FTNTNZVXVRNkJIcWp1MDdHd3lFYzZaUQpJNjlLRVpLaHMvbk5GRHlVOHhwT0JoL0EwR1dMT0wrb01JeU9NSkZ6a2RPZURDQzlnVzNabmJBOHo0dHZUc3VHCmI3c2ZidTJlSE5MK09ucWlzamdFYStrZWlIQXZXaTlCa2dGbWdZWnNaWk5ENnovWkpHUi84MElzekV4THRqL3YKNFFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==");
        this.privateKey = Config_1.Config.getEnv("license_public_key", "");
        this.validlicense = false;
        this.licenserror = "Not validated yet";
        this.data = null;
        this._ofid = null;
    }
    LicenseFile.prototype.ofid = function () {
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(this._ofid))
            return this._ofid;
        var crypto = require('crypto');
        var openflow_uniqueid = Config_1.Config.openflow_uniqueid || crypto.createHash('md5').update(Config_1.Config.domain).digest("hex");
        Config_1.Config.openflow_uniqueid = openflow_uniqueid;
        this._ofid = openflow_uniqueid;
        return openflow_uniqueid;
    };
    LicenseFile.prototype.validate = function () {
        var _this = this;
        this.validlicense = false;
        if (openflow_api_1.NoderedUtil.IsNullEmpty(this.license_public_key)) {
            this.licenserror = "Missing license_public_key";
            throw new Error("Missing license_public_key");
        }
        if (openflow_api_1.NoderedUtil.IsNullEmpty(Config_1.Config.license_key)) {
            this.licenserror = "Missing license_key";
            throw new Error("Missing license_key");
        }
        var license_public_key = Buffer.from(this.license_public_key, "base64").toString("utf8");
        var license_key = Buffer.from(Config_1.Config.license_key, "base64").toString("ascii");
        try {
            var data = null;
            try {
                data = this.parse({
                    publicKey: license_public_key,
                    licenseFile: license_key,
                    template: this.template_v2
                });
            }
            catch (error) {
            }
            if (data == null) {
                data = this.parse({
                    publicKey: license_public_key,
                    licenseFile: license_key,
                    template: this.template_v1
                });
            }
            if (data.valid !== true) {
                this.licenserror = "license is not valid";
            }
            else {
                this.data = data.data;
                this.data.expirationDate = new Date(this.data.expirationDate);
                this.data.licenseVersion = parseInt(this.data.licenseVersion);
                var now = new Date();
                this.data.expirationDate = new Date(data.data.expirationDate);
                if (this.data.expirationDate < now) {
                    this.licenserror = "License has expired at " + this.data.expirationDate;
                }
                else {
                    if (!openflow_api_1.NoderedUtil.IsNullEmpty(this.data.domain)) {
                        if (this.data.domain != Config_1.Config.domain) {
                            this.licenserror = "License is not valid for this openflow instance!";
                        }
                        else {
                            this.licenserror = "";
                            this.validlicense = true;
                        }
                    }
                    else {
                        this.licenserror = "";
                        this.validlicense = true;
                    }
                }
            }
        }
        catch (error) {
            this.licenserror = "license validation failed " + error;
        }
        if (!this.validlicense) {
            if (!openflow_api_1.NoderedUtil.IsNullUndefinded(this.validateTimer))
                clearInterval(this.validateTimer);
            throw new Error(this.licenserror);
        }
        else {
            if (openflow_api_1.NoderedUtil.IsNullUndefinded(this.validateTimer)) {
                this.validateTimer = setInterval(function () {
                    try {
                        _this.validate();
                    }
                    catch (error) {
                    }
                }, 3600000);
            }
        }
    };
    /**
     *  Generate license file
     *
     * @param options
     * @param options.data {object|string} - data to sign
     * @param options.template - custom license file template
     * @param [options.privateKeyPath] {string} - path to private key
     * @param [options.privateKey] {string} - private key content
     */
    LicenseFile.prototype.generate = function (options) {
        var privateKey;
        if (!options.data) {
            throw new Error('LicenseFile::generate: options.data is required');
        }
        if (!options.template) {
            throw new Error('LicenseFile::generate: options.template is required');
        }
        if (typeof options.privateKey === 'string') {
            privateKey = options.privateKey;
        }
        else if (typeof options.privateKeyPath === 'string') {
            privateKey = fs.readFileSync(options.privateKeyPath, 'utf8');
        }
        else {
            throw new Error('LicenseFile::generate: privateKeyPath or privateKey is required');
        }
        var serial = this._generateSerial(Object.assign({}, options, { privateKey: privateKey }));
        if (typeof options.data === 'string') {
            options.data = { string: options.data };
        }
        options.data.serial = serial;
        return this._render(options.template, this._prepareDataObject(options.data));
    };
    /**
     * Parse license file
     *
     * @param options
     * @param options.template - custom license file template
     * @param [options.publicKeyPath] {string} - path to public key
     * @param [options.publicKey] {string} - path to public key
     * @param [options.licenseFilePath] {string} - path to license file
     * @param [options.licenseFile] {string} - license file content
     */
    LicenseFile.prototype.parse = function (options) {
        var publicKey;
        var licenseFile;
        if (!options.template) {
            throw new Error('LicenseFile::parse: options.template is required');
        }
        if (typeof options.publicKey === 'string') {
            publicKey = options.publicKey;
        }
        else if (typeof options.publicKeyPath === 'string') {
            publicKey = fs.readFileSync(options.publicKeyPath, 'utf8');
        }
        else {
            throw new Error('LicenseFile::parse: publicKeyPath or publicKey is required');
        }
        if (typeof options.licenseFile === 'string') {
            licenseFile = options.licenseFile;
        }
        else if (typeof options.licenseFilePath === 'string') {
            licenseFile = fs.readFileSync(options.licenseFilePath, 'utf8');
        }
        else {
            throw new Error('LicenseFile::parse: licenseFilePath or licenseFile is required');
        }
        var tokens = [];
        var regExpString = options.template.replace(/{{&(\w+)}}/g, function (match, token) {
            tokens.push(token);
            return '(.*)';
        });
        var result = licenseFile.match(new RegExp(regExpString));
        if (!result) {
            throw new Error("License file corrupted");
        }
        if (result.length - tokens.length !== 1) {
            throw new Error("License file corrupted, tokens expected: " + tokens.length + ", actual: " + (result.length - 1));
        }
        var dataObj = {
            data: {}, serial: ""
        };
        for (var i = 0; i < tokens.length; i++) {
            if (tokens[i] === 'serial') {
                dataObj[tokens[i]] = result[i + 1];
            }
            else {
                dataObj['data'][tokens[i]] = result[i + 1];
            }
        }
        var verify = crypto.createVerify('RSA-SHA256');
        verify.update(stringify(dataObj.data));
        var valid = verify.verify(publicKey, dataObj.serial, 'base64');
        return {
            valid: valid,
            serial: dataObj.serial,
            data: dataObj.data
        };
    };
    /**
     *
     * @param options
     * @param options.data
     * @param options.privateKey
     * @private
     */
    LicenseFile.prototype._generateSerial = function (options) {
        var sign = crypto.createSign('RSA-SHA256');
        var data = options.data;
        if (typeof options.data === 'object') {
            data = stringify(this._prepareDataObject(options.data));
        }
        sign.update(data);
        return sign.sign(options.privateKey, 'base64');
    };
    LicenseFile.prototype._render = function (template, data) {
        for (var property in data) {
            if (data.hasOwnProperty(property)) {
                var regExp = new RegExp("{{&" + property + "}}", "g");
                template = template.replace(regExp, data[property]);
            }
        }
        return template;
    };
    LicenseFile.prototype._prepareDataObject = function (data) {
        var result = {};
        for (var property in data) {
            if (data.hasOwnProperty(property)) {
                result[property] = typeof data[property] === 'string' ? data[property] : data[property] + '';
            }
        }
        return result;
    };
    return LicenseFile;
}());
exports.LicenseFile = LicenseFile;
//# sourceMappingURL=license-file.js.map