Skip to content

Commit a3e8840

Browse files
committedJun 14, 2019
[oauth2] Fix token refresh timeout cancellation with singleshot timer
Also, avoid O2 library error by checking for refresh token and URL
1 parent ba8458d commit a3e8840

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed
 

‎src/auth/oauth2/qgsauthoauth2method.cpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgso2.h"
2424
#include "qgsauthoauth2config.h"
2525
#include "qgsauthoauth2edit.h"
26+
#include "qgsguiutils.h"
2627
#include "qgsnetworkaccessmanager.h"
2728
#include "qgslogger.h"
2829
#include "qgsmessagelog.h"
@@ -142,23 +143,50 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
142143

143144
if ( expired )
144145
{
145-
msg = QStringLiteral( "Token expired, attempting refresh for authcfg %1" ).arg( authcfg );
146-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
147-
148-
// Try to get a refresh token first
149-
// go into local event loop and wait for a fired refresh-related slot
150-
QEventLoop rloop( nullptr );
151-
connect( o2, &QgsO2::refreshFinished, &rloop, &QEventLoop::quit );
152-
153-
// Asynchronously attempt the refresh
154-
// TODO: This already has a timed reply setup in O2 base class (and in QgsNetworkAccessManager!)
155-
// May need to address this or app crashes will occur!
156-
o2->refresh();
157-
158-
// block request update until asynchronous linking loop is quit
159-
rloop.exec( QEventLoop::ExcludeUserInputEvents );
160-
161-
// refresh result should set o2 to (un)linked
146+
if ( o2->refreshToken().isEmpty() || o2->refreshTokenUrl().isEmpty() )
147+
{
148+
msg = QStringLiteral( "Token expired, but no refresh token or URL defined for authcfg %1" ).arg( authcfg );
149+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
150+
// clear any previous token session properties
151+
o2->unlink();
152+
}
153+
else
154+
{
155+
msg = QStringLiteral( "Token expired, attempting refresh for authcfg %1" ).arg( authcfg );
156+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
157+
158+
// Try to get a refresh token first
159+
// go into local event loop and wait for a fired refresh-related slot
160+
QEventLoop rloop( nullptr );
161+
connect( o2, &QgsO2::refreshFinished, &rloop, &QEventLoop::quit );
162+
163+
// add singlshot timer to quit refresh after an alloted timeout
164+
// this should keep the local event loop from blocking forever
165+
QTimer r_timer( nullptr );
166+
int r_reqtimeout = o2->oauth2config()->requestTimeout() * 1000;
167+
r_timer.setInterval( r_reqtimeout );
168+
r_timer.setSingleShot( true );
169+
connect( &r_timer, &QTimer::timeout, &rloop, &QEventLoop::quit );
170+
r_timer.start();
171+
172+
QgsTemporaryCursorOverride waitCursor( Qt::WaitCursor );
173+
174+
// Asynchronously attempt the refresh
175+
// TODO: This already has a timed reply setup in O2 base class (and in QgsNetworkAccessManager!)
176+
// May need to address this or app crashes will occur!
177+
o2->refresh();
178+
179+
// block request update until asynchronous linking loop is quit
180+
rloop.exec();
181+
if ( r_timer.isActive() )
182+
{
183+
r_timer.stop();
184+
}
185+
186+
waitCursor.release();
187+
188+
// refresh result should set o2 to (un)linked
189+
}
162190
}
163191
}
164192

0 commit comments

Comments
 (0)
Please sign in to comment.