Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Make network authentication request handling thread safe, avoid
races and crashes when auth requests occur in non-main thread Like the recent SSL error handling, this commit abstracts out the network authentication handling into a thread safe approach.
- Loading branch information
1 parent
6fa3bf8
commit 8ee2e79
Showing
9 changed files
with
485 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*************************************************************************** | ||
qgsappauthrequesthandler.cpp | ||
--------------------------- | ||
begin : January 2019 | ||
copyright : (C) 2019 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot 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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsappauthrequesthandler.h" | ||
#include "qgslogger.h" | ||
#include "qgsauthcertutils.h" | ||
#include "qgsapplication.h" | ||
#include "qgsauthmanager.h" | ||
#include "qgisapp.h" | ||
#include "qgscredentials.h" | ||
#include <QAuthenticator> | ||
|
||
void QgsAppAuthRequestHandler::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth ) | ||
{ | ||
QString username = auth->user(); | ||
QString password = auth->password(); | ||
|
||
if ( username.isEmpty() && password.isEmpty() && reply->request().hasRawHeader( "Authorization" ) ) | ||
{ | ||
QByteArray header( reply->request().rawHeader( "Authorization" ) ); | ||
if ( header.startsWith( "Basic " ) ) | ||
{ | ||
QByteArray auth( QByteArray::fromBase64( header.mid( 6 ) ) ); | ||
int pos = auth.indexOf( ':' ); | ||
if ( pos >= 0 ) | ||
{ | ||
username = auth.left( pos ); | ||
password = auth.mid( pos + 1 ); | ||
} | ||
} | ||
} | ||
|
||
for ( ;; ) | ||
{ | ||
bool ok; | ||
|
||
{ | ||
QMutexLocker lock( QgsCredentials::instance()->mutex() ); | ||
ok = QgsCredentials::instance()->get( | ||
QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ), | ||
username, password, | ||
QObject::tr( "Authentication required" ) ); | ||
} | ||
if ( !ok ) | ||
return; | ||
|
||
if ( auth->user() != username || ( password != auth->password() && !password.isNull() ) ) | ||
break; | ||
|
||
// credentials didn't change - stored ones probably wrong? clear password and retry | ||
{ | ||
QMutexLocker lock( QgsCredentials::instance()->mutex() ); | ||
QgsCredentials::instance()->put( | ||
QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ), | ||
username, QString() ); | ||
} | ||
} | ||
|
||
// save credentials | ||
{ | ||
QMutexLocker lock( QgsCredentials::instance()->mutex() ); | ||
QgsCredentials::instance()->put( | ||
QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ), | ||
username, password | ||
); | ||
} | ||
|
||
auth->setUser( username ); | ||
auth->setPassword( password ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*************************************************************************** | ||
qgsappauthrequesthandler.h | ||
------------------------- | ||
begin : January 2019 | ||
copyright : (C) 2019 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot 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. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSAPPAUTHREQUESTHANDLER_H | ||
#define QGSAPPAUTHREQUESTHANDLER_H | ||
|
||
#include "qgsnetworkaccessmanager.h" | ||
|
||
class QgsAppAuthRequestHandler : public QgsNetworkAuthenticationHandler | ||
{ | ||
|
||
public: | ||
|
||
void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth ); | ||
|
||
}; | ||
|
||
|
||
#endif // QGSAPPAUTHREQUESTHANDLER_H |
Oops, something went wrong.