From d8888432c33e060ae081da305838f814ce4bde35 Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Tue, 10 Sep 2013 23:03:52 +0200 Subject: [PATCH] Send basic auth in utf8. Implies circumventing QAuthenticator and handling authentication manually again. Fixes #941 --- src/creds/httpcredentials.cpp | 51 ++++++++++++++++------------------- src/creds/httpcredentials.h | 1 - 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/creds/httpcredentials.cpp b/src/creds/httpcredentials.cpp index 08c4cc6c1b..e92a1fc9eb 100644 --- a/src/creds/httpcredentials.cpp +++ b/src/creds/httpcredentials.cpp @@ -67,11 +67,25 @@ int getauth(const char *prompt, } // ns +class HttpCredentialsAccessManager : public MirallAccessManager { +public: + HttpCredentialsAccessManager(const HttpCredentials *cred, QObject* parent = 0) + : MirallAccessManager(parent), _cred(cred) {} +protected: + QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) { + QByteArray credHash = QByteArray(_cred->user().toUtf8()+":"+_cred->password().toUtf8()).toBase64(); + QNetworkRequest req(request); + req.setRawHeader(QByteArray("Authorization"), QByteArray("Basic ") + credHash); + return MirallAccessManager::createRequest(op, req, outgoingData);\ + } +private: + const HttpCredentials *_cred; +}; + HttpCredentials::HttpCredentials() : _user(), _password(), - _ready(false), - _attempts() + _ready(false) {} HttpCredentials::HttpCredentials(const QString& user, const QString& password) @@ -134,7 +148,7 @@ QString HttpCredentials::password() const QNetworkAccessManager* HttpCredentials::getQNAM() const { - MirallAccessManager* qnam = new MirallAccessManager; + MirallAccessManager* qnam = new HttpCredentialsAccessManager(this); connect( qnam, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this, SLOT(slotAuthentication(QNetworkReply*,QAuthenticator*))); @@ -180,30 +194,12 @@ void HttpCredentials::slotCredentialsFetched(bool ok) void HttpCredentials::slotAuthentication(QNetworkReply* reply, QAuthenticator* authenticator) { - if( !(authenticator && reply) ) return; - - qDebug() << "Authenticating request for " << reply->url(); - - if (_attempts.contains(reply)) { - ++_attempts[reply]; - } else { - connect(reply, SIGNAL(finished()), - this, SLOT(slotReplyFinished())); - _attempts[reply] = 1; - } - // TODO: Replace it with something meaningful... - //if( reply->url().toString().startsWith( webdavUrl( _connection ) ) ) { - if (_attempts[reply] > 1) { - qDebug() << "Too many attempts to authenticate. Stop request."; - reply->close(); - } else { - authenticator->setUser( _user ); - authenticator->setPassword( _password ); - } - //} else { - // qDebug() << "WRN: attempt to authenticate to different url - closing."; - // reply->close(); - //} + Q_UNUSED(authenticator) + // we cannot use QAuthenticator, because it sends username and passwords with latin1 + // instead of utf8 encoding. Instead, we send it manually. Thus, if we reach this signal, + // those credentials were invalid and we terminate. + qDebug() << "Credentials invalid. Stop request."; + reply->close(); } void HttpCredentials::slotReplyFinished() @@ -212,7 +208,6 @@ void HttpCredentials::slotReplyFinished() disconnect(reply, SIGNAL(finished()), this, SLOT(slotReplyFinished())); - _attempts.remove (reply); } } // ns Mirall diff --git a/src/creds/httpcredentials.h b/src/creds/httpcredentials.h index 07d52640e9..3a2aadab79 100644 --- a/src/creds/httpcredentials.h +++ b/src/creds/httpcredentials.h @@ -56,7 +56,6 @@ private: QString _user; QString _password; bool _ready; - QMap _attempts; }; } // ns Mirall