CartoDB-SQL-API/app/auth/auth_api.js
Raul Ochoa 480a9f27b4 New authentication mechanism: checks in advance if credentials are provided
in order to do a single request to redis to retrieve the required database
connection parameters.
2014-08-05 16:20:06 +02:00

35 lines
842 B
JavaScript

var ApiKeyAuth = require('./apikey'),
OAuthAuth = require('./oauth');
function AuthApi(req, requestParams) {
this.req = req;
this.authBacked = getAuthBackend(req, requestParams);
this._hasCredentials = null;
}
AuthApi.prototype.hasCredentials = function() {
if (this._hasCredentials === null) {
this._hasCredentials = this.authBacked.hasCredentials();
}
return this._hasCredentials;
};
AuthApi.prototype.verifyCredentials = function(options, callback) {
if (this.hasCredentials()) {
this.authBacked.verifyCredentials(options, callback);
} else {
callback(null, false);
}
};
function getAuthBackend(req, requestParams) {
if (requestParams.api_key) {
return new ApiKeyAuth(req);
} else {
return new OAuthAuth(req);
}
}
module.exports = AuthApi;