Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-136897

QOAuthUriSchemeReplyHandler does not see proper Stage?

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • P3: Somewhat important
    • None
    • 6.8.3, 6.9.0
    • None
    • Android
    • 1
    • Foundation Sprint 131

    Description

      Hello,

      I am switching from QOAuthHttpServerReplyHandler  to QOAuthUriSchemeReplyHandler in order to be able to use https as redirect url,

      so I use:

      oauth_google.h:
       

      #define REDIRECT_URI "https://127.0.0.1:5476/"
      
      class GoogleSSO : public QObject
      {
         Q_OBJECT
      
         public:
            explicit GoogleSSO(QObject *parent = nullptr);
            virtual ~GoogleSSO();
      
         public slots:
            Q_INVOKABLE void authenticate();
      
         private:
            QOAuth2AuthorizationCodeFlow *google;
            QOAuthUriSchemeReplyHandler *replyHandler;
      };

       
       
      oauth_google.cpp:
       

      #include "oauth2_google.h"
      
      GoogleSSO::GoogleSSO(QObject *parent) : QObject(parent)
      {
         this->google = new QOAuth2AuthorizationCodeFlow(this);
            this->google->setClientIdentifier(...);
            this->google->setClientIdentifierSharedKey(...);
            this->google->setState(...);
            this->google->responseType() = ...;
            this->google->setAuthorizationUrl(QUrl(...));
            this->google->setRequestedScopeTokens(...);
            this->google->setTokenUrl(QUrl(...));
      
         QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, this, [&](const QUrl &url) {
            qDebug() << "URL Built:" << url;
         });
      
          this->google->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QMultiMap<QString, QVariant> *parameters) {
            if (stage == QAbstractOAuth::Stage::RequestingAuthorization)			// stage when building url just before opening the browser with the very first url
            {
      qDebug() << "im in stage RequestingAuthorization";			// this works fine
              // The only way to get refresh_token from Google Cloud
      	parameters->insert("access_type", "offline");						// Needed for Refresh Token (as AccessToken expires shortly)
      	parameters->insert("prompt", "consent");							// Param required to get data everytime	
            }
            else if (stage == QAbstractOAuth::Stage::RequestingAccessToken)
            {
      qDebug() << "im in stage RequestingAccessToken";			// replyHandler does not recognize it, however URL realy proper... so to my understanding QAbstractOAuth::Stage does not recognize this stage properly
      	// Percent-decode the "code" parameter so Google can match it
      	QByteArray code = parameters->value("code").toByteArray();
      	parameters->replace("code", QUrl::fromPercentEncoding(code));		// need to fix the URL syntaxt to manage spaces
            }
            else {
      qDebug() << "im in stage OTHER";
            }
          });
      
      
         // ERROR LOGIN
         QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived, this, [&](const QVariantMap &data) {
            QString error = data.value("error").toString();
            if (!error.isEmpty()) {
               qDebug() << "ERROR";
               replyHandler->close();
            }
         });
      
         // SUCCESS LOGIN
         QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, this, [this]() {
            qDebug() << "SUCCESS";
            replyHandler->close();
         });
      }
      
      // Invoked externally to initiate
      void GoogleSSO::authenticate() {
         replyHandler = new QOAuthUriSchemeReplyHandler(QUrl(REDIRECT_URI), this);    // or this->google instead of this?
         replyHandler->setRedirectUrl(QUrl(REDIRECT_URI));
         this->google->setReplyHandler(replyHandler);
      
         if (replyHandler->listen()) {
            qDebug() << "STARTNG TO LISTEN";
            this->google->grant();   // Initialize OAUTH2 Workflow
         }
         else { qDebug() << "ReplyHandler ERROR"; }
      }
      
      // Destroy
      GoogleSSO::~GoogleSSO()
      {
         //replyHandler->close();   // causes crash as during closing of app, because replyHandler doesnt exist anymore, therefore calling null pointer
         delete this->google;
      }

       
       and upon calling authenticate() google login opens, i enter email/password, it asks for second factor authentication, so i approve from my smartphone, it redirects to REDIRECT_URI and the webview shows me:

      which has url: https://127.0.0.1:5476/?state=XqEWcNQD&code=4%2F0AUJR-x4VEDmZ-vbCOnKxi_ZyMK6yD99tfz51XsOuYLYXpzVPdm4mujgZr48tqgu8dv0j0g&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent

       - so url which was changed by QAbstractOAuth::Stage::RequestingAuthorization, however at this point the QAbstractOAuth should recognize it as Stage::RequestingAccessToken? but the replyHandler also should see this webpage (so it looks like is not listening?)...

      so I dont know what is realy wrong here

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vuokko Juha Vuolle
            shokarta Jiri Zaloudek
            Vladimir Minenko Vladimir Minenko
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes