我使用 Qt5。在使用 QOAuth2AuthorizationCodeFlow 時,我沒有找到任何有關如何啟用 PKCE 的檔案。
如果有,請提供鏈接。如果沒有支持,如何添加這個功能呢?
我添加了code_challengeand 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();
}
uj5u.com熱心網友回復:
TL;DR 是的,您正在使用它。
閱讀新流程,您會注意到 PKCE、 和 使用了三個code_verifier新code_challenge引數code_challenge_method。
這些在您的代碼中使用,因此您已經在使用 PKCE。
uj5u.com熱心網友回復:
下一步是設定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;
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448166.html
