Qt 4.8
Public Functions | Protected Variables | List of all members
QNetworkAccessAuthenticationManager Class Reference

#include <qnetworkaccessauthenticationmanager_p.h>

Public Functions

void cacheCredentials (const QUrl &url, const QAuthenticator *auth)
 
void cacheProxyCredentials (const QNetworkProxy &proxy, const QAuthenticator *auth)
 
void clearCache ()
 
QNetworkAuthenticationCredential fetchCachedCredentials (const QUrl &url, const QAuthenticator *auth=0)
 Fetch the credential data from the credential cache. More...
 
QNetworkAuthenticationCredential fetchCachedProxyCredentials (const QNetworkProxy &proxy, const QAuthenticator *auth=0)
 
 QNetworkAccessAuthenticationManager ()
 

Protected Variables

QNetworkAccessCache authenticationCache
 
QMutex mutex
 

Detailed Description

Definition at line 83 of file qnetworkaccessauthenticationmanager_p.h.

Constructors and Destructors

◆ QNetworkAccessAuthenticationManager()

QNetworkAccessAuthenticationManager::QNetworkAccessAuthenticationManager ( )
inline

Definition at line 86 of file qnetworkaccessauthenticationmanager_p.h.

86 { };

Functions

◆ cacheCredentials()

void QNetworkAccessAuthenticationManager::cacheCredentials ( const QUrl url,
const QAuthenticator auth 
)

Definition at line 228 of file qnetworkaccessauthenticationmanager.cpp.

Referenced by QHttpThreadDelegate::cacheCredentialsSlot().

230 {
231  Q_ASSERT(authenticator);
232  QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain
233  QString realm = authenticator->realm();
234 
235  QMutexLocker mutexLocker(&mutex);
236 
237  // Set two credentials actually: one with and one without the username in the URL
238  QUrl copy = url;
239  copy.setUserName(authenticator->user());
240  do {
241  QByteArray cacheKey = authenticationKey(copy, realm);
242  if (authenticationCache.hasEntry(cacheKey)) {
245  auth->insert(domain, authenticator->user(), authenticator->password());
247  } else {
249  auth->insert(domain, authenticator->user(), authenticator->password());
250  authenticationCache.addEntry(cacheKey, auth);
251  }
252 
253  if (copy.userName().isEmpty()) {
254  break;
255  } else {
256  copy.setUserName(QString());
257  }
258  } while (true);
259 }
static QByteArray authenticationKey(const QUrl &url, const QString &realm)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
CacheableObject * requestEntryNow(const QByteArray &key)
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void insert(const QString &domain, const QString &user, const QString &password)
void addEntry(const QByteArray &key, CacheableObject *entry)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString userName() const
Returns the user name of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4667
bool hasEntry(const QByteArray &key) const
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
void releaseEntry(const QByteArray &key)
void setUserName(const QString &userName)
Sets the URL&#39;s user name to userName.
Definition: qurl.cpp:4648

◆ cacheProxyCredentials()

void QNetworkAccessAuthenticationManager::cacheProxyCredentials ( const QNetworkProxy proxy,
const QAuthenticator auth 
)

Definition at line 150 of file qnetworkaccessauthenticationmanager.cpp.

152 {
153  Q_ASSERT(authenticator);
155  Q_ASSERT(p.type() != QNetworkProxy::NoProxy);
156 
157  QMutexLocker mutexLocker(&mutex);
158 
159  QString realm = authenticator->realm();
160  QNetworkProxy proxy = p;
161  proxy.setUser(authenticator->user());
162 
163  // don't cache null passwords, empty password may be valid though
164  if (authenticator->password().isNull())
165  return;
166 
167  // Set two credentials: one with the username and one without
168  do {
169  // Set two credentials actually: one with and one without the realm
170  do {
171  QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
172  if (cacheKey.isEmpty())
173  return; // should not happen
174 
176  auth->insert(QString(), authenticator->user(), authenticator->password());
177  authenticationCache.addEntry(cacheKey, auth); // replace the existing one, if there's any
178 
179  if (realm.isEmpty()) {
180  break;
181  } else {
182  realm.clear();
183  }
184  } while (true);
185 
186  if (proxy.user().isEmpty())
187  break;
188  else
189  proxy.setUser(QString());
190  } while (true);
191 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm)
void setUser(const QString &userName)
Sets the user name for proxy authentication to be user.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void insert(const QString &domain, const QString &user, const QString &password)
The QNetworkProxy class provides a network layer proxy.
void addEntry(const QByteArray &key, CacheableObject *entry)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QString user() const
Returns the user name used for authentication.

