2013-07-29 12:28:45 +00:00
/*
* Copyright ( C ) by Krzesimir Nowak < krzesimir @ endocode . 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
2016-10-25 09:00:07 +00:00
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
2013-07-29 12:28:45 +00:00
*
* 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 .
*/
2013-08-07 12:57:22 +00:00
# include <QApplication>
2013-08-06 14:17:55 +00:00
# include <QDebug>
2013-07-29 12:28:45 +00:00
# include <QNetworkCookie>
2014-05-14 09:22:56 +00:00
# include <QNetworkCookieJar>
2013-07-29 12:28:45 +00:00
# include <QWebFrame>
# include <QWebPage>
2013-11-07 17:47:38 +00:00
# include <QMessageBox>
2014-02-27 12:18:42 +00:00
# include <QNetworkReply>
2015-02-17 14:28:14 +00:00
# include <QSettings>
2016-07-25 15:47:23 +00:00
# include <QMainWindow>
2013-07-29 12:28:45 +00:00
2014-05-14 09:11:45 +00:00
# include "creds/shibboleth/shibbolethwebview.h"
# include "creds/shibbolethcredentials.h"
2014-07-10 22:31:24 +00:00
# include "account.h"
# include "logger.h"
2014-11-09 22:25:57 +00:00
# include "accessmanager.h"
2014-07-10 22:31:24 +00:00
# include "theme.h"
2015-02-17 14:28:14 +00:00
# include "configfile.h"
namespace {
const char ShibbolethWebViewGeometryC [ ] = " ShibbolethWebView/Geometry " ;
}
2013-07-29 12:28:45 +00:00
2014-11-09 21:34:07 +00:00
namespace OCC
2013-07-29 12:28:45 +00:00
{
2015-10-15 13:51:23 +00:00
class UserAgentWebPage : public QWebPage {
public :
2016-05-17 10:28:07 +00:00
UserAgentWebPage ( QObject * parent ) : QWebPage ( parent )
{
if ( ! qgetenv ( " OWNCLOUD_SHIBBOLETH_DEBUG " ) . isEmpty ( ) ) {
settings ( ) - > setAttribute ( QWebSettings : : DeveloperExtrasEnabled , true ) ;
}
}
2015-10-15 13:51:23 +00:00
QString userAgentForUrl ( const QUrl & url ) const {
return QWebPage : : userAgentForUrl ( url ) + " " + Utility : : userAgentString ( ) ;
}
} ;
2014-12-18 11:09:48 +00:00
ShibbolethWebView : : ShibbolethWebView ( AccountPtr account , QWidget * parent )
2014-05-14 09:11:45 +00:00
: QWebView ( parent )
, _account ( account )
2014-05-15 07:43:07 +00:00
, _accepted ( false )
2014-07-07 11:52:55 +00:00
, _cursorOverriden ( false )
2013-08-06 14:17:55 +00:00
{
2014-05-14 09:11:45 +00:00
// no minimize
setWindowFlags ( Qt : : Dialog ) ;
setAttribute ( Qt : : WA_DeleteOnClose ) ;
2015-02-17 14:28:14 +00:00
2015-10-15 13:51:23 +00:00
QWebPage * page = new UserAgentWebPage ( this ) ;
2013-08-07 12:57:22 +00:00
connect ( page , SIGNAL ( loadStarted ( ) ) ,
this , SLOT ( slotLoadStarted ( ) ) ) ;
connect ( page , SIGNAL ( loadFinished ( bool ) ) ,
2013-11-07 17:47:38 +00:00
this , SLOT ( slotLoadFinished ( bool ) ) ) ;
2013-08-06 14:17:55 +00:00
2015-08-13 15:09:39 +00:00
// Make sure to accept the same SSL certificate issues as the regular QNAM we use for syncing
QObject : : connect ( page - > networkAccessManager ( ) , SIGNAL ( sslErrors ( QNetworkReply * , QList < QSslError > ) ) ,
_account . data ( ) , SLOT ( slotHandleSslErrors ( QNetworkReply * , QList < QSslError > ) ) ) ;
2014-05-14 09:11:45 +00:00
2015-06-18 18:41:00 +00:00
// The Account keeps ownership of the cookie jar, it must outlive this webview.
account - > lendCookieJarTo ( page - > networkAccessManager ( ) ) ;
2014-05-14 09:11:45 +00:00
connect ( page - > networkAccessManager ( ) - > cookieJar ( ) ,
SIGNAL ( newCookiesForUrl ( QList < QNetworkCookie > , QUrl ) ) ,
this , SLOT ( onNewCookiesForUrl ( QList < QNetworkCookie > , QUrl ) ) ) ;
2016-07-25 15:47:23 +00:00
2013-11-07 17:47:38 +00:00
page - > mainFrame ( ) - > load ( account - > url ( ) ) ;
this - > setPage ( page ) ;
setWindowTitle ( tr ( " %1 - Authenticate " ) . arg ( Theme : : instance ( ) - > appNameGUI ( ) ) ) ;
2014-05-28 08:03:07 +00:00
2016-07-25 15:47:23 +00:00
// Debug view to display the cipher suite
if ( ! qgetenv ( " OWNCLOUD_SHIBBOLETH_DEBUG " ) . isEmpty ( ) ) {
// open an additional window to display some cipher debug info
QWebPage * debugPage = new UserAgentWebPage ( this ) ;
debugPage - > mainFrame ( ) - > load ( QUrl ( " https://cc.dcsec.uni-hannover.de/ " ) ) ;
QWebView * debugView = new QWebView ( this ) ;
debugView - > setPage ( debugPage ) ;
QMainWindow * window = new QMainWindow ( this ) ;
window - > setWindowTitle ( tr ( " SSL Chipher Debug View " ) ) ;
window - > setCentralWidget ( debugView ) ;
window - > show ( ) ;
}
2014-05-28 08:03:07 +00:00
// If we have a valid cookie, it's most likely expired. We can use this as
// as a criteria to tell the user why the browser window pops up
2015-01-16 14:18:01 +00:00
QNetworkCookie shibCookie = ShibbolethCredentials : : findShibCookie ( _account . data ( ) , ShibbolethCredentials : : accountCookies ( _account . data ( ) ) ) ;
2014-05-28 08:03:07 +00:00
if ( shibCookie ! = QNetworkCookie ( ) ) {
Logger : : instance ( ) - > postOptionalGuiLog ( tr ( " Reauthentication required " ) , tr ( " Your session has expired. You need to re-login to continue to use the client. " ) ) ;
}
2015-02-17 14:28:14 +00:00
ConfigFile config ;
QSettings settings ( config . configFile ( ) ) ;
resize ( 900 , 700 ) ; // only effective the first time, later overridden by restoreGeometry
restoreGeometry ( settings . value ( ShibbolethWebViewGeometryC ) . toByteArray ( ) ) ;
2013-08-06 14:17:55 +00:00
}
2013-08-07 12:57:22 +00:00
ShibbolethWebView : : ~ ShibbolethWebView ( )
{
2015-02-17 14:28:14 +00:00
ConfigFile config ;
QSettings settings ( config . configFile ( ) ) ;
settings . setValue ( ShibbolethWebViewGeometryC , saveGeometry ( ) ) ;
2013-08-07 12:57:22 +00:00
}
2013-08-06 14:17:55 +00:00
void ShibbolethWebView : : onNewCookiesForUrl ( const QList < QNetworkCookie > & cookieList , const QUrl & url )
2013-07-29 12:28:45 +00:00
{
2014-05-14 09:11:45 +00:00
if ( url . host ( ) = = _account - > url ( ) . host ( ) ) {
2015-01-16 14:18:01 +00:00
QNetworkCookie shibCookie = ShibbolethCredentials : : findShibCookie ( _account . data ( ) , cookieList ) ;
2014-05-14 09:11:45 +00:00
if ( shibCookie ! = QNetworkCookie ( ) ) {
2015-01-26 14:56:57 +00:00
Q_EMIT shibbolethCookieReceived ( shibCookie ) ;
2014-05-15 07:43:07 +00:00
accept ( ) ;
2014-05-14 09:11:45 +00:00
close ( ) ;
}
2013-07-29 12:28:45 +00:00
}
}
2014-05-14 09:11:45 +00:00
void ShibbolethWebView : : closeEvent ( QCloseEvent * event )
2013-08-06 12:35:11 +00:00
{
2014-07-07 11:52:55 +00:00
if ( _cursorOverriden ) {
QApplication : : restoreOverrideCursor ( ) ;
}
2014-05-15 07:43:07 +00:00
if ( ! _accepted ) {
Q_EMIT rejected ( ) ;
}
2014-05-14 09:11:45 +00:00
QWebView : : closeEvent ( event ) ;
2013-08-06 12:35:11 +00:00
}
2013-08-07 12:57:22 +00:00
void ShibbolethWebView : : slotLoadStarted ( )
{
2014-07-07 11:52:55 +00:00
if ( ! _cursorOverriden ) {
QApplication : : setOverrideCursor ( QCursor ( Qt : : WaitCursor ) ) ;
_cursorOverriden = true ;
}
2013-08-07 12:57:22 +00:00
}
2013-11-07 17:47:38 +00:00
void ShibbolethWebView : : slotLoadFinished ( bool success )
2013-08-07 12:57:22 +00:00
{
2014-07-07 11:52:55 +00:00
if ( _cursorOverriden ) {
QApplication : : restoreOverrideCursor ( ) ;
}
2013-11-07 17:47:38 +00:00
if ( ! title ( ) . isNull ( ) ) {
2015-02-17 14:28:14 +00:00
setWindowTitle ( QString : : fromLatin1 ( " %1 - %2 (%3) " ) . arg ( Theme : : instance ( ) - > appNameGUI ( ) , title ( ) , url ( ) . host ( ) ) ) ;
2013-11-07 17:47:38 +00:00
}
if ( ! success ) {
2014-02-27 15:59:07 +00:00
qDebug ( ) < < Q_FUNC_INFO < < " Could not load Shibboleth login page to log you in. " ;
2013-11-07 17:47:38 +00:00
}
2013-08-07 12:57:22 +00:00
}
2014-05-15 07:43:07 +00:00
void ShibbolethWebView : : accept ( )
{
_accepted = true ;
}
2014-11-09 21:34:07 +00:00
} // namespace OCC