"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    interceptRoles: function() {
        return interceptRoles;
    },
    validateWhitelistedIp: function() {
        return validateWhitelistedIp;
    },
    validateWizardCompleted: function() {
        return validateWizardCompleted;
    }
});
const _awilixexpress = require("awilix-express");
const _serversettingsconstants = require("../constants/server-settings.constants");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const validateWizardCompleted = (0, _awilixexpress.inject)(({ configService, settingsStore, loggerFactory })=>async (req, res, next)=>{
        const logger = loggerFactory(validateWhitelistedIp.name);
        const isDemoMode = configService.isDemoMode();
        if (isDemoMode || !!settingsStore.getWizardSettings()?.wizardCompleted) {
            next();
            return;
        }
        const allowedPaths = [
            "/api/first-time-setup/complete",
            "/api/first-time-setup/validate",
            "/api/test",
            "/api/auth/login-required"
        ];
        if (allowedPaths.includes(req.path) || !req.path.startsWith("/api")) {
            next();
            return;
        } else {
            logger.error("Wizard not completed", req.path);
            throw new _runtimeexceptions.ForbiddenError(`First-time-setup not completed, these api paths are enabled: ${allowedPaths.join(", ")}`);
        }
    });
const validateWhitelistedIp = (0, _awilixexpress.inject)(({ settingsStore, loggerFactory })=>async (req, res, next)=>{
        const logger = loggerFactory(validateWhitelistedIp.name);
        const serverSettings = settingsStore.getSettings();
        if (!serverSettings && !settingsStore.getServerSettings() || !settingsStore.getServerSettings()?.whitelistEnabled) {
            next();
            return;
        }
        const whitelist = serverSettings[_serversettingsconstants.serverSettingsKey].whitelistedIpAddresses;
        const ipAddress = req.socket.remoteAddress;
        if (whitelist?.length && !whitelist.includes(ipAddress) && ipAddress !== "::ffff:127.0.0.1") {
            const subnextMatched = whitelist.find((w)=>{
                return ipAddress.startsWith(w);
            });
            if (!subnextMatched) {
                logger.error("IP did not match whitelist filters", req.socket.remoteAddress);
                throw new _runtimeexceptions.ForbiddenError("Bad IP: " + req.socket.remoteAddress);
            }
        }
        next();
    });
const interceptRoles = (0, _awilixexpress.inject)(({ settingsStore, roleService, isTypeormMode })=>async (req, res, next)=>{
        const serverSettings = await settingsStore.getSettings();
        if (isTypeormMode) {
            req.roles = req.user?.roles.map((r)=>r.roleId);
        } else {
            req.roles = req.user?.roles;
        }
        if (serverSettings && !req.user) {
            const roleName = await roleService.getAppDefaultRole();
            req.roles = [
                roleName
            ];
        }
        next();
    });

//# sourceMappingURL=global.middleware.js.map