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';
2019-12-24 01:19:08 +08:00
var ApiKeyAuth = require('./apikey');
var OAuthAuth = require('./oauth');
2019-12-24 01:19:08 +08:00
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';
}
};
2019-12-24 01:19:08 +08:00
AuthApi.prototype.hasCredentials = function () {
if (this._hasCredentials === null) {
2018-05-24 17:04:14 +08:00
this._hasCredentials = this.authBackend.hasCredentials();
}
return this._hasCredentials;
};
2019-12-24 01:19:08 +08:00
AuthApi.prototype.getCredentials = function () {
2018-05-24 17:04:14 +08:00
return this.authBackend.getCredentials();
};
2019-12-24 01:19:08 +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);
}
};
2019-12-24 01:19:08 +08:00
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;