"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nodered = exports.Singin = exports.Audit = void 0;
const Config_1 = require("./Config");
const openflow_api_1 = require("@openiap/openflow-api");
const Crypt_1 = require("./Crypt");
const Logger_1 = require("./Logger");
class Audit {
    static async LoginSuccess(user, type, provider, remoteip, clientagent, clientversion, parent) {
        const span = Logger_1.Logger.otel.startSubSpan("Audit.LoginSuccess", parent);
        try {
            const log = new Singin();
            openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read, openflow_api_1.Rights.update, openflow_api_1.Rights.invoke]);
            log.remoteip = remoteip;
            log.ip = Audit.dot2num(log.remoteip);
            log.success = true;
            log.type = type;
            log.provider = provider;
            log.userid = user._id;
            log.name = user.name;
            log.username = user.username;
            log.clientagent = clientagent;
            log.clientversion = clientversion;
            await Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), span);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("Audit", "DoSign", error);
            span === null || span === void 0 ? void 0 : span.recordException(error);
        }
        finally {
            Logger_1.Logger.otel.endSpan(span);
        }
    }
    static async ImpersonateSuccess(user, impostor, clientagent, clientversion, parent) {
        const span = Logger_1.Logger.otel.startSubSpan("Audit.ImpersonateSuccess", parent);
        try {
            const log = new Singin();
            openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
            openflow_api_1.Base.addRight(log, impostor._id, impostor.name, [openflow_api_1.Rights.read]);
            log.success = true;
            log._type = "impersonate";
            log.type = "impersonate";
            log.userid = user._id;
            log.name = user.name;
            log.username = user.username;
            log.impostoruserid = impostor._id;
            log.impostorname = impostor.name;
            log.impostorusername = impostor.username;
            log.clientagent = clientagent;
            log.clientversion = clientversion;
            Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), span);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("Audit", "DoSign", error);
            span === null || span === void 0 ? void 0 : span.recordException(error);
        }
        finally {
            Logger_1.Logger.otel.endSpan(span);
        }
    }
    static async ImpersonateFailed(user, impostor, clientagent, clientversion, parent) {
        const span = Logger_1.Logger.otel.startSubSpan("Audit.ImpersonateFailed", parent);
        try {
            const log = new Singin();
            openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
            openflow_api_1.Base.addRight(log, impostor._id, impostor.name, [openflow_api_1.Rights.read]);
            log.success = false;
            log._type = "impersonate";
            log.type = "impersonate";
            log.userid = user._id;
            log.name = impostor.name + " -> " + user.name;
            log.username = user.username;
            log.impostoruserid = impostor._id;
            log.impostorname = impostor.name;
            log.clientagent = clientagent;
            log.clientversion = clientversion;
            Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), span);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("Audit", "DoSign", error);
            span === null || span === void 0 ? void 0 : span.recordException(error);
        }
        finally {
            Logger_1.Logger.otel.endSpan(span);
        }
    }
    static async LoginFailed(username, type, provider, remoteip, clientagent, clientversion, parent) {
        const span = Logger_1.Logger.otel.startSubSpan("Audit.LoginFailed", parent);
        try {
            const log = new Singin();
            log.remoteip = remoteip;
            log.ip = Audit.dot2num(log.remoteip);
            log.success = false;
            log.type = type;
            log.name = "[failed]" + username;
            log.provider = provider;
            log.username = username;
            log.clientagent = clientagent;
            log.clientversion = clientversion;
            Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), span);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("Audit", "LoginFailed", error);
            span === null || span === void 0 ? void 0 : span.recordException(error);
        }
        finally {
            Logger_1.Logger.otel.endSpan(span);
        }
    }
    static async NoderedAction(user, success, name, type, image, instancename, parent) {
        const span = Logger_1.Logger.otel.startSubSpan("Audit.NoderedAction", parent);
        try {
            const log = new Nodered();
            openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
            log.success = success;
            log.type = type;
            log.userid = user._id;
            log.name = name;
            log.username = user.username;
            log.instancename = instancename;
            log.image = image;
            if (!openflow_api_1.NoderedUtil.IsNullEmpty(image) && image.indexOf(':') > -1) {
                log.imagename = image.split(':')[0];
                log.imageversion = image.split(':')[1];
            }
            else {
                log.imagename = image;
            }
            if (!openflow_api_1.NoderedUtil.IsNullEmpty(instancename))
                log.name = instancename;
            await Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), span);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("Audit", "NoderedAction", error);
            span === null || span === void 0 ? void 0 : span.recordException(error);
        }
        finally {
            Logger_1.Logger.otel.endSpan(span);
        }
    }
    static dot2num(dot) {
        if (openflow_api_1.NoderedUtil.IsNullEmpty(dot))
            return 0;
        if (dot.indexOf(".") == -1)
            return 0;
        var d = dot.split('.');
        return ((((((+d[0]) * 256) + (+d[1])) * 256) + (+d[2])) * 256) + (+d[3]);
    }
    static num2dot(num) {
        if (openflow_api_1.NoderedUtil.IsNullEmpty(num))
            return "";
        if (num < 1)
            return "";
        var d = (num % 256).toString();
        for (var i = 3; i > 0; i--) {
            num = Math.floor(num / 256);
            d = num % 256 + '.' + d;
        }
        return d;
    }
}
exports.Audit = Audit;
class Singin extends openflow_api_1.Base {
    constructor() {
        super();
        this._type = "signin";
    }
}
exports.Singin = Singin;
class Nodered extends openflow_api_1.Base {
    constructor() {
        super();
        this._type = "nodered";
    }
}
exports.Nodered = Nodered;
//# sourceMappingURL=Audit.js.map