LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
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>
38 #include <QFuture>
39 #include <QFutureInterface>
41 #include <util/sll/queuemanager.h>
42 #include <util/sll/urloperator.h>
43 #include <util/sll/slotclosure.h>
44 #include <util/xpc/util.h>
46 #include <xmlsettingsdialog/basesettingsmanager.h>
47 
48 namespace LeechCraft
49 {
50 namespace Util
51 {
52 namespace SvcAuth
53 {
54  namespace
55  {
56  QUrl URLFromClientID (const QString& id, const QStringList& scope)
57  {
58  auto url = QUrl::fromEncoded ("https://oauth.vk.com/authorize?redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&state=");
59  UrlOperator { url }
60  ("client_id", id)
61  ("scope", scope.join (","));
62  return url;
63  }
64  }
65 
66  VkAuthManager::VkAuthManager (const QString& accName,
67  const QString& id, const QStringList& scope,
68  const QByteArray& cookies, ICoreProxy_ptr proxy,
69  QueueManager *queueMgr, QObject *parent)
70  : QObject (parent)
71  , Proxy_ (proxy)
72  , AccountHR_ (accName)
73  , AuthNAM_ (new QNetworkAccessManager (this))
74  , Cookies_ (new Util::CustomCookieJar (this))
75  , Queue_ (queueMgr)
76  , ValidFor_ (0)
77  , IsRequesting_ (false)
78  , ID_ (id)
79  , URL_ (URLFromClientID (ID_, scope))
80  , IsRequestScheduled_ (false)
81  , ScheduleTimer_ (new QTimer (this))
82  {
83  AuthNAM_->setCookieJar (Cookies_);
84  Cookies_->Load (cookies);
85 
86  ScheduleTimer_->setSingleShot (true);
87  connect (ScheduleTimer_,
88  SIGNAL (timeout ()),
89  this,
90  SLOT (execScheduledRequest ()));
91  }
92 
94  {
95  return !Token_.isEmpty () &&
96  (!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
97  }
98 
100  {
101  return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
102  }
103 
104  void VkAuthManager::UpdateScope (const QStringList& scope)
105  {
106  const auto& newUrl = URLFromClientID (ID_, scope);
107  if (URL_ == newUrl)
108  return;
109 
110  URL_ = newUrl;
111  Token_.clear ();
112  ReceivedAt_ = QDateTime ();
113  ValidFor_ = 0;
114  }
115 
117  {
118  if (!IsAuthenticated ())
119  {
120  if (!SilentMode_)
121  RequestAuthKey ();
122  else
123  {
124  for (const auto& queue : PrioManagedQueues_)
125  queue->clear ();
126  for (const auto& queue : ManagedQueues_)
127  queue->clear ();
128  }
129 
130  return;
131  }
132 
133  InvokeQueues (Token_);
134  emit gotAuthKey (Token_);
135  }
136 
138  {
139  if (SilentMode_ && !IsAuthenticated ())
140  return {};
141 
142  QFutureInterface<QString> iface;
143  iface.reportStarted ();
144 
146  {
147  [this, iface] () mutable { iface.reportFinished (&Token_); },
148  this,
149  SIGNAL (gotAuthKey (QString)),
150  this
151  };
152 
153  return iface.future ();
154  }
155 
157  {
158  if (!Queue_)
159  {
160  qWarning () << Q_FUNC_INFO
161  << "cannot manage request queue if queue manager wasn't set";
162  return {};
163  }
164 
165  ManagedQueues_ << queue;
166 
167  return Util::MakeScopeGuard ([this, queue] { ManagedQueues_.removeAll (queue); });
168  }
169 
171  {
172  if (!Queue_)
173  {
174  qWarning () << Q_FUNC_INFO
175  << "cannot manage request queue if queue manager wasn't set";
176  return {};
177  }
178 
179  PrioManagedQueues_ << queue;
180 
181  return Util::MakeScopeGuard ([this, queue] { PrioManagedQueues_.removeAll (queue); });
182  }
183 
184  void VkAuthManager::SetSilentMode (bool silent)
185  {
186  SilentMode_ = silent;
187  }
188 
189  void VkAuthManager::InvokeQueues (const QString& token)
190  {
191  ScheduleTrack (token);
192 
193  for (auto queue : PrioManagedQueues_)
194  while (!queue->isEmpty ())
195  {
196  const auto& pair = queue->takeFirst ();
197  const auto& f = pair.first;
198  Queue_->Schedule ([f, token] { f (token); }, nullptr, pair.second);
199  }
200 
201  for (auto queue : ManagedQueues_)
202  while (!queue->isEmpty ())
203  {
204  const auto& f = queue->takeFirst ();
205  Queue_->Schedule ([f, token] { f (token); });
206  }
207  }
208 
209  void VkAuthManager::RequestURL (const QUrl& url)
210  {
211  qDebug () << Q_FUNC_INFO << url;
212  auto reply = AuthNAM_->get (QNetworkRequest (url));
213  connect (reply,
214  SIGNAL (finished ()),
215  this,
216  SLOT (handleGotForm ()));
217  }
218 
219  void VkAuthManager::RequestAuthKey ()
220  {
221  if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
222  ScheduleTimer_->stop ();
223 
224  if (IsRequesting_)
225  return;
226 
227  RequestURL (URL_);
228  IsRequesting_ = true;
229  }
230 
231  bool VkAuthManager::CheckReply (QUrl location)
232  {
233  if (location.path () != "/blank.html")
234  return CheckError (location);
235 
236  location = QUrl::fromEncoded (location.toEncoded ().replace ('#', '?'));
237 #if QT_VERSION < 0x050000
238  Token_ = location.queryItemValue ("access_token");
239  ValidFor_ = location.queryItemValue ("expires_in").toInt ();
240 #else
241  const QUrlQuery query { location };
242  Token_ = query.queryItemValue ("access_token");
243  ValidFor_ = query.queryItemValue ("expires_in").toInt ();
244 #endif
245  ReceivedAt_ = QDateTime::currentDateTime ();
246  qDebug () << Q_FUNC_INFO << Token_ << ValidFor_;
247  IsRequesting_ = false;
248 
249  InvokeQueues (Token_);
250  emit gotAuthKey (Token_);
251  emit justAuthenticated ();
252 
253  return true;
254  }
255 
256  bool VkAuthManager::CheckError (const QUrl& url)
257  {
258  if (url.path () != "/error")
259  return false;
260 
261 #if QT_VERSION < 0x050000
262  const auto errNum = url.queryItemValue ("err").toInt ();
263 #else
264  const auto errNum = QUrlQuery { url }.queryItemValue ("err").toInt ();
265 #endif
266 
267  IsRequesting_ = false;
268 
269  qWarning () << Q_FUNC_INFO
270  << "got error"
271  << errNum;
272  if (errNum == 2)
273  {
274  clearAuthData ();
275 
276  RequestAuthKey ();
277  return true;
278  }
279 
280  const auto& e = Util::MakeNotification ("VK.com",
281  tr ("VK.com authentication for %1 failed because of error %2. "
282  "Report upstream please.")
283  .arg (AccountHR_)
284  .arg (errNum),
285  PCritical_);
286  Proxy_->GetEntityManager ()->HandleEntity (e);
287 
288  return true;
289  }
290 
291  void VkAuthManager::ScheduleTrack (const QString& key)
292  {
293  if (HasTracked_)
294  return;
295 
296  if (!Proxy_->GetSettingsManager ()->property ("TrackVK").toBool ())
297  return;
298 
299  HasTracked_ = true;
300 
301  QUrl url { "https://api.vk.com/method/stats.trackVisitor" };
302  Util::UrlOperator { url }
303  ("access_token", key);
304 
305  auto reply = AuthNAM_->get (QNetworkRequest { url });
306  connect (reply,
307  SIGNAL (finished ()),
308  reply,
309  SLOT (deleteLater ()));
310  }
311 
313  {
314  Cookies_->Load ({});
315  Token_.clear ();
316  ReceivedAt_ = QDateTime ();
317  ValidFor_ = 0;
318  }
319 
320  namespace
321  {
322  class CloseEventFilter : public QObject
323  {
324  const std::function<void ()> Handler_;
325  public:
326  CloseEventFilter (const std::function<void ()>& handler, QObject *handlee)
327  : QObject { handlee }
328  , Handler_ { handler }
329  {
330  handlee->installEventFilter (this);
331  }
332 
333  bool eventFilter (QObject*, QEvent *event)
334  {
335  if (event->type () == QEvent::Close)
336  Handler_ ();
337  return false;
338  }
339  };
340  }
341 
343  {
344  auto view = new QWebView;
345  view->setWindowTitle (tr ("VK.com authentication for %1")
346  .arg (AccountHR_));
347  view->setWindowFlags (Qt::Window);
348  view->resize (800, 600);
349  view->page ()->setNetworkAccessManager (AuthNAM_);
350  view->show ();
351 
352  view->setUrl (URL_);
353 
354  connect (view,
355  SIGNAL (urlChanged (QUrl)),
356  this,
357  SLOT (handleViewUrlChanged (QUrl)));
358 
359  new CloseEventFilter ([this] { emit authCanceled (); }, view);
360  }
361 
362  void VkAuthManager::execScheduledRequest ()
363  {
364  IsRequestScheduled_ = false;
365 
366  RequestAuthKey ();
367  }
368 
369  void VkAuthManager::handleGotForm ()
370  {
371  auto reply = qobject_cast<QNetworkReply*> (sender ());
372  reply->deleteLater ();
373 
374  if (reply->error () != QNetworkReply::NoError)
375  {
376  qWarning () << Q_FUNC_INFO
377  << reply->error ()
378  << reply->errorString ();
379 
380  IsRequesting_ = false;
381 
382  if (!IsRequestScheduled_)
383  {
384  IsRequestScheduled_ = true;
385  ScheduleTimer_->start (30000);
386  }
387 
388  return;
389  }
390 
391  const auto& location = reply->header (QNetworkRequest::LocationHeader).toUrl ();
392  if (location.isEmpty ())
393  {
394  reauth ();
395  return;
396  }
397 
398  if (CheckReply (location))
399  return;
400 
401  RequestURL (location);
402  }
403 
404  void VkAuthManager::handleViewUrlChanged (const QUrl& url)
405  {
406  if (!CheckReply (url))
407  return;
408 
409  emit cookiesChanged (Cookies_->Save ());
410  sender ()->deleteLater ();
411  }
412 }
413 }
414 }
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:127
void Schedule(std::function< void()> functor, QObject *dependent=nullptr, QueuePriority prio=QueuePriority::Normal)
Adds the given functor.
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
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)