blob: 318527af6e513cfb28f9bddd29e5b6d594f7b4ad [file] [log] [blame]
asanka5ffd5d72016-03-23 16:20:491// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]228404f2010-06-24 04:31:412// 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
danakj1fd259a02016-04-16 03:17:098#include <memory>
[email protected]cee63122010-07-20 04:43:319#include <set>
[email protected]228404f2010-06-24 04:31:4110#include <string>
11
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
Emily Starkf2c9bbd2019-04-09 17:08:5813#include "base/optional.h"
gab47aa7da2017-06-02 16:09:4314#include "base/threading/thread_checker.h"
Bence Béky7236fb72018-08-01 14:35:0915#include "net/base/completion_once_callback.h"
[email protected]172da1b2011-08-12 15:52:2616#include "net/base/net_export.h"
Matt Menkebe090422019-10-18 20:25:2617#include "net/base/network_isolation_key.h"
[email protected]228404f2010-06-24 04:31:4118#include "net/http/http_auth.h"
Kevin DiClementef07119082019-08-12 13:31:3419#include "net/http/http_auth_preferences.h"
Asanka Herath20e89092019-05-10 04:33:2020#include "net/log/net_log_with_source.h"
[email protected]f89276a72013-07-12 06:41:5421#include "url/gurl.h"
[email protected]228404f2010-06-24 04:31:4122
23namespace net {
24
25class AuthChallengeInfo;
[email protected]f3cf9802011-10-28 18:44:5826class AuthCredentials;
[email protected]560c0432010-07-13 20:45:3127class HttpAuthHandler;
[email protected]3598c6022010-09-17 23:13:0928class HttpAuthHandlerFactory;
29class HttpAuthCache;
[email protected]228404f2010-06-24 04:31:4130class HttpRequestHeaders;
Eric Orthbe2efac2019-03-06 01:11:1131class HostResolver;
mikecironef22f9812016-10-04 03:40:1932class NetLogWithSource;
[email protected]228404f2010-06-24 04:31:4133struct HttpRequestInfo;
asanka5ffd5d72016-03-23 16:20:4934class SSLInfo;
[email protected]228404f2010-06-24 04:31:4135
Asanka Herathd856bd012019-05-08 03:17:5836// HttpAuthController is the main entry point for external callers into the HTTP
37// authentication stack. A single instance of an HttpAuthController can be used
38// to handle authentication to a single "target", where "target" is a HTTP
39// server or a proxy. During its lifetime, the HttpAuthController can make use
40// of multiple authentication handlers (implemented as HttpAuthHandler
41// subclasses), and respond to multiple challenges.
Matt Menke84586d82017-09-28 19:39:2842//
Asanka Herathd856bd012019-05-08 03:17:5843// Individual HTTP authentication schemes can have additional requirements other
44// than what's prescribed in RFC 7235. See HandleAuthChallenge() for details.
[email protected]172da1b2011-08-12 15:52:2645class NET_EXPORT_PRIVATE HttpAuthController
gab47aa7da2017-06-02 16:09:4346 : public base::RefCounted<HttpAuthController> {
[email protected]228404f2010-06-24 04:31:4147 public:
Asanka Herathd856bd012019-05-08 03:17:5848 // Construct a new HttpAuthController.
49 //
50 // * |target| is either PROXY or SERVER and determines the authentication
51 // headers to use ("WWW-Authenticate"/"Authorization" vs.
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:4352 // "Proxy-Authenticate"/"Proxy-Authorization") and how ambient
Asanka Herathd856bd012019-05-08 03:17:5853 // credentials are used.
54 //
55 // * |auth_url| specifies the target URL. The origin of the URL identifies the
56 // target host. The path (hierarchical part defined in RFC 3986 section
57 // 3.3) of the URL is used by HTTP basic authentication to determine
58 // cached credentials can be used to preemptively send an authorization
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:4359 // header. See RFC 7617 section 2.2 (Reusing Credentials) for details.
Asanka Herathd856bd012019-05-08 03:17:5860 // If |target| is PROXY, then |auth_url| should have no hierarchical
61 // part since that is meaningless.
62 //
Matt Menkebe090422019-10-18 20:25:2663 // * |network_isolation_key| specifies the NetworkIsolationKey associated with
64 // the resource load. Depending on settings, credentials may be scoped
65 // to a single NetworkIsolationKey.
66 //
Asanka Herathd856bd012019-05-08 03:17:5867 // * |http_auth_cache| specifies the credentials cache to use. During
68 // authentication if explicit (user-provided) credentials are used and
69 // they can be cached to respond to authentication challenges in the
70 // future, they are stored in the cache. In addition, the HTTP Digest
71 // authentication is stateful across requests. So the |http_auth_cache|
72 // is also used to maintain state for this authentication scheme.
73 //
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:4374 // * |http_auth_handler_factory| is used to construct instances of
75 // HttpAuthHandler subclasses to handle scheme-specific authentication
Asanka Herathd856bd012019-05-08 03:17:5876 // logic. The |http_auth_handler_factory| is also responsible for
77 // determining whether the authentication stack should use a specific
78 // authentication scheme or not.
79 //
80 // * |host_resolver| is used for determining the canonical hostname given a
81 // possibly non-canonical host name. Name canonicalization is used for
82 // NTLM and Negotiate HTTP authentication schemes.
Kevin DiClementef07119082019-08-12 13:31:3483 //
84 // * |allow_default_credentials| is used for determining if the current
85 // context allows ambient authentication using default credentials.
Rohit Agarwal2653f472019-11-12 19:39:2786 HttpAuthController(HttpAuth::Target target,
87 const GURL& auth_url,
88 const NetworkIsolationKey& network_isolation_key,
89 HttpAuthCache* http_auth_cache,
90 HttpAuthHandlerFactory* http_auth_handler_factory,
91 HostResolver* host_resolver);
[email protected]228404f2010-06-24 04:31:4192
93 // Generate an authentication token for |target| if necessary. The return
94 // value is a net error code. |OK| will be returned both in the case that
95 // a token is correctly generated synchronously, as well as when no tokens
96 // were necessary.
asanka5e8286c32017-02-23 16:13:4297 int MaybeGenerateAuthToken(const HttpRequestInfo* request,
Bence Béky7236fb72018-08-01 14:35:0998 CompletionOnceCallback callback,
asanka5e8286c32017-02-23 16:13:4299 const NetLogWithSource& net_log);
[email protected]228404f2010-06-24 04:31:41100
101 // Adds either the proxy auth header, or the origin server auth header,
102 // as specified by |target_|.
asanka5e8286c32017-02-23 16:13:42103 void AddAuthorizationHeader(HttpRequestHeaders* authorization_headers);
[email protected]228404f2010-06-24 04:31:41104
105 // Checks for and handles HTTP status code 401 or 407.
[email protected]65d34382010-07-01 18:12:26106 // |HandleAuthChallenge()| returns OK on success, or a network error code
107 // otherwise. It may also populate |auth_info_|.
asanka5e8286c32017-02-23 16:13:42108 int HandleAuthChallenge(scoped_refptr<HttpResponseHeaders> headers,
109 const SSLInfo& ssl_info,
110 bool do_not_send_server_auth,
111 bool establishing_tunnel,
112 const NetLogWithSource& net_log);
[email protected]228404f2010-06-24 04:31:41113
[email protected]228404f2010-06-24 04:31:41114 // Store the supplied credentials and prepare to restart the auth.
asanka5e8286c32017-02-23 16:13:42115 void ResetAuth(const AuthCredentials& credentials);
[email protected]228404f2010-06-24 04:31:41116
asanka5e8286c32017-02-23 16:13:42117 bool HaveAuthHandler() const;
[email protected]228404f2010-06-24 04:31:41118
asanka5e8286c32017-02-23 16:13:42119 bool HaveAuth() const;
[email protected]228404f2010-06-24 04:31:41120
Bence Béky3238f2e12017-09-22 22:44:49121 // Return whether the authentication scheme is incompatible with HTTP/2
122 // and thus the server would presumably reject a request on HTTP/2 anyway.
123 bool NeedsHTTP11() const;
124
Emily Starkf2c9bbd2019-04-09 17:08:58125 // Swaps the authentication challenge info into |other|.
126 void TakeAuthInfo(base::Optional<AuthChallengeInfo>* other);
[email protected]228404f2010-06-24 04:31:41127
asanka5e8286c32017-02-23 16:13:42128 bool IsAuthSchemeDisabled(HttpAuth::Scheme scheme) const;
129 void DisableAuthScheme(HttpAuth::Scheme scheme);
130 void DisableEmbeddedIdentity();
[email protected]cee63122010-07-20 04:43:31131
davidben8c7089a2017-04-17 20:38:22132 // Called when the connection has been closed, so the current handler (which
133 // contains state bound to the connection) should be dropped. If retrying on a
134 // new connection, the next call to MaybeGenerateAuthToken will retry the
135 // current auth scheme.
136 void OnConnectionClosed();
137
[email protected]3598c6022010-09-17 23:13:09138 private:
[email protected]463f8352011-02-18 14:26:55139 // Actions for InvalidateCurrentHandler()
140 enum InvalidateHandlerAction {
141 INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS,
[email protected]26d84b02011-08-31 14:07:08142 INVALIDATE_HANDLER_AND_DISABLE_SCHEME,
[email protected]463f8352011-02-18 14:26:55143 INVALIDATE_HANDLER
144 };
145
[email protected]3598c6022010-09-17 23:13:09146 // So that we can mock this object.
[email protected]e772db3f2010-07-12 18:11:13147 friend class base::RefCounted<HttpAuthController>;
[email protected]3598c6022010-09-17 23:13:09148
David Benjamin5af27d42018-08-24 17:07:36149 ~HttpAuthController();
[email protected]e772db3f2010-07-12 18:11:13150
Asanka Herath20e89092019-05-10 04:33:20151 // If this controller's NetLog hasn't been created yet, creates it and
152 // associates it with |caller_net_log|. Does nothing after the first
153 // invocation.
154 void BindToCallingNetLog(const NetLogWithSource& caller_net_log);
155
[email protected]228404f2010-06-24 04:31:41156 // Searches the auth cache for an entry that encompasses the request's path.
157 // If such an entry is found, updates |identity_| and |handler_| with the
158 // cache entry's data and returns true.
Asanka Herath20e89092019-05-10 04:33:20159 bool SelectPreemptiveAuth(const NetLogWithSource& caller_net_log);
[email protected]228404f2010-06-24 04:31:41160
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43161 // Invalidates the current handler. If |action| is
[email protected]cd0efd22011-02-24 15:25:55162 // INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS, then also invalidate
163 // the cached credentials used by the handler.
[email protected]463f8352011-02-18 14:26:55164 void InvalidateCurrentHandler(InvalidateHandlerAction action);
[email protected]eca50e122010-09-11 14:03:30165
[email protected]228404f2010-06-24 04:31:41166 // Invalidates any auth cache entries after authentication has failed.
167 // The identity that was rejected is |identity_|.
168 void InvalidateRejectedAuthFromCache();
169
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43170 // Allows reusing last used identity source. If the authentication handshake
Asanka Herathbc3f8f62018-11-16 23:08:30171 // breaks down halfway, then the controller needs to restart it from the
172 // beginning and resue the same identity.
173 void PrepareIdentityForReuse();
174
[email protected]228404f2010-06-24 04:31:41175 // Sets |identity_| to the next identity that the transaction should try. It
176 // chooses candidates by searching the auth cache and the URL for a
177 // username:password. Returns true if an identity was found.
178 bool SelectNextAuthIdentityToTry();
179
180 // Populates auth_info_ with the challenge information, so that
[email protected]f3cf9802011-10-28 18:44:58181 // URLRequestHttpJob can prompt for credentials.
[email protected]228404f2010-06-24 04:31:41182 void PopulateAuthChallenge();
183
asankae2257db2016-10-11 22:03:16184 // Handle the result of calling GenerateAuthToken on an HttpAuthHandler. The
185 // return value of this function should be used as the return value of the
186 // GenerateAuthToken operation.
187 int HandleGenerateTokenResult(int result);
[email protected]76b0f682011-03-30 16:54:54188
asanka463ca4262016-11-16 02:34:31189 void OnGenerateAuthTokenDone(int result);
[email protected]cee63122010-07-20 04:43:31190
[email protected]228404f2010-06-24 04:31:41191 // Indicates if this handler is for Proxy auth or Server auth.
192 HttpAuth::Target target_;
193
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43194 // Holds the {scheme, host, port, path} for the authentication target.
[email protected]228404f2010-06-24 04:31:41195 const GURL auth_url_;
196
197 // Holds the {scheme, host, port} for the authentication target.
198 const GURL auth_origin_;
199
200 // The absolute path of the resource needing authentication.
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43201 // For proxy authentication, the path is empty.
[email protected]228404f2010-06-24 04:31:41202 const std::string auth_path_;
203
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43204 // NetworkIsolationKey associated with the request.
Matt Menkebe090422019-10-18 20:25:26205 const NetworkIsolationKey network_isolation_key_;
206
[email protected]228404f2010-06-24 04:31:41207 // |handler_| encapsulates the logic for the particular auth-scheme.
Asanka Herath26b1de82019-05-23 04:03:04208 // This includes the challenge's parameters. If nullptr, then there is no
[email protected]228404f2010-06-24 04:31:41209 // associated auth handler.
danakj1fd259a02016-04-16 03:17:09210 std::unique_ptr<HttpAuthHandler> handler_;
[email protected]228404f2010-06-24 04:31:41211
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43212 // |identity_| holds the credentials that should be used by the handler_ to
213 // generate challenge responses. This identity can come from a number of
214 // places (url, cache, prompt).
[email protected]228404f2010-06-24 04:31:41215 HttpAuth::Identity identity_;
216
217 // |auth_token_| contains the opaque string to pass to the proxy or
218 // server to authenticate the client.
219 std::string auth_token_;
220
221 // Contains information about the auth challenge.
Emily Starkf2c9bbd2019-04-09 17:08:58222 base::Optional<AuthChallengeInfo> auth_info_;
[email protected]228404f2010-06-24 04:31:41223
[email protected]f3cf9802011-10-28 18:44:58224 // True if we've used the username:password embedded in the URL. This
[email protected]228404f2010-06-24 04:31:41225 // makes sure we use the embedded identity only once for the transaction,
226 // preventing an infinite auth restart loop.
227 bool embedded_identity_used_;
228
229 // True if default credentials have already been tried for this transaction
230 // in response to an HTTP authentication challenge.
231 bool default_credentials_used_;
232
[email protected]3598c6022010-09-17 23:13:09233 // These two are owned by the HttpNetworkSession/IOThread, which own the
Eric Lawrence [MSFT]855f0969e2020-11-11 07:03:43234 // objects which reference |this|. Therefore, these raw pointers are valid
[email protected]3598c6022010-09-17 23:13:09235 // for the lifetime of this object.
236 HttpAuthCache* const http_auth_cache_;
237 HttpAuthHandlerFactory* const http_auth_handler_factory_;
Eric Orthbe2efac2019-03-06 01:11:11238 HostResolver* const host_resolver_;
[email protected]cee63122010-07-20 04:43:31239
[email protected]547fc792011-01-13 13:31:17240 std::set<HttpAuth::Scheme> disabled_schemes_;
[email protected]cee63122010-07-20 04:43:31241
Bence Béky7236fb72018-08-01 14:35:09242 CompletionOnceCallback callback_;
gab47aa7da2017-06-02 16:09:43243
Asanka Herath20e89092019-05-10 04:33:20244 // NetLog to be used for logging in this controller.
245 NetLogWithSource net_log_;
246
gab47aa7da2017-06-02 16:09:43247 THREAD_CHECKER(thread_checker_);
[email protected]228404f2010-06-24 04:31:41248};
249
250} // namespace net
251
252#endif // NET_HTTP_HTTP_AUTH_CONTROLLER_H_