Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkaccessauthenticationmanager.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qnetworkaccessauthenticationmanager_p.h" | - | ||||||||||||
35 | #include "qnetworkaccessmanager.h" | - | ||||||||||||
36 | #include "qnetworkaccessmanager_p.h" | - | ||||||||||||
37 | - | |||||||||||||
38 | #include "QtCore/qbuffer.h" | - | ||||||||||||
39 | #include "QtCore/qurl.h" | - | ||||||||||||
40 | #include "QtCore/qvector.h" | - | ||||||||||||
41 | #include "QtCore/QMutexLocker" | - | ||||||||||||
42 | #include "QtNetwork/qauthenticator.h" | - | ||||||||||||
43 | - | |||||||||||||
44 | #include <algorithm> | - | ||||||||||||
45 | - | |||||||||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||||||||
47 | - | |||||||||||||
48 | - | |||||||||||||
49 | - | |||||||||||||
50 | - | |||||||||||||
51 | class QNetworkAuthenticationCache: private QVector<QNetworkAuthenticationCredential>, | - | ||||||||||||
52 | public QNetworkAccessCache::CacheableObject | - | ||||||||||||
53 | { | - | ||||||||||||
54 | public: | - | ||||||||||||
55 | QNetworkAuthenticationCache() | - | ||||||||||||
56 | { | - | ||||||||||||
57 | setExpires(false); | - | ||||||||||||
58 | setShareable(true); | - | ||||||||||||
59 | reserve(1); | - | ||||||||||||
60 | } executed 315 times by 2 tests: end of block Executed by:
| 315 | ||||||||||||
61 | - | |||||||||||||
62 | QNetworkAuthenticationCredential *findClosestMatch(const QString &domain) | - | ||||||||||||
63 | { | - | ||||||||||||
64 | iterator it = std::lower_bound(begin(), end(), domain); | - | ||||||||||||
65 | if (it == end() && !isEmpty())
| 31-366 | ||||||||||||
66 | --it; executed 31 times by 1 test: --it; Executed by:
| 31 | ||||||||||||
67 | if (it == end() || !domain.startsWith(it->domain))
| 0-397 | ||||||||||||
68 | return 0; executed 315 times by 2 tests: return 0; Executed by:
| 315 | ||||||||||||
69 | return &*it; executed 397 times by 1 test: return &*it; Executed by:
| 397 | ||||||||||||
70 | } | - | ||||||||||||
71 | - | |||||||||||||
72 | void insert(const QString &domain, const QString &user, const QString &password) | - | ||||||||||||
73 | { | - | ||||||||||||
74 | QNetworkAuthenticationCredential *closestMatch = findClosestMatch(domain); | - | ||||||||||||
75 | if (closestMatch && closestMatch->domain == domain) {
| 0-358 | ||||||||||||
76 | // we're overriding the current credentials | - | ||||||||||||
77 | closestMatch->user = user; | - | ||||||||||||
78 | closestMatch->password = password; | - | ||||||||||||
79 | } else { executed 358 times by 1 test: end of block Executed by:
| 358 | ||||||||||||
80 | QNetworkAuthenticationCredential newCredential; | - | ||||||||||||
81 | newCredential.domain = domain; | - | ||||||||||||
82 | newCredential.user = user; | - | ||||||||||||
83 | newCredential.password = password; | - | ||||||||||||
84 | - | |||||||||||||
85 | if (closestMatch)
| 0-315 | ||||||||||||
86 | QVector<QNetworkAuthenticationCredential>::insert(++closestMatch, newCredential); never executed: QVector<QNetworkAuthenticationCredential>::insert(++closestMatch, newCredential); | 0 | ||||||||||||
87 | else | - | ||||||||||||
88 | QVector<QNetworkAuthenticationCredential>::insert(end(), newCredential); executed 315 times by 2 tests: QVector<QNetworkAuthenticationCredential>::insert(end(), newCredential); Executed by:
| 315 | ||||||||||||
89 | } | - | ||||||||||||
90 | } | - | ||||||||||||
91 | - | |||||||||||||
92 | virtual void dispose() Q_DECL_OVERRIDE { delete this; } executed 315 times by 2 tests: end of block Executed by:
| 315 | ||||||||||||
93 | }; | - | ||||||||||||
94 | - | |||||||||||||
95 | #ifndef QT_NO_NETWORKPROXY | - | ||||||||||||
96 | static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm) | - | ||||||||||||
97 | { | - | ||||||||||||
98 | QUrl key; | - | ||||||||||||
99 | - | |||||||||||||
100 | switch (proxy.type()) { | - | ||||||||||||
101 | case QNetworkProxy::Socks5Proxy: executed 78 times by 2 tests: case QNetworkProxy::Socks5Proxy: Executed by:
| 78 | ||||||||||||
102 | key.setScheme(QLatin1String("proxy-socks5")); | - | ||||||||||||
103 | break; executed 78 times by 2 tests: break; Executed by:
| 78 | ||||||||||||
104 | - | |||||||||||||
105 | case QNetworkProxy::HttpProxy: executed 104 times by 2 tests: case QNetworkProxy::HttpProxy: Executed by:
| 104 | ||||||||||||
106 | case QNetworkProxy::HttpCachingProxy: executed 21 times by 1 test: case QNetworkProxy::HttpCachingProxy: Executed by:
| 21 | ||||||||||||
107 | key.setScheme(QLatin1String("proxy-http")); | - | ||||||||||||
108 | break; executed 125 times by 2 tests: break; Executed by:
| 125 | ||||||||||||
109 | - | |||||||||||||
110 | case QNetworkProxy::FtpCachingProxy: never executed: case QNetworkProxy::FtpCachingProxy: | 0 | ||||||||||||
111 | key.setScheme(QLatin1String("proxy-ftp")); | - | ||||||||||||
112 | break; never executed: break; | 0 | ||||||||||||
113 | - | |||||||||||||
114 | case QNetworkProxy::DefaultProxy: never executed: case QNetworkProxy::DefaultProxy: | 0 | ||||||||||||
115 | case QNetworkProxy::NoProxy: never executed: case QNetworkProxy::NoProxy: | 0 | ||||||||||||
116 | // shouldn't happen | - | ||||||||||||
117 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
118 | - | |||||||||||||
119 | // no default: | - | ||||||||||||
120 | // let there be errors if a new proxy type is added in the future | - | ||||||||||||
121 | } | - | ||||||||||||
122 | - | |||||||||||||
123 | if (key.scheme().isEmpty())
| 0-203 | ||||||||||||
124 | // proxy type not handled | - | ||||||||||||
125 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
126 | - | |||||||||||||
127 | key.setUserName(proxy.user()); | - | ||||||||||||
128 | key.setHost(proxy.hostName()); | - | ||||||||||||
129 | key.setPort(proxy.port()); | - | ||||||||||||
130 | key.setFragment(realm); | - | ||||||||||||
131 | return "auth:" + key.toEncoded(); executed 203 times by 2 tests: return "auth:" + key.toEncoded(); Executed by:
| 203 | ||||||||||||
132 | } | - | ||||||||||||
133 | #endif | - | ||||||||||||
134 | - | |||||||||||||
135 | static inline QByteArray authenticationKey(const QUrl &url, const QString &realm) | - | ||||||||||||
136 | { | - | ||||||||||||
137 | QUrl copy = url; | - | ||||||||||||
138 | copy.setFragment(realm); | - | ||||||||||||
139 | return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery); executed 932 times by 4 tests: return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery); Executed by:
| 932 | ||||||||||||
140 | } | - | ||||||||||||
141 | - | |||||||||||||
142 | - | |||||||||||||
143 | #ifndef QT_NO_NETWORKPROXY | - | ||||||||||||
144 | void QNetworkAccessAuthenticationManager::cacheProxyCredentials(const QNetworkProxy &p, | - | ||||||||||||
145 | const QAuthenticator *authenticator) | - | ||||||||||||
146 | { | - | ||||||||||||
147 | Q_ASSERT(authenticator); | - | ||||||||||||
148 | Q_ASSERT(p.type() != QNetworkProxy::DefaultProxy); | - | ||||||||||||
149 | Q_ASSERT(p.type() != QNetworkProxy::NoProxy); | - | ||||||||||||
150 | - | |||||||||||||
151 | QMutexLocker mutexLocker(&mutex); | - | ||||||||||||
152 | - | |||||||||||||
153 | QString realm = authenticator->realm(); | - | ||||||||||||
154 | QNetworkProxy proxy = p; | - | ||||||||||||
155 | proxy.setUser(authenticator->user()); | - | ||||||||||||
156 | - | |||||||||||||
157 | // don't cache null passwords, empty password may be valid though | - | ||||||||||||
158 | if (authenticator->password().isNull())
| 6-54 | ||||||||||||
159 | return; executed 6 times by 2 tests: return; Executed by:
| 6 | ||||||||||||
160 | - | |||||||||||||
161 | // Set two credentials: one with the username and one without | - | ||||||||||||
162 | do { | - | ||||||||||||
163 | // Set two credentials actually: one with and one without the realm | - | ||||||||||||
164 | do { | - | ||||||||||||
165 | QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); | - | ||||||||||||
166 | if (cacheKey.isEmpty())
| 0-138 | ||||||||||||
167 | return; // should not happen never executed: return; | 0 | ||||||||||||
168 | - | |||||||||||||
169 | QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; | - | ||||||||||||
170 | auth->insert(QString(), authenticator->user(), authenticator->password()); | - | ||||||||||||
171 | authenticationCache.addEntry(cacheKey, auth); // replace the existing one, if there's any | - | ||||||||||||
172 | - | |||||||||||||
173 | if (realm.isEmpty()) {
| 30-108 | ||||||||||||
174 | break; executed 108 times by 2 tests: break; Executed by:
| 108 | ||||||||||||
175 | } else { | - | ||||||||||||
176 | realm.clear(); | - | ||||||||||||
177 | } executed 30 times by 2 tests: end of block Executed by:
| 30 | ||||||||||||
178 | } while (true); | - | ||||||||||||
179 | - | |||||||||||||
180 | if (proxy.user().isEmpty())
| 54 | ||||||||||||
181 | break; executed 54 times by 2 tests: break; Executed by:
| 54 | ||||||||||||
182 | else | - | ||||||||||||
183 | proxy.setUser(QString()); executed 54 times by 2 tests: proxy.setUser(QString()); Executed by:
| 54 | ||||||||||||
184 | } while (true); | - | ||||||||||||
185 | } executed 54 times by 2 tests: end of block Executed by:
| 54 | ||||||||||||
186 | - | |||||||||||||
187 | QNetworkAuthenticationCredential | - | ||||||||||||
188 | QNetworkAccessAuthenticationManager::fetchCachedProxyCredentials(const QNetworkProxy &p, | - | ||||||||||||
189 | const QAuthenticator *authenticator) | - | ||||||||||||
190 | { | - | ||||||||||||
191 | QNetworkProxy proxy = p; | - | ||||||||||||
192 | if (proxy.type() == QNetworkProxy::DefaultProxy) {
| 0-65 | ||||||||||||
193 | proxy = QNetworkProxy::applicationProxy(); | - | ||||||||||||
194 | } never executed: end of block | 0 | ||||||||||||
195 | if (!proxy.password().isEmpty())
| 0-65 | ||||||||||||
196 | return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them never executed: return QNetworkAuthenticationCredential(); | 0 | ||||||||||||
197 | - | |||||||||||||
198 | QString realm; | - | ||||||||||||
199 | if (authenticator)
| 1-64 | ||||||||||||
200 | realm = authenticator->realm(); executed 1 time by 1 test: realm = authenticator->realm(); Executed by:
| 1 | ||||||||||||
201 | - | |||||||||||||
202 | QMutexLocker mutexLocker(&mutex); | - | ||||||||||||
203 | QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); | - | ||||||||||||
204 | if (cacheKey.isEmpty())
| 0-65 | ||||||||||||
205 | return QNetworkAuthenticationCredential(); never executed: return QNetworkAuthenticationCredential(); | 0 | ||||||||||||
206 | if (!authenticationCache.hasEntry(cacheKey))
| 8-57 | ||||||||||||
207 | return QNetworkAuthenticationCredential(); executed 57 times by 2 tests: return QNetworkAuthenticationCredential(); Executed by:
| 57 | ||||||||||||
208 | - | |||||||||||||
209 | QNetworkAuthenticationCache *auth = | - | ||||||||||||
210 | static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); | - | ||||||||||||
211 | QNetworkAuthenticationCredential cred = *auth->findClosestMatch(QString()); | - | ||||||||||||
212 | authenticationCache.releaseEntry(cacheKey); | - | ||||||||||||
213 | - | |||||||||||||
214 | // proxy cache credentials always have exactly one item | - | ||||||||||||
215 | Q_ASSERT_X(!cred.isNull(), "QNetworkAccessManager", | - | ||||||||||||
216 | "Internal inconsistency: found a cache key for a proxy, but it's empty"); | - | ||||||||||||
217 | return cred; executed 8 times by 1 test: return cred; Executed by:
| 8 | ||||||||||||
218 | } | - | ||||||||||||
219 | - | |||||||||||||
220 | #endif | - | ||||||||||||
221 | - | |||||||||||||
222 | void QNetworkAccessAuthenticationManager::cacheCredentials(const QUrl &url, | - | ||||||||||||
223 | const QAuthenticator *authenticator) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | Q_ASSERT(authenticator); | - | ||||||||||||
226 | if (authenticator->isNull())
| 3-273 | ||||||||||||
227 | return; executed 3 times by 1 test: return; Executed by:
| 3 | ||||||||||||
228 | QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain | - | ||||||||||||
229 | QString realm = authenticator->realm(); | - | ||||||||||||
230 | - | |||||||||||||
231 | QMutexLocker mutexLocker(&mutex); | - | ||||||||||||
232 | - | |||||||||||||
233 | // Set two credentials actually: one with and one without the username in the URL | - | ||||||||||||
234 | QUrl copy = url; | - | ||||||||||||
235 | copy.setUserName(authenticator->user()); | - | ||||||||||||
236 | do { | - | ||||||||||||
237 | QByteArray cacheKey = authenticationKey(copy, realm); | - | ||||||||||||
238 | if (authenticationCache.hasEntry(cacheKey)) {
| 177-358 | ||||||||||||
239 | QNetworkAuthenticationCache *auth = | - | ||||||||||||
240 | static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); | - | ||||||||||||
241 | auth->insert(domain, authenticator->user(), authenticator->password()); | - | ||||||||||||
242 | authenticationCache.releaseEntry(cacheKey); | - | ||||||||||||
243 | } else { executed 358 times by 1 test: end of block Executed by:
| 358 | ||||||||||||
244 | QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; | - | ||||||||||||
245 | auth->insert(domain, authenticator->user(), authenticator->password()); | - | ||||||||||||
246 | authenticationCache.addEntry(cacheKey, auth); | - | ||||||||||||
247 | } executed 177 times by 1 test: end of block Executed by:
| 177 | ||||||||||||
248 | - | |||||||||||||
249 | if (copy.userName().isEmpty()) {
| 262-273 | ||||||||||||
250 | break; executed 273 times by 1 test: break; Executed by:
| 273 | ||||||||||||
251 | } else { | - | ||||||||||||
252 | copy.setUserName(QString()); | - | ||||||||||||
253 | } executed 262 times by 1 test: end of block Executed by:
| 262 | ||||||||||||
254 | } while (true); | - | ||||||||||||
255 | } executed 273 times by 1 test: end of block Executed by:
| 273 | ||||||||||||
256 | - | |||||||||||||
257 | /*! | - | ||||||||||||
258 | Fetch the credential data from the credential cache. | - | ||||||||||||
259 | - | |||||||||||||
260 | If auth is 0 (as it is when called from createRequest()), this will try to | - | ||||||||||||
261 | look up with an empty realm. That fails in most cases for HTTP (because the | - | ||||||||||||
262 | realm is seldom empty for HTTP challenges). In any case, QHttpNetworkConnection | - | ||||||||||||
263 | never sends the credentials on the first attempt: it needs to find out what | - | ||||||||||||
264 | authentication methods the server supports. | - | ||||||||||||
265 | - | |||||||||||||
266 | For FTP, realm is always empty. | - | ||||||||||||
267 | */ | - | ||||||||||||
268 | QNetworkAuthenticationCredential | - | ||||||||||||
269 | QNetworkAccessAuthenticationManager::fetchCachedCredentials(const QUrl &url, | - | ||||||||||||
270 | const QAuthenticator *authentication) | - | ||||||||||||
271 | { | - | ||||||||||||
272 | if (!url.password().isEmpty())
| 16-397 | ||||||||||||
273 | return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them executed 16 times by 1 test: return QNetworkAuthenticationCredential(); Executed by:
| 16 | ||||||||||||
274 | - | |||||||||||||
275 | QString realm; | - | ||||||||||||
276 | if (authentication)
| 89-308 | ||||||||||||
277 | realm = authentication->realm(); executed 89 times by 1 test: realm = authentication->realm(); Executed by:
| 89 | ||||||||||||
278 | - | |||||||||||||
279 | QByteArray cacheKey = authenticationKey(url, realm); | - | ||||||||||||
280 | - | |||||||||||||
281 | QMutexLocker mutexLocker(&mutex); | - | ||||||||||||
282 | if (!authenticationCache.hasEntry(cacheKey))
| 31-366 | ||||||||||||
283 | return QNetworkAuthenticationCredential(); executed 366 times by 4 tests: return QNetworkAuthenticationCredential(); Executed by:
| 366 | ||||||||||||
284 | - | |||||||||||||
285 | QNetworkAuthenticationCache *auth = | - | ||||||||||||
286 | static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey)); | - | ||||||||||||
287 | QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path()); | - | ||||||||||||
288 | QNetworkAuthenticationCredential ret; | - | ||||||||||||
289 | if (cred)
| 0-31 | ||||||||||||
290 | ret = *cred; executed 31 times by 1 test: ret = *cred; Executed by:
| 31 | ||||||||||||
291 | authenticationCache.releaseEntry(cacheKey); | - | ||||||||||||
292 | return ret; executed 31 times by 1 test: return ret; Executed by:
| 31 | ||||||||||||
293 | } | - | ||||||||||||
294 | - | |||||||||||||
295 | void QNetworkAccessAuthenticationManager::clearCache() | - | ||||||||||||
296 | { | - | ||||||||||||
297 | authenticationCache.clear(); | - | ||||||||||||
298 | } executed 646 times by 2 tests: end of block Executed by:
| 646 | ||||||||||||
299 | - | |||||||||||||
300 | QT_END_NAMESPACE | - | ||||||||||||
301 | - | |||||||||||||
Source code | Switch to Preprocessed file |