"use strict";
exports.id = 6177;
exports.ids = [6177];
exports.modules = {

/***/ 526177:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


/*
 *  This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.load_server_settings_from_env = exports.getServerSettings = exports.setPassportsCached = exports.getPassportsCached = exports.resetServerSettingsCache = void 0;
var lru_cache_1 = __importDefault(__webpack_require__(336942));
var site_settings_extras_1 = __webpack_require__(299834);
var schema_1 = __webpack_require__(47896);
var consts_1 = __webpack_require__(615096);
var pool_1 = __importDefault(__webpack_require__(541808));
var async_utils_1 = __webpack_require__(644181);
var logger_1 = __importDefault(__webpack_require__(227554));
var L = (0, logger_1.default)("server:server-settings");
// We're just using this to cache this result for a **few seconds**.
var CACHE_TIME_SECONDS =  false ? 0 : 15;
// TODO add something for the passports data type?
var cache = new lru_cache_1.default({
    max: 10,
    maxAge: 1000 * CACHE_TIME_SECONDS,
});
var KEY = "server-settings";
function resetServerSettingsCache() {
    cache.reset();
}
exports.resetServerSettingsCache = resetServerSettingsCache;
function getPassportsCached() {
    return cache.get("passports");
}
exports.getPassportsCached = getPassportsCached;
function setPassportsCached(val) {
    return cache.set("passports", val);
}
exports.setPassportsCached = setPassportsCached;
function getServerSettings() {
    var _a;
    return __awaiter(this, void 0, void 0, function () {
        var pool, rows, settings, raw, rows_1, rows_1_1, row, rows_2, rows_2_1, row, name_1, value, spec, toVal, _b, _c, config, key, spec;
        var e_1, _d, e_2, _e, e_3, _f;
        return __generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    if (cache.has(KEY)) {
                        return [2 /*return*/, cache.get(KEY)]; // can't be null
                    }
                    pool = (0, pool_1.default)();
                    return [4 /*yield*/, pool.query("SELECT name, value FROM server_settings")];
                case 1:
                    rows = (_g.sent()).rows;
                    settings = { _timestamp: Date.now() };
                    raw = {};
                    try {
                        for (rows_1 = __values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) {
                            row = rows_1_1.value;
                            raw[row.name] = row.value;
                        }
                    }
                    catch (e_1_1) { e_1 = { error: e_1_1 }; }
                    finally {
                        try {
                            if (rows_1_1 && !rows_1_1.done && (_d = rows_1.return)) _d.call(rows_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                    }
                    try {
                        // process values, including any post-processing.
                        for (rows_2 = __values(rows), rows_2_1 = rows_2.next(); !rows_2_1.done; rows_2_1 = rows_2.next()) {
                            row = rows_2_1.value;
                            name_1 = row.name, value = row.value;
                            spec = (_a = schema_1.site_settings_conf[name_1]) !== null && _a !== void 0 ? _a : site_settings_extras_1.EXTRAS[name_1];
                            // we only process values we know
                            if (spec == null)
                                continue;
                            toVal = spec.to_val;
                            settings[name_1] = toVal != null ? toVal(value, raw) : value;
                        }
                    }
                    catch (e_2_1) { e_2 = { error: e_2_1 }; }
                    finally {
                        try {
                            if (rows_2_1 && !rows_2_1.done && (_e = rows_2.return)) _e.call(rows_2);
                        }
                        finally { if (e_2) throw e_2.error; }
                    }
                    try {
                        // set default values for missing keys
                        for (_b = __values([site_settings_extras_1.EXTRAS, schema_1.site_settings_conf]), _c = _b.next(); !_c.done; _c = _b.next()) {
                            config = _c.value;
                            for (key in config) {
                                if (settings[key] == null) {
                                    spec = config[key];
                                    settings[key] =
                                        (spec === null || spec === void 0 ? void 0 : spec.to_val) != null ? spec.to_val(spec.default, raw) : spec.default;
                                }
                            }
                        }
                    }
                    catch (e_3_1) { e_3 = { error: e_3_1 }; }
                    finally {
                        try {
                            if (_c && !_c.done && (_f = _b.return)) _f.call(_b);
                        }
                        finally { if (e_3) throw e_3.error; }
                    }
                    cache.set(KEY, settings);
                    return [2 /*return*/, settings];
            }
        });
    });
}
exports.getServerSettings = getServerSettings;
/*
This stores environment variables for server settings in the DB to make the life of an admin easier.
e.g. COCALC_SETTING_DNS, COCALC_SETTING_EMAIL_SMTP_SERVER, COCALC_SETTING_EMAIL_SMTP_PASSWORD, ...
Loaded once at startup, right after configuring the db schema, see hub/hub.ts.
*/
function load_server_settings_from_env(db) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function () {
        var PREFIX, _c, _d, config, _e, _f, _i, key, envvar, envval, valid, e_4_1;
        var e_4, _g;
        return __generator(this, function (_h) {
            switch (_h.label) {
                case 0:
                    PREFIX = consts_1.SERVER_SETTINGS_ENV_PREFIX;
                    // reset all readonly values
                    return [4 /*yield*/, db.async_query({
                            query: "UPDATE server_settings",
                            set: { readonly: false },
                            where: ["1=1"], // otherwise there is an exception about not restricting the query
                        })];
                case 1:
                    // reset all readonly values
                    _h.sent();
                    _h.label = 2;
                case 2:
                    _h.trys.push([2, 9, 10, 11]);
                    _c = __values([site_settings_extras_1.EXTRAS, schema_1.site_settings_conf]), _d = _c.next();
                    _h.label = 3;
                case 3:
                    if (!!_d.done) return [3 /*break*/, 8];
                    config = _d.value;
                    _e = [];
                    for (_f in config)
                        _e.push(_f);
                    _i = 0;
                    _h.label = 4;
                case 4:
                    if (!(_i < _e.length)) return [3 /*break*/, 7];
                    key = _e[_i];
                    envvar = "".concat(PREFIX, "_").concat(key.toUpperCase());
                    envval = process.env[envvar];
                    if (envval == null)
                        return [3 /*break*/, 6];
                    // ATTN do not expose the value, could be a password
                    L.debug("picking up $".concat(envvar, " and saving it in the database"));
                    valid = (_b = ((_a = schema_1.site_settings_conf[key]) !== null && _a !== void 0 ? _a : site_settings_extras_1.EXTRAS[key])) === null || _b === void 0 ? void 0 : _b.valid;
                    if (valid != null) {
                        if (Array.isArray(valid) && !valid.includes(envval)) {
                            throw new Error("The value of $".concat(envvar, " is invalid. allowed are ").concat(valid, "."));
                        }
                        else if (typeof valid == "function" && !valid(envval)) {
                            throw new Error("The validation function rejected the value of $".concat(envvar, "."));
                        }
                    }
                    return [4 /*yield*/, (0, async_utils_1.callback2)(db.set_server_setting, {
                            name: key,
                            value: envval,
                            readonly: true,
                        })];
                case 5:
                    _h.sent();
                    _h.label = 6;
                case 6:
                    _i++;
                    return [3 /*break*/, 4];
                case 7:
                    _d = _c.next();
                    return [3 /*break*/, 3];
                case 8: return [3 /*break*/, 11];
                case 9:
                    e_4_1 = _h.sent();
                    e_4 = { error: e_4_1 };
                    return [3 /*break*/, 11];
                case 10:
                    try {
                        if (_d && !_d.done && (_g = _c.return)) _g.call(_c);
                    }
                    finally { if (e_4) throw e_4.error; }
                    return [7 /*endfinally*/];
                case 11: return [2 /*return*/];
            }
        });
    });
}
exports.load_server_settings_from_env = load_server_settings_from_env;
//# sourceMappingURL=server-settings.js.map

