LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 <QtDebug>
34 #include <QTimer>
35 #include <QEvent>
36 #include <QWebView>
38 #include <util/sll/queuemanager.h>
39 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44 namespace SvcAuth
45 {
46  namespace
47  {
48  QUrl URLFromClientID (const QString& id, const QStringList& scope)
49  {
50  QUrl url = QUrl::fromEncoded ("https://oauth.vk.com/authorize?redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&state=");
51  url.addQueryItem ("client_id", id);
52  url.addQueryItem ("scope", scope.join (","));
53  return url;
54  }
55  }
56 
57  VkAuthManager::VkAuthManager (const QString& accName,
58  const QString& id, const QStringList& scope,
59  const QByteArray& cookies, ICoreProxy_ptr proxy,
60  QueueManager *queueMgr, QObject *parent)
61  : QObject (parent)
62  , Proxy_ (proxy)
63  , AccountHR_ (accName)
64  , AuthNAM_ (new QNetworkAccessManager (this))
65  , Cookies_ (new Util::CustomCookieJar (this))
66  , Queue_ (queueMgr)
67  , ValidFor_ (0)
68  , IsRequesting_ (false)
69  , ID_ (id)
70  , URL_ (URLFromClientID (ID_, scope))
71  , IsRequestScheduled_ (false)
72  , ScheduleTimer_ (new QTimer (this))
73  {
74  AuthNAM_->setCookieJar (Cookies_);
75  Cookies_->Load (cookies);
76 
77  ScheduleTimer_->setSingleShot (true);
78  connect (ScheduleTimer_,
79  SIGNAL (timeout ()),
80  this,
81  SLOT (execScheduledRequest ()));
82  }
83 
85  {
86  return !Token_.isEmpty () &&
87  (!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
88  }
89 
91  {
92  return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
93  }
94 
95  void VkAuthManager::UpdateScope (const QStringList& scope)
96  {
97  const auto& newUrl = URLFromClientID (ID_, scope);
98  if (URL_ == newUrl)
99  return;
100 
101  URL_ = newUrl;
102  Token_.clear ();
103  ReceivedAt_ = QDateTime ();
104  ValidFor_ = 0;
105  }
106 
108  {
109  if (SilentMode_)
110  {
111  PrioManagedQueues_.clear ();
112  ManagedQueues_.clear ();
113  return;
114  }
115 
116  if (!IsAuthenticated ())
117  {
118  RequestAuthKey ();
119  return;
120  }
121 
122  InvokeQueues (Token_);
123  emit gotAuthKey (Token_);
124  }
125 
127  {
128  if (!Queue_)
129  {
130  qWarning () << Q_FUNC_INFO
131  << "cannot manage request queue if queue manager wasn't set";
132  return;
133  }
134 
135  ManagedQueues_ << queue;
136  }
137 
139  {
140  ManagedQueues_.removeAll (queue);
141  }
142 
144  {
145  if (!Queue_)
146  {
147  qWarning () << Q_FUNC_INFO
148  << "cannot manage request queue if queue manager wasn't set";
149  return;
150  }
151 
152  PrioManagedQueues_ << queue;
153  }
154 
156  {
157  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  for (auto queue : PrioManagedQueues_)
168  while (!queue->isEmpty ())
169  {
170  const auto& pair = queue->takeFirst ();
171  const auto& f = pair.first;
172  Queue_->Schedule ([f, token] { f (token); }, nullptr, pair.second);
173  }
174 
175  for (auto queue : ManagedQueues_)
176  while (!queue->isEmpty ())
177  {
178  const auto& f = queue->takeFirst ();
179  Queue_->Schedule ([f, token] { f (token); });
180  }
181  }
182 
183  void VkAuthManager::HandleError ()
184  {
185  IsRequesting_ = false;
186  }
187 
188  void VkAuthManager::RequestURL (const QUrl& url)
189  {
190  qDebug () << Q_FUNC_INFO << url;
191  auto reply = AuthNAM_->get (QNetworkRequest (url));
192  connect (reply,
193  SIGNAL (finished ()),
194  this,
195  SLOT (handleGotForm ()));
196  }
197 
198  void VkAuthManager::RequestAuthKey ()
199  {
200  if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
201  ScheduleTimer_->stop ();
202 
203  if (IsRequesting_)
204  return;
205 
206  RequestURL (URL_);
207  IsRequesting_ = true;
208  }
209 
210  bool VkAuthManager::CheckIsBlank (QUrl location)
211  {
212  if (location.path () != "/blank.html")
213  return false;
214 
215  location = QUrl::fromEncoded (location.toEncoded ().replace ('#', '?'));
216  Token_ = location.queryItemValue ("access_token");
217  ValidFor_ = location.queryItemValue ("expires_in").toInt ();
218  ReceivedAt_ = QDateTime::currentDateTime ();
219  qDebug () << Q_FUNC_INFO << Token_ << ValidFor_;
220  IsRequesting_ = false;
221 
222  InvokeQueues (Token_);
223  emit gotAuthKey (Token_);
224  emit justAuthenticated ();
225 
226  return true;
227  }
228 
230  {
231  Cookies_->Load ({});
232  Token_.clear ();
233  ReceivedAt_ = QDateTime ();
234  ValidFor_ = 0;
235  }
236 
237  namespace
238  {
239  class CloseEventFilter : public QObject
240  {
241  const std::function<void ()> Handler_;
242  public:
243  CloseEventFilter (const std::function<void ()>& handler, QObject *handlee)
244  : QObject { handlee }
245  , Handler_ { handler }
246  {
247  handlee->installEventFilter (this);
248  }
249 
250  bool eventFilter (QObject*, QEvent *event)
251  {
252  if (event->type () == QEvent::Close)
253  Handler_ ();
254  return false;
255  }
256  };
257  }
258 
260  {
261  auto view = new QWebView;
262  view->setWindowTitle (tr ("VK.com authentication for %1")
263  .arg (AccountHR_));
264  view->setWindowFlags (Qt::Window);
265  view->resize (800, 600);
266  view->page ()->setNetworkAccessManager (AuthNAM_);
267  view->show ();
268 
269  view->setUrl (URL_);
270 
271  connect (view,
272  SIGNAL (urlChanged (QUrl)),
273  this,
274  SLOT (handleViewUrlChanged (QUrl)));
275 
276  new CloseEventFilter ([this] { emit authCanceled (); }, view);
277  }
278 
279  void VkAuthManager::execScheduledRequest ()
280  {
281  IsRequestScheduled_ = false;
282 
283  RequestAuthKey ();
284  }
285 
286  void VkAuthManager::handleGotForm ()
287  {
288  auto reply = qobject_cast<QNetworkReply*> (sender ());
289  reply->deleteLater ();
290 
291  if (reply->error () != QNetworkReply::NoError)
292  {
293  qWarning () << Q_FUNC_INFO
294  << reply->errorString ();
295 
296  IsRequesting_ = false;
297 
298  if (!IsRequestScheduled_)
299  {
300  IsRequestScheduled_ = true;
301  ScheduleTimer_->start (30000);
302  }
303 
304  return;
305  }
306 
307  const auto& location = reply->header (QNetworkRequest::LocationHeader).toUrl ();
308  if (location.isEmpty ())
309  {
310  reauth ();
311  return;
312  }
313 
314  if (CheckIsBlank (location))
315  return;
316 
317  RequestURL (location);
318  }
319 
320  void VkAuthManager::handleViewUrlChanged (const QUrl& url)
321  {
322  if (!CheckIsBlank (url))
323  return;
324 
325  emit cookiesChanged (Cookies_->Save ());
326  sender ()->deleteLater ();
327  }
328 }
329 }
330 }
void UpdateScope(const QStringList &)
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
void Schedule(std::function< void()> functor, QObject *dependent=0, QueuePriority prio=QueuePriority::Normal)
Adds the given functor.
A simple scheduling manager for a queue of functors.
Definition: queuemanager.h:57
void cookiesChanged(const QByteArray &)
unsigned long Window
Definition: xwrapper.h:43
VkAuthManager(const QString &accountName, const QString &clientId, const QStringList &scope, const QByteArray &cookies, ICoreProxy_ptr, QueueManager *=nullptr, QObject *=nullptr)
const std::function< void()> Handler_