UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

220 lines (204 loc) 5.8 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## /* API for handling the messages described smc-util/message.coffee AGPLv3, (c) 2017, SageMath, Inc. */ var Cache, Client, HELP_EMAIL, async, auth_cache, defaults, get_client, handle_message, log, messages, misc, required; async = require('async'); Cache = require('lru-cache'); auth_cache = new Cache({ max: 100, maxAge: 60000 }); misc = require('smc-util/misc'); ({defaults, required} = misc); messages = require('smc-util/message'); ({HELP_EMAIL} = require("smc-util/theme")); ({Client} = require('../client')); log = function(name, logger) { return function(m) { return logger.debug(`API.${name}: ${m}`); }; }; exports.http_message_api_v1 = function(opts) { var client, dbg, err, f, mesg, resp; try { opts = defaults(opts, { event: required, body: required, api_key: required, database: required, compute_server: required, ip_address: required, logger: required, cb: required }); } catch (error) { err = error; opts.cb(err); return; } dbg = log('http_message_api_v1', opts.logger); dbg(`event=${JSON.stringify(opts.event)}, body=${JSON.stringify(opts.body)}`); f = messages[opts.event]; if (f == null) { opts.cb(`unknown endpoint '${opts.event}'`); return; } if (!messages.api_messages[opts.event]) { opts.cb(`endpoint '${opts.event}' is not part of the HTTP API`); return; } try { mesg = f(opts.body, true); } catch (error) { err = error; opts.cb(`invalid parameters '${err}'`); return; } if (mesg.event === 'query' && mesg.multi_response) { otps.cb("multi_response queries aren't supported"); return; } // client often expects id to be defined. if (mesg.id == null) { mesg.id = misc.uuid(); } client = resp = void 0; return async.series([ function(cb) { return get_client({ api_key: opts.api_key, logger: opts.logger, database: opts.database, compute_server: opts.compute_server, ip_address: opts.ip_address, cb: function(err, c) { client = c; return cb(err); } }); }, function(cb) { return handle_message({ client: client, mesg: mesg, logger: opts.logger, cb: function(err, r) { resp = r; return cb(err); } }); } ], function(err) { return opts.cb(err, resp); }); }; get_client = function(opts) { var account_id, dbg; opts = defaults(opts, { api_key: required, logger: required, database: required, compute_server: required, ip_address: required, cb: required }); dbg = log('get_client', opts.logger); account_id = auth_cache.get(opts.api_key); return async.series([ function(cb) { if (account_id != null) { return cb(); } else { return opts.database.get_account_with_api_key({ api_key: opts.api_key, cb: function(err, a) { if (err) { cb(err); return; } if (a == null) { cb("No account found. Is your API key wrong?"); return; } // we got an account id associated with the given api key account_id = a; // briefly cache api key. see "expire" time in ms above. auth_cache.set(opts.api_key, account_id); return cb(); } }); } }, function(cb) { // check if user is banned: return opts.database.is_banned_user({ account_id: account_id, cb: function(err, is_banned) { if (err) { cb(err); return; } if (is_banned) { cb(`User is BANNED. If this is a mistake, please contact ${HELP_EMAIL}`); return; } return cb(); } }); } ], function(err) { var client, options; if (err) { opts.cb(err); return; } options = { logger: opts.logger, database: opts.database, compute_server: opts.compute_server }; client = new Client(options); client.push_to_client = (mesg, cb) => { client.emit('push_to_client', mesg); return typeof cb === "function" ? cb() : void 0; }; client.ip_address = opts.ip_address; client.account_id = account_id; return opts.cb(void 0, client); }); }; handle_message = function(opts) { var dbg, f, name; opts = defaults(opts, { mesg: required, client: required, logger: void 0, cb: required }); dbg = log('handle_message', opts.logger); dbg(`${JSON.stringify(opts.mesg)}, ${opts.client.id}`); name = `mesg_${opts.mesg.event}`; f = opts.client[name]; if (f == null) { opts.cb(`unknown message event type '${opts.mesg.event}'`); return; } opts.client.once('push_to_client', function(mesg) { return opts.cb(void 0, mesg); }); return f(opts.mesg); }; }).call(this); //# sourceMappingURL=handler.js.map