diff -r a180113055cb -r 83d6a149c755 example/TwitterAuthApp/src/requestSP.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/TwitterAuthApp/src/requestSP.cpp Mon Oct 11 21:59:54 2010 +0530 @@ -0,0 +1,567 @@ +/** +* Copyright (c) 2010 Sasken Communication Technologies Ltd. +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of the "{License}" +* which accompanies this distribution, and is available +* at the URL "{LicenseUrl}". +* +* Initial Contributors: +* Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution +* +* Contributors: +* Siddhartha Chandra, Sasken Communication Technologies Ltd +* Description: +* class to handle calls to rest Server API's +*/ + +#include "requestSP.h" +#include "sessionSP.h" +#include "xmlParser.h" +#include "errorCodes.h" + +#include +#include +#include +#include +#include +#include + +#include "hash.h" + +#include +#include +#include +/////////////////////////////////////////////////////////////////////////////////////////////////// +// global + +static const QString kAPIVersion = "1.0"; +static const QString kAPIFormat = "XML"; +static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +static bool caseInsensitiveLessThan(const QString &s1, const QString &s2) +{ + return s1.toLower() < s2.toLower(); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Static class functions + +FBRequest* FBRequest::request() +{ + return FBRequest::requestWithSession(FBSession::session()); +} + +FBRequest* FBRequest::requestWithSession (FBSession* aSession) +{ + FBRequest* request = new FBRequest(aSession); + return request; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// instance public functions +FBRequest::FBRequest(FBSession* aSession) : iSession ( aSession ), iNetworkAccessManager ( this ) +{} + +const QDateTime& FBRequest::timeStamp() const +{ + return iTimestamp; +} + +void FBRequest::connect() +{ + qDebug()<<"Inside FBRequest::connect()"; + //emit requestLoading(); + + /*QString url ; + url = iUrl; + qDebug()<<"url"<stroauth_nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + iSession->stroauth_timestamp + "\", oauth_consumer_key=\"" + kConsumerKey + "\", oauth_signature=\"" + iSession->stroauth_signature.toAscii().toPercentEncoding() + "\", oauth_version=\"1.0\""; + qDebug()<<"Authorization: "<show(); + proxysettings(); + QNetworkReply* reply = iNetworkAccessManager.post(request, postBody); + + QObject::connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); + QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), + this, SLOT(networkReplyError(QNetworkReply::NetworkError))); + + //} +} +void FBRequest::proxysettings() +{ +#ifdef EMULATORTESTING + qDebug()<<"proxysettings"; + + // Reading the keys, CSM Stubbed - START + QFile file("c:\\data\\DoNotShare.txt"); + if (!file.open(QIODevice::ReadOnly)) + { + qDebug()<<"File to read the windows username and password could not be opened, returning!!!"; + return; + } + + QByteArray arr = file.readAll(); + QList list = arr.split(' '); + file.close(); + + QString username(list[0]); + QString password(list[1]); + + QString httpProxy = "10.1.0.214";//ipwproxy.sasken.com + QString httpPort = "3128"; + + QString httpUser =username;/* This could be taken thru an QDialog implmentation to remove the Hard coding */ + QString httpPass =password;/* This could be taken thru an QDialog implmentation to remove the Hard coding */ + + /*==Classes used from Network Module==*/ + QNetworkProxy proxy; + + proxy.setType(QNetworkProxy::HttpProxy); + proxy.setHostName(httpProxy); + proxy.setPort(httpPort.toInt()); + proxy.setUser(httpUser); + proxy.setPassword(httpPass); + + QNetworkProxy::setApplicationProxy(proxy); +#endif +} +/////////////////////////////////////////////////////////////////////////////////////////////////// +// instance private functions +QString FBRequest::sha1() +{ + qDebug()<<"Insha1"; + CSHA1* sha=CSHA1::NewL(); + TBuf8<100> keyVal; + keyVal.Copy(_L8("sasken")); + CHMAC* hmac=CHMAC::NewL(keyVal,sha); + //TBuf8<1024> baseString; + TPtrC8 hashedSig(hmac->Hash(_L8("sasken"))); + TBuf8<1024>bufr; + bufr.Copy(hashedSig); + QByteArray digest = QByteArray::fromRawData((char*)bufr.Ptr(),bufr.Length()); + qDebug()<<"digest.toHex();"<bufr; + bufr.Copy(hashedSig); + QByteArray digest = QByteArray::fromRawData((char*)bufr.Ptr(),bufr.Length()); + qDebug()<<"digest.toHex();"<apiURL(); +} + +QString FBRequest::generateGetURL() const +{ + qDebug()<<"Inside FBRequest::generateGetURL()"; + const QUrl url(iUrl); + const QString queryPrefix = url.hasQuery() ? "&" : "?"; + + QStringList pairs; + DictionaryIterator i(iParams); + + while (i.hasNext()) { + i.next(); + pairs << i.key().toUtf8() + "=" + i.value().toUtf8(); + } + + return iUrl + queryPrefix + pairs.join("&"); +} + +QString FBRequest::generateCallId() const { + QDateTime dateTime = QDateTime::currentDateTime(); + uint secs = dateTime.toTime_t(); + QString result = QString::number(secs, 10); + return result; +} +/* + * + httpMethod + "&" + + url_encode( base_uri ) + "&" + + sorted_query_params.each { | k, v | + url_encode ( k ) + "%3D" + + url_encode ( v ) + }.join("%26") + * + */ +QString FBRequest::generateSig(Dictionary Params) +{ + QString joined; + joined = "POST"; + joined += "&"; + joined += kRequestTokenUrl.toAscii().toPercentEncoding() + "&"; + + QStringList keys = Params.keys(); + qSort(keys.begin(), keys.end(), caseInsensitiveLessThan); + QListIterator i(keys); + int count = 0; + while (i.hasNext()) + { + count++; + if(count > 1) + joined.append("%26"); + const QString key = i.next(); + joined.append(key.toAscii().toPercentEncoding()); + joined.append("%3D"); + joined.append(Params.value(key).toAscii().toPercentEncoding()); + } + qDebug()<<"joined"< sid(joined.utf16()); + + QByteArray digest1 = QByteArray::fromRawData((char*)sid.Ptr(),sid.Length()); + qDebug()<<"digest1: "<buff;//(sid); + buff.Copy(sid); + QByteArray digest2 = QByteArray::fromRawData((char*)buff.Ptr(),buff.Length()); + qDebug()<<"digest2: "<stroauth_TokenSecret.toUtf8().toPercentEncoding()); + qDebug()<<"Key:"< (key.constData()),key.size()); + + //Take a copy of the data + HBufC8* buffer12 = HBufC8::New(myDataDescriptor.Length()); + + Q_CHECK_PTR(buffer12); + buffer12->Des().Copy(myDataDescriptor ); + TPtr8 tptr = buffer12->Des(); + + TBuf8<100> keyVal; + keyVal.Copy(tptr); + + CHMAC* hmac=CHMAC::NewL(keyVal,sha); + + TPtrC8 hashedSig(hmac->Hash(buff)); + TBuf8<1024>bufr; + bufr.Copy(hashedSig); + QByteArray digest = QByteArray::fromRawData((char*)bufr.Ptr(),bufr.Length()); + qDebug()<<"digest.toHex() "<send(this); +} + +void FBRequest::cancel() +{ + qDebug()<<"Inside FBRequest::cancel()"; +} + + +void FBRequest::call (const QString& aMethod, const Dictionary& aParams) +{ + QByteArray dataParams; + callWithDataParams(aMethod, aParams, dataParams, false); + +} + +void FBRequest::callWithDataParams (const QString& aMethod, const Dictionary& aParams, const QByteArray& aDataParam, bool aDataParamPicture) +{ + Q_UNUSED(aParams) + Q_UNUSED(aDataParam) + Q_UNUSED(aDataParamPicture) + iUrl = urlForMethod(aMethod); + iUrl += aMethod; + qDebug()<<"URL inside call with Data Params"<stroauth_nonce; + iParams["oauth_signature_method"] = "HMAC-SHA1"; + iParams["oauth_timestamp"] = iSession->stroauth_timestamp; + iParams["oauth_consumer_key"] = kConsumerKey; + iParams["sig"]=iSession->stroauth_signature; + iParams["oauth_version"] = "1.0";*/ + //iDataParam = aDataParam; + // iDataParamPicture = aDataParamPicture; + + // iParams["method"] = iMethod; + // iParams["api_key"] = iSession->apiKey(); + // iParams["v"] = kAPIVersion; + // iParams["format"] = kAPIFormat; + + /* if (!isSpecialMethod()) + { + iParams["session_key"] = iSession->sessionKey(); + iParams["call_id"] = generateCallId(); + + if (iSession->sessionSecret().length()) + { + iParams["ss"] = "1"; + } + }*/ + + // XXX: workaround what seems to be a Qt bug with the extras-devel libraries. + // QString signature = generateSig(iParams); + // iParams["sig"] = signature; + // XXX: end workaround. + + iSession->send(this); +} + + +QVariant FBRequest::parseXMLResponse ( const QByteArray& aResponseData, FBError& aError) +{ + qDebug()<<"Inside FBRequest::handleResponseData()"; + QXmlInputSource input; + input.setData(aResponseData); + + FBXMLHandler handler; + QXmlSimpleReader parser; + parser.setContentHandler(&handler); + bool result = parser.parse(&input); + + QVariant rootObject = handler.rootObject(); + + if (handler.parseError() || !result) + { + aError.setCode( FBRESPONSE_PARSE_ERROR ); + aError.setDescription("parser was unable to parse the xml response from facebook server."); + + return QVariant(); + } + else if (handler.rootName().compare("error_response")==0) + { + QVariantHash errorDict = rootObject.toHash(); + + bool result; + int errorCode = errorDict.value("error_code").toInt(&result); + + aError.setCode( errorCode ); + aError.setDescription( errorDict.value("error_msg").toString() ); + + return rootObject; + } + else + { + return rootObject; + } + +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// instance provate slots +void FBRequest::networkReplyError ( QNetworkReply::NetworkError aCode ) +{ + emit requestFailedWithNetworkError(aCode ); +} + +void FBRequest::networkReplyFinished () +{ +qDebug()<<"Inside FBRequest::networkReplyFinished()"; + QNetworkReply* reply = static_cast ( sender() ); + QByteArray responseData = reply->readAll(); + if(responseData != "") + { + qDebug()<<"response data:"<MyString = responseData.split('&'); + QListtoken = MyString[0].split('='); + qDebug()<stroauth_Token=token.at(1); + QListtoken_secret = MyString[1].split('='); + qDebug()<stroauth_TokenSecret=token_secret.at(1); + + qDebug()<<"iSession->stroauth_Token"<stroauth_Token; + if(iSession->stroauth_Token != "") + { + emit LoadLoginPage(); + qDebug()<<"Signal:LoadLoginPage() is emitted"; + } + } +} +void FBRequest::networkReplyFinishedNew () +{ +qDebug()<<"Inside FBRequest::networkReplyFinishedNew()"; + QNetworkReply* reply = static_cast ( sender() ); + QByteArray responseData = reply->readAll(); + if(responseData != "") + { + qDebug()<<"response data:"<MyString = responseData.split('&'); + QListtoken = MyString[0].split('='); + qDebug()<stroauth_Token=token.at(1); + QListtoken_secret = MyString[1].split('='); + qDebug()<stroauth_TokenSecret=token_secret.at(1); + + qDebug()<<"iSession->stroauth_Token"<stroauth_Token; + qDebug()<<"iSession->stroauth_TokenSecret"<stroauth_TokenSecret; + iSession->save(); + } +} +void FBRequest::connect_req(QString uri) + { + qDebug()<<"Inside FBRequest::connect_req()"; + + QNetworkRequest request; + + QSslConfiguration config( QSslConfiguration::defaultConfiguration() ); + request.setSslConfiguration( config ); + QUrl myurl; + QString Authorization; + if(uri == "/request_token") + { + myurl = kRequestTokenUrl; + Authorization = "OAuth oauth_nonce=\"" + iSession->stroauth_nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + iSession->stroauth_timestamp + "\", oauth_consumer_key=\"" + kConsumerKey + "\", oauth_signature=\"" + iSession->stroauth_signature.toAscii().toPercentEncoding() + "\", oauth_version=\"1.0\""; + } + else + { + myurl = kAccessTokenUrl; + Authorization = "OAuth oauth_nonce=\"" + iSession->stroauth_nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + iSession->stroauth_timestamp + "\", oauth_consumer_key=\"" + kConsumerKey + "\", oauth_token=\"" + iSession->stroauth_Token + "\", oauth_verifier=\"" + iSession->stroauth_verifier + "\", oauth_signature=\"" + iSession->stroauth_signature.toAscii().toPercentEncoding() + "\", oauth_version=\"1.0\""; + } + request.setUrl(myurl); + + + qDebug()<<"myurl:"<show(); + proxysettings(); + QNetworkReply* reply = iNetworkAccessManager.post(request, postBody); + + QObject::connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinishedNew())); + QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(networkReplyError(QNetworkReply::NetworkError))); + + + }