我用Qt5。我没有找到任何关于如何在使用QOAuth2AuthorizationCodeFlow时启用PKCE的文档。
如果是,请提供链接。如果没有支持,如何将此功能添加到其中?
我添加了code_challenge和code_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();
}发布于 2022-03-16 08:22:47
下一步是在code_verifier阶段设置RequestingAccessToken。
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;
}
});发布于 2022-03-15 12:34:11
是的,你在用它。
阅读新潮流时,您注意到PKCE、code_verifier、code_challenge和code_challenge_method使用了三个新参数。
这些在您的代码中使用,所以您已经在使用PKCE了。
https://stackoverflow.com/questions/71480646
复制相似问题