/***/ }),

/***/ 650571:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.versionCookieName = exports.NOT_SIGNED_IN = void 0;
// Do NOT change this - this exact string is assumed in @cocalc/hub/user-remember-me and @cocalc/util/client
// Of course, they do use this very constant.  It's just that if you change this you might have
// to rebuild and restart all servers, etc., which gets complicated...
exports.NOT_SIGNED_IN = "not signed in";
var VERSION_COOKIE_NAME = "cocalc_version";
function versionCookieName(base_path) {
    return ((base_path.length <= 1 ? "" : encodeURIComponent(base_path)) +
        VERSION_COOKIE_NAME);
}
exports.versionCookieName = versionCookieName;
//# sourceMappingURL=auth.js.map

/***/ }),

/***/ 615096:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {


/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.SERVER_SETTINGS_ENV_PREFIX = exports.DUMMY_SECRET = exports.versionCookieName = exports.NOT_SIGNED_IN = void 0;
/*
RANDOM constants that we might use anywhere in cocalc go here.
*/
var auth_1 = __webpack_require__(650571);
Object.defineProperty(exports, "NOT_SIGNED_IN", ({ enumerable: true, get: function () { return auth_1.NOT_SIGNED_IN; } }));
Object.defineProperty(exports, "versionCookieName", ({ enumerable: true, get: function () { return auth_1.versionCookieName; } }));
var project_1 = __webpack_require__(574934);
Object.defineProperty(exports, "DUMMY_SECRET", ({ enumerable: true, get: function () { return project_1.DUMMY_SECRET; } }));
var server_settings_1 = __webpack_require__(626373);
Object.defineProperty(exports, "SERVER_SETTINGS_ENV_PREFIX", ({ enumerable: true, get: function () { return server_settings_1.SERVER_SETTINGS_ENV_PREFIX; } }));
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 574934:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.DOC_CLOUD_STORAGE_URL = exports.DUMMY_SECRET = void 0;
exports.DUMMY_SECRET = "[SECRET HIDDEN]";
exports.DOC_CLOUD_STORAGE_URL = "https://doc.cocalc.com/project-settings.html#cloud-storage-remote-file-systems";
//# sourceMappingURL=project.js.map

/***/ }),

/***/ 626373:
/***/ ((__unused_webpack_module, exports) => {


/*
 *  This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.SERVER_SETTINGS_ENV_PREFIX = void 0;
exports.SERVER_SETTINGS_ENV_PREFIX = "COCALC_SETTING";
//# sourceMappingURL=server_settings.js.map

/***/ })

};
;