/*! Fetch the credential data from the credential cache. If auth is 0 (as it is when called from createRequest()), this will try to look up with an empty realm. That fails in most cases for HTTP (because the realm is seldom empty for HTTP challenges). In any case, QHttpNetworkConnection never sends the credentials on the first attempt: it needs to find out what authentication methods the server supports. For FTP, realm is always empty. */ QNetworkAuthenticationCredential QNetworkAccessAuthenticationManager::fetchCachedCredentials(const QUrl &url, const QAuthenticator *authentication) { if (!url.password().isEmpty()) return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them QString realm; if (authentication) realm = authentication->realm(); QByteArray cacheKey = authenticationKey(url, realm); QMutexLocker mutexLocker(&mutex); if (!authenticationCache.hasEntry(cacheKey)) return QNetworkAuthenticationCredential(); QNetworkAuthenticationCache *auth = static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path()); QNetworkAuthenticationCredential ret; if (cred) ret = *cred; authenticationCache.releaseEntry(cacheKey); return ret; }
void QNetworkAccessAuthenticationManager::cacheCredentials(const QUrl &url, const QAuthenticator *authenticator) { Q_ASSERT(authenticator); QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain QString realm = authenticator->realm(); QMutexLocker mutexLocker(&mutex); // Set two credentials actually: one with and one without the username in the URL QUrl copy = url; copy.setUserName(authenticator->user()); do { QByteArray cacheKey = authenticationKey(copy, realm); if (authenticationCache.hasEntry(cacheKey)) { QNetworkAuthenticationCache *auth = static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); auth->insert(domain, authenticator->user(), authenticator->password()); authenticationCache.releaseEntry(cacheKey); } else { QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; auth->insert(domain, authenticator->user(), authenticator->password()); authenticationCache.addEntry(cacheKey, auth); } if (copy.userName().isEmpty()) { break; } else { copy.setUserName(QString()); } } while (true); }
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::addCredentials(const QNetworkProxy &p, const QAuthenticator *authenticator) { Q_ASSERT(authenticator); Q_ASSERT(p.type() != QNetworkProxy::DefaultProxy); Q_ASSERT(p.type() != QNetworkProxy::NoProxy); QString realm = authenticator->realm(); QNetworkProxy proxy = p; proxy.setUser(authenticator->user()); // Set two credentials: one with the username and one without do { // Set two credentials actually: one with and one without the realm do { QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); if (cacheKey.isEmpty()) return; // should not happen QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; auth->insert(QString(), authenticator->user(), authenticator->password()); cache.addEntry(cacheKey, auth); // replace the existing one, if there's any if (realm.isEmpty()) { break; } else { realm.clear(); } } while (true); if (proxy.user().isEmpty()) break; else proxy.setUser(QString()); } while (true); }
/*! Fetch the credential data from the credential cache. If auth is 0 (as it is when called from createRequest()), this will try to look up with an empty realm. That fails in most cases for HTTP (because the realm is seldom empty for HTTP challenges). In any case, QHttpNetworkConnection never sends the credentials on the first attempt: it needs to find out what authentication methods the server supports. For FTP, realm is always empty. */ QNetworkAuthenticationCredential * QNetworkAccessManagerPrivate::fetchCachedCredentials(const QUrl &url, const QAuthenticator *authentication) { if (!url.password().isEmpty()) return 0; // no need to set credentials if it already has them QString realm; if (authentication) realm = authentication->realm(); QByteArray cacheKey = authenticationKey(url, realm); if (!cache.hasEntry(cacheKey)) return 0; QNetworkAuthenticationCache *auth = static_cast<QNetworkAuthenticationCache *>(cache.requestEntryNow(cacheKey)); QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path()); cache.releaseEntry(cacheKey); return cred; }