refactored engine and situareservice
[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
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <QtGui>
24 #include <QtDebug>
25 #include <QDateTime>
26 #include "facebookauthentication.h"
27 #include "facebookcommon.h"
28
29 FacebookAuthentication::FacebookAuthentication(QWidget *parent)
30     : QMainWindow(parent)
31 {
32     qDebug() << __PRETTY_FUNCTION__;
33
34     m_webView = new QWebView;
35     m_mainlayout = new QHBoxLayout;
36
37     m_facebookLoginPage = formLoginPage(FACEBOOK_LOGINBASE, SITUARE_PUBLIC_FACEBOOKAPI_KEY,
38                                         INTERVAL1, SITUARE_LOGIN_SUCCESS, INTERVAL2,
39                                         SITUARE_LOGIN_FAILURE, FACEBOOK_LOGIN_ENDING);
40
41     connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
42             this, SLOT(updateCredentials(const QUrl &)));
43
44     readCredentials(m_loginCredentials);
45 }
46
47 FacebookAuthentication::~FacebookAuthentication()
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50     delete m_webView;
51     delete m_mainlayout;
52 }
53
54 void FacebookAuthentication::start()
55 {
56     qDebug() << __PRETTY_FUNCTION__;    
57     if (!verifyCredentials(m_loginCredentials)){
58         m_webView->setZoomFactor(FACEBOOK_LOGINPAGE_FONT_SIZE);
59         m_webView->load(m_facebookLoginPage);
60         setCentralWidget(m_webView);
61         this->show();
62     }
63     else
64         emit credentialsReady(m_loginCredentials);
65 }
66
67
68 bool FacebookAuthentication::updateCredentials(const QUrl &url)
69 {    
70     qDebug() << __PRETTY_FUNCTION__;
71
72     bool foundSessionKey = FALSE;
73     bool foundUserID = FALSE;
74     bool foundExpires = FALSE;
75     bool foundSessionSecret = FALSE;
76     bool foundSig = FALSE;
77
78     if (url.isValid()){
79          qDebug() << "url is valid" << endl;
80
81         QString callbackUrl = url.toString();
82         QString urlEdit(callbackUrl);
83         qDebug() << "callbackUrl:  " << endl << callbackUrl.toAscii() << endl;
84
85         if ( callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0 ){
86             qDebug() << "login success" << endl;
87
88             // let's find out session key            
89             int indexOfCredential = callbackUrl.indexOf(SESSION_KEY);
90
91             if (indexOfCredential != -1){
92                 foundSessionKey = TRUE;
93
94                 indexOfCredential += 14; //lenght of SESSION_KEY
95                 urlEdit.remove(0,indexOfCredential);
96                 indexOfCredential = urlEdit.indexOf(USER_ID);
97                 urlEdit.remove(indexOfCredential, urlEdit.length());
98                 urlEdit.remove("\",\"");
99
100                 qDebug() << "Session Key" << endl << urlEdit.toAscii() << endl;
101                 m_loginCredentials.setSessionKey(urlEdit);
102             }
103
104             // let's find out uid            
105             urlEdit = callbackUrl;
106             indexOfCredential = callbackUrl.indexOf(USER_ID);
107
108             if (indexOfCredential != -1){
109                 foundUserID = TRUE;
110
111                 indexOfCredential += 5; //length of USER_ID:
112                 urlEdit.remove(0,indexOfCredential);
113                 indexOfCredential = urlEdit.indexOf(EXPIRES);
114                 urlEdit.remove(indexOfCredential, urlEdit.length());
115                 urlEdit.remove(",\"");
116
117                 qDebug() << "userID" << endl << urlEdit.toAscii() << endl;
118                 m_loginCredentials.setUserID(urlEdit);
119             }
120
121             // let's find out expires           
122             urlEdit = callbackUrl;
123             indexOfCredential = callbackUrl.indexOf(EXPIRES);
124
125             if (indexOfCredential != -1){
126                 foundExpires = TRUE;
127
128                 indexOfCredential += 9; //length of EXPIRES
129                 urlEdit.remove(0,indexOfCredential);
130                 indexOfCredential = urlEdit.indexOf(SESSION_SECRET);
131                 urlEdit.remove(indexOfCredential, urlEdit.length());
132                 urlEdit.remove(",\"");
133
134                 qDebug() << "Expires" << endl << urlEdit.toAscii() << endl;
135                 m_loginCredentials.setExpires(urlEdit);
136             }
137
138             // let's find out sessionsecret            
139             urlEdit = callbackUrl;
140             indexOfCredential = callbackUrl.indexOf(SESSION_SECRET);
141
142             if (indexOfCredential != -1){
143                 foundSessionSecret = TRUE;
144
145                 indexOfCredential += 9; //" length of SESSION_SECRET
146                 urlEdit.remove(0,indexOfCredential);
147                 indexOfCredential = urlEdit.indexOf(SIGNATURE);
148                 urlEdit.remove(indexOfCredential, urlEdit.length());
149                 urlEdit.remove("\",\"");
150
151                 qDebug() << "Session Secret" << endl << urlEdit.toAscii() << endl;
152                 m_loginCredentials.setSessionSecret(urlEdit);
153             }
154
155             // let's find out sig            
156             urlEdit = callbackUrl;
157             indexOfCredential = callbackUrl.indexOf(SIGNATURE);
158
159             if (indexOfCredential != -1){
160                 foundSig = TRUE;
161
162                 indexOfCredential += 6; //" length of SIGNATURE
163                 urlEdit.remove(0,indexOfCredential);
164                 urlEdit.remove("\"}");
165
166                 qDebug() << "Signature" << endl << urlEdit.toAscii() << endl;
167                 m_loginCredentials.setSig(urlEdit);
168             }
169
170             m_webView->hide();
171             writeCredentials(m_loginCredentials);
172             emit credentialsReady(m_loginCredentials);
173         }
174
175         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
176             qWarning() << "login failure" << endl;
177             emit loginFailure();
178         }
179
180         else if ( callbackUrl.indexOf(LOGIN_PAGE) == 0){
181             qDebug() << "correct loginPage";
182         }
183
184         else {
185             qDebug() << "totally wrong webPage";
186             emit loginFailure();
187             start();
188         }
189     }
190
191     else {
192         qDebug() << " Loading of page failed invalid URL" << endl;
193         emit loginFailure();
194         return FALSE;
195     }
196
197
198     return (foundSessionKey && foundUserID && foundExpires && foundSessionSecret && foundSig);
199 }
200
201 void FacebookAuthentication::writeCredentials(const FacebookCredentials &credentials)
202 {
203     qDebug() << __PRETTY_FUNCTION__;
204     QSettings settings(DIRECTORY_NAME, FILE_NAME);
205
206     settings.setValue("Session Key", credentials.sessionKey());
207     settings.setValue("User ID", credentials.userID());
208     settings.setValue("Expires", credentials.expires());
209     settings.setValue("Session Secret", credentials.sessionSecret());
210     settings.setValue("Sig", credentials.sig());
211 }
212
213 void FacebookAuthentication::readCredentials(FacebookCredentials &credentialsFromFile)
214 {
215     qDebug() << __PRETTY_FUNCTION__;
216
217     QSettings settings(DIRECTORY_NAME, FILE_NAME);
218
219     credentialsFromFile.setSessionKey(settings.value("Session Key", "Error").toString());
220     credentialsFromFile.setUserID(settings.value("User ID", "Error").toString());
221     credentialsFromFile.setExpires(settings.value("Expires", "Error").toString());
222     credentialsFromFile.setSessionSecret(settings.value("Session Secret", "Error").toString());
223     credentialsFromFile.setSig(settings.value("Sig", "Error").toString());
224 }
225
226  FacebookCredentials FacebookAuthentication::loginCredentials() const
227  {
228      qDebug() << __PRETTY_FUNCTION__;
229      return m_loginCredentials;
230  }
231
232  bool FacebookAuthentication::verifyCredentials(const FacebookCredentials &credentials) const
233  {
234      qDebug() << __PRETTY_FUNCTION__;
235
236      // if expires value is 0, then credentials are valid forever
237      if(credentials.expires() == "0") {
238          return true;
239      }
240      else {
241          QString expires = credentials.expires();
242          QDateTime expireTime;
243          expireTime.setTime_t(expires.toInt());
244          QString expiresString = expireTime.toString("dd.MM.yyyy  hh:mm:ss");
245          qDebug() << expiresString.toAscii();
246
247          QDateTime currentTime;
248          currentTime = QDateTime::currentDateTime();
249          QString currentTimeString = currentTime.toString("dd.MM.yyyy  hh:mm:ss");
250          qDebug() << currentTimeString.toAscii();
251
252          return currentTime < expireTime;
253      }
254  }
255
256  QString FacebookAuthentication::formLoginPage(const QString & part1, const QString & part2,
257                                  const QString & part3, const QString & part4,
258                                  const QString & part5, const QString & part6,
259                                  const QString & part7) const
260  {
261      QString loginPage;
262      loginPage.append(part1);
263      loginPage.append(part2);
264      loginPage.append(part3);
265      loginPage.append(part4);
266      loginPage.append(part5);
267      loginPage.append(part5);
268      loginPage.append(part6);
269      loginPage.append(part7);
270
271      return loginPage;
272  }