UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

341 lines 16 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebappConfiguration = exports.clear_cache = void 0; // This unifies the entire webapp configuration – endpoint /customize // The main goal is to optimize this, to use as little DB interactions // as necessary, use caching, etc. // This manages the webapp's configuration based on the hostname // (allows whitelabeling). var parse_domain_1 = require("parse-domain"); var debug_1 = __importDefault(require("debug")); var L = debug_1.default("hub:webapp-config"); var awaiting_1 = require("awaiting"); var async_utils_1 = require("smc-util/async-utils"); var auth_1 = require("./auth"); var server_settings_1 = __importDefault(require("./servers/server-settings")); var site_settings_extras_1 = require("smc-util/db-schema/site-settings-extras"); var schema_1 = require("smc-util/schema"); var utils_1 = require("./utils"); var lru_cache_1 = __importDefault(require("lru-cache")); var CACHE = new lru_cache_1.default({ max: 10, maxAge: 3 * 60 * 1000 }); // 3 minutes function clear_cache() { CACHE.reset(); } exports.clear_cache = clear_cache; function get_passport_manager_async() { return __awaiter(this, void 0, void 0, function () { var ms, pp_manager; return __generator(this, function (_a) { switch (_a.label) { case 0: ms = 100; _a.label = 1; case 1: if (!true) return [3 /*break*/, 5]; pp_manager = auth_1.get_passport_manager(); if (!(pp_manager != null)) return [3 /*break*/, 2]; return [2 /*return*/, pp_manager]; case 2: L("WARNING: Passport Manager not available yet -- trying again in " + ms + "ms"); return [4 /*yield*/, awaiting_1.delay(ms)]; case 3: _a.sent(); ms = Math.min(10000, 1.3 * ms); _a.label = 4; case 4: return [3 /*break*/, 1]; case 5: return [2 /*return*/]; } }); }); } // these are special values, but can be overwritten by the specific theme var VANITY_HARDCODED = { allow_anonymous_sign_in: false, //kucalc: "onprem", // TODO: maybe do this, not sure //commercial: false, }; var WebappConfiguration = /** @class */ (function () { function WebappConfiguration(_a) { var db = _a.db; this.db = db; this.init(); } WebappConfiguration.prototype.init = function () { return __awaiter(this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: // this.data.pub updates automatically – do not modify it! _a = this; return [4 /*yield*/, server_settings_1.default()]; case 1: // this.data.pub updates automatically – do not modify it! _a.data = _c.sent(); _b = this; return [4 /*yield*/, get_passport_manager_async()]; case 2: _b.passport_manager = _c.sent(); return [2 /*return*/]; } }); }); }; // server settings with whitelabeling settings // TODO post-process all values WebappConfiguration.prototype.settings = function (vid) { return __awaiter(this, void 0, void 0, function () { var res, data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(this.db._query, { query: "SELECT id, settings FROM whitelabeling", cache: true, where: { "id = $::TEXT": vid }, })]; case 1: res = _a.sent(); if (this.data == null) { // settings not yet initialized return [2 /*return*/, {}]; } data = res.rows[0]; if (data != null) { return [2 /*return*/, __assign(__assign({}, this.data.all), data.settings)]; } else { return [2 /*return*/, this.data.all]; } return [2 /*return*/]; } }); }); }; // derive the vanity ID from the host string WebappConfiguration.prototype.get_vanity_id = function (host) { var host_parsed = parse_domain_1.parseDomain(host); if (host_parsed.type === parse_domain_1.ParseResultType.Listed) { // vanity for vanity.cocalc.com or foo.p for foo.p.cocalc.com return host_parsed.subDomains.join("."); } return undefined; }; WebappConfiguration.prototype.theme = function (vid) { var _a; return __awaiter(this, void 0, void 0, function () { var res, data, theme, _b, _c, _d, key, value, config; var e_1, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(this.db._query, { query: "SELECT id, theme FROM whitelabeling", cache: true, where: { "id = $::TEXT": vid }, })]; case 1: res = _f.sent(); data = res.rows[0]; if (data != null) { theme = {}; try { for (_b = __values(Object.entries(data.theme)), _c = _b.next(); !_c.done; _c = _b.next()) { _d = __read(_c.value, 2), key = _d[0], value = _d[1]; config = (_a = schema_1.site_settings_conf[key]) !== null && _a !== void 0 ? _a : site_settings_extras_1.EXTRAS[key]; if (typeof (config === null || config === void 0 ? void 0 : config.to_val) == "function") { theme[key] = config.to_val(value); } else { if (typeof value == "string" || typeof value == "boolean") { theme[key] = value; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_e = _b.return)) _e.call(_b); } finally { if (e_1) throw e_1.error; } } L("vanity theme=" + JSON.stringify(theme)); return [2 /*return*/, theme]; } else { L("theme id=" + vid + " not found"); return [2 /*return*/, {}]; } return [2 /*return*/]; } }); }); }; WebappConfiguration.prototype.get_vanity = function (vid) { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(vid != null && vid !== "")) return [3 /*break*/, 2]; L("vanity ID = \"" + vid + "\""); _a = [__assign({}, VANITY_HARDCODED)]; return [4 /*yield*/, this.theme(vid)]; case 1: return [2 /*return*/, __assign.apply(void 0, _a.concat([(_b.sent())]))]; case 2: return [2 /*return*/, {}]; } }); }); }; // returns the global configuration + eventually vanity specific site config settings WebappConfiguration.prototype.get_configuration = function (_a) { var host = _a.host, country = _a.country; return __awaiter(this, void 0, void 0, function () { var vid, config, vanity; return __generator(this, function (_b) { if (this.data == null) { // settings not yet initialized return [2 /*return*/, {}]; } vid = this.get_vanity_id(host); config = this.data.pub; vanity = this.get_vanity(vid); return [2 /*return*/, __assign(__assign(__assign({}, config), vanity), { country: country, dns: host })]; }); }); }; WebappConfiguration.prototype.get_strategies = function () { var key = "strategies"; var strategies = CACHE.get(key); if (strategies == null) { strategies = this.passport_manager.get_strategies_v2(); CACHE.set(key, strategies); } return strategies; }; WebappConfiguration.prototype.get_config = function (_a) { var country = _a.country, host = _a.host; return __awaiter(this, void 0, void 0, function () { var _b, configuration, registration, strategies; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, Promise.all([ this.get_configuration({ host: host, country: country }), utils_1.have_active_registration_tokens(this.db), ])]; case 1: _b = __read.apply(void 0, [_c.sent(), 2]), configuration = _b[0], registration = _b[1]; strategies = this.get_strategies(); return [2 /*return*/, { configuration: configuration, registration: registration, strategies: strategies }]; } }); }); }; // it returns a shallow copy, hence you can modify/add keys in the returned map! WebappConfiguration.prototype.get = function (_a) { var country = _a.country, host = _a.host; return __awaiter(this, void 0, void 0, function () { var key, config; return __generator(this, function (_b) { switch (_b.label) { case 0: key = "config::" + country + "::" + host; config = CACHE.get(key); if (!(config == null)) return [3 /*break*/, 2]; return [4 /*yield*/, this.get_config({ country: country, host: host })]; case 1: config = _b.sent(); CACHE.set(key, config); return [3 /*break*/, 3]; case 2: L("cache hit -- '" + key + "'"); _b.label = 3; case 3: return [2 /*return*/, config]; } }); }); }; return WebappConfiguration; }()); exports.WebappConfiguration = WebappConfiguration; //# sourceMappingURL=webapp-configuration.js.map