2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2011-06-13 11:23:02 +08:00
|
|
|
// too bound to the request object, but ok for now
|
2015-05-13 00:00:30 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var OAuthUtil = require('oauth-client');
|
|
|
|
var step = require('step');
|
2019-10-04 00:24:39 +08:00
|
|
|
var CdbRequest = require('../models/cartodb-request');
|
2016-07-07 20:20:36 +08:00
|
|
|
var cdbReq = new CdbRequest();
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2015-05-13 00:00:30 +08:00
|
|
|
var oAuth = (function(){
|
2011-08-18 00:27:45 +08:00
|
|
|
var me = {
|
|
|
|
oauth_database: 3,
|
2012-06-30 07:54:21 +08:00
|
|
|
oauth_user_key: "rails:oauth_access_tokens:<%= oauth_access_key %>",
|
|
|
|
is_oauth_request: true
|
2011-08-22 20:33:12 +08:00
|
|
|
};
|
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
// oauth token cases:
|
|
|
|
// * in GET request
|
2011-08-22 20:33:12 +08:00
|
|
|
// * in header
|
2011-08-18 00:27:45 +08:00
|
|
|
me.parseTokens = function(req){
|
2015-05-13 00:00:30 +08:00
|
|
|
var query_oauth = _.clone(req.method === "POST" ? req.body: req.query);
|
2011-08-18 00:27:45 +08:00
|
|
|
var header_oauth = {};
|
2011-08-22 20:33:12 +08:00
|
|
|
var oauth_variables = ['oauth_body_hash',
|
|
|
|
'oauth_consumer_key',
|
|
|
|
'oauth_token',
|
|
|
|
'oauth_signature_method',
|
2011-08-18 00:27:45 +08:00
|
|
|
'oauth_signature',
|
|
|
|
'oauth_timestamp',
|
|
|
|
'oauth_nonce',
|
|
|
|
'oauth_version'];
|
|
|
|
|
2011-08-22 20:33:12 +08:00
|
|
|
// pull only oauth tokens out of query
|
|
|
|
var non_oauth = _.difference(_.keys(query_oauth), oauth_variables);
|
|
|
|
_.each(non_oauth, function(key){ delete query_oauth[key]; });
|
2011-08-18 00:27:45 +08:00
|
|
|
|
|
|
|
// pull oauth tokens out of header
|
|
|
|
var header_string = req.headers.authorization;
|
2011-08-22 20:33:12 +08:00
|
|
|
if (!_.isUndefined(header_string)) {
|
2011-08-18 00:27:45 +08:00
|
|
|
_.each(oauth_variables, function(oauth_key){
|
2015-05-13 00:00:30 +08:00
|
|
|
var matched_string = header_string.match(new RegExp(oauth_key + '=\"([^\"]+)\"'));
|
|
|
|
if (!_.isNull(matched_string)) {
|
2011-08-18 00:27:45 +08:00
|
|
|
header_oauth[oauth_key] = decodeURIComponent(matched_string[1]);
|
2015-05-13 00:00:30 +08:00
|
|
|
}
|
2011-08-18 00:27:45 +08:00
|
|
|
});
|
2011-08-22 20:33:12 +08:00
|
|
|
}
|
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
//merge header and query oauth tokens. preference given to header oauth
|
2011-08-22 20:33:12 +08:00
|
|
|
return _.defaults(header_oauth, query_oauth);
|
|
|
|
};
|
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
// remove oauthy tokens from an object
|
|
|
|
me.splitParams = function(obj) {
|
2011-12-26 19:51:15 +08:00
|
|
|
var removed = null;
|
|
|
|
for (var prop in obj) {
|
|
|
|
if (/^oauth_\w+$/.test(prop)) {
|
|
|
|
if(!removed) {
|
|
|
|
removed = {};
|
|
|
|
}
|
|
|
|
removed[prop] = obj[prop];
|
|
|
|
delete obj[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
2011-08-22 20:33:12 +08:00
|
|
|
};
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2016-07-07 20:20:36 +08:00
|
|
|
me.getAllowedHosts= function() {
|
|
|
|
var oauthConfig = global.settings.oauth || {};
|
|
|
|
return oauthConfig.allowedHosts || ['carto.com', 'cartodb.com'];
|
|
|
|
};
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
// do new fancy get User ID
|
2014-08-06 18:51:55 +08:00
|
|
|
me.verifyRequest = function(req, metadataBackend, callback) {
|
2011-08-18 00:27:45 +08:00
|
|
|
var that = this;
|
2011-12-26 19:51:15 +08:00
|
|
|
//TODO: review this
|
2014-08-06 18:51:55 +08:00
|
|
|
var httpProto = req.protocol;
|
2016-07-07 20:20:36 +08:00
|
|
|
if(!httpProto || (httpProto !== 'http' && httpProto !== 'https')) {
|
|
|
|
var msg = "Unknown HTTP protocol " + httpProto + ".";
|
|
|
|
var unknownProtocolErr = new Error(msg);
|
|
|
|
unknownProtocolErr.http_status = 500;
|
|
|
|
return callback(unknownProtocolErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
var username = cdbReq.userByReq(req);
|
|
|
|
var requestTokens;
|
2011-08-18 00:27:45 +08:00
|
|
|
var signature;
|
2011-08-22 20:33:12 +08:00
|
|
|
|
2015-05-13 00:00:30 +08:00
|
|
|
step(
|
2011-08-18 00:27:45 +08:00
|
|
|
function getTokensFromURL(){
|
|
|
|
return oAuth.parseTokens(req);
|
|
|
|
},
|
2016-07-07 20:20:36 +08:00
|
|
|
function getOAuthHash(err, _requestTokens) {
|
2018-11-08 01:05:39 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2012-06-30 07:54:21 +08:00
|
|
|
|
|
|
|
// this is oauth request only if oauth headers are present
|
2016-07-07 20:20:36 +08:00
|
|
|
this.is_oauth_request = !_.isEmpty(_requestTokens);
|
2012-06-30 07:54:21 +08:00
|
|
|
|
|
|
|
if (this.is_oauth_request) {
|
2016-07-07 20:20:36 +08:00
|
|
|
requestTokens = _requestTokens;
|
|
|
|
that.getOAuthHash(metadataBackend, requestTokens.oauth_token, this);
|
2012-06-30 07:54:21 +08:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-08-18 00:27:45 +08:00
|
|
|
},
|
2016-07-07 20:20:36 +08:00
|
|
|
function regenerateSignature(err, oAuthHash){
|
2018-11-08 01:05:39 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-05-13 00:00:30 +08:00
|
|
|
if (!this.is_oauth_request) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-30 07:54:21 +08:00
|
|
|
|
2016-07-07 20:20:36 +08:00
|
|
|
var consumer = OAuthUtil.createConsumer(oAuthHash.consumer_key, oAuthHash.consumer_secret);
|
|
|
|
var access_token = OAuthUtil.createToken(oAuthHash.access_token_token, oAuthHash.access_token_secret);
|
2011-08-18 00:27:45 +08:00
|
|
|
var signer = OAuthUtil.createHmac(consumer, access_token);
|
2011-08-22 20:33:12 +08:00
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
var method = req.method;
|
2016-07-07 20:20:36 +08:00
|
|
|
var hostsToValidate = {};
|
|
|
|
var requestHost = req.headers.host;
|
|
|
|
hostsToValidate[requestHost] = true;
|
|
|
|
that.getAllowedHosts().forEach(function(allowedHost) {
|
|
|
|
hostsToValidate[username + '.' + allowedHost] = true;
|
|
|
|
});
|
2011-08-18 00:27:45 +08:00
|
|
|
|
2016-07-07 20:20:36 +08:00
|
|
|
that.splitParams(req.query);
|
2011-12-26 19:51:15 +08:00
|
|
|
// remove oauth_signature from body
|
|
|
|
if(req.body) {
|
2015-05-13 00:00:30 +08:00
|
|
|
delete req.body.oauth_signature;
|
2011-12-26 19:51:15 +08:00
|
|
|
}
|
2016-07-07 20:20:36 +08:00
|
|
|
signature = requestTokens.oauth_signature;
|
|
|
|
// remove signature from requestTokens
|
|
|
|
delete requestTokens.oauth_signature;
|
|
|
|
var requestParams = _.extend({}, req.body, requestTokens, req.query);
|
|
|
|
|
|
|
|
var hosts = Object.keys(hostsToValidate);
|
|
|
|
var requestSignatures = hosts.map(function(host) {
|
|
|
|
var url = httpProto + '://' + host + req.path;
|
|
|
|
return signer.sign(method, url, requestParams);
|
|
|
|
});
|
|
|
|
|
|
|
|
return requestSignatures.reduce(function(validSignature, requestSignature) {
|
|
|
|
if (signature === requestSignature && !_.isUndefined(requestSignature)) {
|
|
|
|
validSignature = true;
|
|
|
|
}
|
|
|
|
return validSignature;
|
|
|
|
}, false);
|
2011-08-18 00:27:45 +08:00
|
|
|
},
|
2016-07-07 20:20:36 +08:00
|
|
|
function finishValidation(err, hasValidSignature) {
|
2018-06-05 19:21:56 +08:00
|
|
|
const authorizationLevel = hasValidSignature ? 'master' : null;
|
|
|
|
return callback(err, authorizationLevel);
|
2011-08-18 00:27:45 +08:00
|
|
|
}
|
|
|
|
);
|
2011-08-22 20:33:12 +08:00
|
|
|
};
|
|
|
|
|
2014-08-06 18:51:55 +08:00
|
|
|
me.getOAuthHash = function(metadataBackend, oAuthAccessKey, callback){
|
|
|
|
metadataBackend.getOAuthHash(oAuthAccessKey, callback);
|
2011-08-18 00:27:45 +08:00
|
|
|
};
|
2011-08-22 20:33:12 +08:00
|
|
|
|
2011-06-13 11:23:02 +08:00
|
|
|
return me;
|
2015-05-13 00:00:30 +08:00
|
|
|
})();
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2018-02-27 02:02:05 +08:00
|
|
|
function OAuthAuth(req, metadataBackend) {
|
2014-08-05 22:20:06 +08:00
|
|
|
this.req = req;
|
2018-02-27 02:02:05 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2014-08-05 22:20:06 +08:00
|
|
|
this.isOAuthRequest = null;
|
|
|
|
}
|
|
|
|
|
2018-02-27 02:02:05 +08:00
|
|
|
OAuthAuth.prototype.verifyCredentials = function(callback) {
|
2014-08-05 22:20:06 +08:00
|
|
|
if (this.hasCredentials()) {
|
2018-02-27 02:02:05 +08:00
|
|
|
oAuth.verifyRequest(this.req, this.metadataBackend, callback);
|
2014-08-05 22:20:06 +08:00
|
|
|
} else {
|
|
|
|
callback(null, false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-25 00:57:54 +08:00
|
|
|
OAuthAuth.prototype.getCredentials = function() {
|
|
|
|
return oAuth.parseTokens(this.req);
|
|
|
|
};
|
|
|
|
|
2014-08-05 22:20:06 +08:00
|
|
|
OAuthAuth.prototype.hasCredentials = function() {
|
|
|
|
if (this.isOAuthRequest === null) {
|
|
|
|
var passed_tokens = oAuth.parseTokens(this.req);
|
|
|
|
this.isOAuthRequest = !_.isEmpty(passed_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isOAuthRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = OAuthAuth;
|
|
|
|
module.exports.backend = oAuth;
|