xref: /aosp_15_r20/external/cronet/net/http/http_auth_controller.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_HTTP_HTTP_AUTH_CONTROLLER_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_HTTP_HTTP_AUTH_CONTROLLER_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <memory>
9*6777b538SAndroid Build Coastguard Worker #include <optional>
10*6777b538SAndroid Build Coastguard Worker #include <set>
11*6777b538SAndroid Build Coastguard Worker #include <string>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
16*6777b538SAndroid Build Coastguard Worker #include "net/base/completion_once_callback.h"
17*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/network_anonymization_key.h"
19*6777b538SAndroid Build Coastguard Worker #include "net/http/http_auth.h"
20*6777b538SAndroid Build Coastguard Worker #include "net/http/http_auth_preferences.h"
21*6777b538SAndroid Build Coastguard Worker #include "net/log/net_log_with_source.h"
22*6777b538SAndroid Build Coastguard Worker #include "url/gurl.h"
23*6777b538SAndroid Build Coastguard Worker #include "url/scheme_host_port.h"
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker namespace net {
26*6777b538SAndroid Build Coastguard Worker 
27*6777b538SAndroid Build Coastguard Worker class AuthChallengeInfo;
28*6777b538SAndroid Build Coastguard Worker class AuthCredentials;
29*6777b538SAndroid Build Coastguard Worker class HttpAuthHandler;
30*6777b538SAndroid Build Coastguard Worker class HttpAuthHandlerFactory;
31*6777b538SAndroid Build Coastguard Worker class HttpAuthCache;
32*6777b538SAndroid Build Coastguard Worker class HttpRequestHeaders;
33*6777b538SAndroid Build Coastguard Worker class HostResolver;
34*6777b538SAndroid Build Coastguard Worker class NetLogWithSource;
35*6777b538SAndroid Build Coastguard Worker struct HttpRequestInfo;
36*6777b538SAndroid Build Coastguard Worker class SSLInfo;
37*6777b538SAndroid Build Coastguard Worker 
38*6777b538SAndroid Build Coastguard Worker // HttpAuthController is the main entry point for external callers into the HTTP
39*6777b538SAndroid Build Coastguard Worker // authentication stack. A single instance of an HttpAuthController can be used
40*6777b538SAndroid Build Coastguard Worker // to handle authentication to a single "target", where "target" is a HTTP
41*6777b538SAndroid Build Coastguard Worker // server or a proxy. During its lifetime, the HttpAuthController can make use
42*6777b538SAndroid Build Coastguard Worker // of multiple authentication handlers (implemented as HttpAuthHandler
43*6777b538SAndroid Build Coastguard Worker // subclasses), and respond to multiple challenges.
44*6777b538SAndroid Build Coastguard Worker //
45*6777b538SAndroid Build Coastguard Worker // Individual HTTP authentication schemes can have additional requirements other
46*6777b538SAndroid Build Coastguard Worker // than what's prescribed in RFC 7235. See HandleAuthChallenge() for details.
47*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE HttpAuthController
48*6777b538SAndroid Build Coastguard Worker     : public base::RefCounted<HttpAuthController> {
49*6777b538SAndroid Build Coastguard Worker  public:
50*6777b538SAndroid Build Coastguard Worker   // Construct a new HttpAuthController.
51*6777b538SAndroid Build Coastguard Worker   //
52*6777b538SAndroid Build Coastguard Worker   // * |target| is either PROXY or SERVER and determines the authentication
53*6777b538SAndroid Build Coastguard Worker   //       headers to use ("WWW-Authenticate"/"Authorization" vs.
54*6777b538SAndroid Build Coastguard Worker   //       "Proxy-Authenticate"/"Proxy-Authorization") and how ambient
55*6777b538SAndroid Build Coastguard Worker   //       credentials are used.
56*6777b538SAndroid Build Coastguard Worker   //
57*6777b538SAndroid Build Coastguard Worker   // * |auth_url| specifies the target URL. The origin of the URL identifies the
58*6777b538SAndroid Build Coastguard Worker   //       target host. The path (hierarchical part defined in RFC 3986 section
59*6777b538SAndroid Build Coastguard Worker   //       3.3) of the URL is used by HTTP basic authentication to determine
60*6777b538SAndroid Build Coastguard Worker   //       cached credentials can be used to preemptively send an authorization
61*6777b538SAndroid Build Coastguard Worker   //       header. See RFC 7617 section 2.2 (Reusing Credentials) for details.
62*6777b538SAndroid Build Coastguard Worker   //       If |target| is PROXY, then |auth_url| should have no hierarchical
63*6777b538SAndroid Build Coastguard Worker   //       part since that is meaningless.
64*6777b538SAndroid Build Coastguard Worker   //
65*6777b538SAndroid Build Coastguard Worker   // * |network_anonymization_key| specifies the NetworkAnonymizationKey
66*6777b538SAndroid Build Coastguard Worker   //       associated with the resource load. Depending on settings, credentials
67*6777b538SAndroid Build Coastguard Worker   //       may be scoped to a single NetworkAnonymizationKey.
68*6777b538SAndroid Build Coastguard Worker   //
69*6777b538SAndroid Build Coastguard Worker   // * |http_auth_cache| specifies the credentials cache to use. During
70*6777b538SAndroid Build Coastguard Worker   //       authentication if explicit (user-provided) credentials are used and
71*6777b538SAndroid Build Coastguard Worker   //       they can be cached to respond to authentication challenges in the
72*6777b538SAndroid Build Coastguard Worker   //       future, they are stored in the cache. In addition, the HTTP Digest
73*6777b538SAndroid Build Coastguard Worker   //       authentication is stateful across requests. So the |http_auth_cache|
74*6777b538SAndroid Build Coastguard Worker   //       is also used to maintain state for this authentication scheme.
75*6777b538SAndroid Build Coastguard Worker   //
76*6777b538SAndroid Build Coastguard Worker   // * |http_auth_handler_factory| is used to construct instances of
77*6777b538SAndroid Build Coastguard Worker   //       HttpAuthHandler subclasses to handle scheme-specific authentication
78*6777b538SAndroid Build Coastguard Worker   //       logic. The |http_auth_handler_factory| is also responsible for
79*6777b538SAndroid Build Coastguard Worker   //       determining whether the authentication stack should use a specific
80*6777b538SAndroid Build Coastguard Worker   //       authentication scheme or not.
81*6777b538SAndroid Build Coastguard Worker   //
82*6777b538SAndroid Build Coastguard Worker   // * |host_resolver| is used for determining the canonical hostname given a
83*6777b538SAndroid Build Coastguard Worker   //       possibly non-canonical host name. Name canonicalization is used for
84*6777b538SAndroid Build Coastguard Worker   //       NTLM and Negotiate HTTP authentication schemes.
85*6777b538SAndroid Build Coastguard Worker   //
86*6777b538SAndroid Build Coastguard Worker   // * |allow_default_credentials| is used for determining if the current
87*6777b538SAndroid Build Coastguard Worker   //       context allows ambient authentication using default credentials.
88*6777b538SAndroid Build Coastguard Worker   HttpAuthController(HttpAuth::Target target,
89*6777b538SAndroid Build Coastguard Worker                      const GURL& auth_url,
90*6777b538SAndroid Build Coastguard Worker                      const NetworkAnonymizationKey& network_anonymization_key,
91*6777b538SAndroid Build Coastguard Worker                      HttpAuthCache* http_auth_cache,
92*6777b538SAndroid Build Coastguard Worker                      HttpAuthHandlerFactory* http_auth_handler_factory,
93*6777b538SAndroid Build Coastguard Worker                      HostResolver* host_resolver);
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker   // Generate an authentication token for |target| if necessary. The return
96*6777b538SAndroid Build Coastguard Worker   // value is a net error code. |OK| will be returned both in the case that
97*6777b538SAndroid Build Coastguard Worker   // a token is correctly generated synchronously, as well as when no tokens
98*6777b538SAndroid Build Coastguard Worker   // were necessary.
99*6777b538SAndroid Build Coastguard Worker   int MaybeGenerateAuthToken(const HttpRequestInfo* request,
100*6777b538SAndroid Build Coastguard Worker                              CompletionOnceCallback callback,
101*6777b538SAndroid Build Coastguard Worker                              const NetLogWithSource& net_log);
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   // Adds either the proxy auth header, or the origin server auth header,
104*6777b538SAndroid Build Coastguard Worker   // as specified by |target_|.
105*6777b538SAndroid Build Coastguard Worker   void AddAuthorizationHeader(HttpRequestHeaders* authorization_headers);
106*6777b538SAndroid Build Coastguard Worker 
107*6777b538SAndroid Build Coastguard Worker   // Checks for and handles HTTP status code 401 or 407.
108*6777b538SAndroid Build Coastguard Worker   // |HandleAuthChallenge()| returns OK on success, or a network error code
109*6777b538SAndroid Build Coastguard Worker   // otherwise. It may also populate |auth_info_|.
110*6777b538SAndroid Build Coastguard Worker   int HandleAuthChallenge(scoped_refptr<HttpResponseHeaders> headers,
111*6777b538SAndroid Build Coastguard Worker                           const SSLInfo& ssl_info,
112*6777b538SAndroid Build Coastguard Worker                           bool do_not_send_server_auth,
113*6777b538SAndroid Build Coastguard Worker                           bool establishing_tunnel,
114*6777b538SAndroid Build Coastguard Worker                           const NetLogWithSource& net_log);
115*6777b538SAndroid Build Coastguard Worker 
116*6777b538SAndroid Build Coastguard Worker   // Store the supplied credentials and prepare to restart the auth.
117*6777b538SAndroid Build Coastguard Worker   void ResetAuth(const AuthCredentials& credentials);
118*6777b538SAndroid Build Coastguard Worker 
119*6777b538SAndroid Build Coastguard Worker   bool HaveAuthHandler() const;
120*6777b538SAndroid Build Coastguard Worker 
121*6777b538SAndroid Build Coastguard Worker   bool HaveAuth() const;
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker   // Return whether the authentication scheme is incompatible with HTTP/2
124*6777b538SAndroid Build Coastguard Worker   // and thus the server would presumably reject a request on HTTP/2 anyway.
125*6777b538SAndroid Build Coastguard Worker   bool NeedsHTTP11() const;
126*6777b538SAndroid Build Coastguard Worker 
127*6777b538SAndroid Build Coastguard Worker   // Swaps the authentication challenge info into |other|.
128*6777b538SAndroid Build Coastguard Worker   void TakeAuthInfo(std::optional<AuthChallengeInfo>* other);
129*6777b538SAndroid Build Coastguard Worker 
130*6777b538SAndroid Build Coastguard Worker   bool IsAuthSchemeDisabled(HttpAuth::Scheme scheme) const;
131*6777b538SAndroid Build Coastguard Worker   void DisableAuthScheme(HttpAuth::Scheme scheme);
132*6777b538SAndroid Build Coastguard Worker   void DisableEmbeddedIdentity();
133*6777b538SAndroid Build Coastguard Worker 
134*6777b538SAndroid Build Coastguard Worker   // Called when the connection has been closed, so the current handler (which
135*6777b538SAndroid Build Coastguard Worker   // contains state bound to the connection) should be dropped. If retrying on a
136*6777b538SAndroid Build Coastguard Worker   // new connection, the next call to MaybeGenerateAuthToken will retry the
137*6777b538SAndroid Build Coastguard Worker   // current auth scheme.
138*6777b538SAndroid Build Coastguard Worker   void OnConnectionClosed();
139*6777b538SAndroid Build Coastguard Worker 
140*6777b538SAndroid Build Coastguard Worker  private:
141*6777b538SAndroid Build Coastguard Worker   // Actions for InvalidateCurrentHandler()
142*6777b538SAndroid Build Coastguard Worker   enum InvalidateHandlerAction {
143*6777b538SAndroid Build Coastguard Worker     INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS,
144*6777b538SAndroid Build Coastguard Worker     INVALIDATE_HANDLER_AND_DISABLE_SCHEME,
145*6777b538SAndroid Build Coastguard Worker     INVALIDATE_HANDLER
146*6777b538SAndroid Build Coastguard Worker   };
147*6777b538SAndroid Build Coastguard Worker 
148*6777b538SAndroid Build Coastguard Worker   // So that we can mock this object.
149*6777b538SAndroid Build Coastguard Worker   friend class base::RefCounted<HttpAuthController>;
150*6777b538SAndroid Build Coastguard Worker 
151*6777b538SAndroid Build Coastguard Worker   ~HttpAuthController();
152*6777b538SAndroid Build Coastguard Worker 
153*6777b538SAndroid Build Coastguard Worker   // If this controller's NetLog hasn't been created yet, creates it and
154*6777b538SAndroid Build Coastguard Worker   // associates it with |caller_net_log|. Does nothing after the first
155*6777b538SAndroid Build Coastguard Worker   // invocation.
156*6777b538SAndroid Build Coastguard Worker   void BindToCallingNetLog(const NetLogWithSource& caller_net_log);
157*6777b538SAndroid Build Coastguard Worker 
158*6777b538SAndroid Build Coastguard Worker   // Searches the auth cache for an entry that encompasses the request's path.
159*6777b538SAndroid Build Coastguard Worker   // If such an entry is found, updates |identity_| and |handler_| with the
160*6777b538SAndroid Build Coastguard Worker   // cache entry's data and returns true.
161*6777b538SAndroid Build Coastguard Worker   bool SelectPreemptiveAuth(const NetLogWithSource& caller_net_log);
162*6777b538SAndroid Build Coastguard Worker 
163*6777b538SAndroid Build Coastguard Worker   // Invalidates the current handler. If |action| is
164*6777b538SAndroid Build Coastguard Worker   // INVALIDATE_HANDLER_AND_CACHED_CREDENTIALS, then also invalidate
165*6777b538SAndroid Build Coastguard Worker   // the cached credentials used by the handler.
166*6777b538SAndroid Build Coastguard Worker   void InvalidateCurrentHandler(InvalidateHandlerAction action);
167*6777b538SAndroid Build Coastguard Worker 
168*6777b538SAndroid Build Coastguard Worker   // Invalidates any auth cache entries after authentication has failed.
169*6777b538SAndroid Build Coastguard Worker   // The identity that was rejected is |identity_|.
170*6777b538SAndroid Build Coastguard Worker   void InvalidateRejectedAuthFromCache();
171*6777b538SAndroid Build Coastguard Worker 
172*6777b538SAndroid Build Coastguard Worker   // Allows reusing last used identity source. If the authentication handshake
173*6777b538SAndroid Build Coastguard Worker   // breaks down halfway, then the controller needs to restart it from the
174*6777b538SAndroid Build Coastguard Worker   // beginning and resue the same identity.
175*6777b538SAndroid Build Coastguard Worker   void PrepareIdentityForReuse();
176*6777b538SAndroid Build Coastguard Worker 
177*6777b538SAndroid Build Coastguard Worker   // Sets |identity_| to the next identity that the transaction should try. It
178*6777b538SAndroid Build Coastguard Worker   // chooses candidates by searching the auth cache and the URL for a
179*6777b538SAndroid Build Coastguard Worker   // username:password. Returns true if an identity was found.
180*6777b538SAndroid Build Coastguard Worker   bool SelectNextAuthIdentityToTry();
181*6777b538SAndroid Build Coastguard Worker 
182*6777b538SAndroid Build Coastguard Worker   // Populates auth_info_ with the challenge information, so that
183*6777b538SAndroid Build Coastguard Worker   // URLRequestHttpJob can prompt for credentials.
184*6777b538SAndroid Build Coastguard Worker   void PopulateAuthChallenge();
185*6777b538SAndroid Build Coastguard Worker 
186*6777b538SAndroid Build Coastguard Worker   // Handle the result of calling GenerateAuthToken on an HttpAuthHandler. The
187*6777b538SAndroid Build Coastguard Worker   // return value of this function should be used as the return value of the
188*6777b538SAndroid Build Coastguard Worker   // GenerateAuthToken operation.
189*6777b538SAndroid Build Coastguard Worker   int HandleGenerateTokenResult(int result);
190*6777b538SAndroid Build Coastguard Worker 
191*6777b538SAndroid Build Coastguard Worker   void OnGenerateAuthTokenDone(int result);
192*6777b538SAndroid Build Coastguard Worker 
193*6777b538SAndroid Build Coastguard Worker   // Indicates if this handler is for Proxy auth or Server auth.
194*6777b538SAndroid Build Coastguard Worker   HttpAuth::Target target_;
195*6777b538SAndroid Build Coastguard Worker 
196*6777b538SAndroid Build Coastguard Worker   // Holds the {scheme, host, port, path} for the authentication target.
197*6777b538SAndroid Build Coastguard Worker   const GURL auth_url_;
198*6777b538SAndroid Build Coastguard Worker 
199*6777b538SAndroid Build Coastguard Worker   // Holds the {scheme, host, port} for the authentication target.
200*6777b538SAndroid Build Coastguard Worker   const url::SchemeHostPort auth_scheme_host_port_;
201*6777b538SAndroid Build Coastguard Worker 
202*6777b538SAndroid Build Coastguard Worker   // The absolute path of the resource needing authentication.
203*6777b538SAndroid Build Coastguard Worker   // For proxy authentication, the path is empty.
204*6777b538SAndroid Build Coastguard Worker   const std::string auth_path_;
205*6777b538SAndroid Build Coastguard Worker 
206*6777b538SAndroid Build Coastguard Worker   // NetworkAnonymizationKey associated with the request.
207*6777b538SAndroid Build Coastguard Worker   const NetworkAnonymizationKey network_anonymization_key_;
208*6777b538SAndroid Build Coastguard Worker 
209*6777b538SAndroid Build Coastguard Worker   // |handler_| encapsulates the logic for the particular auth-scheme.
210*6777b538SAndroid Build Coastguard Worker   // This includes the challenge's parameters. If nullptr, then there is no
211*6777b538SAndroid Build Coastguard Worker   // associated auth handler.
212*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<HttpAuthHandler> handler_;
213*6777b538SAndroid Build Coastguard Worker 
214*6777b538SAndroid Build Coastguard Worker   // |identity_| holds the credentials that should be used by the handler_ to
215*6777b538SAndroid Build Coastguard Worker   // generate challenge responses. This identity can come from a number of
216*6777b538SAndroid Build Coastguard Worker   // places (url, cache, prompt).
217*6777b538SAndroid Build Coastguard Worker   HttpAuth::Identity identity_;
218*6777b538SAndroid Build Coastguard Worker 
219*6777b538SAndroid Build Coastguard Worker   // |auth_token_| contains the opaque string to pass to the proxy or
220*6777b538SAndroid Build Coastguard Worker   // server to authenticate the client.
221*6777b538SAndroid Build Coastguard Worker   std::string auth_token_;
222*6777b538SAndroid Build Coastguard Worker 
223*6777b538SAndroid Build Coastguard Worker   // Contains information about the auth challenge.
224*6777b538SAndroid Build Coastguard Worker   std::optional<AuthChallengeInfo> auth_info_;
225*6777b538SAndroid Build Coastguard Worker 
226*6777b538SAndroid Build Coastguard Worker   // True if we've used the username:password embedded in the URL.  This
227*6777b538SAndroid Build Coastguard Worker   // makes sure we use the embedded identity only once for the transaction,
228*6777b538SAndroid Build Coastguard Worker   // preventing an infinite auth restart loop.
229*6777b538SAndroid Build Coastguard Worker   bool embedded_identity_used_ = false;
230*6777b538SAndroid Build Coastguard Worker 
231*6777b538SAndroid Build Coastguard Worker   // True if default credentials have already been tried for this transaction
232*6777b538SAndroid Build Coastguard Worker   // in response to an HTTP authentication challenge.
233*6777b538SAndroid Build Coastguard Worker   bool default_credentials_used_ = false;
234*6777b538SAndroid Build Coastguard Worker 
235*6777b538SAndroid Build Coastguard Worker   // These two are owned by the HttpNetworkSession/IOThread, which own the
236*6777b538SAndroid Build Coastguard Worker   // objects which reference |this|. Therefore, these raw pointers are valid
237*6777b538SAndroid Build Coastguard Worker   // for the lifetime of this object.
238*6777b538SAndroid Build Coastguard Worker   const raw_ptr<HttpAuthCache> http_auth_cache_;
239*6777b538SAndroid Build Coastguard Worker   const raw_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
240*6777b538SAndroid Build Coastguard Worker   const raw_ptr<HostResolver> host_resolver_;
241*6777b538SAndroid Build Coastguard Worker 
242*6777b538SAndroid Build Coastguard Worker   std::set<HttpAuth::Scheme> disabled_schemes_;
243*6777b538SAndroid Build Coastguard Worker 
244*6777b538SAndroid Build Coastguard Worker   CompletionOnceCallback callback_;
245*6777b538SAndroid Build Coastguard Worker 
246*6777b538SAndroid Build Coastguard Worker   // NetLog to be used for logging in this controller.
247*6777b538SAndroid Build Coastguard Worker   NetLogWithSource net_log_;
248*6777b538SAndroid Build Coastguard Worker 
249*6777b538SAndroid Build Coastguard Worker   THREAD_CHECKER(thread_checker_);
250*6777b538SAndroid Build Coastguard Worker };
251*6777b538SAndroid Build Coastguard Worker 
252*6777b538SAndroid Build Coastguard Worker }  // namespace net
253*6777b538SAndroid Build Coastguard Worker 
254*6777b538SAndroid Build Coastguard Worker #endif  // NET_HTTP_HTTP_AUTH_CONTROLLER_H_
255