Added error handling for invalid JSON string
[situare] / src / facebookservice / facebookauthentication.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Ville Tiensuu - ville.tiensuu@ixonos.com
6        Kaj Wallin - kaj.wallin@ixonos.com
7        Henri Lampela - henri.lampela@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include <QtDebug>
25 #include <QDateTime>
26 #include <QSettings>
27 #include <QStringList>
28 #include <QVariantMap>
29
30 #ifdef Q_WS_MAEMO_5
31 #include <QMaemo5InformationBox>
32 #endif // Q_WS_MAEMO_5
33
34 #include "facebookauthentication.h"
35 #include "facebookcommon.h"
36 #include "common.h"
37 #include "parser.h"
38
39 FacebookAuthentication::FacebookAuthentication(QObject *parent)
40     : QObject(parent),
41     m_freshLogin(false)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45 }
46
47 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     m_loginCredentials.clearCredentials();
52     QSettings settings(DIRECTORY_NAME, FILE_NAME);
53
54     if(!keepUsername) {
55         settings.remove(USERNAME);
56         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
57         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
58     }
59
60     settings.remove(COOKIES);
61     settings.remove(USER_UNSEND_MESSAGE);
62     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
63 }
64
65 const QString FacebookAuthentication::loadUsername()
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     QSettings settings(DIRECTORY_NAME, FILE_NAME);
70     return settings.value(USERNAME, EMPTY).toString();
71 }
72
73 FacebookCredentials FacebookAuthentication::loginCredentials() const
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76     return m_loginCredentials;
77 }
78
79 void FacebookAuthentication::saveUsername(const QString &username)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     QSettings settings(DIRECTORY_NAME, FILE_NAME);
84     settings.setValue(USERNAME, username);
85 }
86
87 void FacebookAuthentication::start()
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90
91     QSettings settings(DIRECTORY_NAME, FILE_NAME);
92
93     QStringList cookies = settings.value(COOKIES).toStringList();
94     if(!cookies.isEmpty()) {
95         emit loginUsingCookies();
96     }
97     else {
98         m_freshLogin = true;
99         emit newLoginRequest();
100     }
101 }
102
103 bool FacebookAuthentication::updateCredentials(const QUrl &url)
104 {
105     qDebug() << __PRETTY_FUNCTION__ << url.toString();
106
107     bool found = false;
108
109     if (url.isValid()){
110          qDebug() << "url is valid";
111
112         QString callbackUrl = url.toString();
113         qDebug() << "callbackUrl:  " << callbackUrl.toAscii();
114
115         if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
116             qDebug() << "login success";
117
118             // let's find out session credentials
119             if(callbackUrl.contains(SESSION_KEY)) {
120
121                 QJson::Parser parser;
122                 bool ok;
123
124                 // split string into string part and json part
125                 QStringList list = url.toString().split("=");
126
127                 for(int i=0;i<list.count();i++) {
128                     // if string starts with json item
129                     if(list.at(i).startsWith("{")) {
130                         QByteArray jsonString = list.at(i).toAscii();
131                         QVariantMap result = parser.parse (jsonString, &ok).toMap();
132
133                         if (!ok) {
134                             emit error(SituareError::INVALID_JSON);
135                             found = false;
136                         } else {
137                             qDebug() << "Session Key" << result[SESSION_KEY].toString();
138                             m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
139
140     //                        // commeted out until qjson parser can handle 64-bit integers
141     //                        qDebug() << "userID" << result[USER_ID].toString();
142     //                        m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
143
144                             // dirty fix, get user id from session_key
145                             QStringList list = result[SESSION_KEY].toString().split("-");
146                             m_loginCredentials.setUserID(list.at(1));
147                             qDebug() << m_loginCredentials.userID();
148
149                             qDebug() << "Expires" << result[EXPIRES].toString();
150                             m_loginCredentials.setExpires(result[EXPIRES].toString());
151
152                             qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
153                             m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
154
155                             qDebug() << "Signature" << result[SIGNATURE].toString();
156                             m_loginCredentials.setSig(result[SIGNATURE].toString());
157
158                             found = true;
159                             m_freshLogin = false;
160                             emit saveCookiesRequest();
161                             emit credentialsReady(m_loginCredentials);
162                         }
163                     }
164                 }       
165             }   
166         }
167         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
168             qDebug() << "login failure";
169             qDebug() << callbackUrl;
170             clearAccountInformation(true);
171             if(m_freshLogin) {
172                 emit error(SituareError::LOGIN_FAILED);
173                 emit loginFailure();
174             }
175             else {
176                 m_freshLogin = true;
177                 emit error(SituareError::SESSION_EXPIRED);
178             }
179         }
180         else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
181             qDebug() << "correct loginPage";
182         }
183         else {
184             qDebug() << "totally wrong webPage";
185             // we should not get a wrong page at this point
186             emit loginFailure();
187         }
188     }
189     else {
190         qDebug() << " Loading of page failed invalid URL" << endl;
191         // we should not get a wrong page at this point
192         emit loginFailure();
193     }
194     return found;
195 }