From 55feb0d08a1c0ffc8721cbfd3defa359bc593358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Thu, 16 Aug 2018 16:09:55 +0200 Subject: QHttpNetworkConnectionChannel: remove *authMethod We store the authenticator right alongside it, which knows the method. The biggest change from this is changing a, self-proclaimed, duplicate method from QHttpNetworkReply. Finding the method name ahead-of-time is not actually needed, all we need to know is that a supported authentication method is requested. Also moved that specific functionality to a more logical location: QAuthenticatorPrivate. Change-Id: I11627803ccb42b8ec33a28ef1d1e00bf60dc6da9 Reviewed-by: Edward Welbourne Reviewed-by: Timur Pocheptsov --- src/network/access/qhttpnetworkconnection.cpp | 65 +++++++++++++-------------- 1 file changed, 31 insertions(+), 34 deletions(-) (limited to 'src/network/access/qhttpnetworkconnection.cpp') diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp index 18b9e38b9a..73510e6bef 100644 --- a/src/network/access/qhttpnetworkconnection.cpp +++ b/src/network/access/qhttpnetworkconnection.cpp @@ -397,10 +397,12 @@ void QHttpNetworkConnectionPrivate::copyCredentials(int fromChannel, QAuthentica // NTLM and Negotiate do multi-phase authentication. // Copying credentialsbetween authenticators would mess things up. if (fromChannel >= 0) { - const QHttpNetworkConnectionChannel &channel = channels[fromChannel]; - const QAuthenticatorPrivate::Method method = isProxy ? channel.proxyAuthMethod : channel.authMethod; - if (method == QAuthenticatorPrivate::Ntlm || method == QAuthenticatorPrivate::Negotiate) + QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(*auth); + if (priv + && (priv->method == QAuthenticatorPrivate::Ntlm + || priv->method == QAuthenticatorPrivate::Negotiate)) { return; + } } // select another channel @@ -432,19 +434,16 @@ bool QHttpNetworkConnectionPrivate::handleAuthenticateChallenge(QAbstractSocket //create the response header to be used with QAuthenticatorPrivate. QList > fields = reply->header(); - //find out the type of authentication protocol requested. - QAuthenticatorPrivate::Method authMethod = reply->d_func()->authenticationMethod(isProxy); - if (authMethod != QAuthenticatorPrivate::None) { + // Check that any of the proposed authenticate methods are supported + const QByteArray header = isProxy ? "proxy-authenticate" : "www-authenticate"; + const QByteArrayList &authenticationMethods = reply->d_func()->headerFieldValues(header); + const bool isSupported = std::any_of(authenticationMethods.begin(), authenticationMethods.end(), + QAuthenticatorPrivate::isMethodSupported); + if (isSupported) { int i = indexOf(socket); //Use a single authenticator for all domains. ### change later to use domain/realm - QAuthenticator* auth = nullptr; - if (isProxy) { - auth = &channels[i].proxyAuthenticator; - channels[i].proxyAuthMethod = authMethod; - } else { - auth = &channels[i].authenticator; - channels[i].authMethod = authMethod; - } + QAuthenticator *auth = isProxy ? &channels[i].proxyAuthenticator + : &channels[i].authenticator; //proceed with the authentication. if (auth->isNull()) auth->detach(); @@ -453,10 +452,6 @@ bool QHttpNetworkConnectionPrivate::handleAuthenticateChallenge(QAbstractSocket // Update method in case it changed if (priv->method == QAuthenticatorPrivate::None) return false; - if (isProxy) - channels[i].proxyAuthMethod = priv->method; - else - channels[i].authMethod = priv->method; if (priv->phase == QAuthenticatorPrivate::Done) { pauseConnection(); @@ -591,28 +586,30 @@ void QHttpNetworkConnectionPrivate::createAuthorization(QAbstractSocket *socket, int i = indexOf(socket); + QAuthenticator *authenticator = &channels[i].authenticator; + QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(*authenticator); // Send "Authorization" header, but not if it's NTLM and the socket is already authenticated. - if (channels[i].authMethod != QAuthenticatorPrivate::None) { - if ((channels[i].authMethod != QAuthenticatorPrivate::Ntlm && request.headerField("Authorization").isEmpty()) || channels[i].lastStatus == 401) { - QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(channels[i].authenticator); - if (priv && priv->method != QAuthenticatorPrivate::None) { - QByteArray response = priv->calculateResponse(request.methodName(), request.uri(false), request.url().host()); - request.setHeaderField("Authorization", response); - channels[i].authenticationCredentialsSent = true; - } + if (priv && priv->method != QAuthenticatorPrivate::None) { + if ((priv->method != QAuthenticatorPrivate::Ntlm + && request.headerField("Authorization").isEmpty()) + || channels[i].lastStatus == 401) { + QByteArray response = priv->calculateResponse(request.methodName(), request.uri(false), + request.url().host()); + request.setHeaderField("Authorization", response); + channels[i].authenticationCredentialsSent = true; } } #if QT_CONFIG(networkproxy) + authenticator = &channels[i].proxyAuthenticator; + priv = QAuthenticatorPrivate::getPrivate(*authenticator); // Send "Proxy-Authorization" header, but not if it's NTLM and the socket is already authenticated. - if (channels[i].proxyAuthMethod != QAuthenticatorPrivate::None) { - if (!(channels[i].proxyAuthMethod == QAuthenticatorPrivate::Ntlm && channels[i].lastStatus != 407)) { - QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(channels[i].proxyAuthenticator); - if (priv && priv->method != QAuthenticatorPrivate::None) { - QByteArray response = priv->calculateResponse(request.methodName(), request.uri(false), networkProxy.hostName()); - request.setHeaderField("Proxy-Authorization", response); - channels[i].proxyCredentialsSent = true; - } + if (priv && priv->method != QAuthenticatorPrivate::None) { + if (priv->method != QAuthenticatorPrivate::Ntlm || channels[i].lastStatus == 407) { + QByteArray response = priv->calculateResponse(request.methodName(), request.uri(false), + networkProxy.hostName()); + request.setHeaderField("Proxy-Authorization", response); + channels[i].proxyCredentialsSent = true; } } #endif // QT_CONFIG(networkproxy) -- cgit v1.2.3