[email protected] | 87c99b6a | 2011-05-13 20:06:48 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [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 | #include "net/http/http_auth.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 9 | #include "base/strings/string_tokenizer.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 11 | #include "net/base/net_errors.h" |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 12 | #include "net/http/http_auth_challenge_tokenizer.h" |
[email protected] | 40ecae22 | 2011-08-24 20:59:56 | [diff] [blame] | 13 | #include "net/http/http_auth_handler.h" |
| 14 | #include "net/http/http_auth_handler_factory.h" |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 15 | #include "net/http/http_auth_scheme.h" |
[email protected] | 87c99b6a | 2011-05-13 20:06:48 | [diff] [blame] | 16 | #include "net/http/http_request_headers.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 17 | #include "net/http/http_response_headers.h" |
| 18 | #include "net/http/http_util.h" |
| 19 | |
| 20 | namespace net { |
| 21 | |
[email protected] | 2858bbf | 2010-10-05 23:46:02 | [diff] [blame] | 22 | HttpAuth::Identity::Identity() : source(IDENT_SRC_NONE), invalid(true) {} |
| 23 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 24 | // static |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 25 | void HttpAuth::ChooseBestChallenge( |
| 26 | HttpAuthHandlerFactory* http_auth_handler_factory, |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 27 | const HttpResponseHeaders& response_headers, |
| 28 | const SSLInfo& ssl_info, |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 29 | Target target, |
| 30 | const GURL& origin, |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 31 | const std::set<Scheme>& disabled_schemes, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 32 | const BoundNetLog& net_log, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 33 | std::unique_ptr<HttpAuthHandler>* handler) { |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 34 | DCHECK(http_auth_handler_factory); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 35 | DCHECK(handler->get() == NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 36 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 37 | // Choose the challenge whose authentication handler gives the maximum score. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 38 | std::unique_ptr<HttpAuthHandler> best; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | const std::string header_name = GetChallengeHeaderName(target); |
| 40 | std::string cur_challenge; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 41 | size_t iter = 0; |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 42 | while (response_headers.EnumerateHeader(&iter, header_name, &cur_challenge)) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 43 | std::unique_ptr<HttpAuthHandler> cur; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 44 | int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 45 | cur_challenge, target, ssl_info, origin, net_log, &cur); |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 46 | if (rv != OK) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 47 | VLOG(1) << "Unable to create AuthHandler. Status: " |
| 48 | << ErrorToString(rv) << " Challenge: " << cur_challenge; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 49 | continue; |
| 50 | } |
[email protected] | 17be1a6 | 2010-07-27 19:20:23 | [diff] [blame] | 51 | if (cur.get() && (!best.get() || best->score() < cur->score()) && |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 52 | (disabled_schemes.find(cur->auth_scheme()) == disabled_schemes.end())) |
[email protected] | 17be1a6 | 2010-07-27 19:20:23 | [diff] [blame] | 53 | best.swap(cur); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 54 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 55 | handler->swap(best); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 58 | // static |
| 59 | HttpAuth::AuthorizationResult HttpAuth::HandleChallengeResponse( |
| 60 | HttpAuthHandler* handler, |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 61 | const HttpResponseHeaders& response_headers, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 62 | Target target, |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 63 | const std::set<Scheme>& disabled_schemes, |
[email protected] | c4857e0 | 2010-09-16 13:41:36 | [diff] [blame] | 64 | std::string* challenge_used) { |
[email protected] | d66f881 | 2010-10-20 16:52:15 | [diff] [blame] | 65 | DCHECK(handler); |
[email protected] | c4857e0 | 2010-09-16 13:41:36 | [diff] [blame] | 66 | DCHECK(challenge_used); |
[email protected] | d66f881 | 2010-10-20 16:52:15 | [diff] [blame] | 67 | challenge_used->clear(); |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 68 | HttpAuth::Scheme current_scheme = handler->auth_scheme(); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 69 | if (disabled_schemes.find(current_scheme) != disabled_schemes.end()) |
| 70 | return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 71 | std::string current_scheme_name = SchemeToString(current_scheme); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 72 | const std::string header_name = GetChallengeHeaderName(target); |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 73 | size_t iter = 0; |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 74 | std::string challenge; |
| 75 | HttpAuth::AuthorizationResult authorization_result = |
| 76 | HttpAuth::AUTHORIZATION_RESULT_INVALID; |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 77 | while (response_headers.EnumerateHeader(&iter, header_name, &challenge)) { |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 78 | HttpAuthChallengeTokenizer props(challenge.begin(), challenge.end()); |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 79 | if (!base::LowerCaseEqualsASCII(props.scheme(), |
| 80 | current_scheme_name.c_str())) |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 81 | continue; |
| 82 | authorization_result = handler->HandleAnotherChallenge(&props); |
[email protected] | c4857e0 | 2010-09-16 13:41:36 | [diff] [blame] | 83 | if (authorization_result != HttpAuth::AUTHORIZATION_RESULT_INVALID) { |
| 84 | *challenge_used = challenge; |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 85 | return authorization_result; |
[email protected] | c4857e0 | 2010-09-16 13:41:36 | [diff] [blame] | 86 | } |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 87 | } |
[email protected] | c4857e0 | 2010-09-16 13:41:36 | [diff] [blame] | 88 | // Finding no matches is equivalent to rejection. |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 89 | return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 90 | } |
| 91 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 92 | // static |
| 93 | std::string HttpAuth::GetChallengeHeaderName(Target target) { |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 94 | switch (target) { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 95 | case AUTH_PROXY: |
| 96 | return "Proxy-Authenticate"; |
| 97 | case AUTH_SERVER: |
| 98 | return "WWW-Authenticate"; |
| 99 | default: |
| 100 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 101 | return std::string(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | // static |
| 106 | std::string HttpAuth::GetAuthorizationHeaderName(Target target) { |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 107 | switch (target) { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 108 | case AUTH_PROXY: |
[email protected] | 87c99b6a | 2011-05-13 20:06:48 | [diff] [blame] | 109 | return HttpRequestHeaders::kProxyAuthorization; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 110 | case AUTH_SERVER: |
[email protected] | 87c99b6a | 2011-05-13 20:06:48 | [diff] [blame] | 111 | return HttpRequestHeaders::kAuthorization; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 112 | default: |
| 113 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 114 | return std::string(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 118 | // static |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 119 | std::string HttpAuth::GetAuthTargetString(Target target) { |
| 120 | switch (target) { |
| 121 | case AUTH_PROXY: |
| 122 | return "proxy"; |
| 123 | case AUTH_SERVER: |
| 124 | return "server"; |
| 125 | default: |
| 126 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 127 | return std::string(); |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // static |
| 132 | const char* HttpAuth::SchemeToString(Scheme scheme) { |
| 133 | static const char* const kSchemeNames[] = { |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 134 | kBasicAuthScheme, kDigestAuthScheme, kNtlmAuthScheme, |
| 135 | kNegotiateAuthScheme, kSpdyProxyAuthScheme, kMockAuthScheme}; |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 136 | static_assert(arraysize(kSchemeNames) == AUTH_SCHEME_MAX, |
| 137 | "http auth scheme names incorrect size"); |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 138 | if (scheme < AUTH_SCHEME_BASIC || scheme >= AUTH_SCHEME_MAX) { |
| 139 | NOTREACHED(); |
| 140 | return "invalid_scheme"; |
| 141 | } |
| 142 | return kSchemeNames[scheme]; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 145 | } // namespace net |