QtNetworkAuth 是否支持 PKCE

Does QtNetworkAuth support PKCE

我用的是Qt5。我没有找到任何关于如何在使用 QOAuth2AuthorizationCodeFlow 时启用 PKCE 的文档。

如果是,请提供 link。如果没有支持,这个功能怎么加进去呢?

我添加了code_challengecode_challenge_method,但还不够。不知道下一步是什么

#include <QtNetworkAuth/QtNetworkAuth>

void loginHelper()
{
   auto* authFlow = new QOAuth2AuthorizationCodeFlow;
   QObject::connect(authFlow, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
   authFlow->setScope("openid profile email mobile");
   authFlow->setAuthorizationUrl(QUrl("https://accounts.XYZ.com/core/connect/authorize")); // url is changed
   authFlow->setClientIdentifier("desktop.test");
   authFlow->setAccessTokenUrl(QUrl("https://accounts.XYZ.com/core/connect/token")); // url is changed
   authFlow->setClientIdentifierSharedKey("0323af0d-efe2-fcec-b450-72f102530a77");
   authFlow->setModifyParametersFunction([=](QAbstractOAuth::Stage, QVariantMap* params)
      {
         params->insert("code_challenge", "1Kht0Wkyt_WvDngoM_AIOYPPOWG8lzVG1g1zk28TjSo");
         params->insert("code_challenge_method", "S256");
      });
   auto* replyHandler = new QOAuthHttpServerReplyHandler(1234); // port number
   authFlow->setReplyHandler(replyHandler);
   QObject::connect(authFlow, &QOAuth2AuthorizationCodeFlow::granted, []()
      {
         qDebug() << "Access Granted!";
      });
   authFlow->grant();
}

TL;DR 是的,您正在使用它。

阅读 the new flow,您注意到 PKCE 使用了三个新参数,code_verifiercode_challengecode_challenge_method

您的代码中使用了这些,因此您已经在使用 PKCE。

下一步是在 RequestingAccessToken 阶段设置 code_verifier

auto code_verifier = (QUuid::createUuid().toString(QUuid::WithoutBraces) +
   QUuid::createUuid().toString(QUuid::WithoutBraces)).toLatin1(); // 43 <= length <= 128
auto code_challenge = QCryptographicHash::hash(code_verifier, QCryptographicHash::Sha256).toBase64(
   QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
authFlow.setModifyParametersFunction([=](QAbstractOAuth::Stage stage, QVariantMap* params)
{
   switch (stage)
   {
   case QAbstractOAuth::Stage::RequestingAuthorization:
      params->insert("code_challenge", code_challenge);
      params->insert("code_challenge_method", "S256");
      break;
   case QAbstractOAuth::Stage::RequestingAccessToken:
      params->insert("code_verifier", code_verifier);
      break;
   }
});