smc-hub
Version:
CoCalc: Backend webserver component
109 lines (99 loc) • 2.95 kB
JavaScript
// 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
//########################################################################
/*
User management of their API key.
(c) SageMath, Inc. LGPLv3
*/
var async, defaults, is_password_correct, message, misc, required;
async = require('async');
misc = require('smc-util/misc');
message = require('smc-util/message'); // salvus message protocol
({defaults, required} = misc);
({is_password_correct} = require('../auth'));
exports.api_key_action = function(opts) {
var api_key;
opts = defaults(opts, {
database: required,
account_id: void 0,
password: void 0,
passport: void 0,
action: void 0, // 'get', 'delete', 'regenerate'
cb: required
});
if ((opts.password == null) && !opts.passport) {
opts.cb("password must be given if passport is not true");
return;
}
if (opts.action == null) {
opts.cb("action must be given");
return;
}
if (opts.account_id == null) {
opts.cb("must be signed in");
return;
}
api_key = void 0;
return async.series([
function(cb) {
if (opts.passport) {
// user already authenticated using passport.
cb();
return;
}
return is_password_correct({
database: opts.database,
password: opts.password,
account_id: opts.account_id,
allow_empty_password: false,
cb: function(err,
is_correct) {
if (err != null) {
return cb(err);
} else if (!is_correct) {
return cb("password is invalid");
} else {
return cb(); // auth failed
}
}
});
},
function(cb) {
// do the action
switch (opts.action) {
case 'get':
return opts.database.get_api_key({
account_id: opts.account_id,
cb: function(err,
x) {
api_key = x;
return cb(err);
}
});
case 'delete':
return opts.database.delete_api_key({
account_id: opts.account_id,
cb: cb
});
case 'regenerate':
return opts.database.regenerate_api_key({
account_id: opts.account_id,
cb: function(err,
x) {
api_key = x;
return cb(err);
}
});
default:
return cb(`unknown action '${opts.action}'`);
}
}
], function(err) {
return opts.cb(err, api_key);
});
};
}).call(this);
//# sourceMappingURL=manage.js.map