Skip to content

Commit

Permalink
[oauth2] Verify replies still exist and attribute gets are valid
Browse files Browse the repository at this point in the history
Fixes unreported crash, due to access on nonexistent replies.
  • Loading branch information
dakcarto authored and nyalldawson committed May 19, 2019
1 parent 491ca33 commit 71573bd
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/auth/oauth2/qgsauthoauth2method.cpp
Expand Up @@ -390,6 +390,12 @@ void QgsAuthOAuth2Method::onReplyFinished()
{
QgsMessageLog::logMessage( tr( "Network reply finished" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
if ( !reply )
{
QString msg = tr( "Network reply finished but no reply object accessible" );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
return;
}
QgsMessageLog::logMessage( tr( "Results: %1" ).arg( QString( reply->readAll() ) ),
AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
}
Expand All @@ -413,12 +419,22 @@ void QgsAuthOAuth2Method::onNetworkError( QNetworkReply::NetworkError err )

// TODO: update debug messages to output to QGIS

int status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
msg = tr( "Network error, HTTP status: %1" ).arg(
reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString() );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
if ( !status.isValid() )
{
msg = tr( "Network error but no reply object attributes found" );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
return;
}
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
if ( phrase.isValid() )
{
msg = tr( "Network error, HTTP status: %1" ).arg( phrase.toString() );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
}

if ( status == 401 )

if ( status.toInt() == 401 )
{
msg = tr( "Attempting token refresh…" );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
Expand Down Expand Up @@ -454,6 +470,12 @@ void QgsAuthOAuth2Method::onNetworkError( QNetworkReply::NetworkError err )
void QgsAuthOAuth2Method::onRefreshFinished( QNetworkReply::NetworkError err )
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
if ( !reply )
{
QString msg = tr( "Token refresh finished but no reply object accessible" );
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
return;
}
if ( err != QNetworkReply::NoError )
{
QgsMessageLog::logMessage( tr( "Token refresh error: %1" ).arg( reply->errorString() ),
Expand Down

0 comments on commit 71573bd

Please sign in to comment.