CartoDB-SQL-API/lib/auth/auth-api.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
var ApiKeyAuth = require('./apikey'),
OAuthAuth = require('./oauth');
function AuthApi(req, requestParams) {
this.req = req;
2018-05-24 17:04:14 +08:00
this.authBackend = getAuthBackend(req, requestParams);
this._hasCredentials = null;
}
AuthApi.prototype.getType = function () {
2018-05-24 17:04:14 +08:00
if (this.authBackend instanceof ApiKeyAuth) {
return 'apiKey';
2018-05-24 17:04:14 +08:00
} else if (this.authBackend instanceof OAuthAuth) {
return 'oAuth';
}
};
AuthApi.prototype.hasCredentials = function() {
if (this._hasCredentials === null) {
2018-05-24 17:04:14 +08:00
this._hasCredentials = this.authBackend.hasCredentials();
}
return this._hasCredentials;
};
AuthApi.prototype.getCredentials = function() {
2018-05-24 17:04:14 +08:00
return this.authBackend.getCredentials();
};
2018-02-27 02:02:05 +08:00
AuthApi.prototype.verifyCredentials = function(callback) {
if (this.hasCredentials()) {
2018-05-24 17:04:14 +08:00
this.authBackend.verifyCredentials(callback);
} else {
callback(null, false);
}
};
function getAuthBackend(req, requestParams) {
if (requestParams.api_key) {
return new ApiKeyAuth(req, requestParams.metadataBackend, requestParams.user, requestParams.api_key);
} else {
2018-02-27 02:02:05 +08:00
return new OAuthAuth(req, requestParams.metadataBackend);
}
}
module.exports = AuthApi;