auth.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2009 Andrew Stromme <astromme@chatonka.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Library General Public License as
  6. * published by the Free Software Foundation; either version 2, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "auth.h"
  20. #include <QHashIterator>
  21. #include <QCryptographicHash>
  22. #include <QNetworkAccessManager>
  23. #include <QNetworkRequest>
  24. #include <QCoreApplication>
  25. #include <QWebView>
  26. #include <QPushButton>
  27. #include <KDebug>
  28. #include <KIO/NetAccess>
  29. #include <QVBoxLayout>
  30. #include <KLocale>
  31. RTM::Auth::Auth(RTM::Permissions permissions, const QString& apiKey, const QString& sharedSecret)
  32. : frobRequest(0),
  33. tokenRequest(0)
  34. {
  35. arguments.insert("perms", getTextPermissions(permissions));
  36. this->apiKey = apiKey;
  37. this->sharedSecret = sharedSecret;
  38. arguments.insert("api_key", apiKey);
  39. m_state = RTM::Mutable;
  40. }
  41. RTM::Auth::~Auth() {
  42. frobRequest->deleteLater();
  43. tokenRequest->deleteLater();
  44. }
  45. void RTM::Auth::showLoginWebpage()
  46. {
  47. if (frobRequest)
  48. frobRequest->deleteLater();
  49. frobRequest = new RTM::Request("rtm.auth.getFrob", apiKey, sharedSecret);
  50. connect(frobRequest, SIGNAL(replyReceived(RTM::Request*)), SLOT(showLoginWindowInternal(RTM::Request*)));
  51. frobRequest->sendRequest();
  52. }
  53. void RTM::Auth::showLoginWindowInternal(RTM::Request *rawReply)
  54. {
  55. QString reply = rawReply->data();
  56. //QString reply = rawReply->readAll(); //FIXME: I have no idea why this line isn't working?
  57. frob = reply.remove(0, reply.indexOf("<frob>")+6);
  58. frob.truncate(frob.indexOf("</frob>"));
  59. kDebug() << "Frob: " << frob;
  60. arguments.insert("frob", frob);
  61. QWidget *authWidget = new QWidget();
  62. QVBoxLayout *layout = new QVBoxLayout(authWidget);
  63. QPushButton *button = new QPushButton(authWidget);
  64. QWebView *authPage = new QWebView(authWidget);
  65. button->setText(i18n("Click here after you have logged in and authorized the applet"));
  66. authPage->setUrl(getAuthUrl());
  67. authPage->resize(800, 600);
  68. authPage->scroll(0, 200);
  69. layout->addWidget(authPage);
  70. layout->addWidget(button);
  71. connect(button, SIGNAL(clicked(bool)), authWidget, SLOT(hide()));
  72. connect(button, SIGNAL(clicked(bool)), authWidget, SLOT(deleteLater()));
  73. connect(button, SIGNAL(clicked(bool)), SLOT(pageClosed())); // Last because it takes more time.
  74. authWidget->show();
  75. }
  76. void RTM::Auth::pageClosed() {
  77. continueAuthForToken();
  78. }
  79. QString RTM::Auth::getAuthUrl() {
  80. if (frob.isEmpty())
  81. kWarning() << "Warning, Frob is EMPTY";
  82. return requestUrl();
  83. }
  84. QString RTM::Auth::getTextPermissions(RTM::Permissions permissions)
  85. {
  86. QString textPermissions;
  87. switch (permissions)
  88. {
  89. case RTM::None:
  90. textPermissions = "none";
  91. break;
  92. case RTM::Read:
  93. textPermissions = "read";
  94. break;
  95. case RTM::Write:
  96. textPermissions = "write";
  97. break;
  98. case RTM::Delete:
  99. textPermissions = "delete";
  100. break;
  101. default:
  102. kDebug() << "ERROR: No Permissions";
  103. break;
  104. }
  105. return textPermissions;
  106. }
  107. QString RTM::Auth::requestUrl() {
  108. kDebug() << "RTM::Auth::getRequestUrl()" << m_state << RTM::Mutable;
  109. switch(m_state) {
  110. case RTM::Mutable:
  111. sign();
  112. break;
  113. case RTM::Hashed:
  114. unsign();
  115. sign();
  116. break;
  117. case RTM::RequestSent:
  118. break;
  119. case RTM::RequestReceived:
  120. break;
  121. }
  122. QString url = RTM::baseAuthUrl;
  123. foreach(const QString &key, arguments.keys())
  124. url.append('&' + key + '=' + arguments.value(key));
  125. return url;
  126. }
  127. void RTM::Auth::continueAuthForToken()
  128. {
  129. kDebug() << "Token Time";
  130. if (tokenRequest)
  131. tokenRequest->deleteLater();
  132. tokenRequest = new RTM::Request("rtm.auth.getToken", apiKey, sharedSecret);
  133. tokenRequest->addArgument("frob", arguments.value("frob"));
  134. connect(tokenRequest, SIGNAL(replyReceived(RTM::Request*)), SLOT(tokenResponse(RTM::Request*)));
  135. tokenRequest->sendRequest();
  136. }
  137. void RTM::Auth::tokenResponse(RTM::Request* response)
  138. {
  139. QString reply = response->data();
  140. kDebug() << "Reply: " << reply;
  141. QString token = reply.remove(0, reply.indexOf("<token>")+7);
  142. token.truncate(token.indexOf("</token>"));
  143. kDebug() << "Token: " << token;
  144. emit tokenReceived(token);
  145. }