CartoDB-SQL-API/app/services/user_database_service.js

90 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-02-23 00:35:03 +08:00
const _ = require('underscore');
function isApiKeyFound(apikey) {
return apikey.type !== null &&
apikey.user !== null &&
apikey.databasePassword !== null &&
apikey.databaseRole !== null;
}
function UserDatabaseService(metadataBackend) {
this.metadataBackend = metadataBackend;
}
2018-02-23 00:49:02 +08:00
function errorUserNotFoundMessageTemplate (user) {
return `Sorry, we can't find CARTO user '${user}'. Please check that you have entered the correct domain.`;
}
/**
* 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)
*/
2018-02-23 00:49:02 +08:00
UserDatabaseService.prototype.getConnectionParams = function (username, apikeyToken, authenticated, callback) {
this.metadataBackend.getAllUserDBParams(username, (err, dbParams) => {
2018-02-22 22:58:52 +08:00
if (err) {
err.http_status = 404;
2018-02-23 00:49:02 +08:00
err.message = errorUserNotFoundMessageTemplate(username);
2018-02-22 22:58:52 +08:00
return callback(err);
}
2018-02-22 22:58:52 +08:00
const dbopts = {
port: global.settings.db_port,
pass: global.settings.db_pubuser_pass
};
2018-02-22 22:58:52 +08:00
dbopts.host = dbParams.dbhost;
dbopts.dbname = dbParams.dbname;
dbopts.user = (!!dbParams.dbpublicuser) ? dbParams.dbpublicuser : global.settings.db_pubuser;
2018-02-23 00:35:03 +08:00
const user = _.template(global.settings.db_user, {user_id: dbParams.dbuser});
let pass = null;
2018-02-22 22:58:52 +08:00
if (global.settings.hasOwnProperty('db_user_pass')) {
pass = _.template(global.settings.db_user_pass, {
user_id: dbParams.dbuser,
user_password: dbParams.dbpass
});
2018-02-22 22:58:52 +08:00
}
2018-02-23 00:35:03 +08:00
if (authenticated) {
2018-02-22 22:58:52 +08:00
dbopts.user = user;
dbopts.pass = pass;
}
2018-02-22 22:58:52 +08:00
let authDbOpts = _.defaults({ user: user, pass: pass }, dbopts);
2018-02-22 22:58:52 +08:00
if (!apikeyToken) {
return callback(null, dbopts, authDbOpts);
}
2018-02-23 00:49:02 +08:00
this.metadataBackend.getApikey(username, apikeyToken, (err, apikey) => {
if (err) {
2018-02-22 22:58:52 +08:00
err.http_status = 404;
2018-02-23 00:49:02 +08:00
err.message = errorUserNotFoundMessageTemplate(username);
return callback(err);
}
2018-02-22 22:58:52 +08:00
if (!isApiKeyFound(apikey)) {
return callback(null, dbopts, authDbOpts);
}
2018-02-22 22:58:52 +08:00
dbopts.user = apikey.databaseRole;
dbopts.pass = apikey.databasePassword;
2018-02-22 22:58:52 +08:00
authDbOpts = _.defaults({ user: user, pass: pass }, dbopts);
2018-02-22 22:58:52 +08:00
callback(null, dbopts, authDbOpts);
});
});
};
2015-12-10 22:06:25 +08:00
module.exports = UserDatabaseService;