如何使用QOAuth2AuthorizationCodeFlow和QOAuthHttpServerReplyHandler设置redirect_uri [英] How to set redirect_uri using QOAuth2AuthorizationCodeFlow and QOAuthHttpServerReplyHandler
问题描述
对于使用QT的networkauth和新的QOAuth2AuthorizationCodeFlow对象的OAuth 2.0,如何设置redirect_uri?我的代码如下.结果导致发送了以下身份验证网址:
QOAuth2AuthorizationCodeFlow :: buildAuthenticateUrl: http://localhost ",会导致Google出现错误400 redirect_uri_mismatch,这显然是在预期要提供的实际重定向主机名.
GoogleGateway :: GoogleGateway(){自动google =新的QOAuth2AuthorizationCodeFlow;google-> setScope("email");this-> connect(google,& QOAuth2AuthorizationCodeFlow :: authorizeWithBrowser,& QDesktopServices :: openUrl);QString val;QFile文件;file.setFileName("/home/me/client_secret.json");file.open(QIODevice :: ReadOnly | QIODevice :: Text);val = file.readAll();file.close();QJsonDocument文档= QJsonDocument :: fromJson(val.toUtf8());QJsonObject对象= document.object();const auto settingsObject = object ["web"].toObject();const QUrl authUri(settingsObject ["auth_uri"].toString());const auto clientId = settingsObject ["client_id"].toString();const QUrl tokenUri(settingsObject ["token_uri"].toString());const auto clientSecret(settingsObject ["client_secret"].toString());const auto redirectUris = settingsObject ["redirect_uris"].toArray();const QUrl redirectUri(redirectUris [0] .toString());const auto port = static_cast< quint16>(redirectUri.port());google-> setAuthorizationUrl(authUri);google-> setClientIdentifier(clientId);google-> setAccessTokenUrl(tokenUri);google-> setClientIdentifierSharedKey(clientSecret);自动ReplyHandler =新的QOAuthHttpServerReplyHandler(端口,此);google-> setReplyHandler(replyHandler);google-> grant();}
要设置redirect_uri,我尝试替换:
auto replyHandler = new QOAuthHttpServerReplyHandler(port,this);
使用
QHostAddress主机地址= QHostAddress(quint32(1233 ...));自动ReplyHandler =新的QOAuthHttpServerReplyHandler(hostaddress,port,this);
结果保持不变.
也尝试插入:
replyHandler-> setProperty("redirect_uri","http://abc.xyz.com:65535/cb");
结果也没有变化.
在Qt/5.8/Src/qtnetworkauth/src/oauth/qoauthhttpserverreplyhandler.cpp中,我们看到回调地址看起来是可疑的硬编码:
QString QOAuthHttpServerReplyHandler :: callback()常量{Q_D(const QOAuthHttpServerReplyHandler);Q_ASSERT(d-> httpServer.isListening());const QUrl url(QString :: fromLatin1("http://localhost:%1/cb").arg(d-> httpServer.serverPort()));返回url.toString(QUrl :: EncodeDelimiters);}
依次在Qt/5.8/Src/qtnetworkauth/src/oauth/qoauth2authorizationcodeflow.cpp中使用此callback()来设置redirectUri值:
QUrl QOAuth2AuthorizationCodeFlow :: buildAuthenticateUrl(const QVariantMap& parameters){Q_D(QOAuth2AuthorizationCodeFlow);使用Key = QAbstractOAuth2Private :: OAuth2KeyString;如果(d-> state.isEmpty())setState(QAbstractOAuth2Private :: generateRandomState());Q_ASSERT(!d-> state.isEmpty());const QString state = d-> state;QVariantMap p(参数);QUrl url(d-> authorizationUrl);p.insert(Key :: responseType,responseType());p.insert(Key :: clientIdentifier,d-> clientCredentials.first);p.insert(Key :: redirectUri,callback());p.insert(Key :: scope,d-> scope);p.insert(Key :: state,state);如果(d-> modifyParametersFunction)d-> modifyParametersFunction(Stage :: RequestingAuthorization,& p);url.setQuery(d-> createQuery(p));connect(d-> replyHandler.data(),& QAbstractOAuthReplyHandler :: callbackReceived,this,& QOAuth2AuthorizationCodeFlow :: authorizationCallbackReceived,Qt :: UniqueConnection);setStatus(QAbstractOAuth :: Status :: NotAuthenticated);qDebug("QOAuth2AuthorizationCodeFlow :: buildAuthenticateUrl:%s",qPrintable(url.toString()));返回网址;}
这是一个错误吗?
我只是通过子类化 MyOAuthHttpServerReplyHandler
并重写 callback()
的定义以返回URI来解决此问题我想要.For OAuth 2.0 using QT's networkauth and the new QOAuth2AuthorizationCodeFlow object, how can I set the redirect_uri? My code is below. It results in the following authenticate url being sent:
QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl: https://accounts.google.com/o/oauth2/auth?client_id=123-abc.apps.googleusercontent.com&redirect_uri=http://localhost:65535/cb&response_type=code&scope=email&state=iEIYn5sN
The setting of redirect_uri to "http://localhost", results in an Error 400 redirect_uri_mismatch from google which is obviously expecting the actual redirect hostname to be provided.
GoogleGateway::GoogleGateway() {
auto google = new QOAuth2AuthorizationCodeFlow;
google->setScope("email");
this->connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
QString val;
QFile file;
file.setFileName("/home/me/client_secret.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument document = QJsonDocument::fromJson(val.toUtf8());
QJsonObject object = document.object();
const auto settingsObject = object["web"].toObject();
const QUrl authUri(settingsObject["auth_uri"].toString());
const auto clientId = settingsObject["client_id"].toString();
const QUrl tokenUri(settingsObject["token_uri"].toString());
const auto clientSecret(settingsObject["client_secret"].toString());
const auto redirectUris = settingsObject["redirect_uris"].toArray();
const QUrl redirectUri(redirectUris[0].toString());
const auto port = static_cast<quint16>(redirectUri.port());
google->setAuthorizationUrl(authUri);
google->setClientIdentifier(clientId);
google->setAccessTokenUrl(tokenUri);
google->setClientIdentifierSharedKey(clientSecret);
auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
google->setReplyHandler(replyHandler);
google->grant();
}
To set the redirect_uri, I've tried replacing:
auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
with
QHostAddress hostaddress = QHostAddress(quint32(1233...));
auto replyHandler = new QOAuthHttpServerReplyHandler(hostaddress, port, this);
with no change in the result.
Have also tried inserting:
replyHandler->setProperty("redirect_uri", "http://abc.xyz.com:65535/cb");
also with no change in the result.
In Qt/5.8/Src/qtnetworkauth/src/oauth/qoauthhttpserverreplyhandler.cpp, we see that the callback address looks suspiciously hard-coded:
QString QOAuthHttpServerReplyHandler::callback() const
{
Q_D(const QOAuthHttpServerReplyHandler);
Q_ASSERT(d->httpServer.isListening());
const QUrl url(QString::fromLatin1("http://localhost:%1/cb").arg(d->httpServer.serverPort()));
return url.toString(QUrl::EncodeDelimiters);
}
This callback() is in turn used in Qt/5.8/Src/qtnetworkauth/src/oauth/qoauth2authorizationcodeflow.cpp to set the redirectUri value:
QUrl QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl(const QVariantMap ¶meters)
{
Q_D(QOAuth2AuthorizationCodeFlow);
using Key = QAbstractOAuth2Private::OAuth2KeyString;
if (d->state.isEmpty())
setState(QAbstractOAuth2Private::generateRandomState());
Q_ASSERT(!d->state.isEmpty());
const QString state = d->state;
QVariantMap p(parameters);
QUrl url(d->authorizationUrl);
p.insert(Key::responseType, responseType());
p.insert(Key::clientIdentifier, d->clientCredentials.first);
p.insert(Key::redirectUri, callback());
p.insert(Key::scope, d->scope);
p.insert(Key::state, state);
if (d->modifyParametersFunction)
d->modifyParametersFunction(Stage::RequestingAuthorization, &p);
url.setQuery(d->createQuery(p));
connect(d->replyHandler.data(), &QAbstractOAuthReplyHandler::callbackReceived, this,
&QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived, Qt::UniqueConnection);
setStatus(QAbstractOAuth::Status::NotAuthenticated);
qDebug("QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl: %s", qPrintable(url.toString()));
return url;
}
Is this a bug?
I just solved this by subclassing MyOAuthHttpServerReplyHandler
and overriding the definition of callback()
to return the URI I wanted.
这篇关于如何使用QOAuth2AuthorizationCodeFlow和QOAuthHttpServerReplyHandler设置redirect_uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!