[SCM] ktp-auth-handler packaging branch, master, updated. debian/15.12.1-2-282-g080758e

Maximiliano Curia maxy at moszumanska.debian.org
Fri May 27 23:58:45 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-auth-handler.git;a=commitdiff;h=8c6233c

The following commit has been merged in the master branch:
commit 8c6233c44dc30a9030da852702f2ffbb7bd7c9a5
Author: Daniele E. Domenichelli <daniele.domenichelli at gmail.com>
Date:   Thu Apr 5 04:31:55 2012 +0200

    Retrieve and save refresh token for X-MESSENGER-OAUTH2 authentication
    
    Introduce dependency on QJSON
---
 CMakeLists.txt                        |  3 ++
 cmake/modules/FindQJSON.cmake         | 46 +++++++++++++++++
 x-messenger-oauth2-auth-operation.cpp | 14 ++---
 x-messenger-oauth2-prompt.cpp         | 96 +++++++++++++++++++++++++++--------
 x-messenger-oauth2-prompt.h           |  8 ++-
 5 files changed, 137 insertions(+), 30 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e6b1df7..35dc9c4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,6 +12,7 @@ set(KDE_MIN_VERSION "4.4.75")
 find_package (KDE4 4.4.75 REQUIRED)
 find_package (TelepathyQt4 0.8.9 REQUIRED)
 find_package (KTp REQUIRED)
+find_package (QJSON REQUIRED)
 
 include (KDE4Defaults)
 include (MacroLibrary)
