diff --git a/backends/twitter/libtwitcurl/oauthlib.cpp b/backends/twitter/libtwitcurl/oauthlib.cpp index 2a10eea9c2c7d0c4ed9065c37cd7c23c242a04fc..d3403d428545dc6965a61858c988bfff3ee4029b 100644 --- a/backends/twitter/libtwitcurl/oauthlib.cpp +++ b/backends/twitter/libtwitcurl/oauthlib.cpp @@ -31,6 +31,31 @@ oAuth::~oAuth() { } +/*++ +* @method: oAuth::clone +* +* @description: creates a clone of oAuth object +* +* @input: none +* +* @output: cloned oAuth object +* +*--*/ +oAuth oAuth::clone() +{ + oAuth cloneObj; + cloneObj.m_consumerKey = m_consumerKey; + cloneObj.m_consumerSecret = m_consumerSecret; + cloneObj.m_oAuthTokenKey = m_oAuthTokenKey; + cloneObj.m_oAuthTokenSecret = m_oAuthTokenSecret; + cloneObj.m_oAuthPin = m_oAuthPin; + cloneObj.m_nonce = m_nonce; + cloneObj.m_timeStamp = m_timeStamp; + cloneObj.m_oAuthScreenName = m_oAuthScreenName; + return cloneObj; +} + + /*++ * @method: oAuth::getConsumerKey * @@ -238,6 +263,72 @@ void oAuth::generateNonceTimeStamp() m_timeStamp.assign( szTime ); } +/*++ +* @method: oAuth::buildOAuthRawDataKeyValPairs +* +* @description: this method prepares key-value pairs from the data part of the URL +* or from the URL post fields data, as required by OAuth header +* and signature generation. +* +* @input: rawData - Raw data either from the URL itself or from post fields. +* Should already be url encoded. +* urlencodeData - If true, string will be urlencoded before converting +* to key value pairs. +* +* @output: rawDataKeyValuePairs - Map in which key-value pairs are populated +* +* @remarks: internal method +* +*--*/ +void oAuth::buildOAuthRawDataKeyValPairs( const std::string& rawData, + bool urlencodeData, + oAuthKeyValuePairs& rawDataKeyValuePairs ) +{ + /* Raw data if it's present. Data should already be urlencoded once */ + if( rawData.length() ) + { + size_t nSep = std::string::npos; + size_t nPos = std::string::npos; + std::string dataKeyVal; + std::string dataKey; + std::string dataVal; + + /* This raw data part can contain many key value pairs: key1=value1&key2=value2&key3=value3 */ + std::string dataPart = rawData; + while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) ) + { + /* Extract first key=value pair */ + dataKeyVal = dataPart.substr( 0, nSep ); + + /* Split them */ + nPos = dataKeyVal.find_first_of( "=" ); + if( std::string::npos != nPos ) + { + dataKey = dataKeyVal.substr( 0, nPos ); + dataVal = dataKeyVal.substr( nPos + 1 ); + + /* Put this key=value pair in map */ + rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal; + } + dataPart = dataPart.substr( nSep + 1 ); + } + + /* For the last key=value */ + dataKeyVal = dataPart.substr( 0, nSep ); + + /* Split them */ + nPos = dataKeyVal.find_first_of( "=" ); + if( std::string::npos != nPos ) + { + dataKey = dataKeyVal.substr( 0, nPos ); + dataVal = dataKeyVal.substr( nPos + 1 ); + + /* Put this key=value pair in map */ + rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal; + } + } +} + /*++ * @method: oAuth::buildOAuthTokenKeyValuePairs * @@ -247,7 +338,6 @@ void oAuth::generateNonceTimeStamp() * @input: includeOAuthVerifierPin - flag to indicate whether oauth_verifer key-value * pair needs to be included. oauth_verifer is only * used during exchanging request token with access token. -* rawData - url encoded data. this is used during signature generation. * oauthSignature - base64 and url encoded OAuth signature. * generateTimestamp - If true, then generate new timestamp for nonce. * @@ -257,7 +347,6 @@ void oAuth::generateNonceTimeStamp() * *--*/ bool oAuth::buildOAuthTokenKeyValuePairs( const bool includeOAuthVerifierPin, - const std::string& rawData, const std::string& oauthSignature, oAuthKeyValuePairs& keyValueMap, const bool generateTimestamp ) @@ -301,21 +390,6 @@ bool oAuth::buildOAuthTokenKeyValuePairs( const bool includeOAuthVerifierPin, /* Version */ keyValueMap[oAuthLibDefaults::OAUTHLIB_VERSION_KEY] = std::string( "1.0" ); - /* Data if it's present */ - if( rawData.length() ) - { - /* Data should already be urlencoded once */ - std::string dummyStrKey; - std::string dummyStrValue; - size_t nPos = rawData.find_first_of( "=" ); - if( std::string::npos != nPos ) - { - dummyStrKey = rawData.substr( 0, nPos ); - dummyStrValue = rawData.substr( nPos + 1 ); - keyValueMap[dummyStrKey] = dummyStrValue; - } - } - return ( keyValueMap.size() ) ? true : false; } @@ -417,7 +491,7 @@ bool oAuth::getSignature( const eOAuthHttpRequestType eType, * * @input: eType - HTTP request type * rawUrl - raw url of the HTTP request -* rawData - HTTP data +* rawData - HTTP data (post fields) * includeOAuthVerifierPin - flag to indicate whether or not oauth_verifier needs to included * in OAuth header * @@ -450,53 +524,24 @@ bool oAuth::getOAuthHeader( const eOAuthHttpRequestType eType, /* Get only key=value data part */ std::string dataPart = rawUrl.substr( nPos + 1 ); - /* This dataPart can contain many key value pairs: key1=value1&key2=value2&key3=value3 */ - size_t nSep = std::string::npos; - size_t nPos2 = std::string::npos; - std::string dataKeyVal; - std::string dataKey; - std::string dataVal; - while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) ) - { - /* Extract first key=value pair */ - dataKeyVal = dataPart.substr( 0, nSep ); - - /* Split them */ - nPos2 = dataKeyVal.find_first_of( "=" ); - if( std::string::npos != nPos2 ) - { - dataKey = dataKeyVal.substr( 0, nPos2 ); - dataVal = dataKeyVal.substr( nPos2 + 1 ); - - /* Put this key=value pair in map */ - rawKeyValuePairs[dataKey] = urlencode( dataVal ); - } - dataPart = dataPart.substr( nSep + 1 ); - } - - /* For the last key=value */ - dataKeyVal = dataPart.substr( 0, nSep ); - - /* Split them */ - nPos2 = dataKeyVal.find_first_of( "=" ); - if( std::string::npos != nPos2 ) - { - dataKey = dataKeyVal.substr( 0, nPos2 ); - dataVal = dataKeyVal.substr( nPos2 + 1 ); - - /* Put this key=value pair in map */ - rawKeyValuePairs[dataKey] = urlencode( dataVal ); - } + /* Split the data in URL as key=value pairs */ + buildOAuthRawDataKeyValPairs( dataPart, true, rawKeyValuePairs ); } + /* Split the raw data if it's present, as key=value pairs. Data should already be urlencoded once */ + buildOAuthRawDataKeyValPairs( rawData, false, rawKeyValuePairs ); + /* Build key-value pairs needed for OAuth request token, without signature */ - buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, rawData, std::string( "" ), rawKeyValuePairs, true ); + buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, std::string( "" ), rawKeyValuePairs, true ); /* Get url encoded base64 signature using request type, url and parameters */ getSignature( eType, pureUrl, rawKeyValuePairs, oauthSignature ); + /* Clear map so that the parameters themselves are not sent along with the OAuth values */ + rawKeyValuePairs.clear(); + /* Now, again build key-value pairs with signature this time */ - buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, std::string( "" ), oauthSignature, rawKeyValuePairs, false ); + buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, oauthSignature, rawKeyValuePairs, false ); /* Get OAuth header in string format */ paramsSeperator = ",";