OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_auth.h" | 5 #include "net/http/http_auth.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/http/http_auth_handler_basic.h" | 12 #include "net/http/http_auth_handler_basic.h" |
13 #include "net/http/http_auth_handler_digest.h" | 13 #include "net/http/http_auth_handler_digest.h" |
14 #include "net/http/http_auth_handler_negotiate.h" | 14 #include "net/http/http_auth_handler_negotiate.h" |
15 #include "net/http/http_auth_handler_ntlm.h" | 15 #include "net/http/http_auth_handler_ntlm.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 HttpAuth::Identity::Identity() : source(IDENT_SRC_NONE), invalid(true) {} | 21 HttpAuth::Identity::Identity() : source(IDENT_SRC_NONE), invalid(true) {} |
22 | 22 |
23 // static | 23 // static |
24 void HttpAuth::ChooseBestChallenge( | 24 void HttpAuth::ChooseBestChallenge( |
25 HttpAuthHandlerFactory* http_auth_handler_factory, | 25 HttpAuthHandlerFactory* http_auth_handler_factory, |
26 const HttpResponseHeaders* headers, | 26 const HttpResponseHeaders* headers, |
27 Target target, | 27 Target target, |
28 const GURL& origin, | 28 const GURL& origin, |
29 const std::set<std::string>& disabled_schemes, | 29 const std::set<Scheme>& disabled_schemes, |
30 const BoundNetLog& net_log, | 30 const BoundNetLog& net_log, |
31 scoped_ptr<HttpAuthHandler>* handler) { | 31 scoped_ptr<HttpAuthHandler>* handler) { |
32 DCHECK(http_auth_handler_factory); | 32 DCHECK(http_auth_handler_factory); |
33 DCHECK(handler->get() == NULL); | 33 DCHECK(handler->get() == NULL); |
34 | 34 |
35 // Choose the challenge whose authentication handler gives the maximum score. | 35 // Choose the challenge whose authentication handler gives the maximum score. |
36 scoped_ptr<HttpAuthHandler> best; | 36 scoped_ptr<HttpAuthHandler> best; |
37 const std::string header_name = GetChallengeHeaderName(target); | 37 const std::string header_name = GetChallengeHeaderName(target); |
38 std::string cur_challenge; | 38 std::string cur_challenge; |
39 void* iter = NULL; | 39 void* iter = NULL; |
40 while (headers->EnumerateHeader(&iter, header_name, &cur_challenge)) { | 40 while (headers->EnumerateHeader(&iter, header_name, &cur_challenge)) { |
41 scoped_ptr<HttpAuthHandler> cur; | 41 scoped_ptr<HttpAuthHandler> cur; |
42 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 42 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
43 cur_challenge, target, origin, net_log, &cur); | 43 cur_challenge, target, origin, net_log, &cur); |
44 if (rv != OK) { | 44 if (rv != OK) { |
45 VLOG(1) << "Unable to create AuthHandler. Status: " | 45 VLOG(1) << "Unable to create AuthHandler. Status: " |
46 << ErrorToString(rv) << " Challenge: " << cur_challenge; | 46 << ErrorToString(rv) << " Challenge: " << cur_challenge; |
47 continue; | 47 continue; |
48 } | 48 } |
49 if (cur.get() && (!best.get() || best->score() < cur->score()) && | 49 if (cur.get() && (!best.get() || best->score() < cur->score()) && |
50 (disabled_schemes.find(cur->scheme()) == disabled_schemes.end())) | 50 (disabled_schemes.find(cur->auth_scheme()) == disabled_schemes.end())) |
51 best.swap(cur); | 51 best.swap(cur); |
52 } | 52 } |
53 handler->swap(best); | 53 handler->swap(best); |
54 } | 54 } |
55 | 55 |
56 // static | 56 // static |
57 HttpAuth::AuthorizationResult HttpAuth::HandleChallengeResponse( | 57 HttpAuth::AuthorizationResult HttpAuth::HandleChallengeResponse( |
58 HttpAuthHandler* handler, | 58 HttpAuthHandler* handler, |
59 const HttpResponseHeaders* headers, | 59 const HttpResponseHeaders* headers, |
60 Target target, | 60 Target target, |
61 const std::set<std::string>& disabled_schemes, | 61 const std::set<Scheme>& disabled_schemes, |
62 std::string* challenge_used) { | 62 std::string* challenge_used) { |
63 DCHECK(handler); | 63 DCHECK(handler); |
64 DCHECK(headers); | 64 DCHECK(headers); |
65 DCHECK(challenge_used); | 65 DCHECK(challenge_used); |
66 challenge_used->clear(); | 66 challenge_used->clear(); |
67 const std::string& current_scheme = handler->scheme(); | 67 HttpAuth::Scheme current_scheme = handler->auth_scheme(); |
68 if (disabled_schemes.find(current_scheme) != disabled_schemes.end()) | 68 if (disabled_schemes.find(current_scheme) != disabled_schemes.end()) |
69 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 69 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
70 std::string current_scheme_name = GetSchemeName(current_scheme); | |
70 const std::string header_name = GetChallengeHeaderName(target); | 71 const std::string header_name = GetChallengeHeaderName(target); |
71 void* iter = NULL; | 72 void* iter = NULL; |
72 std::string challenge; | 73 std::string challenge; |
73 HttpAuth::AuthorizationResult authorization_result = | 74 HttpAuth::AuthorizationResult authorization_result = |
74 HttpAuth::AUTHORIZATION_RESULT_INVALID; | 75 HttpAuth::AUTHORIZATION_RESULT_INVALID; |
75 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { | 76 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { |
76 HttpAuth::ChallengeTokenizer props(challenge.begin(), challenge.end()); | 77 HttpAuth::ChallengeTokenizer props(challenge.begin(), challenge.end()); |
77 if (!LowerCaseEqualsASCII(props.scheme(), current_scheme.c_str())) | 78 if (!LowerCaseEqualsASCII(props.scheme(), current_scheme_name.c_str())) |
78 continue; | 79 continue; |
79 authorization_result = handler->HandleAnotherChallenge(&props); | 80 authorization_result = handler->HandleAnotherChallenge(&props); |
80 if (authorization_result != HttpAuth::AUTHORIZATION_RESULT_INVALID) { | 81 if (authorization_result != HttpAuth::AUTHORIZATION_RESULT_INVALID) { |
81 *challenge_used = challenge; | 82 *challenge_used = challenge; |
82 return authorization_result; | 83 return authorization_result; |
83 } | 84 } |
84 } | 85 } |
85 // Finding no matches is equivalent to rejection. | 86 // Finding no matches is equivalent to rejection. |
86 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 87 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
87 } | 88 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 return "Proxy-Authorization"; | 145 return "Proxy-Authorization"; |
145 case AUTH_SERVER: | 146 case AUTH_SERVER: |
146 return "Authorization"; | 147 return "Authorization"; |
147 default: | 148 default: |
148 NOTREACHED(); | 149 NOTREACHED(); |
149 return ""; | 150 return ""; |
150 } | 151 } |
151 } | 152 } |
152 | 153 |
153 // static | 154 // static |
154 std::string HttpAuth::GetAuthTargetString( | 155 std::string HttpAuth::GetAuthTargetString(Target target) { |
155 HttpAuth::Target target) { | 156 switch (target) { |
156 return target == HttpAuth::AUTH_PROXY ? "proxy" : "server"; | 157 case AUTH_PROXY: |
158 return "proxy"; | |
159 case AUTH_SERVER: | |
160 return "server"; | |
161 default: | |
162 NOTREACHED(); | |
163 return ""; | |
164 } | |
165 } | |
166 | |
167 // static | |
168 std::string HttpAuth::GetSchemeName(Scheme scheme) { | |
169 static const char* const kSchemeNames[] = { | |
170 "basic", | |
171 "digest", | |
172 "ntlm", | |
173 "negotiate", | |
174 "mock", | |
175 }; | |
176 COMPILE_ASSERT(arraysize(kSchemeNames) == AUTH_SCHEME_MAX, | |
177 http_auth_scheme_names_incorrect_size); | |
178 if (scheme < AUTH_SCHEME_BASIC || scheme >= AUTH_SCHEME_MAX) | |
179 return "invalid_scheme"; | |
eroman
2011/01/10 19:32:39
Can you add a NOTREACHED here?
cbentzel
2011/01/11 16:54:18
Done.
| |
180 return kSchemeNames[scheme]; | |
157 } | 181 } |
158 | 182 |
159 } // namespace net | 183 } // namespace net |
OLD | NEW |