2015-12-07 16:40:51 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var step = require('step');
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
2018-02-14 23:22:36 +08:00
|
|
|
function isApiKeyFound(apikey) {
|
2018-02-12 23:41:35 +08:00
|
|
|
return apikey.type !== null &&
|
|
|
|
apikey.user !== null &&
|
|
|
|
apikey.databasePassword !== null &&
|
|
|
|
apikey.databaseRole !== null;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
*/
|
2018-02-22 18:46:34 +08:00
|
|
|
UserDatabaseService.prototype.getConnectionParams = function (cdbUsername, apikeyToken, isAuthenticated, callback) {
|
2016-01-21 23:17:17 +08:00
|
|
|
var self = this;
|
2015-12-07 16:40:51 +08:00
|
|
|
|
|
|
|
var dbopts = {
|
|
|
|
port: global.settings.db_port,
|
|
|
|
pass: global.settings.db_pubuser_pass
|
|
|
|
};
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getDatabaseConnectionParams() {
|
2016-01-28 21:38:02 +08:00
|
|
|
self.metadataBackend.getAllUserDBParams(cdbUsername, this);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2018-02-14 23:22:36 +08:00
|
|
|
function getApiKey (err, dbParams) {
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:22:36 +08:00
|
|
|
const next = this;
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-02-22 18:46:34 +08:00
|
|
|
if (apikeyToken === undefined) {
|
2018-02-14 23:22:36 +08:00
|
|
|
return next(null, dbopts, dbParams);
|
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-02-22 18:46:34 +08:00
|
|
|
self.getApiKey(cdbUsername, apikeyToken, function (err, apikey) {
|
2018-02-14 23:22:36 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-02-22 18:46:34 +08:00
|
|
|
if (!apikey) {
|
2018-02-14 23:22:36 +08:00
|
|
|
return next(null, dbopts, dbParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, dbopts, dbParams, apikey);
|
|
|
|
});
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2018-02-22 18:46:34 +08:00
|
|
|
function setDBAuth(err, dbopts, dbParams, apikey) {
|
|
|
|
const next = this;
|
2017-08-09 18:50:16 +08:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:22:36 +08:00
|
|
|
dbopts.host = dbParams.dbhost;
|
|
|
|
dbopts.dbname = dbParams.dbname;
|
|
|
|
dbopts.user = (!!dbParams.dbpublicuser) ? dbParams.dbpublicuser : global.settings.db_pubuser;
|
|
|
|
|
2016-01-28 21:38:02 +08:00
|
|
|
var user = _.template(global.settings.db_user, {user_id: dbParams.dbuser});
|
|
|
|
var pass = null;
|
2018-02-14 23:22:36 +08:00
|
|
|
|
2016-01-28 21:38:02 +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-14 23:22:36 +08:00
|
|
|
if (isAuthenticated) {
|
|
|
|
if (apikey) {
|
|
|
|
dbopts.user = apikey.databaseRole;
|
|
|
|
dbopts.pass = apikey.databasePassword;
|
|
|
|
} else {
|
|
|
|
dbopts.user = user;
|
|
|
|
dbopts.pass = pass;
|
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:22:36 +08:00
|
|
|
var authDbOpts = _.defaults({ user: user, pass: pass }, dbopts);
|
2016-01-28 21:38:02 +08:00
|
|
|
|
2018-02-22 19:45:55 +08:00
|
|
|
return next(null, dbopts, authDbOpts);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2018-02-22 19:45:55 +08:00
|
|
|
function errorHandle(err, dbopts, authDbOpts) {
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-02-22 19:45:55 +08:00
|
|
|
callback(null, dbopts, authDbOpts);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-02-22 18:46:34 +08:00
|
|
|
UserDatabaseService.prototype.getApiKey = function (cdbUsername, apikeyToken, callback) {
|
|
|
|
this.metadataBackend.getApikey(cdbUsername, apikeyToken, (err, apikey) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isApiKeyFound(apikey)) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, apikey);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-10 22:06:25 +08:00
|
|
|
module.exports = UserDatabaseService;
|