X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Ffacebookservice%2Ffacebookauthentication.cpp;h=431c4121d3a12864d3f4eacfcfe156f3750c0705;hb=e176c7696df20f5206da23fa95ebcc1a0a24b0aa;hp=1eefd6fbf8b1aed8c916886e0f4346630f36ede4;hpb=499cadb635846333c7c6300e0ffced23ed62fbae;p=situare diff --git a/src/facebookservice/facebookauthentication.cpp b/src/facebookservice/facebookauthentication.cpp index 1eefd6f..431c412 100644 --- a/src/facebookservice/facebookauthentication.cpp +++ b/src/facebookservice/facebookauthentication.cpp @@ -21,183 +21,198 @@ USA. */ +#include + #include #include +#include #include #include #include +#include #ifdef Q_WS_MAEMO_5 #include #endif // Q_WS_MAEMO_5 +#include "common.h" +#include "../error.h" +#include "network/networkcookiejar.h" +#include "situareservice/situarecommon.h" +#include "ui/mainwindow.h" + #include "facebookauthentication.h" -#include "facebookcommon.h" -#include "../common.h" -#include "parser.h" -FacebookAuthentication::FacebookAuthentication(QObject *parent) - : QObject(parent), - m_loginAttempts(0) +const QString FB_LOGIN_SUCCESS_URL = "http://www.facebook.com/connect/login_success.html"; +const QString FB_LOGIN_URL = "https://www.facebook.com/login.php"; +FacebookAuthentication::FacebookAuthentication(MainWindow *mainWindow, QObject *parent) + : QObject(parent), + m_loggedIn(false), + m_browser(0), + m_mainWindow(mainWindow) { qDebug() << __PRETTY_FUNCTION__; +} +void FacebookAuthentication::browserDestroyed() +{ + qWarning() << __PRETTY_FUNCTION__; + + m_mainWindow->toggleProgressIndicator(false); + m_browser = 0; } -void FacebookAuthentication::clearAccountInformation(bool keepUsername) +void FacebookAuthentication::clearAccountInformation(bool clearUserInformation) { - qDebug() << __PRETTY_FUNCTION__; + qWarning() << __PRETTY_FUNCTION__ << "clearUserInformation:" << clearUserInformation; - m_loginCredentials.clearCredentials(); - QSettings settings(DIRECTORY_NAME, FILE_NAME); + QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME); - if(!keepUsername) - settings.remove(USERNAME); + settings.remove(USER_UNSEND_MESSAGE); + settings.remove(USER_UNSEND_MESSAGE_PUBLISH); - settings.remove(COOKIES); + if (clearUserInformation) { + NetworkCookieJar::clearCookiesSetting(); + settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED); + settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL); + } } -QUrl FacebookAuthentication::formLoginPageUrl(const QStringList &urlParts) const +void FacebookAuthentication::destroyLogin() { - qDebug() << __PRETTY_FUNCTION__; + qWarning() << __PRETTY_FUNCTION__; - return QUrl(urlParts.join(EMPTY)); + m_mainWindow->destroyLoginDialog(); + m_browser->deleteLater(); } -const QString FacebookAuthentication::loadUsername() +bool FacebookAuthentication::isLoggedIn() const { - qDebug() << __PRETTY_FUNCTION__; + qWarning() << __PRETTY_FUNCTION__; - QSettings settings(DIRECTORY_NAME, FILE_NAME); - return settings.value(USERNAME, EMPTY).toString(); + return m_loggedIn; } -FacebookCredentials FacebookAuthentication::loginCredentials() const +void FacebookAuthentication::login() { - qDebug() << __PRETTY_FUNCTION__; - return m_loginCredentials; -} + qWarning() << __PRETTY_FUNCTION__; -void FacebookAuthentication::saveUsername(const QString &username) -{ - qDebug() << __PRETTY_FUNCTION__; + if (!m_browser) { + m_browser = new QWebView(m_mainWindow); - QSettings settings(DIRECTORY_NAME, FILE_NAME); - settings.setValue(USERNAME, username); -} + if (m_browser) { + m_browser->page()->networkAccessManager()->setCookieJar(new NetworkCookieJar()); -void FacebookAuthentication::start() -{ - qDebug() << __PRETTY_FUNCTION__; + connect(m_browser, SIGNAL(urlChanged(QUrl)), + this, SLOT(urlChanged(QUrl))); - QStringList list; - list.append(FACEBOOK_LOGINBASE); - list.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY); - list.append(INTERVAL1); - list.append(SITUARE_LOGIN_SUCCESS); - list.append(INTERVAL2); - list.append(SITUARE_LOGIN_FAILURE); - list.append(FACEBOOK_LOGIN_ENDING); + connect(m_browser, SIGNAL(destroyed(QObject*)), + this, SLOT(browserDestroyed())); - QSettings settings(DIRECTORY_NAME, FILE_NAME); + connect(m_browser->page()->networkAccessManager(), + SIGNAL(sslErrors(QNetworkReply*, QList)), + this, SLOT(sslErrors(QNetworkReply*, QList))); - QString cookies = settings.value(COOKIES, EMPTY).toString(); - if(!cookies.isEmpty()) { - emit loginUsingCookies(); + connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)), + this, SLOT(networkReplyHandler(QNetworkReply*))); + } } - else { - emit newLoginRequest(formLoginPageUrl(list)); + + if (m_browser) { + QString url = FB_LOGIN_URL + "?"; + url.append("api_key=" + API_KEY +"&"); + url.append("display=touch&"); + url.append("fbconnect=1&"); + url.append("next=" + FB_LOGIN_SUCCESS_URL + "&"); + url.append("return_session=1&"); + url.append("session_version=3&"); + url.append("v=1.0&"); + url.append("req_perms=publish_stream"); + + m_browser->load(QUrl(url)); + + m_mainWindow->toggleProgressIndicator(true); } } -bool FacebookAuthentication::updateCredentials(const QUrl &url) +void FacebookAuthentication::logOut(bool clearUserInformation) { - qDebug() << __PRETTY_FUNCTION__; - - bool found = false; + qWarning() << __PRETTY_FUNCTION__; - if (url.isValid()){ - qDebug() << "url is valid"; - - QString callbackUrl = url.toString(); - qDebug() << "callbackUrl: " << callbackUrl.toAscii(); + clearAccountInformation(clearUserInformation); + m_loggedIn = false; + emit loggedOut(); +} - if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) { - qDebug() << "login success"; +void FacebookAuthentication::networkReplyHandler(QNetworkReply *reply) +{ + qWarning() <<__PRETTY_FUNCTION__; - // let's find out session credentials - if(callbackUrl.contains(SESSION_KEY)) { + if ((reply->error() != QNetworkReply::NoError) + && (reply->error() != QNetworkReply::OperationCanceledError)) { - QJson::Parser parser; - bool ok; + qCritical() << __PRETTY_FUNCTION__ << "error:" << reply->error() << reply->errorString(); + emit error(ErrorContext::NETWORK, reply->error()); + destroyLogin(); + } +} - // split string into string part and json part - QStringList list = url.toString().split("="); +QString FacebookAuthentication::parseSession(const QUrl &url) +{ + qWarning() << __PRETTY_FUNCTION__; - for(int i=0;i -1) && (end > -1)) + return urlString.mid(begin, end - begin + 1); + else + return QString(); +} - qDebug() << "Expires" << result[EXPIRES].toString(); - m_loginCredentials.setExpires(result[EXPIRES].toString()); +void FacebookAuthentication::sslErrors(QNetworkReply *reply, const QList &errors) +{ + qWarning() << __PRETTY_FUNCTION__; - qDebug() << "Session Secret" << result[SESSION_SECRET].toString(); - m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString()); + Q_UNUSED(errors); + reply->ignoreSslErrors(); +} - qDebug() << "Signature" << result[SIGNATURE].toString(); - m_loginCredentials.setSig(result[SIGNATURE].toString()); - } - } - found = true; - emit saveCookiesRequest(); - } - emit credentialsReady(m_loginCredentials); - } - else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){ - qWarning() << "login failure" << endl; - qDebug() << callbackUrl; - ++m_loginAttempts; - /* emit loginFailure for every second login attemps, since webview loads login - error page (loadingDone() signal is emitted) and we need to avoid that because - at this point we don't have new login parameters */ - if(m_loginAttempts % 2) { - clearAccountInformation(true); - emit loginFailure(); - } - } - else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) { - qDebug() << "correct loginPage"; - } - else { - qDebug() << "totally wrong webPage"; - // we should not get a wrong page at this point - emit loginFailure(); +void FacebookAuthentication::urlChanged(const QUrl &url) +{ + qWarning() << __PRETTY_FUNCTION__ << url.toString(); + + /* + Redirects: + * Login with cookie failed: + 1) http://m.facebook.com/login.php?api_key=cf77865a5070f2c2ba3b52cbf3371579&cancel_url=http://www.facebook.com/connect/login_failure.html&display=touch&fbconnect=1&next=http://www.facebook.com/connect/uiserver.php?app_id=286811277465&next=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&display=touch&cancel_url=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_failure.html&perms=publish_stream&return_session=1&session_version=3&fbconnect=1&canvas=0&legacy_return=1&method=permissions.request&return_session=1&session_version=3&v=1.0&req_perms=publish_stream&app_id=286811277465&refsrc=http://www.facebook.com/login.php&fbb=ra985c5e9 + + * Login with cookie succeeded: + 1) http://www.facebook.com/connect/uiserver.php?app_id=286811277465&next=http://www.facebook.com/connect/login_success.html&display=touch&cancel_url=http://www.facebook.com/connect/login_failure.html&perms=publish_stream&return_session=1&session_version=3&fbconnect=1&canvas=0&legacy_return=1&method=permissions.request&session={"session_key":"2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973","uid":100001006647973,"expires":1289228400,"secret":"q4_Hn5qRdxnVT_qh3ztv5w__","sig":"c9d29ca857bacec48b952e7d2826a3ca"}&fbb=rb28f24e5 + 2) http://www.facebook.com/connect/login_success.html?perms=publish_stream&selected_profiles=100001006647973&session={"session_key":"2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973","uid":"100001006647973","expires":1289228400,"secret":"q4_Hn5qRdxnVT_qh3ztv5w__","access_token":"286811277465|2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973|LVTHGW82A98SGvv6Fl43DlCrFT0","sig":"8edd8d611047bcd162abbe9983b25a56"} + */ + + if (!url.toString().contains("session={")) { + // url parameter doesn't contain session data, so login with cookies failed + qWarning() << __PRETTY_FUNCTION__ << "working credentials required"; + m_mainWindow->buildLoginDialog(m_browser); + } else if (url.toString().startsWith(FB_LOGIN_SUCCESS_URL)) { + // login succeeded + const QString session = parseSession(url); + qWarning() << __PRETTY_FUNCTION__ << "login finished, parsed session:" << session; + if (!session.isEmpty()) { + destroyLogin(); + m_loggedIn = true; + emit loggedIn(session); } } else { - qDebug() << " Loading of page failed invalid URL" << endl; - // we should not get a wrong page at this point - emit loginFailure(); - return false; + qWarning() << __PRETTY_FUNCTION__ << "credentials accepted, getting the access_token"; } - return found; }