2015-12-07 16:40:51 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var step = require('step');
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
2016-01-21 23:17:17 +08:00
|
|
|
function UserDatabaseService(metadataBackend) {
|
|
|
|
this.metadataBackend = metadataBackend;
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
2016-01-28 21:25:03 +08:00
|
|
|
/**
|
|
|
|
* Callback is invoked with `dbParams` and `authDbParams`.
|
|
|
|
* `dbParams` depends on AuthApi verification so it might return a public user with just SELECT permission, where
|
|
|
|
* `authDbParams` will always return connection params as AuthApi had authorized the connection.
|
|
|
|
* That might be useful when you have to run a query with and without permissions.
|
|
|
|
*
|
|
|
|
* @param {AuthApi} authApi
|
|
|
|
* @param {String} cdbUsername
|
|
|
|
* @param {Function} callback (err, dbParams, authDbParams)
|
|
|
|
*/
|
2016-01-28 21:14:31 +08:00
|
|
|
UserDatabaseService.prototype.getConnectionParams = function (authApi, cdbUsername, callback) {
|
2016-01-21 23:17:17 +08:00
|
|
|
var self = this;
|
2015-12-07 16:40:51 +08:00
|
|
|
|
|
|
|
var dbParams;
|
|
|
|
var dbopts = {
|
|
|
|
port: global.settings.db_port,
|
|
|
|
pass: global.settings.db_pubuser_pass
|
|
|
|
};
|
|
|
|
|
|
|
|
// 1. Get database from redis via the username stored in the host header subdomain
|
|
|
|
// 2. Run the request through OAuth to get R/W user id if signed
|
|
|
|
// 3. Set to user authorization params
|
|
|
|
step(
|
|
|
|
function getDatabaseConnectionParams() {
|
2016-01-28 21:38:02 +08:00
|
|
|
self.metadataBackend.getAllUserDBParams(cdbUsername, this);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
|
|
|
function authenticate(err, userDBParams) {
|
2016-01-21 23:17:17 +08:00
|
|
|
var next = this;
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
err.http_status = 404;
|
|
|
|
err.message = "Sorry, we can't find CartoDB user '" + cdbUsername + "'. " +
|
|
|
|
"Please check that you have entered the correct domain.";
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbParams = userDBParams;
|
|
|
|
|
|
|
|
dbopts.host = dbParams.dbhost;
|
|
|
|
dbopts.dbname = dbParams.dbname;
|
|
|
|
dbopts.user = (!!dbParams.dbpublicuser) ? dbParams.dbpublicuser : global.settings.db_pubuser;
|
|
|
|
|
|
|
|
authApi.verifyCredentials({
|
2016-01-21 23:17:17 +08:00
|
|
|
metadataBackend: self.metadataBackend,
|
2015-12-07 16:40:51 +08:00
|
|
|
apiKey: dbParams.apikey
|
2016-01-21 23:17:17 +08:00
|
|
|
}, next);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2017-08-09 18:50:16 +08:00
|
|
|
function getUserLimits (err, isAuthenticated) {
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.metadataBackend.getUserTimeoutRenderLimits(cdbUsername, function (err, timeoutRenderLimit) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var userLimits = {
|
|
|
|
timeout: isAuthenticated ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic
|
|
|
|
};
|
|
|
|
|
|
|
|
next(null, isAuthenticated, userLimits);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function setDBAuth(err, isAuthenticated, userLimits) {
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2016-01-28 21:38:02 +08:00
|
|
|
var user = _.template(global.settings.db_user, {user_id: dbParams.dbuser});
|
|
|
|
var pass = null;
|
|
|
|
if (global.settings.hasOwnProperty('db_user_pass')) {
|
|
|
|
pass = _.template(global.settings.db_user_pass, {
|
|
|
|
user_id: dbParams.dbuser,
|
|
|
|
user_password: dbParams.dbpass
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
if (_.isBoolean(isAuthenticated) && isAuthenticated) {
|
|
|
|
dbopts.authenticated = isAuthenticated;
|
2016-01-28 21:38:02 +08:00
|
|
|
dbopts.user = user;
|
|
|
|
dbopts.pass = pass;
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
2016-01-28 21:38:02 +08:00
|
|
|
var authDbOpts = _.defaults({user: user, pass: pass}, dbopts);
|
|
|
|
|
2017-08-09 18:50:16 +08:00
|
|
|
return this(null, dbopts, authDbOpts, userLimits);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2017-11-25 00:57:54 +08:00
|
|
|
function getApiKey (err, dbopts, authDbOpts, userLimits) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
const next = this;
|
|
|
|
|
|
|
|
if (authApi.getType() !== 'apiKey') {
|
|
|
|
return next(null, dbopts, authDbOpts, userLimits);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.metadataBackend.getApiKey(cdbUsername, authApi.getCredentials(), 'sql', (err, apiKey) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey) {
|
|
|
|
const unauthorizedError = new Error('permission denied');
|
|
|
|
unauthorizedError.http_status = 401;
|
|
|
|
|
|
|
|
return next(unauthorizedError);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey.grantsSql) {
|
|
|
|
const forbiddenError = new Error('forbidden');
|
|
|
|
forbiddenError.http_status = 403;
|
|
|
|
|
|
|
|
return next(forbiddenError);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiKey.type !== 'default') {
|
|
|
|
dbopts = _.extend(dbopts, { user: apiKey.dbRole, pass: apiKey.dbPassword });
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, dbopts, authDbOpts, userLimits);
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 18:50:16 +08:00
|
|
|
function errorHandle(err, dbopts, authDbOpts, userLimits) {
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2017-08-09 18:50:16 +08:00
|
|
|
callback(null, dbopts, authDbOpts, userLimits);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-12-10 22:06:25 +08:00
|
|
|
module.exports = UserDatabaseService;
|