|
1 /** |
|
2 * Copyright (c) 2010 Sasken Communication Technologies Ltd. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "{License}" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "{LicenseUrl}". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Siddhartha Chandra, Sasken Communication Technologies Ltd |
|
14 * Description: |
|
15 * Login Dialog class of Authentication Application |
|
16 */ |
|
17 |
|
18 #include <qdebug.h> |
|
19 |
|
20 #include "authAppConstants.h" |
|
21 #include "sessionSP.h" |
|
22 #include "errorCodes.h" |
|
23 #include "loginDialog.h" |
|
24 #include "keys.h" |
|
25 #include <QNetworkCookie> |
|
26 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
27 // global |
|
28 |
|
29 //static const QString kLoginURL = "http://www.facebook.com/login.php"; |
|
30 |
|
31 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
32 void FBLoginDialog::connectToGetSession(const QString& aToken) |
|
33 { |
|
34 qDebug()<<"Inside FBLoginDialog::connectToGetSession()"; |
|
35 iGetSessionRequest = FBRequest::requestWithSession(iSession); |
|
36 |
|
37 //connect(iGetSessionRequest, SIGNAL(LoadLoginPage()), this, SLOT(LoadLoginPage())); |
|
38 connect(iGetSessionRequest, SIGNAL(requestDidLoad(QVariant)), this, SLOT(requestDidLoad(QVariant))); |
|
39 connect(iGetSessionRequest, SIGNAL(requestFailedWithNetworkError(QNetworkReply::NetworkError)), this, SLOT(requestFailedWithNetworkError(QNetworkReply::NetworkError))); |
|
40 connect(iGetSessionRequest, SIGNAL(requestFailedWithFacebookError(FBError)), this, SLOT(requestFailedWithFacebookError(FBError))); |
|
41 |
|
42 Dictionary params; |
|
43 params["auth_token"] = aToken; |
|
44 |
|
45 if (iSession->apiSecret().length()) |
|
46 { |
|
47 params["generate_session_secret"]="1"; |
|
48 } |
|
49 |
|
50 if (iSession->getSessionProxy().length()) |
|
51 { |
|
52 iGetSessionRequest->post(iSession->getSessionProxy(),params); |
|
53 } |
|
54 else |
|
55 { |
|
56 iGetSessionRequest->call("facebook.auth.getSession", params); |
|
57 } |
|
58 } |
|
59 void FBLoginDialog::connectToGetToken() |
|
60 { |
|
61 qDebug()<<"Inside FBLoginDialog::connectToGetToken()"; |
|
62 iGetSessionRequest = FBRequest::requestWithSession(iSession); |
|
63 |
|
64 // connect(iGetSessionRequest, SIGNAL(requestDidLoad(QVariant)), this, SLOT(requestDidLoad(QVariant))); |
|
65 // connect(iGetSessionRequest, SIGNAL(requestFailedWithNetworkError(QNetworkReply::NetworkError)), this, SLOT(requestFailedWithNetworkError(QNetworkReply::NetworkError))); |
|
66 // connect(iGetSessionRequest, SIGNAL(requestFailedWithFacebookError(FBError)), this, SLOT(requestFailedWithFacebookError(FBError))); |
|
67 |
|
68 Dictionary postParams; |
|
69 iSession->stroauth_nonce = generateNONCE(43); |
|
70 iSession->stroauth_timestamp = generateTimeStamp();//QString::number(generateTimeStamp()); |
|
71 qDebug()<<"iSession->stroauth_timestamp"<<iSession->stroauth_timestamp; |
|
72 //postParams["oauth_callback"] = kBase; |
|
73 postParams["oauth_consumer_key"] = kConsumerKey; |
|
74 postParams["oauth_signature_method"] = "HMAC-SHA1"; |
|
75 postParams["oauth_timestamp"] = iSession->stroauth_timestamp; |
|
76 postParams["oauth_nonce"] = iSession->stroauth_nonce; |
|
77 postParams["oauth_version"] = "1.0"; |
|
78 FBRequest* req = FBRequest::requestWithSession(iSession); |
|
79 iSession->stroauth_signature = req->generateSig(postParams); |
|
80 iGetSessionRequest->call("/uas/oauth/accessToken", postParams); |
|
81 |
|
82 } |
|
83 void FBLoginDialog::loadLoginPage() |
|
84 { |
|
85 iGetSessionRequest = FBRequest::requestWithSession(iSession); |
|
86 qDebug()<<"Inside FBLoginDialog::loadLoginPage()"; |
|
87 Dictionary getParams, postParams; |
|
88 iSession->stroauth_nonce = generateNONCE(43); |
|
89 iSession->stroauth_timestamp = generateTimeStamp();//QString::number(generateTimeStamp()); |
|
90 qDebug()<<"iSession->stroauth_timestamp"<<iSession->stroauth_timestamp; |
|
91 |
|
92 postParams["oauth_consumer_key"] = kConsumerKey; |
|
93 postParams["oauth_signature_method"] = "HMAC-SHA1"; |
|
94 postParams["oauth_timestamp"] = iSession->stroauth_timestamp; |
|
95 postParams["oauth_nonce"] = iSession->stroauth_nonce; |
|
96 postParams["oauth_version"] = "1.0"; |
|
97 iSession->stroauth_signature = iGetSessionRequest->generateSig(postParams); |
|
98 |
|
99 connect(iGetSessionRequest, SIGNAL(LoadLoginPage()), this, SLOT(LoadLoginPage())); |
|
100 |
|
101 iGetSessionRequest->connect(); |
|
102 } |
|
103 |
|
104 QByteArray FBLoginDialog::generateTimeStamp() |
|
105 { |
|
106 uint time = QDateTime::currentDateTime().toTime_t(); |
|
107 QByteArray timestamp = QByteArray::number( time ); |
|
108 return timestamp; |
|
109 } |
|
110 QString FBLoginDialog::generateNONCE(const qint64 Length) |
|
111 { |
|
112 srand(time(0)); |
|
113 |
|
114 //read upto milliseconds |
|
115 QString RetString; |
|
116 |
|
117 QString Letters( |
|
118 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); |
|
119 |
|
120 //append a randomly generated string to RetString |
|
121 for (int i = 0; i < Length; i++) |
|
122 { |
|
123 RetString.insert((i), Letters.at(rand() % Letters.size())); |
|
124 } |
|
125 |
|
126 return RetString; |
|
127 } |
|
128 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
129 |
|
130 FBLoginDialog::FBLoginDialog() : FBDialog () {} |
|
131 |
|
132 FBLoginDialog::FBLoginDialog(FBSession* aSession) : FBDialog(aSession) {} |
|
133 |
|
134 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
135 // FBDialog |
|
136 |
|
137 void FBLoginDialog::load() |
|
138 { |
|
139 qDebug()<<"Inside FBLoginDialog::load()"; |
|
140 loadLoginPage(); |
|
141 } |
|
142 |
|
143 void FBLoginDialog::dialogWillDisappear() |
|
144 { |
|
145 qDebug()<<"Inside FBLoginDialog::dialogWillDisappear()"; |
|
146 iGetSessionRequest->cancel(); |
|
147 |
|
148 if (!iSession->isConnected()) |
|
149 { |
|
150 iSession->cancelLogin(); |
|
151 } |
|
152 } |
|
153 |
|
154 void FBLoginDialog::GetSessionKey(const QUrl& aUrl) |
|
155 { |
|
156 qDebug()<<"Inside FBLoginDialog::GetSessionKey()"; |
|
157 const QString authToken = "auth_token="; |
|
158 const QString url ( aUrl.toString() ); |
|
159 |
|
160 int start = url.indexOf(authToken); |
|
161 if (start != -1) |
|
162 { |
|
163 QString token; |
|
164 int end = url.indexOf("&", start); |
|
165 int delta = start + authToken.size() + 1; |
|
166 if (end != -1) |
|
167 { |
|
168 token = url.mid(delta, end - delta ); |
|
169 } |
|
170 else |
|
171 { |
|
172 token = url.right(url.size() - delta + 1); |
|
173 } |
|
174 |
|
175 connectToGetSession(token); |
|
176 } |
|
177 } |
|
178 void FBLoginDialog::FetchKeyFromUrl(const QUrl& aUrl) |
|
179 { |
|
180 const QString url ( aUrl.toString() ); |
|
181 QStringList varList = url.split("{"); |
|
182 QString newvar = varList[1]; |
|
183 varList.clear(); |
|
184 varList = newvar.split(","); |
|
185 QString session_key = varList[0]; |
|
186 QString uid = varList[1]; |
|
187 QString expires = varList[2]; |
|
188 QString secret = varList[3]; |
|
189 varList.clear(); |
|
190 varList = session_key.split(":"); |
|
191 session_key = varList[1]; |
|
192 session_key = session_key.mid(1,session_key.length()-2); |
|
193 varList.clear(); |
|
194 varList = uid.split(":"); |
|
195 uid = varList[1]; |
|
196 varList.clear(); |
|
197 varList = expires.split(":"); |
|
198 expires = varList[1]; |
|
199 varList.clear(); |
|
200 varList = secret.split(":"); |
|
201 secret = varList[1]; |
|
202 secret = secret.mid(1,secret.length()-2); |
|
203 varList.clear(); |
|
204 qDebug()<<"session_key"<<session_key<<"\n"; |
|
205 qDebug()<<"uid"<<uid<<"\n"; |
|
206 qDebug()<<"expires"<<expires<<"\n"; |
|
207 qDebug()<<"secret"<<secret<<"\n"; |
|
208 uint expiry = expires.toUInt();//.toUInt(&conversionError); |
|
209 QDateTime expiration; expiration.setTime_t( expiry ); |
|
210 iSession->beginSession(session_key, secret,expiration); |
|
211 iSession->resume(); |
|
212 } |
|
213 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
214 // slots for signals from FBRequest |
|
215 |
|
216 void FBLoginDialog::requestDidLoad(const QVariant& aResult) |
|
217 { |
|
218 qDebug()<<"Inside FBLoginDialog::requestDidLoad"; |
|
219 bool conversionError = false; |
|
220 QVariantHash object = aResult.toHash(); |
|
221 QString sessionKey = object.value("session_key").toString(); |
|
222 QString sessionSecret = object.value("secret").toString(); |
|
223 |
|
224 QVariant ex = object.value("expires"); |
|
225 uint expires = object.value("expires").toUInt(&conversionError); |
|
226 QDateTime expiration; expiration.setTime_t( expires ); |
|
227 |
|
228 iSession->beginSession(sessionKey, sessionSecret,expiration); |
|
229 iSession->resume(); |
|
230 |
|
231 dismissWithSuccess(true, true); |
|
232 } |
|
233 |
|
234 void FBLoginDialog::requestFailedWithFacebookError (const FBError& aCode ) |
|
235 { |
|
236 qDebug()<<"Inside FBLoginDialog::requestFailedWithFacebookError()"; |
|
237 dismissWithError(aCode, true); |
|
238 } |
|
239 |
|
240 void FBLoginDialog::requestFailedWithNetworkError( QNetworkReply::NetworkError aCode ) |
|
241 { |
|
242 qDebug()<<"Inside FBLoginDialog::requestFailedWithNetworkError()"; |
|
243 dismissWithError(aCode, true); |
|
244 } |
|
245 void FBLoginDialog::LoadLoginPage() |
|
246 { |
|
247 |
|
248 qDebug()<<"Enter: LoadLoginPage()"; |
|
249 QString LoginUrl = kAuthorizeUrl + "?oauth_token="; |
|
250 LoginUrl += iSession->stroauth_Token; |
|
251 |
|
252 /* QNetworkCookieJar* cookieJar = iWebView->page()->networkAccessManager()->cookieJar(); |
|
253 iWebView->page()->networkAccessManager()->setCookieJar(cookieJar); |
|
254 QNetworkCookie testCookie;// "test_cookie", "1"); |
|
255 |
|
256 testCookie.setDomain ( "twitter.com" ); |
|
257 QDateTime dt = QDateTime::currentDateTime(); |
|
258 dt.addDays(1); |
|
259 testCookie.setExpirationDate(dt); |
|
260 testCookie.setHttpOnly(false); |
|
261 testCookie.setName("twittercookie"); |
|
262 testCookie.setValue("1"); |
|
263 testCookie.setPath ( "/" ); |
|
264 testCookie.setSecure ( true); |
|
265 QList<QNetworkCookie> cookieList; |
|
266 cookieList.append(testCookie); |
|
267 cookieJar->setCookiesFromUrl ( cookieList, QUrl("https://www.twitter.com/") ); |
|
268 QWebSettings::globalSettings()->setOfflineStoragePath("C:\\Data");*/ |
|
269 |
|
270 iWebView->load(LoginUrl); |
|
271 qDebug()<<"Exit: LoadLoginPage()"; |
|
272 |
|
273 } |
|
274 void FBLoginDialog::GetAccessToken() |
|
275 { |
|
276 qDebug()<<"Inside FBLoginDialog::GetAccessToken()"; |
|
277 Dictionary postParams; |
|
278 iSession->stroauth_nonce = generateNONCE(43); |
|
279 iSession->stroauth_timestamp = generateTimeStamp(); |
|
280 |
|
281 postParams["oauth_consumer_key"] = kConsumerKey; |
|
282 postParams["oauth_nonce"] = iSession->stroauth_nonce; |
|
283 postParams["oauth_signature_method"] = "HMAC-SHA1"; |
|
284 postParams["oauth_token"] = iSession->stroauth_Token; |
|
285 postParams["oauth_timestamp"] = iSession->stroauth_timestamp; |
|
286 postParams["oauth_verifier"] = iSession->stroauth_verifier; |
|
287 postParams["oauth_version"] = "1.0"; |
|
288 |
|
289 iSession->stroauth_signature = iGetSessionRequest->generateSig(postParams); |
|
290 |
|
291 iGetSessionRequest->connect_req("/access_token"); |
|
292 } |