Skip to content

Commit 0a67592

Browse files
committed
[oauth] Fix tests and rebase agains current master
1 parent b9c2c5c commit 0a67592

File tree

5 files changed

+46
-66
lines changed

5 files changed

+46
-66
lines changed

src/auth/oauth2/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ IF(WITH_INTERNAL_O2 AND CMAKE_GENERATOR MATCHES "Ninja")
124124
COMMAND
125125
)
126126
ENDIF()
127-
QT4_WRAP_UI(OAUTH2_UIS_H ${OAUTH2_UIS})
127+
QT5_WRAP_UI(OAUTH2_UIS_H ${OAUTH2_UIS})
128128

129-
QT4_WRAP_CPP(OAUTH2_MOC_SRCS ${OAUTH2_MOC_HDRS})
129+
QT5_WRAP_CPP(OAUTH2_MOC_SRCS ${OAUTH2_MOC_HDRS})
130130

131-
QT4_ADD_RESOURCES(OAUTH2_RCC_SRCS ${OAUTH2_RCCS})
131+
QT5_ADD_RESOURCES(OAUTH2_RCC_SRCS ${OAUTH2_RCCS})
132132
############################################################
133133
# Set up plugin targets
134134

src/auth/oauth2/qgsauthoauth2method.cpp

+26-26
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
114114
if ( !o2 )
115115
{
116116
msg = QStringLiteral( "Update request FAILED for authcfg %1: null object for requestor" ).arg( authcfg );
117-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
117+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
118118
return false;
119119
}
120120

@@ -125,7 +125,7 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
125125
if ( !QFile::exists( tokencache ) )
126126
{
127127
msg = QStringLiteral( "Token cache removed for authcfg %1: unlinking authenticator" ).arg( authcfg );
128-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
128+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
129129
o2->unlink();
130130
}
131131
}
@@ -143,7 +143,7 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
143143
if ( expired )
144144
{
145145
msg = QStringLiteral( "Token expired, attempting refresh for authcfg %1" ).arg( authcfg );
146-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
146+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
147147

148148
// Try to get a refresh token first
149149
// go into local event loop and wait for a fired refresh-related slot
@@ -220,15 +220,15 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
220220
if ( !o2->linked() )
221221
{
222222
msg = QStringLiteral( "Update request FAILED for authcfg %1: requestor could not link app" ).arg( authcfg );
223-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
223+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
224224
return false;
225225
}
226226
}
227227

228228
if ( o2->token().isEmpty() )
229229
{
230230
msg = QStringLiteral( "Update request FAILED for authcfg %1: access token is empty" ).arg( authcfg );
231-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
231+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
232232
return false;
233233
}
234234

@@ -243,13 +243,13 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
243243
case QgsAuthOAuth2Config::Header:
244244
request.setRawHeader( O2_HTTP_AUTHORIZATION_HEADER, QStringLiteral( "Bearer %1" ).arg( o2->token() ).toAscii() );
245245
msg = QStringLiteral( "Updated request HEADER with access token for authcfg: %1" ).arg( authcfg );
246-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
246+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
247247
break;
248248
case QgsAuthOAuth2Config::Form:
249249
// FIXME: what to do here if the parent request is not POST?
250250
// probably have to skip this until auth system support is moved into QgsNetworkAccessManager
251251
msg = QStringLiteral( "Update request FAILED for authcfg %1: form POST token update is unsupported" ).arg( authcfg );
252-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
252+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
253253
break;
254254
case QgsAuthOAuth2Config::Query:
255255
if ( !query.hasQueryItem( O2_OAUTH2_ACCESS_TOKEN ) )
@@ -263,7 +263,7 @@ bool QgsAuthOAuth2Method::updateNetworkRequest( QNetworkRequest &request, const
263263
{
264264
msg = QStringLiteral( "Updated request QUERY with access token SKIPPED (existing token) for authcfg: %1" ).arg( authcfg );
265265
}
266-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
266+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
267267
break;
268268
}
269269