◆ clearCache()

void QNetworkAccessAuthenticationManager::clearCache ( )

◆ fetchCachedCredentials()

QNetworkAuthenticationCredential QNetworkAccessAuthenticationManager::fetchCachedCredentials ( const QUrl url,
const QAuthenticator authentication = 0 
)

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.

Definition at line 273 of file qnetworkaccessauthenticationmanager.cpp.

Referenced by QHttpThreadDelegate::synchronousAuthenticationRequiredSlot().

275 {
276  if (!url.password().isEmpty())
277  return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
278 
279  QString realm;
280  if (authentication)
281  realm = authentication->realm();
282 
283  QByteArray cacheKey = authenticationKey(url, realm);
284 
285  QMutexLocker mutexLocker(&mutex);
286  if (!authenticationCache.hasEntry(cacheKey))
288 
293  if (cred)
294  ret = *cred;
296  return ret;
297 }
static QByteArray authenticationKey(const QUrl &url, const QString &realm)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QNetworkAuthenticationCredential * findClosestMatch(const QString &domain)
CacheableObject * requestEntryNow(const QByteArray &key)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool hasEntry(const QByteArray &key) const
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
void releaseEntry(const QByteArray &key)
QString password() const
Returns the password of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4754
QString realm() const
returns the realm requiring authentication.

◆ fetchCachedProxyCredentials()

QNetworkAuthenticationCredential QNetworkAccessAuthenticationManager::fetchCachedProxyCredentials ( const QNetworkProxy proxy,
const QAuthenticator auth = 0 
)

Definition at line 194 of file qnetworkaccessauthenticationmanager.cpp.

Referenced by QHttpThreadDelegate::synchronousProxyAuthenticationRequiredSlot().

196 {
197  QNetworkProxy proxy = p;
198  if (proxy.type() == QNetworkProxy::DefaultProxy) {
200  }
201  if (!proxy.password().isEmpty())
202  return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
203 
204  QString realm;
205  if (authenticator)
206  realm = authenticator->realm();
207 
208  QMutexLocker mutexLocker(&mutex);
209  QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
210  if (cacheKey.isEmpty())
212  if (!authenticationCache.hasEntry(cacheKey))
214 
219 
220  // proxy cache credentials always have exactly one item
221  Q_ASSERT_X(!cred.isNull(), "QNetworkAccessManager",
222  "Internal inconsistency: found a cache key for a proxy, but it's empty");
223  return cred;
224 }
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm)
QNetworkAuthenticationCredential * findClosestMatch(const QString &domain)
CacheableObject * requestEntryNow(const QByteArray &key)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QNetworkProxy applicationProxy()
Returns the application level network proxying.
The QNetworkProxy class provides a network layer proxy.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool hasEntry(const QByteArray &key) const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QString password() const
Returns the password used for authentication.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
void releaseEntry(const QByteArray &key)

Properties

◆ authenticationCache

QNetworkAccessCache QNetworkAccessAuthenticationManager::authenticationCache
protected

Definition at line 101 of file qnetworkaccessauthenticationmanager_p.h.

◆ mutex

QMutex QNetworkAccessAuthenticationManager::mutex
protected

Definition at line 102 of file qnetworkaccessauthenticationmanager_p.h.


The documentation for this class was generated from the following files: