From 5c5a2d35583915653408aad0b48a9b648ae3191a Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Tue, 10 Nov 2015 15:26:31 +0100
Subject: Fix certificate-less login in Swift

Commit 8405fa1 changed the client certificate handling inside
Swiften to not fallback to certificate-less authentication
if there is an error.

This commit fixes Swift to only pass a certificate during login
if a file is set and the user wants to authenticate using client
certificates.

Test-Information:

Tested certificate-less login (worked).
Tested certificate-based login (worked).
Tested certificate-based login with deleted certificate file
(correct error reported).

Change-Id: I3b1393688c4802abfd7f66b7cb49ddd8c518e62b

diff --git a/Swift/QtUI/QtLoginWindow.cpp b/Swift/QtUI/QtLoginWindow.cpp
index 17176a9..229df7f 100644
--- a/Swift/QtUI/QtLoginWindow.cpp
+++ b/Swift/QtUI/QtLoginWindow.cpp
@@ -4,44 +4,46 @@
  * See the COPYING file for more information.
  */
 
-#include "QtLoginWindow.h"
+#include <Swift/QtUI/QtLoginWindow.h>
 
-#include <boost/bind.hpp>
-#include <boost/smart_ptr/make_shared.hpp>
 #include <algorithm>
 #include <cassert>
 
+#include <boost/bind.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+
 #include <QApplication>
 #include <QBoxLayout>
+#include <QCloseEvent>
 #include <QComboBox>
+#include <QCursor>
+#include <QDebug>
 #include <QDesktopWidget>
 #include <QFileDialog>
-#include <QStatusBar>
-#include <QToolButton>
+#include <QHBoxLayout>
+#include <QKeyEvent>
 #include <QLabel>
 #include <QMenuBar>
-#include <QHBoxLayout>
-#include <qdebug.h>
-#include <QCloseEvent>
-#include <QCursor>
 #include <QMessageBox>
-#include <QKeyEvent>
- 
-#include <Swift/Controllers/UIEvents/UIEventStream.h>
-#include <Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h>
-#include <Swift/Controllers/UIEvents/RequestFileTransferListUIEvent.h>
-#include <Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h>
-#include <Swift/Controllers/Settings/SettingsProvider.h>
-#include <Swift/Controllers/SettingConstants.h>
-#include <Swift/QtUI/QtUISettingConstants.h>
+#include <QStatusBar>
+#include <QToolButton>
+
 #include <Swiften/Base/Platform.h>
 #include <Swiften/Base/Paths.h>
 
-#include <QtAboutWidget.h>
-#include <QtSwiftUtil.h>
-#include <QtMainWindow.h>
-#include <QtUtilities.h>
-#include <QtConnectionSettingsWindow.h>
+#include <Swift/Controllers/SettingConstants.h>
+#include <Swift/Controllers/Settings/SettingsProvider.h>
+#include <Swift/Controllers/UIEvents/RequestFileTransferListUIEvent.h>
+#include <Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h>
+#include <Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
+
+#include <Swift/QtUI/QtAboutWidget.h>
+#include <Swift/QtUI/QtConnectionSettingsWindow.h>
+#include <Swift/QtUI/QtMainWindow.h>
+#include <Swift/QtUI/QtSwiftUtil.h>
+#include <Swift/QtUI/QtUISettingConstants.h>
+#include <Swift/QtUI/QtUtilities.h>
 
 #ifdef HAVE_SCHANNEL
 #include "CAPICertificateSelector.h"
@@ -394,15 +396,17 @@ void QtLoginWindow::loginClicked() {
 		}
 		CertificateWithKey::ref certificate;
 		std::string certificateString = Q2PSTRING(certificateFile_);
+		if (!certificateString.empty()) {
 #if defined(HAVE_SCHANNEL)
-		if (isCAPIURI(certificateString)) {
-			certificate = boost::make_shared<CAPICertificate>(certificateString, timerFactory_);
-		} else {
-			certificate = boost::make_shared<PKCS12Certificate>(certificateString, createSafeByteArray(Q2PSTRING(password_->text())));
-		}
+			if (isCAPIURI(certificateString)) {
+				certificate = boost::make_shared<CAPICertificate>(certificateString, timerFactory_);
+			} else {
+				certificate = boost::make_shared<PKCS12Certificate>(certificateString, createSafeByteArray(Q2PSTRING(password_->text())));
+			}
 #else
-		certificate = boost::make_shared<PKCS12Certificate>(certificateString, createSafeByteArray(Q2PSTRING(password_->text())));
+			certificate = boost::make_shared<PKCS12Certificate>(certificateString, createSafeByteArray(Q2PSTRING(password_->text())));
 #endif
+		}
 
 		onLoginRequest(Q2PSTRING(username_->currentText()), Q2PSTRING(password_->text()), certificateString, certificate, currentOptions_, remember_->isChecked(), loginAutomatically_->isChecked());
 		if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { /* Mustn't remember logins */
-- 
cgit v0.10.2-6-g49f6