@@ -284,7 +284,7 @@ bool QgsAuthOAuth2Method::updateNetworkReply( QNetworkReply *reply, const QStrin
284284
{
285285
QString msg = QStringLiteral( "Updated reply with token refresh connection FAILED"
286286
" for authcfg %1: null reply object" ).arg( authcfg );
287-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
287+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
288288
return false;
289289
}
290290
reply->setProperty( "authcfg", authcfg );
@@ -296,7 +296,7 @@ bool QgsAuthOAuth2Method::updateNetworkReply( QNetworkReply *reply, const QStrin
296296
// this, &QgsAuthOAuth2Method::onNetworkError, Qt::QueuedConnection );
297297

298298
QString msg = QStringLiteral( "Updated reply with token refresh connection for authcfg: %1" ).arg( authcfg );
299-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
299+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
300300

301301
return true;
302302
}
@@ -311,7 +311,7 @@ void QgsAuthOAuth2Method::onLinkedChanged()
311311
void QgsAuthOAuth2Method::onLinkingFailed()
312312
{
313313
// Login has failed
314-
QgsMessageLog::logMessage( tr( "Authenticator linking (login) has failed" ), AUTH_METHOD_KEY, QgsMessageLog::WARNING );
314+
QgsMessageLog::logMessage( tr( "Authenticator linking (login) has failed" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
315315
}
316316

317317
void QgsAuthOAuth2Method::onLinkingSucceeded()
@@ -320,18 +320,18 @@ void QgsAuthOAuth2Method::onLinkingSucceeded()
320320
if ( !o2 )
321321
{
322322
QgsMessageLog::logMessage( tr( "Linking succeeded, but authenticator access FAILED: null object" ),
323-
AUTH_METHOD_KEY, QgsMessageLog::WARNING );
323+
AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
324324
return;
325325
}
326326

327327
if ( !o2->linked() )
328328
{
329329
QgsMessageLog::logMessage( tr( "Linking apparently succeeded, but authenticator FAILED to verify it is linked" ),
330-
AUTH_METHOD_KEY, QgsMessageLog::WARNING );
330+
AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
331331
return;
332332
}
333333

334-
QgsMessageLog::logMessage( tr( "Linking succeeded" ), AUTH_METHOD_KEY, QgsMessageLog::INFO );
334+
QgsMessageLog::logMessage( tr( "Linking succeeded" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
335335

336336
//###################### DO NOT LEAVE ME UNCOMMENTED ######################
337337
//QgsDebugMsg( QStringLiteral( "Access token: %1" ).arg( o2->token() ) );
@@ -357,15 +357,15 @@ void QgsAuthOAuth2Method::onOpenBrowser( const QUrl &url )
357357
// The user will interact with this browser window to
358358
// enter login name, password, and authorize your application
359359
// to access the Twitter account
360-
QgsMessageLog::logMessage( tr( "Open browser requested" ), AUTH_METHOD_KEY, QgsMessageLog::INFO );
360+
QgsMessageLog::logMessage( tr( "Open browser requested" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
361361

362362
QDesktopServices::openUrl( url );
363363
}
364364

365365
void QgsAuthOAuth2Method::onCloseBrowser()
366366
{
367367
// Close the browser window opened in openBrowser()
368-
QgsMessageLog::logMessage( tr( "Close browser requested" ), AUTH_METHOD_KEY, QgsMessageLog::INFO );
368+
QgsMessageLog::logMessage( tr( "Close browser requested" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
369369

370370
// Bring focus back to QGIS app
371371
if ( qobject_cast<QApplication *>( qApp ) )
@@ -385,10 +385,10 @@ void QgsAuthOAuth2Method::onCloseBrowser()
385385

386386
void QgsAuthOAuth2Method::onReplyFinished()
387387
{
388-
QgsMessageLog::logMessage( tr( "Network reply finished" ), AUTH_METHOD_KEY, QgsMessageLog::INFO );
388+
QgsMessageLog::logMessage( tr( "Network reply finished" ), AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
389389
QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
390390
QgsMessageLog::logMessage( tr( "Results: %1" ).arg( QString( reply->readAll() ) ),
391-
AUTH_METHOD_KEY, QgsMessageLog::INFO );
391+
AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
392392
}
393393

394394
void QgsAuthOAuth2Method::onNetworkError( QNetworkReply::NetworkError err )
@@ -399,32 +399,32 @@ void QgsAuthOAuth2Method::onNetworkError( QNetworkReply::NetworkError err )
399399
if ( !reply )
400400
{
401401
msg = tr( "Network error but no reply object accessible" );
402-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
402+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
403403
return;
404404
}
405405
if ( err != QNetworkReply::NoError )
406406
{
407407
msg = tr( "Network error: %1" ).arg( reply->errorString() );
408-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
408+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
409409
}
410410

411411
// TODO: update debug messages to output to QGIS
412412

413413
int status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
414414
msg = tr( "Network error, HTTP status: %1" ).arg(
415415
reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString() );
416-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
416+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
417417

418418
if ( status == 401 )
419419
{
420420
msg = tr( "Attempting token refresh..." );
421-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
421+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
422422

423423
QString authcfg = reply->property( "authcfg" ).toString();
424424
if ( authcfg.isEmpty() )
425425
{
426426
msg = tr( "Token refresh FAILED: authcfg empty" );
427-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
427+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
428428
return;
429429
}
430430

@@ -438,12 +438,12 @@ void QgsAuthOAuth2Method::onNetworkError( QNetworkReply::NetworkError err )
438438
o2->refresh();
439439

440440
msg = tr( "Background token refresh underway for authcfg: %1" ).arg( authcfg );
441-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::INFO );
441+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Info );
442442
}
443443
else
444444
{
445445
msg = tr( "Background token refresh FAILED for authcfg %1: could not get authenticator object" ).arg( authcfg );
446-
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, QgsMessageLog::WARNING );
446+
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
447447
}
448448
}
449449
}
@@ -454,7 +454,7 @@ void QgsAuthOAuth2Method::onRefreshFinished( QNetworkReply::NetworkError err )
454454
if ( err != QNetworkReply::NoError )
455455
{
456456
QgsMessageLog::logMessage( tr( "Token fefresh error: %1" ).arg( reply->errorString() ),
457-
AUTH_METHOD_KEY, QgsMessageLog::WARNING );
457+
AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
458458
}
459459
}
460460

tests/src/auth/CMakeLists.txt

+8-5
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ MACRO (ADD_QGIS_TEST testname testsrc)
5757
TARGET_LINK_LIBRARIES(qgis_${testname} ${APP_SERVICES_LIBRARY})
5858
ENDIF(APPLE)
5959
TARGET_LINK_LIBRARIES(qgis_${testname}
60-
${QT_QTCORE_LIBRARY}
61-
${QT_QTGUI_LIBRARY}
62-
${QT_QTNETWORK_LIBRARY}
63-
${QT_QTXML_LIBRARY}
60+
${Qt5Core_LIBRARIES}
61+
${Qt5Xml_LIBRARIES}
62+
${Qt5Svg_LIBRARIES}
63+
${Qt5Test_LIBRARIES}
64+
${Qt5Network_LIBRARIES}
65+
${PROJ_LIBRARY}
66+
${GEOS_LIBRARY}
67+
${GDAL_LIBRARY}
6468
${OPTIONAL_QTWEBKIT}
65-
${QT_QTTEST_LIBRARY}
6669
${PROJ_LIBRARY}
6770
${GEOS_LIBRARY}
6871
${GDAL_LIBRARY}

tests/src/python/qgis_wrapped_server.py

+2-25
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,8 @@
203203
qgs_server = QgsServer()
204204

205205

206-
<<<<<<< a929de58a77eb0040b173a59df22ef510cd51023
207-
if os.environ.get('QGIS_SERVER_HTTP_BASIC_AUTH') is not None:
208-
=======
209206
if QGIS_SERVER_HTTP_BASIC_AUTH:
210207
from qgis.server import QgsServerFilter
211-
>>>>>>> [oauth2] Added test for resource owner password grant flow
212208
import base64
213209

214210
class HTTPBasicFilter(QgsServerFilter):
@@ -242,7 +238,6 @@ def responseComplete(self):
242238
qgs_server.serverInterface().registerFilter(filter)
243239

244240

245-
<<<<<<< a929de58a77eb0040b173a59df22ef510cd51023
246241
def num2deg(xtile, ytile, zoom):
247242
"""This returns the NW-corner of the square. Use the function with xtile+1 and/or ytile+1
248243
to get the other corners. With xtile+0.5 & ytile+0.5 it will return the center of the tile."""
@@ -277,7 +272,7 @@ def requestReady(self):
277272

278273
xyzfilter = XYZFilter(qgs_server.serverInterface())
279274
qgs_server.serverInterface().registerFilter(xyzfilter)
280-
=======
275+
281276
if QGIS_SERVER_OAUTH2_AUTH:
282277
from qgis.server import QgsServerFilter
283278
from oauthlib.oauth2 import RequestValidator, LegacyApplicationServer
@@ -415,7 +410,6 @@ def _token(ttl):
415410

416411
filter = OAuth2Filter(qgs_server.serverInterface())
417412
qgs_server.serverInterface().registerFilter(filter)
418-
>>>>>>> [oauth2] Added test for resource owner password grant flow
419413

420414

421415
class Handler(BaseHTTPRequestHandler):
@@ -424,17 +418,11 @@ def do_GET(self, post_body=None):
424418
# CGI vars:
425419
headers = {}
426420
for k, v in self.headers.items():
427-
<<<<<<< a7fb4238893336c8d5b2b4802f63588175e70c83
428421
headers['HTTP_%s' % k.replace(' ', '-').replace('-', '_').replace(' ', '-').upper()] = v
429422
if not self.path.startswith('http'):
430-
self.path = "%s://%s:%s%s" % ('https' if https else 'http', QGIS_SERVER_HOST, self.server.server_port, self.path)
431-
request = QgsBufferServerRequest(self.path, (QgsServerRequest.PostMethod if post_body is not None else QgsServerRequest.GetMethod), headers, post_body)
432-
=======
433-
headers['HTTP_%s' % k.replace(
434-
' ', '-').replace('-', '_').replace(' ', '-').upper()] = v
423+
self.path = "%s://%s:%s%s" % ('https' if HTTPS_ENABLED else 'http', QGIS_SERVER_HOST, self.server.server_port, self.path)
435424
request = QgsBufferServerRequest(
436425
self.path, (QgsServerRequest.PostMethod if post_body is not None else QgsServerRequest.GetMethod), headers, post_body)
437-
>>>>>>> Pass urlencoded URL to the oauth handler
438426
response = QgsBufferServerResponse()
439427
qgs_server.handleRequest(request, response)
440428

@@ -461,20 +449,10 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
461449

462450

463451
if __name__ == '__main__':
464-
<<<<<<< a929de58a77eb0040b173a59df22ef510cd51023
465452
if os.environ.get('MULTITHREADING') == '1':
466453
server = ThreadedHTTPServer((QGIS_SERVER_HOST, QGIS_SERVER_PORT), Handler)
467454
else:
468455
server = HTTPServer((QGIS_SERVER_HOST, QGIS_SERVER_PORT), Handler)
469-
if https:
470-
server.socket = ssl.wrap_socket(server.socket,
471-
certfile=QGIS_SERVER_PKI_CERTIFICATE,
472-
keyfile=QGIS_SERVER_PKI_KEY,
473-
ca_certs=QGIS_SERVER_PKI_AUTHORITY,
474-
cert_reqs=ssl.CERT_REQUIRED,
475-
server_side=True,
476-
ssl_version=ssl.PROTOCOL_TLSv1)
477-
=======
478456
server = HTTPServer((QGIS_SERVER_HOST, QGIS_SERVER_PORT), Handler)
479457
# HTTPS is enabled if any of PKI or OAuth2 are enabled too
480458
if HTTPS_ENABLED:
@@ -497,7 +475,6 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
497475
server_side=True,
498476
ssl_version=ssl.PROTOCOL_TLSv1)
499477

500-
>>>>>>> [oauth2] Added test for resource owner password grant flow
501478
print('Starting server on %s://%s:%s, use <Ctrl-C> to stop' %
502479
('https' if HTTPS_ENABLED else 'http', QGIS_SERVER_HOST, server.server_port), flush=True)
503480

tests/src/python/test_authmanager_oauth2_ows.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
from utilities import unitTestDataPath, waitServer
3838
from qgis.core import (
39-
QgsAuthManager,
39+
QgsApplication,
4040
QgsAuthMethodConfig,
4141
QgsVectorLayer,
4242
QgsRasterLayer,
@@ -87,19 +87,19 @@ def setup_oauth(username, password, token_uri, refresh_token_uri='', authcfg_id=
8787
"version": 1
8888
}
8989

90-
if authcfg_id not in QgsAuthManager.instance().availableAuthMethodConfigs():
90+
if authcfg_id not in QgsApplication.authManager().availableAuthMethodConfigs():
9191
authConfig = QgsAuthMethodConfig('OAuth2')
9292
authConfig.setId(authcfg_id)
9393
authConfig.setName(authcfg_name)
9494
authConfig.setConfig('oauth2config', json.dumps(cfgjson))
95-
if QgsAuthManager.instance().storeAuthenticationConfig(authConfig):
95+
if QgsApplication.authManager().storeAuthenticationConfig(authConfig):
9696
return authcfg_id
9797
else:
9898
authConfig = QgsAuthMethodConfig()
99-
QgsAuthManager.instance().loadAuthenticationConfig(authcfg_id, authConfig, True)
99+
QgsApplication.authManager().loadAuthenticationConfig(authcfg_id, authConfig, True)
100100
authConfig.setName(authcfg_name)
101101
authConfig.setConfig('oauth2config', json.dumps(cfgjson))
102-
if QgsAuthManager.instance().updateAuthenticationConfig(authConfig):
102+
if QgsApplication.authManager().updateAuthenticationConfig(authConfig):
103103
return authcfg_id
104104
return None
105105

@@ -109,7 +109,7 @@ class TestAuthManager(unittest.TestCase):
109109
@classmethod
110110
def setUpAuth(cls):
111111
"""Run before all tests and set up authentication"""
112-
authm = QgsAuthManager.instance()
112+
authm = QgsApplication.authManager()
113113
assert (authm.setMasterPassword('masterpassword', True))
114114
cls.sslrootcert_path = os.path.join(cls.certsdata_path, 'chains_subissuer-issuer-root_issuer2-root2.pem')
115115
assert os.path.isfile(cls.sslrootcert_path)
@@ -178,7 +178,7 @@ def setUpClass(cls):
178178
# This is to test wrong credentials
179179
cls.wrong_authcfg_id = setup_oauth('wrong', 'wrong', cls.token_uri, cls.refresh_token_uri, str(random.randint(0, 10000000)))
180180
# Get the authentication configuration instance:
181-
cls.auth_config = QgsAuthManager.instance().availableAuthMethodConfigs()[cls.authcfg_id]
181+
cls.auth_config = QgsApplication.authManager().availableAuthMethodConfigs()[cls.authcfg_id]
182182
assert cls.auth_config.isValid()
183183

184184
# Wait for the server process to start

0 commit comments

Comments
 (0)