1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_AUTH_H_
6 #define NET_HTTP_HTTP_AUTH_H_
12 #include "base/scoped_ptr.h"
13 #include "base/string16.h"
14 #include "net/http/http_util.h"
16 template <class T
> class scoped_refptr
;
21 class HttpAuthHandler
;
22 class HttpAuthHandlerFactory
;
23 class HttpResponseHeaders
;
25 // Utility class for http authentication.
28 // Http authentication can be done the the proxy server, origin server,
29 // or both. This enum tracks who the target is.
32 // We depend on the valid targets (!= AUTH_NONE) being usable as indexes
33 // in an array, so start from 0.
39 // What the HTTP WWW-Authenticate/Proxy-Authenticate headers indicate about
40 // the previous authorization attempt.
41 enum AuthorizationResult
{
42 AUTHORIZATION_RESULT_ACCEPT
, // The authorization attempt was accepted,
43 // although there still may be additional
44 // rounds of challenges.
46 AUTHORIZATION_RESULT_REJECT
, // The authorization attempt was rejected.
48 AUTHORIZATION_RESULT_STALE
, // (Digest) The nonce used in the
49 // authorization attempt is stale, but
50 // otherwise the attempt was valid.
52 AUTHORIZATION_RESULT_INVALID
, // The authentication challenge headers are
53 // poorly formed (the authorization attempt
54 // itself may have been fine).
57 // Describes where the identity used for authentication came from.
59 // Came from nowhere -- the identity is not initialized.
62 // The identity came from the auth cache, by doing a path-based
63 // lookup (premptive authorization).
64 IDENT_SRC_PATH_LOOKUP
,
66 // The identity was extracted from a URL of the form:
67 // http://<username>:<password>@host:port
70 // The identity was retrieved from the auth cache, by doing a
72 IDENT_SRC_REALM_LOOKUP
,
74 // The identity was provided by RestartWithAuth -- it likely
75 // came from a prompt (or maybe the password manager).
78 // The identity used the default credentials for the computer,
79 // on schemes that support single sign-on.
80 IDENT_SRC_DEFAULT_CREDENTIALS
,
83 // Helper structure used by HttpNetworkTransaction to track
84 // the current identity being used for authorization.
88 IdentitySource source
;
94 // Get the name of the header containing the auth challenge
95 // (either WWW-Authenticate or Proxy-Authenticate).
96 static std::string
GetChallengeHeaderName(Target target
);
98 // Get the name of the header where the credentials go
99 // (either Authorization or Proxy-Authorization).
100 static std::string
GetAuthorizationHeaderName(Target target
);
102 // Returns a string representation of a Target value that can be used in log
104 static std::string
GetAuthTargetString(Target target
);
106 // Iterate through the challenge headers, and pick the best one that
107 // we support. Obtains the implementation class for handling the challenge,
108 // and passes it back in |*handler|. If no supported challenge was found,
109 // |*handler| is set to NULL.
111 // |disabled_schemes| is the set of schemes that we should not use.
113 // |origin| is used by the NTLM and Negotiation authentication scheme to
114 // construct the service principal name. It is ignored by other schemes.
115 static void ChooseBestChallenge(
116 HttpAuthHandlerFactory
* http_auth_handler_factory
,
117 const HttpResponseHeaders
* headers
,
120 const std::set
<std::string
>& disabled_schemes
,
121 const BoundNetLog
& net_log
,
122 scoped_ptr
<HttpAuthHandler
>* handler
);
124 // Handle a 401/407 response from a server/proxy after a previous
125 // authentication attempt. For connection-based authentication schemes, the
126 // new response may be another round in a multi-round authentication sequence.
127 // For request-based schemes, a 401/407 response is typically treated like a
128 // rejection of the previous challenge, except in the Digest case when a
129 // "stale" attribute is present.
131 // |handler| must be non-NULL, and is the HttpAuthHandler from the previous
132 // authentication round.
134 // |headers| must be non-NULL and contain the new HTTP response.
136 // |target| specifies whether the authentication challenge response came
137 // from a server or a proxy.
139 // |disabled_schemes| are the authentication schemes to ignore.
141 // |challenge_used| is the text of the authentication challenge used in
142 // support of the returned AuthorizationResult. If no headers were used for
143 // the result (for example, all headers have unknown authentication schemes),
144 // the value is cleared.
145 static AuthorizationResult
HandleChallengeResponse(
146 HttpAuthHandler
* handler
,
147 const HttpResponseHeaders
* headers
,
149 const std::set
<std::string
>& disabled_schemes
,
150 std::string
* challenge_used
);
152 // Breaks up a challenge string into the the auth scheme and parameter list,
153 // according to RFC 2617 Sec 1.2:
154 // challenge = auth-scheme 1*SP 1#auth-param
156 // Depending on the challenge scheme, it may be appropriate to interpret the
157 // parameters as either a base-64 encoded string or a comma-delimited list
158 // of name-value pairs. param_pairs() and base64_param() methods are provided
159 // to support either usage.
160 class ChallengeTokenizer
{
162 ChallengeTokenizer(std::string::const_iterator begin
,
163 std::string::const_iterator end
)
166 scheme_begin_(begin
),
173 // Get the original text.
174 std::string
challenge_text() const {
175 return std::string(begin_
, end_
);
178 // Get the auth scheme of the challenge.
179 std::string::const_iterator
scheme_begin() const { return scheme_begin_
; }
180 std::string::const_iterator
scheme_end() const { return scheme_end_
; }
181 std::string
scheme() const {
182 return std::string(scheme_begin_
, scheme_end_
);
185 HttpUtil::NameValuePairsIterator
param_pairs() const;
186 std::string
base64_param() const;
189 void Init(std::string::const_iterator begin
,
190 std::string::const_iterator end
);
192 std::string::const_iterator begin_
;
193 std::string::const_iterator end_
;
195 std::string::const_iterator scheme_begin_
;
196 std::string::const_iterator scheme_end_
;
198 std::string::const_iterator params_begin_
;
199 std::string::const_iterator params_end_
;
205 #endif // NET_HTTP_HTTP_AUTH_H_