From 33ae2eb19f7609ff29e034f671174aa426a7c67f Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Thu, 27 Feb 2014 13:18:42 +0100 Subject: [PATCH] Try to handle auth requests by a Shibboleth IdP --- src/CMakeLists.txt | 2 + src/creds/shibboleth/authenticationdialog.cpp | 54 +++++++++++++++++++ src/creds/shibboleth/authenticationdialog.h | 41 ++++++++++++++ src/creds/shibboleth/shibbolethwebview.cpp | 22 ++++++++ src/creds/shibboleth/shibbolethwebview.h | 1 + 5 files changed, 120 insertions(+) create mode 100644 src/creds/shibboleth/authenticationdialog.cpp create mode 100644 src/creds/shibboleth/authenticationdialog.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f9eea207e3..59c482fbfc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -94,6 +94,7 @@ set(libsync_SRCS creds/shibboleth/shibbolethwebview.cpp creds/shibboleth/shibbolethrefresher.cpp creds/shibboleth/shibbolethconfigfile.cpp + creds/shibboleth/authenticationdialog.cpp creds/credentialscommon.cpp 3rdparty/qjson/json.cpp ) @@ -127,6 +128,7 @@ set(libsync_HEADERS creds/shibboleth/shibbolethwebview.h creds/shibboleth/shibbolethrefresher.h creds/shibboleth/shibbolethconfigfile.h + creds/shibboleth/authenticationdialog.h creds/credentialscommon.h 3rdparty/qjson/json.h ) diff --git a/src/creds/shibboleth/authenticationdialog.cpp b/src/creds/shibboleth/authenticationdialog.cpp new file mode 100644 index 0000000000..5ad84e3c9b --- /dev/null +++ b/src/creds/shibboleth/authenticationdialog.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 by Daniel Molkentin + * + * 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; version 2 of the License. + * + * 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. + */ + +#include "authenticationdialog.h" + +#include +#include +#include +#include +#include + +namespace Mirall { + +AuthenticationDialog::AuthenticationDialog(const QString &realm, const QString &domain, QWidget *parent) + : QDialog(parent) + , _user(new QLineEdit) + , _password(new QLineEdit) +{ + setWindowTitle(tr("Authentication Required")); + QVBoxLayout *lay = new QVBoxLayout(this); + QLabel *label = new QLabel(tr("Enter username and password for '%1' at %2.").arg(realm, domain)); + lay->addWidget(label); + + QFormLayout *form = new QFormLayout; + form->addRow(tr("&User:"), _user); + form->addRow(tr("&Password:"), _password); + lay->addLayout(form); + QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal); + connect(box, SIGNAL(accepted()), this, SLOT(accept())); + connect(box, SIGNAL(rejected()), this, SLOT(reject())); + lay->addWidget(box); +} + +QString AuthenticationDialog::user() const +{ + return _user->text(); +} + +QString AuthenticationDialog::password() const +{ + return _password->text(); +} + +} // namespace Mirall diff --git a/src/creds/shibboleth/authenticationdialog.h b/src/creds/shibboleth/authenticationdialog.h new file mode 100644 index 0000000000..adcd5ca015 --- /dev/null +++ b/src/creds/shibboleth/authenticationdialog.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2014 by Daniel Molkentin + * + * 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; version 2 of the License. + * + * 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. + */ + +#ifndef MIRALL_AUTHENTICATIONDIALOG_H +#define MIRALL_AUTHENTICATIONDIALOG_H + +#include + +class QLineEdit; + +namespace Mirall { + +/** @brief Authenticate a user for a specific credential given his credentials */ +class AuthenticationDialog : public QDialog { + Q_OBJECT +public: + AuthenticationDialog(const QString &realm, const QString &domain, QWidget *parent = 0); + + QString user() const; + QString password() const; + +private: + QLineEdit *_user; + QLineEdit *_password; + +}; + + +} // namespace Mirall + +#endif // MIRALL_AUTHENTICATIONDIALOG_H diff --git a/src/creds/shibboleth/shibbolethwebview.cpp b/src/creds/shibboleth/shibbolethwebview.cpp index 825c1a57d0..8991dcb215 100644 --- a/src/creds/shibboleth/shibbolethwebview.cpp +++ b/src/creds/shibboleth/shibbolethwebview.cpp @@ -17,9 +17,12 @@ #include #include #include +#include +#include #include "creds/shibboleth/shibbolethcookiejar.h" #include "creds/shibboleth/shibbolethwebview.h" +#include "creds/shibboleth/authenticationdialog.h" #include "mirall/account.h" #include "mirall/mirallaccessmanager.h" #include "mirall/theme.h" @@ -35,6 +38,8 @@ void ShibbolethWebView::setup(Account *account, ShibbolethCookieJar* jar) // the account object, which already can do this connect(nm, SIGNAL(sslErrors(QNetworkReply*,QList)), account, SLOT(slotHandleErrors(QNetworkReply*,QList))); + connect(nm, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(slotHandleAuthentication(QNetworkReply*,QAuthenticator*))); QWebPage* page = new QWebPage(this); @@ -122,4 +127,21 @@ void ShibbolethWebView::slotLoadFinished(bool success) } } +void ShibbolethWebView::slotHandleAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) +{ + Q_UNUSED(reply) + QUrl url = reply->url(); + // show only scheme, host and port + QUrl reducedUrl; + reducedUrl.setScheme(url.scheme()); + reducedUrl.setHost(url.host()); + reducedUrl.setPort(url.port()); + + AuthenticationDialog dialog(authenticator->realm(), reducedUrl.toString(), this); + if (dialog.exec() == QDialog::Accepted) { + authenticator->setUser(dialog.user()); + authenticator->setPassword(dialog.password()); + } +} + } // ns Mirall diff --git a/src/creds/shibboleth/shibbolethwebview.h b/src/creds/shibboleth/shibbolethwebview.h index 53a5b03371..9624fed8bf 100644 --- a/src/creds/shibboleth/shibbolethwebview.h +++ b/src/creds/shibboleth/shibbolethwebview.h @@ -48,6 +48,7 @@ private Q_SLOTS: void onNewCookiesForUrl(const QList& cookieList, const QUrl& url); void slotLoadStarted(); void slotLoadFinished(bool success = true); + void slotHandleAuthentication(QNetworkReply*,QAuthenticator*); private: void setup(Account *account, ShibbolethCookieJar* jar);