void QNetworkAccessManagerPrivate::proxyAuthenticationRequired(QNetworkAccessBackend *backend, const QNetworkProxy &proxy, QAuthenticator *authenticator) { Q_Q(QNetworkAccessManager); // ### FIXME Tracking of successful authentications // This code is a bit broken right now for SOCKS authentication // first request: proxyAuthenticationRequired gets emitted, credentials gets saved // second request: (proxy != backend->reply->lastProxyAuthentication) does not evaluate to true, // proxyAuthenticationRequired gets emitted again // possible solution: some tracking inside the authenticator // or a new function proxyAuthenticationSucceeded(true|false) if (proxy != backend->reply->lastProxyAuthentication) { QNetworkAuthenticationCredential cred = authenticationManager->fetchCachedProxyCredentials(proxy); if (!cred.isNull()) { authenticator->setUser(cred.user); authenticator->setPassword(cred.password); return; } } // if we emit a signal here in synchronous mode, the user might spin // an event loop, which might recurse and lead to problems if (backend->isSynchronous()) return; backend->reply->lastProxyAuthentication = proxy; emit q->proxyAuthenticationRequired(proxy, authenticator); authenticationManager->cacheProxyCredentials(proxy, authenticator); }
void QNetworkAccessManagerPrivate::authenticationRequired(QNetworkAccessBackend *backend, QAuthenticator *authenticator) { Q_Q(QNetworkAccessManager); // FIXME: Add support for domains (i.e., the leading path) QUrl url = backend->reply->url; // don't try the cache for the same URL twice in a row // being called twice for the same URL means the authentication failed // also called when last URL is empty, e.g. on first call if (backend->reply->urlForLastAuthentication.isEmpty() || url != backend->reply->urlForLastAuthentication) { QNetworkAuthenticationCredential cred = authenticationManager->fetchCachedCredentials(url, authenticator); if (!cred.isNull()) { authenticator->setUser(cred.user); authenticator->setPassword(cred.password); backend->reply->urlForLastAuthentication = url; return; } } // if we emit a signal here in synchronous mode, the user might spin // an event loop, which might recurse and lead to problems if (backend->isSynchronous()) return; backend->reply->urlForLastAuthentication = url; emit q->authenticationRequired(backend->reply->q_func(), authenticator); authenticationManager->cacheCredentials(url, authenticator); }
QNetworkAuthenticationCredential QNetworkAccessAuthenticationManager::fetchCachedProxyCredentials(const QNetworkProxy &p, const QAuthenticator *authenticator) { QNetworkProxy proxy = p; if (proxy.type() == QNetworkProxy::DefaultProxy) { proxy = QNetworkProxy::applicationProxy(); } if (!proxy.password().isEmpty()) return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them QString realm; if (authenticator) realm = authenticator->realm(); QMutexLocker mutexLocker(&mutex); QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); if (cacheKey.isEmpty()) return QNetworkAuthenticationCredential(); if (!authenticationCache.hasEntry(cacheKey)) return QNetworkAuthenticationCredential(); QNetworkAuthenticationCache *auth = static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); QNetworkAuthenticationCredential cred = *auth->findClosestMatch(QString()); authenticationCache.releaseEntry(cacheKey); // proxy cache credentials always have exactly one item Q_ASSERT_X(!cred.isNull(), "QNetworkAccessManager", "Internal inconsistency: found a cache key for a proxy, but it's empty"); return cred; }
void QNetworkAccessManagerPrivate::authenticationRequired(QAuthenticator *authenticator, QNetworkReply *reply, bool synchronous, QUrl &url, QUrl *urlForLastAuthentication) { Q_Q(QNetworkAccessManager); // don't try the cache for the same URL twice in a row // being called twice for the same URL means the authentication failed // also called when last URL is empty, e.g. on first call if (urlForLastAuthentication->isEmpty() || url != *urlForLastAuthentication) { QNetworkAuthenticationCredential cred = authenticationManager->fetchCachedCredentials(url, authenticator); if (!cred.isNull()) { authenticator->setUser(cred.user); authenticator->setPassword(cred.password); *urlForLastAuthentication = url; return; } } // if we emit a signal here in synchronous mode, the user might spin // an event loop, which might recurse and lead to problems if (synchronous) return; *urlForLastAuthentication = url; emit q->authenticationRequired(reply, authenticator); authenticationManager->cacheCredentials(url, authenticator); }
void QNetworkAccessManagerPrivate::proxyAuthenticationRequired(QNetworkAccessBackend *backend, const QNetworkProxy &proxy, QAuthenticator *authenticator) { Q_Q(QNetworkAccessManager); QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(*authenticator); if (proxy != backend->reply->lastProxyAuthentication && (!priv || !priv->hasFailed)) { QNetworkAuthenticationCredential cred = authenticationManager->fetchCachedProxyCredentials(proxy); if (!cred.isNull()) { authenticator->setUser(cred.user); authenticator->setPassword(cred.password); return; } } // if we emit a signal here in synchronous mode, the user might spin // an event loop, which might recurse and lead to problems if (backend->isSynchronous()) return; backend->reply->lastProxyAuthentication = proxy; emit q->proxyAuthenticationRequired(proxy, authenticator); authenticationManager->cacheProxyCredentials(proxy, authenticator); }