[vlmc-devel] commit: YouTube Auth class Uses HTTPS and global proxy (Rohit Yadav )
git at videolan.org
git at videolan.org
Tue Aug 24 11:11:19 CEST 2010
vlmc | branch: master | Rohit Yadav <rohityadav89 at gmail.com> | Mon Aug 23 22:15:35 2010 +0530| [38afc32e84763a3efa20357b95f97363e9360ca6] | committer: Hugo Beauzée-Luyssen
YouTube Auth class Uses HTTPS and global proxy
Signed-off-by: Hugo Beauzée-Luyssen <beauze.h at gmail.com>
> http://git.videolan.org/gitweb.cgi/vlmc.git/?a=commit;h=38afc32e84763a3efa20357b95f97363e9360ca6
---
src/Services/YouTube/YouTubeAuthenticator.cpp | 205 +++++++++++++++++++++++++
src/Services/YouTube/YouTubeAuthenticator.h | 87 +++++++++++
2 files changed, 292 insertions(+), 0 deletions(-)
diff --git a/src/Services/YouTube/YouTubeAuthenticator.cpp b/src/Services/YouTube/YouTubeAuthenticator.cpp
new file mode 100644
index 0000000..633777d
--- /dev/null
+++ b/src/Services/YouTube/YouTubeAuthenticator.cpp
@@ -0,0 +1,205 @@
+/*****************************************************************************
+ * YouTubeAuthenticator.cpp: Auth Class for YouTube
+ *****************************************************************************
+ * Copyright (C) 2010 VideoLAN
+ *
+ * Authors: Rohit Yadav <rohityadav89 AT gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "YouTubeAuthenticator.h"
+#include "YouTubeService.h"
+
+#include <QByteArray>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QString>
+#include <QStringList>
+#include <QUrl>
+
+#include <QDebug>
+
+YouTubeAuthenticator::YouTubeAuthenticator( YouTubeService* service,
+ const QString& username,
+ const QString& password )
+{
+ /* Stores pointer to main service object and user credentials */
+ m_service = service;
+ m_username = username;
+ m_password = password;
+
+ m_nam = new QNetworkAccessManager( this );
+
+ /* If SSL is available, handle SSL errors for better security */
+ #ifndef QT_NO_OPENSSL
+ connect( m_nam, SIGNAL( sslErrors( QNetworkReply*, QList<QSslError> ) ),
+ m_service, SLOT( sslErrors( QNetworkReply*, QList<QSslError> ) ) );
+ #endif
+
+ authInit();
+ setPOSTData();
+}
+
+YouTubeAuthenticator::~YouTubeAuthenticator()
+{
+ delete m_nam;
+}
+
+void
+YouTubeAuthenticator::authInit()
+{
+ m_isAuthenticated = false;
+ m_nick.clear();
+ m_authString.clear();
+ m_authError.clear();
+}
+
+void
+YouTubeAuthenticator::authenticate()
+{
+ /* Network request to be sent */
+ QNetworkRequest request = getNetworkRequest();
+
+ /* Sends the auth details using HTTPS POST*/
+ QNetworkReply* reply = m_nam->post( request, getPOSTData() );
+ m_service->m_state = AuthStart;
+
+ connect( reply, SIGNAL( finished() ), this, SLOT( authFinished() ) );
+ connect( reply, SIGNAL( error(QNetworkReply::NetworkError ) ),
+ m_service, SLOT( networkError( QNetworkReply::NetworkError ) ) );
+}
+
+void
+YouTubeAuthenticator::authFinished()
+{
+ QNetworkReply *reply = static_cast<QNetworkReply *>( sender() );
+ QByteArray data = reply->readAll();
+
+ if( setAuthData( data ) )
+ m_service->m_state = AuthFinish;
+
+ disconnect( reply, SIGNAL( finished() ), this, SLOT( authFinished() ) );
+ disconnect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ),
+ m_service, SLOT( networkError( QNetworkReply::NetworkError ) ) );
+
+ reply->close();
+ reply->deleteLater();
+}
+
+void
+YouTubeAuthenticator::error( QString& e )
+{
+ qDebug() << "[YouTube AUTH ERROR]: " << e;
+ emit authError( e );
+}
+
+bool
+YouTubeAuthenticator::isAuthenticated()
+{
+ return m_isAuthenticated;
+}
+
+const QByteArray&
+YouTubeAuthenticator::getPOSTData()
+{
+ return m_postData;
+}
+
+QNetworkRequest
+YouTubeAuthenticator::getNetworkRequest()
+{
+ authInit();
+ QUrl url( LOGIN_URL );
+
+ QNetworkRequest request;
+ request.setHeader( QNetworkRequest::ContentTypeHeader,
+ "application/x-www-form-urlencoded" );
+ request.setUrl( url );
+
+ return request;
+}
+
+const QString&
+YouTubeAuthenticator::getAuthString()
+{
+ return m_authString;
+}
+
+
+const QString&
+YouTubeAuthenticator::getNick()
+{
+ return m_nick;
+}
+
+void
+YouTubeAuthenticator::setCredentials( const QString& username, const QString& password )
+{
+ m_username = username;
+ m_password = password;
+ setPOSTData();
+}
+
+void
+YouTubeAuthenticator::setPOSTData()
+{
+ m_postData.clear();
+ m_postData += QString("accountType=HOSTED_OR_GOOGLE&Email=%1&Passwd=%2"
+ "&service=youtube&source=VLMC").arg(m_username, m_password);
+}
+
+void
+YouTubeAuthenticator::setServiceProvider( YouTubeService *service )
+{
+ m_service = service;
+}
+
+bool
+YouTubeAuthenticator::setAuthData( QByteArray& data )
+{
+ /* Parses data received after sending auth details */
+ QStringList lines = QString( data ).split( "\n", QString::SkipEmptyParts );
+
+ foreach( QString line, lines )
+ {
+ QStringList tokenList = line.split( "=" );
+
+ if( tokenList.at(0) == "Error" )
+ {
+ m_authError = tokenList.at(1);
+ error( m_authError );
+ continue;
+ }
+
+ if( tokenList.at(0) == "Auth" )
+ {
+ m_authString = tokenList.at(1);
+ continue;
+ }
+
+ if( tokenList.at(0) == "YouTubeUser" )
+ {
+ m_nick = tokenList.at(1);
+ }
+ }
+
+ if( !m_authString.isEmpty() && !m_nick.isEmpty() && m_authError.isEmpty() )
+ m_isAuthenticated = true;
+
+ emit authOver();
+ return m_isAuthenticated;
+}
diff --git a/src/Services/YouTube/YouTubeAuthenticator.h b/src/Services/YouTube/YouTubeAuthenticator.h
new file mode 100644
index 0000000..df7ed96
--- /dev/null
+++ b/src/Services/YouTube/YouTubeAuthenticator.h
@@ -0,0 +1,87 @@
+/*****************************************************************************
+ * YouTubeAuthenticator.h: Auth Class for YouTube
+ *****************************************************************************
+ * Copyright (C) 2010 VideoLAN
+ *
+ * Authors: Rohit Yadav <rohityadav89 AT gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef YOUTUBEAUTHENTICATOR_H
+#define YOUTUBEAUTHENTICATOR_H
+
+#include <QObject>
+
+#define LOGIN_URL "https://www.google.com/youtube/accounts/ClientLogin"
+
+class QByteArray;
+class QNetworkAccessManager;
+class QNetworkRequest;
+class YouTubeService;
+
+class YouTubeAuthenticator : public QObject
+{
+ Q_OBJECT
+
+ public:
+ YouTubeAuthenticator( YouTubeService* service = 0,
+ const QString& username = "",
+ const QString& password = "" );
+ ~YouTubeAuthenticator();
+
+ void authenticate();
+ bool isAuthenticated();
+
+ const QString& getAuthString();
+ const QString& getNick();
+
+ void setServiceProvider( YouTubeService* service );
+ void setCredentials( const QString& username, const QString& password );
+ bool setAuthData( QByteArray& data );
+
+ private:
+ void authInit();
+ void error( QString& error );
+
+ QNetworkRequest getNetworkRequest();
+ const QByteArray& getPOSTData();
+
+ void setPOSTData();
+
+ /* Youtube Credentials */
+ QString m_username;
+ QString m_password;
+
+ /* HTTP/S POST HEADER */
+ QByteArray m_postData;
+
+ /* Youtube tokens */
+ QString m_authString;
+ QString m_nick;
+ bool m_isAuthenticated;
+ QString m_authError;
+
+ YouTubeService* m_service;
+ QNetworkAccessManager* m_nam;
+
+ private slots:
+ void authFinished();
+
+ signals:
+ void authOver();
+ void authError( QString );
+};
+#endif // YOUTUBEAUTHENTICATOR_H
More information about the Vlmc-devel
mailing list