Chromium Code Reviews| 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_handler_factory.h" | 5 #include "net/http/http_auth_handler_factory.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/http/http_auth_challenge_tokenizer.h" | 10 #include "net/http/http_auth_challenge_tokenizer.h" |
| 11 #include "net/http/http_auth_filter.h" | 11 #include "net/http/http_auth_filter.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_ntlm.h" | 14 #include "net/http/http_auth_handler_ntlm.h" |
| 15 #include "net/http/http_auth_preferences.h" | |
| 16 #include "net/http/http_auth_scheme.h" | |
| 15 | 17 |
| 16 #if defined(USE_KERBEROS) | 18 #if defined(USE_KERBEROS) |
| 17 #include "net/http/http_auth_handler_negotiate.h" | 19 #include "net/http/http_auth_handler_negotiate.h" |
| 18 #endif | 20 #endif |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 | 23 |
| 22 int HttpAuthHandlerFactory::CreateAuthHandlerFromString( | 24 int HttpAuthHandlerFactory::CreateAuthHandlerFromString( |
| 23 const std::string& challenge, | 25 const std::string& challenge, |
| 24 HttpAuth::Target target, | 26 HttpAuth::Target target, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 35 HttpAuth::Target target, | 37 HttpAuth::Target target, |
| 36 const GURL& origin, | 38 const GURL& origin, |
| 37 int digest_nonce_count, | 39 int digest_nonce_count, |
| 38 const BoundNetLog& net_log, | 40 const BoundNetLog& net_log, |
| 39 scoped_ptr<HttpAuthHandler>* handler) { | 41 scoped_ptr<HttpAuthHandler>* handler) { |
| 40 HttpAuthChallengeTokenizer props(challenge.begin(), challenge.end()); | 42 HttpAuthChallengeTokenizer props(challenge.begin(), challenge.end()); |
| 41 return CreateAuthHandler(&props, target, origin, CREATE_PREEMPTIVE, | 43 return CreateAuthHandler(&props, target, origin, CREATE_PREEMPTIVE, |
| 42 digest_nonce_count, net_log, handler); | 44 digest_nonce_count, net_log, handler); |
| 43 } | 45 } |
| 44 | 46 |
| 45 // static | |
| 46 scoped_ptr<HttpAuthHandlerRegistryFactory> | |
| 47 HttpAuthHandlerFactory::CreateDefault(HostResolver* host_resolver) { | |
| 48 DCHECK(host_resolver); | |
| 49 scoped_ptr<HttpAuthHandlerRegistryFactory> registry_factory = | |
| 50 make_scoped_ptr(new HttpAuthHandlerRegistryFactory()); | |
| 51 registry_factory->RegisterSchemeFactory( | |
| 52 "basic", new HttpAuthHandlerBasic::Factory()); | |
| 53 registry_factory->RegisterSchemeFactory( | |
| 54 "digest", new HttpAuthHandlerDigest::Factory()); | |
| 55 | |
| 56 // On Android Chrome needs an account type configured to enable Kerberos, | |
| 57 // so the default factory should not include Kerberos. | |
| 58 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) | |
| 59 HttpAuthHandlerNegotiate::Factory* negotiate_factory = | |
| 60 new HttpAuthHandlerNegotiate::Factory(); | |
| 61 #if defined(OS_POSIX) | |
| 62 negotiate_factory->set_library(new GSSAPISharedLibrary(std::string())); | |
| 63 #elif defined(OS_WIN) | |
| 64 negotiate_factory->set_library(new SSPILibraryDefault()); | |
| 65 #endif | |
| 66 negotiate_factory->set_host_resolver(host_resolver); | |
| 67 registry_factory->RegisterSchemeFactory("negotiate", negotiate_factory); | |
| 68 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID) | |
| 69 | |
| 70 HttpAuthHandlerNTLM::Factory* ntlm_factory = | |
| 71 new HttpAuthHandlerNTLM::Factory(); | |
| 72 #if defined(OS_WIN) | |
| 73 ntlm_factory->set_sspi_library(new SSPILibraryDefault()); | |
| 74 #endif | |
| 75 registry_factory->RegisterSchemeFactory("ntlm", ntlm_factory); | |
| 76 return registry_factory; | |
| 77 } | |
| 78 | 47 |
| 79 namespace { | 48 namespace { |
| 80 | 49 |
| 50 const char* default_auth_types[] = {kBasicAuthScheme, kDigestAuthScheme, | |
|
asanka
2015/11/20 15:32:09
const char* const kDefaultAuthTypes[] = {...};
Wi
aberent
2015/11/23 16:34:01
Done.
| |
| 51 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) | |
| 52 kNegotiateAuthScheme, | |
| 53 #endif | |
| 54 kNtlmAuthScheme}; | |
| 55 | |
| 81 bool IsSupportedScheme(const std::vector<std::string>& supported_schemes, | 56 bool IsSupportedScheme(const std::vector<std::string>& supported_schemes, |
| 82 const std::string& scheme) { | 57 const std::string& scheme) { |
| 83 std::vector<std::string>::const_iterator it = std::find( | 58 std::vector<std::string>::const_iterator it = std::find( |
| 84 supported_schemes.begin(), supported_schemes.end(), scheme); | 59 supported_schemes.begin(), supported_schemes.end(), scheme); |
| 85 return it != supported_schemes.end(); | 60 return it != supported_schemes.end(); |
| 86 } | 61 } |
| 87 | 62 |
| 63 void InitAuthHandlerRegistryFactory( | |
| 64 HttpAuthHandlerRegistryFactory* registry_factory, | |
| 65 std::vector<std::string> schemes, | |
| 66 HostResolver* host_resolver | |
| 67 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 68 , | |
| 69 std::string gssapi_library_name | |
| 70 #endif | |
| 71 ) { | |
| 72 if (IsSupportedScheme(schemes, kBasicAuthScheme)) | |
| 73 registry_factory->RegisterSchemeFactory( | |
| 74 kBasicAuthScheme, new HttpAuthHandlerBasic::Factory()); | |
| 75 if (IsSupportedScheme(schemes, kDigestAuthScheme)) | |
| 76 registry_factory->RegisterSchemeFactory( | |
| 77 kDigestAuthScheme, new HttpAuthHandlerDigest::Factory()); | |
| 78 if (IsSupportedScheme(schemes, kNtlmAuthScheme)) { | |
| 79 HttpAuthHandlerNTLM::Factory* ntlm_factory = | |
| 80 new HttpAuthHandlerNTLM::Factory(); | |
| 81 #if defined(OS_WIN) | |
| 82 ntlm_factory->set_sspi_library(new SSPILibraryDefault()); | |
| 83 #endif // defined(OS_WIN) | |
| 84 registry_factory->RegisterSchemeFactory(kNtlmAuthScheme, ntlm_factory); | |
| 85 } | |
| 86 #if defined(USE_KERBEROS) | |
| 87 if (IsSupportedScheme(schemes, kNegotiateAuthScheme)) { | |
| 88 DCHECK(host_resolver); | |
| 89 HttpAuthHandlerNegotiate::Factory* negotiate_factory = | |
| 90 new HttpAuthHandlerNegotiate::Factory(); | |
| 91 #if defined(OS_WIN) | |
| 92 negotiate_factory->set_library(make_scoped_ptr(new SSPILibraryDefault())); | |
| 93 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 94 negotiate_factory->set_library( | |
| 95 make_scoped_ptr(new GSSAPISharedLibrary(gssapi_library_name))); | |
| 96 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 97 negotiate_factory->set_host_resolver(host_resolver); | |
| 98 registry_factory->RegisterSchemeFactory(kNegotiateAuthScheme, | |
| 99 negotiate_factory); | |
| 100 } | |
| 101 #endif // defined(USE_KERBEROS) | |
| 102 } | |
| 103 | |
| 88 } // namespace | 104 } // namespace |
| 89 | 105 |
| 90 HttpAuthHandlerRegistryFactory::HttpAuthHandlerRegistryFactory() { | 106 HttpAuthHandlerRegistryFactory::HttpAuthHandlerRegistryFactory() { |
| 91 } | 107 } |
| 92 | 108 |
| 93 HttpAuthHandlerRegistryFactory::~HttpAuthHandlerRegistryFactory() { | 109 HttpAuthHandlerRegistryFactory::~HttpAuthHandlerRegistryFactory() { |
| 94 STLDeleteContainerPairSecondPointers(factory_map_.begin(), | 110 STLDeleteContainerPairSecondPointers(factory_map_.begin(), |
| 95 factory_map_.end()); | 111 factory_map_.end()); |
| 96 } | 112 } |
| 97 | 113 |
| 98 void HttpAuthHandlerRegistryFactory::SetURLSecurityManager( | 114 void HttpAuthHandlerRegistryFactory::SetHttpAuthPreferences( |
| 99 const std::string& scheme, | 115 const std::string& scheme, |
| 100 URLSecurityManager* security_manager) { | 116 HttpAuthPreferences* prefs) { |
| 101 HttpAuthHandlerFactory* factory = GetSchemeFactory(scheme); | 117 HttpAuthHandlerFactory* factory = GetSchemeFactory(scheme); |
| 102 if (factory) | 118 if (factory) |
| 103 factory->set_url_security_manager(security_manager); | 119 factory->set_http_auth_preferences(prefs); |
| 104 } | 120 } |
| 105 | 121 |
| 106 void HttpAuthHandlerRegistryFactory::RegisterSchemeFactory( | 122 void HttpAuthHandlerRegistryFactory::RegisterSchemeFactory( |
| 107 const std::string& scheme, | 123 const std::string& scheme, |
| 108 HttpAuthHandlerFactory* factory) { | 124 HttpAuthHandlerFactory* factory) { |
| 109 std::string lower_scheme = base::ToLowerASCII(scheme); | 125 std::string lower_scheme = base::ToLowerASCII(scheme); |
| 110 FactoryMap::iterator it = factory_map_.find(lower_scheme); | 126 FactoryMap::iterator it = factory_map_.find(lower_scheme); |
| 127 factory->set_http_auth_preferences(http_auth_preferences()); | |
| 111 if (it != factory_map_.end()) { | 128 if (it != factory_map_.end()) { |
| 112 delete it->second; | 129 delete it->second; |
| 113 } | 130 } |
| 114 if (factory) | 131 if (factory) |
| 115 factory_map_[lower_scheme] = factory; | 132 factory_map_[lower_scheme] = factory; |
| 116 else | 133 else |
| 117 factory_map_.erase(it); | 134 factory_map_.erase(it); |
| 118 } | 135 } |
| 119 | 136 |
| 120 HttpAuthHandlerFactory* HttpAuthHandlerRegistryFactory::GetSchemeFactory( | 137 HttpAuthHandlerFactory* HttpAuthHandlerRegistryFactory::GetSchemeFactory( |
| 121 const std::string& scheme) const { | 138 const std::string& scheme) const { |
| 122 std::string lower_scheme = base::ToLowerASCII(scheme); | 139 std::string lower_scheme = base::ToLowerASCII(scheme); |
| 123 FactoryMap::const_iterator it = factory_map_.find(lower_scheme); | 140 FactoryMap::const_iterator it = factory_map_.find(lower_scheme); |
| 124 if (it == factory_map_.end()) { | 141 if (it == factory_map_.end()) { |
| 125 return NULL; // |scheme| is not registered. | 142 return NULL; // |scheme| is not registered. |
| 126 } | 143 } |
| 127 return it->second; | 144 return it->second; |
| 128 } | 145 } |
| 129 | 146 |
| 130 // static | 147 // static |
| 131 HttpAuthHandlerRegistryFactory* HttpAuthHandlerRegistryFactory::Create( | 148 scoped_ptr<HttpAuthHandlerRegistryFactory> |
| 132 const std::vector<std::string>& supported_schemes, | 149 HttpAuthHandlerFactory::CreateDefault(HostResolver* host_resolver) { |
| 133 URLSecurityManager* security_manager, | 150 scoped_ptr<HttpAuthHandlerRegistryFactory> registry_factory( |
| 134 HostResolver* host_resolver, | 151 new HttpAuthHandlerRegistryFactory()); |
| 135 const std::string& gssapi_library_name, | 152 std::vector<std::string> auth_types( |
| 136 const std::string& auth_android_negotiate_account_type, | 153 default_auth_types, default_auth_types + arraysize(default_auth_types)); |
| 137 bool negotiate_disable_cname_lookup, | 154 InitAuthHandlerRegistryFactory(registry_factory.get(), auth_types, |
| 138 bool negotiate_enable_port) { | 155 host_resolver |
| 139 HttpAuthHandlerRegistryFactory* registry_factory = | 156 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 140 new HttpAuthHandlerRegistryFactory(); | 157 , |
| 141 if (IsSupportedScheme(supported_schemes, "basic")) | 158 std::string() |
| 142 registry_factory->RegisterSchemeFactory( | |
| 143 "basic", new HttpAuthHandlerBasic::Factory()); | |
| 144 if (IsSupportedScheme(supported_schemes, "digest")) | |
| 145 registry_factory->RegisterSchemeFactory( | |
| 146 "digest", new HttpAuthHandlerDigest::Factory()); | |
| 147 if (IsSupportedScheme(supported_schemes, "ntlm")) { | |
| 148 HttpAuthHandlerNTLM::Factory* ntlm_factory = | |
| 149 new HttpAuthHandlerNTLM::Factory(); | |
| 150 ntlm_factory->set_url_security_manager(security_manager); | |
| 151 #if defined(OS_WIN) | |
| 152 ntlm_factory->set_sspi_library(new SSPILibraryDefault()); | |
| 153 #endif | 159 #endif |
| 154 registry_factory->RegisterSchemeFactory("ntlm", ntlm_factory); | 160 ); |
| 155 } | |
| 156 #if defined(USE_KERBEROS) | |
| 157 if (IsSupportedScheme(supported_schemes, "negotiate")) { | |
| 158 HttpAuthHandlerNegotiate::Factory* negotiate_factory = | |
| 159 new HttpAuthHandlerNegotiate::Factory(); | |
| 160 #if defined(OS_ANDROID) | |
| 161 negotiate_factory->set_library(&auth_android_negotiate_account_type); | |
| 162 #elif defined(OS_POSIX) | |
| 163 negotiate_factory->set_library( | |
| 164 new GSSAPISharedLibrary(gssapi_library_name)); | |
| 165 #elif defined(OS_WIN) | |
| 166 negotiate_factory->set_library(new SSPILibraryDefault()); | |
| 167 #endif | |
| 168 negotiate_factory->set_url_security_manager(security_manager); | |
| 169 DCHECK(host_resolver || negotiate_disable_cname_lookup); | |
| 170 negotiate_factory->set_host_resolver(host_resolver); | |
| 171 negotiate_factory->set_disable_cname_lookup(negotiate_disable_cname_lookup); | |
| 172 negotiate_factory->set_use_port(negotiate_enable_port); | |
| 173 registry_factory->RegisterSchemeFactory("negotiate", negotiate_factory); | |
| 174 } | |
| 175 #endif // defined(USE_KERBEROS) | |
| 176 | |
| 177 return registry_factory; | 161 return registry_factory; |
| 178 } | 162 } |
| 179 | 163 |
| 164 // static | |
| 165 scoped_ptr<HttpAuthHandlerRegistryFactory> | |
| 166 HttpAuthHandlerRegistryFactory::Create(HttpAuthPreferences* prefs, | |
| 167 HostResolver* host_resolver) { | |
| 168 scoped_ptr<HttpAuthHandlerRegistryFactory> registry_factory( | |
| 169 new HttpAuthHandlerRegistryFactory()); | |
| 170 registry_factory->set_http_auth_preferences(prefs); | |
| 171 InitAuthHandlerRegistryFactory(registry_factory.get(), prefs->auth_schemes(), | |
| 172 host_resolver | |
| 173 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 174 , | |
| 175 prefs->gssapi_library_name() | |
| 176 #endif | |
| 177 ); | |
| 178 for (std::pair<std::string, HttpAuthHandlerFactory*> factory_entry : | |
| 179 registry_factory->factory_map_) { | |
| 180 factory_entry.second->set_http_auth_preferences(prefs); | |
| 181 } | |
| 182 return registry_factory; | |
| 183 } | |
| 184 | |
| 180 int HttpAuthHandlerRegistryFactory::CreateAuthHandler( | 185 int HttpAuthHandlerRegistryFactory::CreateAuthHandler( |
| 181 HttpAuthChallengeTokenizer* challenge, | 186 HttpAuthChallengeTokenizer* challenge, |
| 182 HttpAuth::Target target, | 187 HttpAuth::Target target, |
| 183 const GURL& origin, | 188 const GURL& origin, |
| 184 CreateReason reason, | 189 CreateReason reason, |
| 185 int digest_nonce_count, | 190 int digest_nonce_count, |
| 186 const BoundNetLog& net_log, | 191 const BoundNetLog& net_log, |
| 187 scoped_ptr<HttpAuthHandler>* handler) { | 192 scoped_ptr<HttpAuthHandler>* handler) { |
| 188 std::string scheme = challenge->scheme(); | 193 std::string scheme = challenge->scheme(); |
| 189 if (scheme.empty()) { | 194 if (scheme.empty()) { |
| 190 handler->reset(); | 195 handler->reset(); |
| 191 return ERR_INVALID_RESPONSE; | 196 return ERR_INVALID_RESPONSE; |
| 192 } | 197 } |
| 193 std::string lower_scheme = base::ToLowerASCII(scheme); | 198 std::string lower_scheme = base::ToLowerASCII(scheme); |
| 194 FactoryMap::iterator it = factory_map_.find(lower_scheme); | 199 FactoryMap::iterator it = factory_map_.find(lower_scheme); |
| 195 if (it == factory_map_.end()) { | 200 if (it == factory_map_.end()) { |
| 196 handler->reset(); | 201 handler->reset(); |
| 197 return ERR_UNSUPPORTED_AUTH_SCHEME; | 202 return ERR_UNSUPPORTED_AUTH_SCHEME; |
| 198 } | 203 } |
| 199 DCHECK(it->second); | 204 DCHECK(it->second); |
| 200 return it->second->CreateAuthHandler(challenge, target, origin, reason, | 205 return it->second->CreateAuthHandler(challenge, target, origin, reason, |
| 201 digest_nonce_count, net_log, handler); | 206 digest_nonce_count, net_log, handler); |
| 202 } | 207 } |
| 203 | 208 |
| 209 #if defined(OS_ANDROID) | |
| 210 void HttpAuthHandlerRegistryFactory::SetAndroidAuthNegotiateAccountType( | |
| 211 const std::string& account_type) { | |
| 212 for (std::pair<std::string, HttpAuthHandlerFactory*> map_entry : | |
| 213 factory_map_) { | |
| 214 map_entry.second->SetAndroidAuthNegotiateAccountType(account_type); | |
| 215 } | |
| 216 } | |
| 217 #endif | |
| 218 | |
| 204 } // namespace net | 219 } // namespace net |
| OLD | NEW |