@@ -24,6 +25,7 @@ add_definitions (${KDE4_DEFINITIONS}
 include_directories (${KDE4_INCLUDES}
                      ${TELEPATHY_QT4_INCLUDE_DIR}
                      ${KTP_INCLUDE_DIR}
+                     ${QJSON_INCLUDE_DIR}
 )
 
 set(ktp_auth_handler_SRCS
@@ -56,6 +58,7 @@ target_link_libraries(ktp-auth-handler
     ${KDE4_KDECORE_LIBS}
     ${KDE4_KDEUI_LIBS}
     ${KDE4_KDEWEBKIT_LIBS}
+    ${QJSON_LIBRARIES}
 )
 
 configure_file(org.freedesktop.Telepathy.Client.KTp.SASLHandler.service.in
diff --git a/cmake/modules/FindQJSON.cmake b/cmake/modules/FindQJSON.cmake
new file mode 100644
index 0000000..c237b0f
--- /dev/null
+++ b/cmake/modules/FindQJSON.cmake
@@ -0,0 +1,46 @@
+# Find QJSON - JSON handling library for Qt
+#
+# This module defines
+#  QJSON_FOUND - whether the qsjon library was found
+#  QJSON_LIBRARIES - the qjson library
+#  QJSON_INCLUDE_DIR - the include path of the qjson library
+#
+
+if (QJSON_INCLUDE_DIR AND QJSON_LIBRARIES)
+
+  # Already in cache
+  set (QJSON_FOUND TRUE)
+
+else (QJSON_INCLUDE_DIR AND QJSON_LIBRARIES)
+#
+#   if (NOT WIN32)
+#     # use pkg-config to get the values of QJSON_INCLUDE_DIRS
+#     # and QJSON_LIBRARY_DIRS to add as hints to the find commands.
+#     include (FindPkgConfig)
+#     pkg_check_modules (QJSON REQUIRED QJson>=0.5)
+#   endif (NOT WIN32)
+
+  find_library (QJSON_LIBRARIES
+    NAMES
+    qjson
+    PATHS
+    ${QJSON_LIBRARY_DIRS}
+    ${LIB_INSTALL_DIR}
+    ${KDE4_LIB_DIR}
+  )
+
+  find_path (QJSON_INCLUDE_DIR
+    NAMES
+    parser.h
+    PATH_SUFFIXES
+    qjson
+    PATHS
+    ${QJSON_INCLUDE_DIRS}
+    ${INCLUDE_INSTALL_DIR}
+    ${KDE4_INCLUDE_DIR}
+  )
+
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(QJSON DEFAULT_MSG QJSON_LIBRARIES QJSON_INCLUDE_DIR)
+
+endif (QJSON_INCLUDE_DIR AND QJSON_LIBRARIES)
diff --git a/x-messenger-oauth2-auth-operation.cpp b/x-messenger-oauth2-auth-operation.cpp
index e4aa800..518367c 100644
--- a/x-messenger-oauth2-auth-operation.cpp
+++ b/x-messenger-oauth2-auth-operation.cpp
@@ -24,7 +24,8 @@
 #include <TelepathyQt/Account>
 #include <KTp/wallet-interface.h>
 
-const QLatin1String XMessengerOAuth2TokenWalletEntry("token");
+const QLatin1String XMessengerOAuth2AccessTokenWalletEntry("access_token");
+const QLatin1String XMessengerOAuth2RefreshTokenWalletEntry("refresh_token");
 
 XMessengerOAuth2AuthOperation::XMessengerOAuth2AuthOperation(
         const Tp::AccountPtr &account,
@@ -47,9 +48,9 @@ void XMessengerOAuth2AuthOperation::onSASLStatusChanged(uint status, const QStri
 {
     switch (status) {
     case Tp::SASLStatusNotStarted :
-        if (KTp::WalletInterface::hasEntry(m_account, XMessengerOAuth2TokenWalletEntry)) {
+        if (KTp::WalletInterface::hasEntry(m_account, XMessengerOAuth2AccessTokenWalletEntry)) {
             m_saslIface->StartMechanismWithData(QLatin1String("X-MESSENGER-OAUTH2"),
-                                                QByteArray::fromBase64(KTp::WalletInterface::entry(m_account, XMessengerOAuth2TokenWalletEntry).toLatin1()));
+                                                QByteArray::fromBase64(KTp::WalletInterface::entry(m_account, XMessengerOAuth2AccessTokenWalletEntry).toLatin1()));
             return;
         }
 
@@ -79,7 +80,7 @@ void XMessengerOAuth2AuthOperation::onSASLStatusChanged(uint status, const QStri
     case Tp::SASLStatusServerFailed:
     {
         kDebug() << "Error authenticating - reason:" << reason << "- details:" << details;
-        if (KTp::WalletInterface::hasEntry(m_account, XMessengerOAuth2TokenWalletEntry)) {
+        if (KTp::WalletInterface::hasEntry(m_account, XMessengerOAuth2AccessTokenWalletEntry)) {
             // We cannot try again (TODO canTryAgain seems to be always false for
             // X-MESSENGER-OAUTH but we should insert a check here)
             // but we can remove the token and request again to set the account
@@ -88,7 +89,7 @@ void XMessengerOAuth2AuthOperation::onSASLStatusChanged(uint status, const QStri
             // next time we will prompt for password and the user won't see any
             // difference except for an authentication error notification
             // TODO There should be a way to renew the token if it is expired.
-            KTp::WalletInterface::removeEntry(m_account, XMessengerOAuth2TokenWalletEntry);
+            KTp::WalletInterface::removeEntry(m_account, XMessengerOAuth2AccessTokenWalletEntry);
             Tp::Presence requestedPresence = m_account->requestedPresence();
             m_account->setRequestedPresence(requestedPresence);
         }
@@ -116,7 +117,8 @@ void XMessengerOAuth2AuthOperation::onDialogFinished(int result)
         return;
     case QDialog::Accepted:
         kDebug() << QLatin1String(m_dialog.data()->accessToken());
-        KTp::WalletInterface::setEntry(m_account, XMessengerOAuth2TokenWalletEntry, QLatin1String(m_dialog.data()->accessToken().toBase64()));
+        KTp::WalletInterface::setEntry(m_account, XMessengerOAuth2AccessTokenWalletEntry, QLatin1String(m_dialog.data()->accessToken().toBase64()));
+        KTp::WalletInterface::setEntry(m_account, XMessengerOAuth2RefreshTokenWalletEntry, QLatin1String(m_dialog.data()->refreshToken().toBase64()));
         m_saslIface->StartMechanismWithData(QLatin1String("X-MESSENGER-OAUTH2"), m_dialog.data()->accessToken());
         if (!m_dialog.isNull()) {
             m_dialog.data()->deleteLater();
diff --git a/x-messenger-oauth2-prompt.cpp b/x-messenger-oauth2-prompt.cpp
index c987ffd..603b99b 100644
--- a/x-messenger-oauth2-prompt.cpp
+++ b/x-messenger-oauth2-prompt.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Daniele E. Domenichelli <daniele.domenichelli at gmail.com>
+ * Copyright (C) 2011, 2012 Daniele E. Domenichelli <daniele.domenichelli at gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,17 +25,28 @@
 #include <KWebView>
 #include <KWebPage>
 
-#include <QtWebKit/QWebSettings>
 #include <QtGui/QProgressBar>
 #include <QtGui/QBoxLayout>
 #include <QtGui/QLayout>
 #include <QtNetwork/QNetworkReply>
 
+#include <QtWebKit/QWebSettings>
+#include <QtWebKit/QWebFrame>
+
+#include <qjson/parser.h>
+
 
 const QLatin1String msnClientID("000000004C070A47");
+const QLatin1String scopes("wl.messenger wl.offline_access");
 const QLatin1String redirectUri("https://oauth.live.com/desktop");
-const QLatin1String request("https://oauth.live.com/authorize?response_type=token&redirect_uri=%1&client_id=%2&scope=wl.messenger");
-const QLatin1String tokenParameter("access_token=");
+const QLatin1String tokenUri("https://oauth.live.com/token");
+
+const QLatin1String authorizeRequest("https://oauth.live.com/authorize?client_id=%1&scope=%2&response_type=code&redirect_uri=%3");
+const QLatin1String tokenRequest("https://oauth.live.com/token?client_id=%1&redirect_uri=%2&code=%3&grant_type=authorization_code");
+
+const QLatin1String codeParameter("code");
+const QLatin1String accessTokenParameter("access_token");
+const QLatin1String refreshTokenParameter("refresh_token");
 
 XMessengerOAuth2Prompt::XMessengerOAuth2Prompt(QWidget* parent) :
     KDialog(parent),
@@ -97,7 +108,7 @@ XMessengerOAuth2Prompt::XMessengerOAuth2Prompt(QWidget* parent) :
             SLOT(onUnsupportedContent(QNetworkReply*)));
 
     // start loading the login URL
-    KUrl url(QString(request).arg(redirectUri).arg(msnClientID));
+    KUrl url(QString(authorizeRequest).arg(msnClientID).arg(scopes).arg(redirectUri));
     m_webView->load(url.url());
 }
 
@@ -110,6 +121,11 @@ QByteArray XMessengerOAuth2Prompt::accessToken() const
     return m_accessToken;
 }
 
+QByteArray XMessengerOAuth2Prompt::refreshToken() const
+{
+    return m_refreshToken;
+}
+
 QSize XMessengerOAuth2Prompt::sizeHint() const
 {
     return QSize(500, 600);
@@ -122,7 +138,8 @@ void XMessengerOAuth2Prompt::onUrlChanged(const QUrl &url)
         // This is not the url containing the token
         return;
     }
-    extractToken(url);
+    kDebug() << "This is the url we are waiting for";
+    extractCode(url);
 }
 
 void XMessengerOAuth2Prompt::onLinkClicked(const QUrl& url)
@@ -137,6 +154,12 @@ void XMessengerOAuth2Prompt::onLoadFinished(bool ok)
         m_loginPageLoaded = true;
         Q_EMIT loginPageLoaded(ok);
     }
+    if (m_webView->url().toString().indexOf(tokenUri) != 0) {
+        // This is not the url containing the token
+        return;
+    }
+    extractTokens(m_webView->page()->currentFrame()->toPlainText());
+
 }
 
 void XMessengerOAuth2Prompt::onUnsupportedContent(QNetworkReply* reply)
@@ -146,37 +169,66 @@ void XMessengerOAuth2Prompt::onUnsupportedContent(QNetworkReply* reply)
     // request
     // FIXME: In the future this might want to remove this
     QUrl url =  reply->url();
+    kDebug() << url;
     if (url.toString().indexOf(redirectUri) != 0) {
         // This is not the url containing the token
         return;
     }
-    extractToken(url);
+    extractCode(url);
 }
 
-void XMessengerOAuth2Prompt::extractToken(const QUrl &url)
+void XMessengerOAuth2Prompt::extractCode(const QUrl &url)
 {
-    QString accessToken;
-    Q_FOREACH (const QString &token, QString::fromLatin1(url.encodedFragment()).split(QLatin1Char('&'))) {
-        // Get the URL fragment part and iterate over the parameters of the request
-        if (token.indexOf(tokenParameter) == 0) {
-            // This is the token that we are looking for (we are not interested
-            // in the "expires_in" part, etc.)
-            accessToken = token;
-            break;
-        }
+    kDebug() << url;
+    QString code = url.queryItemValue(codeParameter);
+
+    if (code.isEmpty()) {
+        // Could not find the access token
+        kWarning() << "Code not found";
+        return;
     }
-    if (accessToken.isEmpty()) {
-        // Could not find the token
-        kWarning() << "Token not found";
+
+    kDebug() << "Code found:" << code;
+
+    // start loading the login URL
+    KUrl tokenUrl(QString(tokenRequest).arg(msnClientID).arg(redirectUri).arg(code));
+    m_webView->load(tokenUrl.url());
+}
+
+
+void XMessengerOAuth2Prompt::extractTokens(const QString &text)
+{
+    kDebug() << text;
+
+    QJson::Parser parser;
+    bool ok;
+
+    QVariantMap result = parser.parse(text.toLatin1(), &ok).toMap();
+    if (!ok) {
+        kWarning() << "An error occured during parsing reply";
+        return;
+    }
+
+    if (!result.contains(refreshTokenParameter)) {
+        // Could not find the refresh token
+        kWarning() << "Refresh token not found";
         return;
     }
 
-    accessToken = accessToken.split(QLatin1Char('=')).at(1);    // Split by "access_token=..." and take latter part
+    m_refreshToken = result[refreshTokenParameter].toByteArray();
+    Q_EMIT refreshTokenTaken(m_refreshToken);
+
+    if (!result.contains(accessTokenParameter)) {
+        // Could not find the access token
+        kWarning() << "Access token not found";
+        return;
+    }
 
     // Wocky will base64 encode, but token actually already is base64, so we
     // decode now and it will be re-encoded.
-    m_accessToken = QByteArray::fromBase64(QByteArray::fromPercentEncoding(accessToken.toUtf8()));
+    m_accessToken = QByteArray::fromBase64(QByteArray::fromPercentEncoding(result[accessTokenParameter].toByteArray()));
     Q_EMIT accessTokenTaken(m_accessToken);
+
     accept();
 }
 
diff --git a/x-messenger-oauth2-prompt.h b/x-messenger-oauth2-prompt.h
index c87d6e7..16bb035 100644
--- a/x-messenger-oauth2-prompt.h
+++ b/x-messenger-oauth2-prompt.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Daniele E. Domenichelli <daniele.domenichelli at gmail.com>
+ * Copyright (C) 2011, 2012 Daniele E. Domenichelli <daniele.domenichelli at gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -34,6 +34,7 @@ public:
     ~XMessengerOAuth2Prompt();
 
     QByteArray accessToken() const;
+    QByteArray refreshToken() const;
 
 protected:
     virtual QSize sizeHint() const;
@@ -41,6 +42,7 @@ protected:
 Q_SIGNALS:
     void loginPageLoaded(bool ok);
     void accessTokenTaken(const QByteArray &accessToken);
+    void refreshTokenTaken(const QByteArray &refreshToken);
 
 private Q_SLOTS:
     void onUrlChanged(const QUrl &url);
@@ -49,12 +51,14 @@ private Q_SLOTS:
     void onUnsupportedContent(QNetworkReply* reply);
 
 private:
-    void extractToken(const QUrl &url);
+    void extractCode(const QUrl &url);
+    void extractTokens(const QString &text);
 
     KWebView *m_webView;
     QProgressBar *m_ProgressBar;
     bool m_loginPageLoaded;
     QByteArray m_accessToken;
+    QByteArray m_refreshToken;
 };
 
 #endif // X_MESSENGER_OAUTH2_PROMPT_H

-- 
ktp-auth-handler packaging



More information about the pkg-kde-commits mailing list