/src/app/qgsappauthrequesthandler.cpp

https://github.com/ricardogsilva/Quantum-GIS · C++ · 102 lines · 75 code · 10 blank · 17 comment · 17 complexity · 37d8b4c31d5c04cbb76ab64953a81898 MD5 · raw file

  1. /***************************************************************************
  2. qgsappauthrequesthandler.cpp
  3. ---------------------------
  4. begin : January 2019
  5. copyright : (C) 2019 by Nyall Dawson
  6. email : nyall dot dawson at gmail dot com
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "qgsappauthrequesthandler.h"
  16. #include "qgslogger.h"
  17. #include "qgsauthcertutils.h"
  18. #include "qgsapplication.h"
  19. #include "qgsauthmanager.h"
  20. #include "qgisapp.h"
  21. #include "qgscredentials.h"
  22. #include <QAuthenticator>
  23. #include <QDesktopServices>
  24. void QgsAppAuthRequestHandler::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
  25. {
  26. Q_ASSERT( qApp->thread() == QThread::currentThread() );
  27. QString username = auth->user();
  28. QString password = auth->password();
  29. if ( username.isEmpty() && password.isEmpty() && reply->request().hasRawHeader( "Authorization" ) )
  30. {
  31. const QByteArray header( reply->request().rawHeader( "Authorization" ) );
  32. if ( header.startsWith( "Basic " ) )
  33. {
  34. const QByteArray auth( QByteArray::fromBase64( header.mid( 6 ) ) );
  35. const int pos = auth.indexOf( ':' );
  36. if ( pos >= 0 )
  37. {
  38. username = auth.left( pos );
  39. password = auth.mid( pos + 1 );
  40. }
  41. }
  42. }
  43. for ( ;; )
  44. {
  45. const bool ok = QgsCredentials::instance()->get(
  46. QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ),
  47. username, password,
  48. QObject::tr( "Authentication required" ) );
  49. if ( !ok )
  50. return;
  51. if ( auth->user() != username || ( password != auth->password() && !password.isNull() ) )
  52. {
  53. // save credentials
  54. QgsCredentials::instance()->put(
  55. QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ),
  56. username, password
  57. );
  58. break;
  59. }
  60. else
  61. {
  62. // credentials didn't change - stored ones probably wrong? clear password and retry
  63. QgsCredentials::instance()->put(
  64. QStringLiteral( "%1 at %2" ).arg( auth->realm(), reply->url().host() ),
  65. username, QString() );
  66. }
  67. }
  68. auth->setUser( username );
  69. auth->setPassword( password );
  70. }
  71. void QgsAppAuthRequestHandler::handleAuthRequestOpenBrowser( const QUrl &url )
  72. {
  73. QDesktopServices::openUrl( url );
  74. }
  75. void QgsAppAuthRequestHandler::handleAuthRequestCloseBrowser()
  76. {
  77. // Bring focus back to QGIS app
  78. if ( qApp )
  79. {
  80. const QList<QWidget *> topWidgets = QgsApplication::topLevelWidgets();
  81. for ( QWidget *topWidget : topWidgets )
  82. {
  83. if ( topWidget->objectName() == QLatin1String( "MainWindow" ) )
  84. {
  85. topWidget->raise();
  86. topWidget->activateWindow();
  87. topWidget->show();
  88. break;
  89. }
  90. }
  91. }
  92. }