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