Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qnetworkaccessauthenticationmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
7
8#include "QtCore/qbuffer.h"
9#include "QtCore/qlist.h"
10#include "QtCore/qurl.h"
11#include "QtCore/QMutexLocker"
12#include "QtNetwork/qauthenticator.h"
13
14#include <algorithm>
15
17
18using namespace Qt::StringLiterals;
19
20class QNetworkAuthenticationCache : private QList<QNetworkAuthenticationCredential>,
22{
23public:
25 {
26 setExpires(false);
27 setShareable(true);
28 reserve(1);
29 }
30
33
34 iterator findClosestMatch(const QString &domain)
35 {
36 iterator it = std::lower_bound(begin(), end(), domain);
37 if (it == end() && !isEmpty())
38 --it;
39 if (it == end() || !domain.startsWith(it->domain))
40 return end();
41 return it;
42 }
43
44 void insert(const QString &domain, const QString &user, const QString &password)
45 {
46 iterator closestMatch = findClosestMatch(domain);
47 if (closestMatch != end() && closestMatch->domain == domain) {
48 // we're overriding the current credentials
49 closestMatch->user = user;
50 closestMatch->password = password;
51 } else {
53 newCredential.domain = domain;
54 newCredential.user = user;
55 newCredential.password = password;
56
57 if (closestMatch != end())
59 else
61 }
62 }
63
64 virtual void dispose() override { delete this; }
65};
66
67#ifndef QT_NO_NETWORKPROXY
69{
70 QUrl key;
71
72 switch (proxy.type()) {
74 key.setScheme("proxy-socks5"_L1);
75 break;
76
79 key.setScheme("proxy-http"_L1);
80 break;
81
83 key.setScheme("proxy-ftp"_L1);
84 break;
85
88 // shouldn't happen
89 return QByteArray();
90
91 // no default:
92 // let there be errors if a new proxy type is added in the future
93 }
94
95 if (key.scheme().isEmpty())
96 // proxy type not handled
97 return QByteArray();
98
99 key.setUserName(proxy.user());
100 key.setHost(proxy.hostName());
101 key.setPort(proxy.port());
102 key.setFragment(realm);
103 return "auth:" + key.toEncoded();
104}
105#endif
106
107static inline QByteArray authenticationKey(const QUrl &url, const QString &realm)
108{
109 QUrl copy = url;
110 copy.setFragment(realm);
111 return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery);
112}
113
114
115#ifndef QT_NO_NETWORKPROXY
117 const QAuthenticator *authenticator)
118{
119 Q_ASSERT(authenticator);
122
123 QMutexLocker mutexLocker(&mutex);
124
125 QString realm = authenticator->realm();
127 proxy.setUser(authenticator->user());
128
129 // don't cache null passwords, empty password may be valid though
130 if (authenticator->password().isNull())
131 return;
132
133 // Set two credentials: one with the username and one without
134 do {
135 // Set two credentials actually: one with and one without the realm
136 do {
138 if (cacheKey.isEmpty())
139 return; // should not happen
140
142 auth->insert(QString(), authenticator->user(), authenticator->password());
143 authenticationCache.addEntry(cacheKey, auth); // replace the existing one, if there's any
144
145 if (realm.isEmpty()) {
146 break;
147 } else {
148 realm.clear();
149 }
150 } while (true);
151
152 if (proxy.user().isEmpty())
153 break;
154 else
156 } while (true);
157}
158
161 const QAuthenticator *authenticator)
162{
166 }
167 if (!proxy.password().isEmpty())
168 return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
169
170 QString realm;
171 if (authenticator)
172 realm = authenticator->realm();
173
174 QMutexLocker mutexLocker(&mutex);
176 if (cacheKey.isEmpty())
180
183 QNetworkAuthenticationCredential cred = *auth->findClosestMatch(QString());
185
186 // proxy cache credentials always have exactly one item
187 Q_ASSERT_X(!cred.isNull(), "QNetworkAccessManager",
188 "Internal inconsistency: found a cache key for a proxy, but it's empty");
189 return cred;
190}
191
192#endif
193
195 const QAuthenticator *authenticator)
196{
197 Q_ASSERT(authenticator);
198 if (authenticator->isNull())
199 return;
200 QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain
201 QString realm = authenticator->realm();
202
203 QMutexLocker mutexLocker(&mutex);
204
205 // Set two credentials actually: one with and one without the username in the URL
206 QUrl copy = url;
207 copy.setUserName(authenticator->user());
208 do {
213 auth->insert(domain, authenticator->user(), authenticator->password());
215 } else {
217 auth->insert(domain, authenticator->user(), authenticator->password());
219 }
220
221 if (copy.userName().isEmpty()) {
222 break;
223 } else {
224 copy.setUserName(QString());
225 }
226 } while (true);
227}
228
242 const QAuthenticator *authentication)
243{
244 if (!url.password().isEmpty())
245 return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
246
247 QString realm;
248 if (authentication)
249 realm = authentication->realm();
250
252
253 QMutexLocker mutexLocker(&mutex);
256
259 auto cred = auth->findClosestMatch(url.path());
261 if (cred != auth->end())
262 ret = *cred;
264 return ret;
265}
266
271
273
The QAuthenticator class provides an authentication object.
QString user() const
Returns the user used for authentication.
QString password() const
Returns the password used for authentication.
QString realm() const
Returns the realm requiring authentication.
bool isNull() const
Returns true if the object has not been initialized.
\inmodule QtCore
Definition qbytearray.h:57
Definition qlist.h:75
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:488
\inmodule QtCore
Definition qmutex.h:313
void cacheCredentials(const QUrl &url, const QAuthenticator *auth)
QNetworkAuthenticationCredential fetchCachedCredentials(const QUrl &url, const QAuthenticator *auth=nullptr)
Fetch the credential data from the credential cache.
QNetworkAuthenticationCredential fetchCachedProxyCredentials(const QNetworkProxy &proxy, const QAuthenticator *auth=nullptr)
void cacheProxyCredentials(const QNetworkProxy &proxy, const QAuthenticator *auth)
bool hasEntry(const QByteArray &key) const
CacheableObject * requestEntryNow(const QByteArray &key)
void addEntry(const QByteArray &key, CacheableObject *entry, qint64 connectionCacheExpiryTimeoutSeconds=-1)
void releaseEntry(const QByteArray &key)
iterator findClosestMatch(const QString &domain)
void insert(const QString &domain, const QString &user, const QString &password)
The QNetworkProxy class provides a network layer proxy.
static QNetworkProxy applicationProxy()
Returns the application level network proxying.
QString user() const
Returns the user name used for authentication.
void setUser(const QString &userName)
Sets the user name for proxy authentication to be user.
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
QString password() const
Returns the password used for authentication.
QString hostName() const
Returns the host name of the proxy host.
quint16 port() const
Returns the port of the proxy host.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
\inmodule QtCore
Definition qurl.h:94
QString password(ComponentFormattingOptions=FullyDecoded) const
Returns the password of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2262
@ RemovePassword
Definition qurl.h:106
@ RemoveQuery
Definition qurl.h:111
@ RemovePath
Definition qurl.h:110
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
QSet< QString >::iterator it
Combined button and popup list for selecting options.
static jboolean copy(JNIEnv *, jobject)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
static int closestMatch(QRgb pixel, const QList< QRgb > &clut)
Definition qimage.cpp:2260
static QByteArray cacheKey(Args &&...args)
return ret
static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm)
static QByteArray authenticationKey(const QUrl &url, const QString &realm)
GLuint64 key
GLfloat GLfloat p
[1]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
QUrl url("example.com")
[constructor-url-reference]
QNetworkProxy proxy
[0]