asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_HTTP_HTTP_AUTH_CONTROLLER_H_ |
| 6 | #define NET_HTTP_HTTP_AUTH_CONTROLLER_H_ |
| 7 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8 | #include <memory> |
[email protected] | cee6312 | 2010-07-20 04:43:31 | [diff] [blame] | 9 | #include <set> |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 14 | #include "net/base/completion_once_callback.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 15 | #include "net/base/net_export.h" |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 16 | #include "net/http/http_auth.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 17 | #include "url/gurl.h" |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 18 | |
| 19 | namespace net { |
| 20 | |
| 21 | class AuthChallengeInfo; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 22 | class AuthCredentials; |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 23 | class HttpAuthHandler; |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 24 | class HttpAuthHandlerFactory; |
| 25 | class HttpAuthCache; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 26 | class HttpRequestHeaders; |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame^] | 27 | class HostResolver; |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 28 | class NetLogWithSource; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 29 | struct HttpRequestInfo; |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 30 | class SSLInfo; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 31 | |
Matt Menke | 84586d8 | 2017-09-28 19:39:28 | [diff] [blame] | 32 | // HttpAuthController is interface between other classes and HttpAuthHandlers. |
| 33 | // It handles all challenges when attempting to make a single request to a |
| 34 | // server, both in the case of trying multiple sets of credentials (Possibly on |
| 35 | // different sockets), and when going through multiple rounds of auth with |
| 36 | // connection-based auth, creating new HttpAuthHandlers as necessary. |
| 37 | // |
| 38 | // It is unaware of when a round of auth uses a new socket, which can lead to |
| 39 | // problems for connection-based auth. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 40 | class NET_EXPORT_PRIVATE HttpAuthController |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 41 | : public base::RefCounted<HttpAuthController> { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 42 | public: |
| 43 | // The arguments are self explanatory except possibly for |auth_url|, which |
| 44 | // should be both the auth target and auth path in a single url argument. |
Matt Menke | 84586d8 | 2017-09-28 19:39:28 | [diff] [blame] | 45 | // |target| indicates whether this is for authenticating with a proxy or |
| 46 | // destination server. |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 47 | HttpAuthController(HttpAuth::Target target, |
| 48 | const GURL& auth_url, |
| 49 | HttpAuthCache* http_auth_cache, |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame^] | 50 | HttpAuthHandlerFactory* http_auth_handler_factory, |
| 51 | HostResolver* host_resolver); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 52 | |
| 53 | // Generate an authentication token for |target| if necessary. The return |
| 54 | // value is a net error code. |OK| will be returned both in the case that |
| 55 | // a token is correctly generated synchronously, as well as when no tokens |
| 56 | // were necessary. |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 57 | int MaybeGenerateAuthToken(const HttpRequestInfo* request, |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 58 | CompletionOnceCallback callback, |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 59 | const NetLogWithSource& net_log); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 60 | |
| 61 | // Adds either the proxy auth header, or the origin server auth header, |
| 62 | // as specified by |target_|. |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 63 | void AddAuthorizationHeader(HttpRequestHeaders* authorization_headers); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 64 | |
| 65 | // Checks for and handles HTTP status code 401 or 407. |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 66 | // |HandleAuthChallenge()| returns OK on success, or a network error code |
| 67 | // otherwise. It may also populate |auth_info_|. |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 68 | int HandleAuthChallenge(scoped_refptr<HttpResponseHeaders> headers, |
| 69 | const SSLInfo& ssl_info, |
| 70 | bool do_not_send_server_auth, |
| 71 | bool establishing_tunnel, |
| 72 | const NetLogWithSource& net_log); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 73 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 74 | // Store the supplied credentials and prepare to restart the auth. |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 75 | void ResetAuth(const AuthCredentials& credentials); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 76 | |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 77 | bool HaveAuthHandler() const; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 78 | |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 79 | bool HaveAuth() const; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 80 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 81 | // Return whether the authentication scheme is incompatible with HTTP/2 |
| 82 | // and thus the server would presumably reject a request on HTTP/2 anyway. |
| 83 | bool NeedsHTTP11() const; |
| 84 | |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 85 | scoped_refptr<AuthChallengeInfo> auth_info(); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 86 | |
asanka | 5e8286c3 | 2017-02-23 16:13:42 | [diff] [blame] | 87 | bool IsAuthSchemeDisabled(HttpAuth::Scheme scheme) const; |
| 88 | void DisableAuthScheme(HttpAuth::Scheme scheme); |
| 89 | void DisableEmbeddedIdentity(); |
[email protected] | cee6312 | 2010-07-20 04:43:31 | [diff] [blame] | 90 | |
davidben | 8c7089a | 2017-04-17 20:38:22 | [diff] [blame] | 91 | // Called when the connection has been closed, so the current handler (which |
| 92 | // contains state bound to the connection) should be dropped. If retrying on a |
| 93 | // new connection, the next call to MaybeGenerateAuthToken will retry the |
| 94 | // current auth scheme. |
| 95 | void OnConnectionClosed(); |
| 96 | |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 97 | private: |
[email protected] | 463f835 | 2011-02-18 14:26:55 | [diff] [blame] | 98 | // Actions for InvalidateCurrentHandler() |
| 99 | enum InvalidateHandlerAction { |
| 100 | INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS, |
[email protected] | 26d84b0 | 2011-08-31 14:07:08 | [diff] [blame] | 101 | INVALIDATE_HANDLER_AND_DISABLE_SCHEME, |
[email protected] | 463f835 | 2011-02-18 14:26:55 | [diff] [blame] | 102 | INVALIDATE_HANDLER |
| 103 | }; |
| 104 | |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 105 | // So that we can mock this object. |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 106 | friend class base::RefCounted<HttpAuthController>; |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 107 | |
David Benjamin | 5af27d4 | 2018-08-24 17:07:36 | [diff] [blame] | 108 | ~HttpAuthController(); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 109 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 110 | // Searches the auth cache for an entry that encompasses the request's path. |
| 111 | // If such an entry is found, updates |identity_| and |handler_| with the |
| 112 | // cache entry's data and returns true. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 113 | bool SelectPreemptiveAuth(const NetLogWithSource& net_log); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 114 | |
[email protected] | cd0efd2 | 2011-02-24 15:25:55 | [diff] [blame] | 115 | // Invalidates the current handler. If |action| is |
| 116 | // INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS, then also invalidate |
| 117 | // the cached credentials used by the handler. |
[email protected] | 463f835 | 2011-02-18 14:26:55 | [diff] [blame] | 118 | void InvalidateCurrentHandler(InvalidateHandlerAction action); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 119 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 120 | // Invalidates any auth cache entries after authentication has failed. |
| 121 | // The identity that was rejected is |identity_|. |
| 122 | void InvalidateRejectedAuthFromCache(); |
| 123 | |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 124 | // Allows reusing last used identity source. If the authentication handshake |
| 125 | // breaks down halfway, then the controller needs to restart it from the |
| 126 | // beginning and resue the same identity. |
| 127 | void PrepareIdentityForReuse(); |
| 128 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 129 | // Sets |identity_| to the next identity that the transaction should try. It |
| 130 | // chooses candidates by searching the auth cache and the URL for a |
| 131 | // username:password. Returns true if an identity was found. |
| 132 | bool SelectNextAuthIdentityToTry(); |
| 133 | |
| 134 | // Populates auth_info_ with the challenge information, so that |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 135 | // URLRequestHttpJob can prompt for credentials. |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 136 | void PopulateAuthChallenge(); |
| 137 | |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 138 | // Handle the result of calling GenerateAuthToken on an HttpAuthHandler. The |
| 139 | // return value of this function should be used as the return value of the |
| 140 | // GenerateAuthToken operation. |
| 141 | int HandleGenerateTokenResult(int result); |
[email protected] | 76b0f68 | 2011-03-30 16:54:54 | [diff] [blame] | 142 | |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 143 | void OnGenerateAuthTokenDone(int result); |
[email protected] | cee6312 | 2010-07-20 04:43:31 | [diff] [blame] | 144 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 145 | // Indicates if this handler is for Proxy auth or Server auth. |
| 146 | HttpAuth::Target target_; |
| 147 | |
| 148 | // Holds the {scheme, host, path, port} for the authentication target. |
| 149 | const GURL auth_url_; |
| 150 | |
| 151 | // Holds the {scheme, host, port} for the authentication target. |
| 152 | const GURL auth_origin_; |
| 153 | |
| 154 | // The absolute path of the resource needing authentication. |
| 155 | // For proxy authentication the path is empty. |
| 156 | const std::string auth_path_; |
| 157 | |
| 158 | // |handler_| encapsulates the logic for the particular auth-scheme. |
| 159 | // This includes the challenge's parameters. If NULL, then there is no |
| 160 | // associated auth handler. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 161 | std::unique_ptr<HttpAuthHandler> handler_; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 162 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 163 | // |identity_| holds the credentials that should be used by |
| 164 | // the handler_ to generate challenge responses. This identity can come from |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 165 | // a number of places (url, cache, prompt). |
| 166 | HttpAuth::Identity identity_; |
| 167 | |
| 168 | // |auth_token_| contains the opaque string to pass to the proxy or |
| 169 | // server to authenticate the client. |
| 170 | std::string auth_token_; |
| 171 | |
| 172 | // Contains information about the auth challenge. |
| 173 | scoped_refptr<AuthChallengeInfo> auth_info_; |
| 174 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 175 | // True if we've used the username:password embedded in the URL. This |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 176 | // makes sure we use the embedded identity only once for the transaction, |
| 177 | // preventing an infinite auth restart loop. |
| 178 | bool embedded_identity_used_; |
| 179 | |
| 180 | // True if default credentials have already been tried for this transaction |
| 181 | // in response to an HTTP authentication challenge. |
| 182 | bool default_credentials_used_; |
| 183 | |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 184 | // These two are owned by the HttpNetworkSession/IOThread, which own the |
| 185 | // objects which reference |this|. Therefore, these raw pointers are valid |
| 186 | // for the lifetime of this object. |
| 187 | HttpAuthCache* const http_auth_cache_; |
| 188 | HttpAuthHandlerFactory* const http_auth_handler_factory_; |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame^] | 189 | HostResolver* const host_resolver_; |
[email protected] | cee6312 | 2010-07-20 04:43:31 | [diff] [blame] | 190 | |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 191 | std::set<HttpAuth::Scheme> disabled_schemes_; |
[email protected] | cee6312 | 2010-07-20 04:43:31 | [diff] [blame] | 192 | |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 193 | CompletionOnceCallback callback_; |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 194 | |
| 195 | THREAD_CHECKER(thread_checker_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace net |
| 199 | |
| 200 | #endif // NET_HTTP_HTTP_AUTH_CONTROLLER_H_ |