From 8ed476ccf68ee6b291b9d145d5e0a6a957bd63c5 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 29 Aug 2011 00:10:30 -0400 Subject: [PATCH] Add QCA2 as an optional dep to tomahawk as well for HMAC signature generation for resolvers Also add a helper JS function --- CMakeLists.txt | 3 ++- data/js/tomahawk.js | 5 +++++ src/CMakeLists.txt | 2 ++ src/resolvers/qtscriptresolver.cpp | 27 +++++++++++++++++++++++++++ src/resolvers/qtscriptresolver.h | 6 ++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94f9e89dc..9626afd2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,7 +102,8 @@ check_taglib_filename( COMPLEX_TAGLIB_FILENAME ) macro_optional_find_package(Boost) macro_log_feature(Boost_FOUND "Boost" "Provides free peer-reviewed portable C++ source libraries" "http://www.boost.org" TRUE "" "") #FIXME: give useful explaination - +macro_optional_find_package(QCA2) +macro_log_feature(QCA2_FOUND "QCA2" "Provides encryption and signing functions required for Grooveshark resolver" "http://delta.affinix.com/qca/" TRUE "" "") # required #While we distribute our own liblastfm2, don't need to look for it diff --git a/data/js/tomahawk.js b/data/js/tomahawk.js index d6a2490b0..c95562a5c 100644 --- a/data/js/tomahawk.js +++ b/data/js/tomahawk.js @@ -91,6 +91,11 @@ var TomahawkResolver = { var configJson = JSON.stringify( config ); window.localStorage[ this.scriptPath() ] = configJson; + + this.newConfigSaved(); + }, + newConfigSaved: function() + { }, resolve: function( qid, artist, album, title ) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d6c8d0f81..94b939cda 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -163,6 +163,7 @@ INCLUDE_DIRECTORIES( ${QJSON_INCLUDE_DIR} ${LIBECHONEST_INCLUDE_DIR} ${LIBECHONEST_INCLUDE_DIR}/.. + ${QCA2_INCLUDE_DIR} ) SET( OS_SPECIFIC_LINK_LIBRARIES "" ) @@ -245,6 +246,7 @@ TARGET_LINK_LIBRARIES( tomahawk ${QXTWEB_LIBRARIES} ${QJSON_LIBRARIES} ${TAGLIB_LIBRARIES} + ${QCA2_LIBRARIES} ) diff --git a/src/resolvers/qtscriptresolver.cpp b/src/resolvers/qtscriptresolver.cpp index 00c555470..9cc49ae01 100644 --- a/src/resolvers/qtscriptresolver.cpp +++ b/src/resolvers/qtscriptresolver.cpp @@ -25,6 +25,7 @@ #include "utils/tomahawkutils.h" #include +#include #include "utils/logger.h" @@ -119,6 +120,32 @@ QtScriptResolverHelper::setResolverConfig( const QVariantMap& config ) m_resolverConfig = config; } +QString +QtScriptResolverHelper::hmac( const QByteArray& key, const QByteArray &input ) +{ + if ( !QCA::isSupported( "hmac(md5)" ) ) + { + tLog() << "HMAC(md5) not supported with qca-ossl plugin, or qca-ossl plugin is not installed! Unable to generate signature!"; + return QByteArray(); + } + + QCA::MessageAuthenticationCode md5hmac1( "hmac(md5)", QCA::SecureArray() ); + QCA::SymmetricKey keyObject( key ); + md5hmac1.setup( keyObject ); + + md5hmac1.update( QCA::SecureArray( input ) ); + QCA::SecureArray resultArray = md5hmac1.final(); + + QString result = QCA::arrayToHex( resultArray.toByteArray() ); + return result.toUtf8(); +} + +QString +QtScriptResolverHelper::md5( const QByteArray& input ) +{ + QByteArray const digest = QCryptographicHash::hash( input, QCryptographicHash::Md5 ); + return QString::fromLatin1( digest.toHex() ); +} void ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID ) diff --git a/src/resolvers/qtscriptresolver.h b/src/resolvers/qtscriptresolver.h index 62981c978..83c4942f5 100644 --- a/src/resolvers/qtscriptresolver.h +++ b/src/resolvers/qtscriptresolver.h @@ -30,6 +30,7 @@ #include #include #include +#include class QtScriptResolver; @@ -41,6 +42,10 @@ public: QtScriptResolverHelper( const QString& scriptPath, QtScriptResolver* parent ); void setResolverConfig( const QVariantMap& config ); + + // Return a HMAC (md5) signature of the input text with the desired key + Q_INVOKABLE QString hmac( const QByteArray& key, const QByteArray& input ); + Q_INVOKABLE QString md5( const QByteArray& input ); public slots: QByteArray readRaw( const QString& fileName ); QString readBase64( const QString& fileName ); @@ -58,6 +63,7 @@ private: QString m_scriptPath; QVariantMap m_resolverConfig; QtScriptResolver* m_resolver; + QCA::Initializer m_qcaInit; }; class ScriptEngine : public QWebPage