Example #1
0
void LoginManager::onReplyFinished(QNetworkReply* reply, RequestType requestType)
      {
      if (!reply)
            return;

      const int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
      QByteArray ba(reply->readAll());
      QJsonObject obj;
      if (!ba.isEmpty()) {
            QJsonDocument jsonResponse = QJsonDocument::fromJson(ba);
            if (jsonResponse.isObject())
                  obj = jsonResponse.object();
            }

      switch (requestType) {
            case RequestType::LOGIN:
                  onLoginReply(reply, code, obj);
                  break;
            case RequestType::GET_USER_INFO:
                  onGetUserReply(reply, code, obj);
                  break;
            case RequestType::GET_SCORE_INFO:
                  onGetScoreInfoReply(reply, code, obj);
                  break;
            case RequestType::UPLOAD_SCORE:
                  onUploadReply(reply, code, obj);
                  break;
            case RequestType::GET_MEDIA_URL:
                  onGetMediaUrlReply(reply, code, obj);
                  break;
            }

      reply->deleteLater();
      }
void Authenticator::authenticate()
{
	//Don't start another authentication request if one is already in progress.  We give 5 seconds for the
	//current request to finish.
	if (!mAuthenticationInProgress || (mAuthenticationInProgress && (mLastAuthenticationRequest.secsTo(QTime::currentTime()) > 5)))
	{
		mAuthenticationInProgress = true;
		mLastAuthenticationRequest = QTime::currentTime();

		//Always grab the latest username and password.
		mUsername = mCredentials->username();
		mPassword = mCredentials->password();

		//Set authenticated to false to prevent using wrong session key if forum was changed.
		mAuthenticated = false;

		//Only attempt to login if a username has been entered.
		if (mUsername.length() > 0)
		{

			QUrl postData;
			postData.addQueryItem("user.login", mUsername);
			postData.addQueryItem("user.password", mPassword);

			QString loginURL = URLProvider::getForumURL() + "authentication/sessions/login";
			QNetworkRequest request(loginURL);
			request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

			QNetworkReply* reply = bb::cascades::QmlDocument::defaultDeclarativeEngine()->networkAccessManager()->post(request, postData.encodedQuery());
			QObject::connect(reply, SIGNAL(finished()), this, SLOT(onLoginReply()));
		}
		else
		{
			qDebug() << "No forum credentials, not attempting login.";
			mAuthenticated = false;
			emit authenticationChanged(mAuthenticated);
		}
	}

}
Example #3
0
void LoginManager::loginInteractive()
      {
      QWebEngineView* webView = new QWebEngineView;
      webView->setWindowModality(Qt::ApplicationModal);
      webView->setAttribute(Qt::WA_DeleteOnClose);

      QWebEnginePage* page = webView->page();
      QWebEngineProfile* profile = page->profile();
      // TODO: logout in editor does not log out in web view
      profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
      profile->setRequestInterceptor(new ApiWebEngineRequestInterceptor(profile));

      //workaround for the crashes sometimes happend in Chromium on macOS with Qt 5.12
      connect(webView, &QWebEngineView::renderProcessTerminated, this, [profile, webView](QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode)
              {
              qDebug() << "Login page loading terminated" << terminationStatus << " " << exitCode;
              profile->clearHttpCache();
              webView->show();
              });
      
      connect(page, &QWebEnginePage::loadFinished, this, [this, page, webView](bool ok) {
            if (!ok)
                  return;
            constexpr QUrl::FormattingOptions cmpOpt = QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::StripTrailingSlash;
            if (!page->url().matches(ApiInfo::loginSuccessUrl, cmpOpt))
                  return;

            page->runJavaScript("JSON.stringify(muGetAuthInfo())", [this, page, webView](const QVariant& v) {
                  onLoginReply(nullptr, HTTP_OK, QJsonDocument::fromJson(v.toString().toUtf8()).object());
                  // We have retrieved an access token, do not remain logged
                  // in with web view profile.
                  page->profile()->cookieStore()->deleteAllCookies();
                  webView->close();
                  });
            });

      webView->load(ApiInfo::loginUrl);
      webView->show();
      }