LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vkauthmanager.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "vkauthmanager.h"
31 #include <QNetworkRequest>
32 #include <QNetworkReply>
33 #include <QNetworkCookie>
34 #include <QtDebug>
35 #include <QTimer>
36 #include <QEvent>
37 #include <QWebView>
39 #include <util/sll/queuemanager.h>
40 #include <util/sll/urloperator.h>
41 #include <util/xpc/util.h>
43 #include <xmlsettingsdialog/basesettingsmanager.h>
44 
45 namespace LeechCraft
46 {
47 namespace Util
48 {
49 namespace SvcAuth
50 {
51  namespace
52  {
53  QUrl URLFromClientID (const QString& id, const QStringList& scope)
54  {
55  auto url = QUrl::fromEncoded ("https://oauth.vk.com/authorize?redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&state=");
56  UrlOperator { url }
57  ("client_id", id)
58  ("scope", scope.join (","));
59  return url;
60  }
61  }
62 
63  VkAuthManager::VkAuthManager (const QString& accName,
64  const QString& id, const QStringList& scope,
65  const QByteArray& cookies, ICoreProxy_ptr proxy,
66  QueueManager *queueMgr, QObject *parent)
67  : QObject (parent)
68  , Proxy_ (proxy)
69  , AccountHR_ (accName)
70  , AuthNAM_ (new QNetworkAccessManager (this))
71  , Cookies_ (new Util::CustomCookieJar (this))
72  , Queue_ (queueMgr)
73  , ValidFor_ (0)
74  , IsRequesting_ (false)
75  , ID_ (id)
76  , URL_ (URLFromClientID (ID_, scope))
77  , IsRequestScheduled_ (false)
78  , ScheduleTimer_ (new QTimer (this))
79  {
80  AuthNAM_->setCookieJar (Cookies_);
81  Cookies_->Load (cookies);
82 
83  ScheduleTimer_->setSingleShot (true);
84  connect (ScheduleTimer_,
85  SIGNAL (timeout ()),
86  this,
87  SLOT (execScheduledRequest ()));
88  }
89 
91  {
92  return !Token_.isEmpty () &&
93  (!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
94  }
95 
97  {
98  return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
99  }
100 
101  void VkAuthManager::UpdateScope (const QStringList& scope)
102  {
103  const auto& newUrl = URLFromClientID (ID_, scope);
104  if (URL_ == newUrl)
105  return;
106 
107  URL_ = newUrl;
108  Token_.clear ();
109  ReceivedAt_ = QDateTime ();
110  ValidFor_ = 0;
111  }
112 
114  {
115  if (SilentMode_)
116  {
117  PrioManagedQueues_.clear ();
118  ManagedQueues_.clear ();
119  return;
120  }
121 
122  if (!IsAuthenticated ())
123  {
124  RequestAuthKey ();
125  return;
126  }
127 
128  InvokeQueues (Token_);
129  emit gotAuthKey (Token_);
130  }
131 
133  {
134  if (!Queue_)
135  {
136  qWarning () << Q_FUNC_INFO
137  << "cannot manage request queue if queue manager wasn't set";
138  return {};
139  }
140 
141  ManagedQueues_ << queue;
142 
143  return Util::MakeScopeGuard ([this, queue] { ManagedQueues_.removeAll (queue); });
144  }
145 
147  {
148  if (!Queue_)
149  {
150  qWarning () << Q_FUNC_INFO
151  << "cannot manage request queue if queue manager wasn't set";
152  return {};
153  }
154 
155  PrioManagedQueues_ << queue;
156 
157  return Util::MakeScopeGuard ([this, queue] { PrioManagedQueues_.removeAll (queue); });
158  }
159 
160  void VkAuthManager::SetSilentMode (bool silent)
161  {
162  SilentMode_ = silent;
163  }
164 
165  void VkAuthManager::InvokeQueues (const QString& token)
166  {
167  ScheduleTrack (token);
168 
169  for (auto queue : PrioManagedQueues_)
170  while (!queue->isEmpty ())
171  {
172  const auto& pair = queue->takeFirst ();
173  const auto& f = pair.first;
174  Queue_->Schedule ([f, token] { f (token); }, nullptr, pair.second);
175  }
176 
177  for (auto queue : ManagedQueues_)
178  while (!queue->isEmpty ())
179  {
180  const auto& f = queue->takeFirst ();
181  Queue_->Schedule ([f, token] { f (token); });
182  }
183  }
184 
185  void VkAuthManager::RequestURL (const QUrl& url)
186  {
187  qDebug () << Q_FUNC_INFO << url;
188  auto reply = AuthNAM_->get (QNetworkRequest (url));
189  connect (reply,
190  SIGNAL (finished ()),
191  this,
192  SLOT (handleGotForm ()));
193  }
194 
195  void VkAuthManager::RequestAuthKey ()
196  {
197  if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
198  ScheduleTimer_->stop ();
199 
200  if (IsRequesting_)
201  return;
202 
203  RequestURL (URL_);
204  IsRequesting_ = true;
205  }
206 
207  bool VkAuthManager::CheckReply (QUrl location)
208  {
209  if (location.path () != "/blank.html")
210  return CheckError (location);
211 
212  location = QUrl::fromEncoded (location.toEncoded ().replace ('#', '?'));
213 #if QT_VERSION < 0x050000
214  Token_ = location.queryItemValue ("access_token");
215  ValidFor_ = location.queryItemValue ("expires_in").toInt ();
216 #else
217  const QUrlQuery query { location };
218  Token_ = query.queryItemValue ("access_token");
219  ValidFor_ = query.queryItemValue ("expires_in").toInt ();
220 #endif
221  ReceivedAt_ = QDateTime::currentDateTime ();
222  qDebug () << Q_FUNC_INFO << Token_ << ValidFor_;
223  IsRequesting_ = false;
224 
225  InvokeQueues (Token_);
226  emit gotAuthKey (Token_);
227  emit justAuthenticated ();
228 
229  return true;
230  }
231 
232  bool VkAuthManager::CheckError (const QUrl& url)
233  {
234  if (url.path () != "/error")
235  return false;
236 
237 #if QT_VERSION < 0x050000
238  const auto errNum = url.queryItemValue ("err").toInt ();
239 #else
240  const auto errNum = QUrlQuery { url }.queryItemValue ("err").toInt ();
241 #endif
242 
243  IsRequesting_ = false;
244 
245  qWarning () << Q_FUNC_INFO
246  << "got error"
247  << errNum;
248  if (errNum == 2)
249  {
250  clearAuthData ();
251 
252  RequestAuthKey ();
253  return true;
254  }
255 
256  const auto& e = Util::MakeNotification ("VK.com",
257  tr ("VK.com authentication for %1 failed because of error %2. "
258  "Report upstream please.")
259  .arg (AccountHR_)
260  .arg (errNum),
261  PCritical_);
262  Proxy_->GetEntityManager ()->HandleEntity (e);
263 
264  return true;
265  }
266 
267  void VkAuthManager::ScheduleTrack (const QString& key)
268  {
269  if (HasTracked_)
270  return;
271 
272  if (!Proxy_->GetSettingsManager ()->property ("TrackVK").toBool ())
273  return;
274 
275  HasTracked_ = true;
276 
277  QUrl url { "https://api.vk.com/method/stats.trackVisitor" };
278  Util::UrlOperator { url }
279  ("access_token", key);
280 
281  auto reply = AuthNAM_->get (QNetworkRequest { url });
282  connect (reply,
283  SIGNAL (finished ()),
284  reply,
285  SLOT (deleteLater ()));
286  }
287 
289  {
290  Cookies_->Load ({});
291  Token_.clear ();
292  ReceivedAt_ = QDateTime ();
293  ValidFor_ = 0;
294  }
295 
296  namespace
297  {
298  class CloseEventFilter : public QObject
299  {
300  const std::function<void ()> Handler_;
301  public:
302  CloseEventFilter (const std::function<void ()>& handler, QObject *handlee)
303  : QObject { handlee }
304  , Handler_ { handler }
305  {
306  handlee->installEventFilter (this);
307  }
308 
309  bool eventFilter (QObject*, QEvent *event)
310  {
311  if (event->type () == QEvent::Close)
312  Handler_ ();
313  return false;
314  }
315  };
316  }
317 
319  {
320  auto view = new QWebView;
321  view->setWindowTitle (tr ("VK.com authentication for %1")
322  .arg (AccountHR_));
323  view->setWindowFlags (Qt::Window);
324  view->resize (800, 600);
325  view->page ()->setNetworkAccessManager (AuthNAM_);
326  view->show ();
327 
328  view->setUrl (URL_);
329 
330  connect (view,
331  SIGNAL (urlChanged (QUrl)),
332  this,
333  SLOT (handleViewUrlChanged (QUrl)));
334 
335  new CloseEventFilter ([this] { emit authCanceled (); }, view);
336  }
337 
338  void VkAuthManager::execScheduledRequest ()
339  {
340  IsRequestScheduled_ = false;
341 
342  RequestAuthKey ();
343  }
344 
345  void VkAuthManager::handleGotForm ()
346  {
347  auto reply = qobject_cast<QNetworkReply*> (sender ());
348  reply->deleteLater ();
349 
350  if (reply->error () != QNetworkReply::NoError)
351  {
352  qWarning () << Q_FUNC_INFO
353  << reply->error ()
354  << reply->errorString ();
355 
356  IsRequesting_ = false;
357 
358  if (!IsRequestScheduled_)
359  {
360  IsRequestScheduled_ = true;
361  ScheduleTimer_->start (30000);
362  }
363 
364  return;
365  }
366 
367  const auto& location = reply->header (QNetworkRequest::LocationHeader).toUrl ();
368  if (location.isEmpty ())
369  {
370  reauth ();
371  return;
372  }
373 
374  if (CheckReply (location))
375  return;
376 
377  RequestURL (location);
378  }
379 
380  void VkAuthManager::handleViewUrlChanged (const QUrl& url)
381  {
382  if (!CheckReply (url))
383  return;
384 
385  emit cookiesChanged (Cookies_->Save ());
386  sender ()->deleteLater ();
387  }
388 }
389 }
390 }
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
Manipulates query part of an QUrl object.
Definition: urloperator.h:73
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:124
void Schedule(std::function< void()> functor, QObject *dependent=0, QueuePriority prio=QueuePriority::Normal)
Adds the given functor.
ScheduleGuard_t Q_REQUIRED_RESULT ManageQueue(RequestQueue_ptr)
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:118
A simple scheduling manager for a queue of functors.
Definition: queuemanager.h:64
void cookiesChanged(const QByteArray &)
unsigned long Window
Definition: xwrapper.h:50
VkAuthManager(const QString &accountName, const QString &clientId, const QStringList &scope, const QByteArray &cookies, ICoreProxy_ptr, QueueManager *=nullptr, QObject *=nullptr)
const std::function< void()> Handler_