2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-24 01:19:08 +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;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function UserDatabaseService (metadataBackend) {
|
2016-01-21 23:17:17 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
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.`;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function isOauthAuthorization ({ apikeyToken, authorizationLevel }) {
|
2018-06-05 19:21:56 +08:00
|
|
|
return (authorizationLevel === 'master') && !apikeyToken;
|
2018-05-28 21:53: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-06-05 19:21:56 +08:00
|
|
|
UserDatabaseService.prototype.getConnectionParams = function (username, apikeyToken, authorizationLevel, callback) {
|
2018-02-23 00:49:02 +08:00
|
|
|
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);
|
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
const commonDBConfiguration = {
|
2018-02-22 22:58:52 +08:00
|
|
|
port: global.settings.db_port,
|
2018-05-25 23:23:24 +08:00
|
|
|
host: dbParams.dbhost,
|
2019-12-24 01:19:08 +08:00
|
|
|
dbname: dbParams.dbname
|
2018-02-22 22:58:52 +08:00
|
|
|
};
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
this.metadataBackend.getMasterApikey(username, (err, masterApikey) => {
|
2015-12-07 16:40:51 +08:00
|
|
|
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);
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
if (!isApiKeyFound(masterApikey)) {
|
2018-05-25 23:23:24 +08:00
|
|
|
const apiKeyNotFoundError = new Error('Unauthorized');
|
|
|
|
apiKeyNotFoundError.type = 'auth';
|
|
|
|
apiKeyNotFoundError.subtype = 'api-key-not-found';
|
|
|
|
apiKeyNotFoundError.http_status = 401;
|
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
return callback(apiKeyNotFoundError);
|
2018-02-22 22:58:52 +08:00
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
const masterDBConfiguration = Object.assign({
|
|
|
|
user: masterApikey.databaseRole,
|
|
|
|
pass: masterApikey.databasePassword
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
commonDBConfiguration);
|
2018-05-28 21:53:51 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if (isOauthAuthorization({ apikeyToken, authorizationLevel })) {
|
2018-06-13 19:17:01 +08:00
|
|
|
return callback(null, masterDBConfiguration, masterDBConfiguration);
|
2018-05-28 21:53:51 +08:00
|
|
|
}
|
2018-02-22 18:46:34 +08:00
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
// Default Api key fallback
|
|
|
|
apikeyToken = apikeyToken || 'default_public';
|
|
|
|
|
|
|
|
this.metadataBackend.getApikey(username, apikeyToken, (err, apikey) => {
|
2018-05-25 23:23:24 +08:00
|
|
|
if (err) {
|
|
|
|
err.http_status = 404;
|
|
|
|
err.message = errorUserNotFoundMessageTemplate(username);
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isApiKeyFound(apikey)) {
|
|
|
|
const apiKeyNotFoundError = new Error('Unauthorized');
|
|
|
|
apiKeyNotFoundError.type = 'auth';
|
|
|
|
apiKeyNotFoundError.subtype = 'api-key-not-found';
|
|
|
|
apiKeyNotFoundError.http_status = 401;
|
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
return callback(apiKeyNotFoundError);
|
2018-05-25 23:23:24 +08:00
|
|
|
}
|
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
const DBConfiguration = Object.assign({
|
|
|
|
user: apikey.databaseRole,
|
|
|
|
pass: apikey.databasePassword
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
commonDBConfiguration);
|
2018-05-25 23:23:24 +08:00
|
|
|
|
2018-05-28 21:53:51 +08:00
|
|
|
callback(null, DBConfiguration, masterDBConfiguration);
|
2018-05-25 23:23:24 +08:00
|
|
|
});
|
2018-02-22 22:58:52 +08:00
|
|
|
});
|
2018-02-22 18:46:34 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-10 22:06:25 +08:00
|
|
|
module.exports = UserDatabaseService;
|