smc-hub
Version:
CoCalc: Backend webserver component
162 lines • 8.66 kB
JavaScript
;
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 });
var lru_cache_1 = __importDefault(require("lru-cache"));
var async_utils_1 = require("smc-util/async-utils");
var logger_1 = __importDefault(require("../logger"));
var database_1 = require("../servers/database");
var _a = require("../access"), user_has_write_access_to_project = _a.user_has_write_access_to_project, user_has_read_access_to_project = _a.user_has_read_access_to_project;
var auth_1 = require("../auth");
var winston = logger_1.default("proxy: has-access");
// 5 minute cache: grant "yes" for a while
var yesCache = new lru_cache_1.default({ max: 20000, maxAge: 1000 * 60 * 5 });
// 10 second cache: recheck "no" more frequently
var noCache = new lru_cache_1.default({ max: 20000, maxAge: 1000 * 10 });
function hasAccess(opts) {
return __awaiter(this, void 0, void 0, function () {
var project_id, remember_me, type, key, _a, _b, cache, access, dbg, x, hash, signed_in_mesg, account_id, email_address, err_1;
var e_1, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (opts.isPersonal) {
// In personal mode, anyone who can access localhost has full
// access to everything, since this is meant to be used on
// single-user personal computer.
return [2 /*return*/, true];
}
project_id = opts.project_id, remember_me = opts.remember_me, type = opts.type;
key = project_id + remember_me + type;
try {
for (_a = __values([yesCache, noCache]), _b = _a.next(); !_b.done; _b = _a.next()) {
cache = _b.value;
if (cache.has(key))
return [2 /*return*/, !!cache.get(key)];
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
dbg = function (m) {
winston.debug(type + " access to " + project_id + ": " + m);
};
_d.label = 1;
case 1:
_d.trys.push([1, 8, , 9]);
dbg("get remember_me message");
x = remember_me.split("$");
hash = auth_1.generate_hash(x[0], x[1], x[2], x[3]);
return [4 /*yield*/, async_utils_1.callback2(database_1.database.get_remember_me, {
hash: hash,
cache: true,
})];
case 2:
signed_in_mesg = _d.sent();
if (signed_in_mesg == null) {
throw Error("not signed in");
}
account_id = signed_in_mesg.account_id, email_address = signed_in_mesg.email_address;
dbg("account_id=\"" + account_id + "\", email_address=\"" + email_address + "\"");
dbg("now check if user has " + type + " access to project");
if (!(type === "write")) return [3 /*break*/, 4];
return [4 /*yield*/, async_utils_1.callback2(user_has_write_access_to_project, {
database: database_1.database,
project_id: project_id,
account_id: account_id,
})];
case 3:
access = _d.sent();
if (access) {
// Record that user is going to actively access
// this project. This is important since it resets
// the idle timeout.
database_1.database.touch({
account_id: account_id,
project_id: project_id,
});
}
return [3 /*break*/, 7];
case 4:
if (!(type == "read")) return [3 /*break*/, 6];
return [4 /*yield*/, async_utils_1.callback2(user_has_read_access_to_project, {
database: database_1.database,
project_id: project_id,
account_id: account_id,
})];
case 5:
access = _d.sent();
return [3 /*break*/, 7];
case 6: throw Error("invalid access type " + type);
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _d.sent();
dbg("error trying to determine access; denying for now -- " + err_1);
access = false;
return [3 /*break*/, 9];
case 9:
if (access) {
yesCache.set(key, access);
}
else {
noCache.set(key, access);
}
return [2 /*return*/, access];
}
});
});
}
exports.default = hasAccess;
//# sourceMappingURL=check-for-access-to-project.js.map