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 #include "net/http/http_auth_handler_factory.h"
7 #include "base/stl_util.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/net_errors.h"
10 #include "net/http/http_auth_challenge_tokenizer.h"
11 #include "net/http/http_auth_filter.h"
12 #include "net/http/http_auth_handler_basic.h"
13 #include "net/http/http_auth_handler_digest.h"
14 #include "net/http/http_auth_handler_ntlm.h"
16 #if defined(USE_KERBEROS)
17 #include "net/http/http_auth_handler_negotiate.h"
22 int HttpAuthHandlerFactory::CreateAuthHandlerFromString(
23 const std::string
& challenge
,
24 HttpAuth::Target target
,
26 const BoundNetLog
& net_log
,
27 scoped_ptr
<HttpAuthHandler
>* handler
) {
28 HttpAuthChallengeTokenizer
props(challenge
.begin(), challenge
.end());
29 return CreateAuthHandler(&props
, target
, origin
, CREATE_CHALLENGE
, 1,
33 int HttpAuthHandlerFactory::CreatePreemptiveAuthHandlerFromString(
34 const std::string
& challenge
,
35 HttpAuth::Target target
,
37 int digest_nonce_count
,
38 const BoundNetLog
& net_log
,
39 scoped_ptr
<HttpAuthHandler
>* handler
) {
40 HttpAuthChallengeTokenizer
props(challenge
.begin(), challenge
.end());
41 return CreateAuthHandler(&props
, target
, origin
, CREATE_PREEMPTIVE
,
42 digest_nonce_count
, net_log
, handler
);
46 HttpAuthHandlerRegistryFactory
* HttpAuthHandlerFactory::CreateDefault(
47 HostResolver
* host_resolver
) {
48 DCHECK(host_resolver
);
49 HttpAuthHandlerRegistryFactory
* registry_factory
=
50 new HttpAuthHandlerRegistryFactory();
51 registry_factory
->RegisterSchemeFactory(
52 "basic", new HttpAuthHandlerBasic::Factory());
53 registry_factory
->RegisterSchemeFactory(
54 "digest", new HttpAuthHandlerDigest::Factory());
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();
62 negotiate_factory
->set_library(new GSSAPISharedLibrary(std::string()));
64 negotiate_factory
->set_library(new SSPILibraryDefault());
66 negotiate_factory
->set_host_resolver(host_resolver
);
67 registry_factory
->RegisterSchemeFactory("negotiate", negotiate_factory
);
68 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID)
70 HttpAuthHandlerNTLM::Factory
* ntlm_factory
=
71 new HttpAuthHandlerNTLM::Factory();
73 ntlm_factory
->set_sspi_library(new SSPILibraryDefault());
75 registry_factory
->RegisterSchemeFactory("ntlm", ntlm_factory
);
76 return registry_factory
;
81 bool IsSupportedScheme(const std::vector
<std::string
>& supported_schemes
,
82 const std::string
& scheme
) {
83 std::vector
<std::string
>::const_iterator it
= std::find(
84 supported_schemes
.begin(), supported_schemes
.end(), scheme
);
85 return it
!= supported_schemes
.end();
90 HttpAuthHandlerRegistryFactory::HttpAuthHandlerRegistryFactory() {
93 HttpAuthHandlerRegistryFactory::~HttpAuthHandlerRegistryFactory() {
94 STLDeleteContainerPairSecondPointers(factory_map_
.begin(),
98 void HttpAuthHandlerRegistryFactory::SetURLSecurityManager(
99 const std::string
& scheme
,
100 URLSecurityManager
* security_manager
) {
101 HttpAuthHandlerFactory
* factory
= GetSchemeFactory(scheme
);
103 factory
->set_url_security_manager(security_manager
);
106 void HttpAuthHandlerRegistryFactory::RegisterSchemeFactory(
107 const std::string
& scheme
,
108 HttpAuthHandlerFactory
* factory
) {
109 std::string lower_scheme
= base::ToLowerASCII(scheme
);
110 FactoryMap::iterator it
= factory_map_
.find(lower_scheme
);
111 if (it
!= factory_map_
.end()) {
115 factory_map_
[lower_scheme
] = factory
;
117 factory_map_
.erase(it
);
120 HttpAuthHandlerFactory
* HttpAuthHandlerRegistryFactory::GetSchemeFactory(
121 const std::string
& scheme
) const {
122 std::string lower_scheme
= base::ToLowerASCII(scheme
);
123 FactoryMap::const_iterator it
= factory_map_
.find(lower_scheme
);
124 if (it
== factory_map_
.end()) {
125 return NULL
; // |scheme| is not registered.
131 HttpAuthHandlerRegistryFactory
* HttpAuthHandlerRegistryFactory::Create(
132 const std::vector
<std::string
>& supported_schemes
,
133 URLSecurityManager
* security_manager
,
134 HostResolver
* host_resolver
,
135 const std::string
& gssapi_library_name
,
136 const std::string
& auth_android_negotiate_account_type
,
137 bool negotiate_disable_cname_lookup
,
138 bool negotiate_enable_port
) {
139 HttpAuthHandlerRegistryFactory
* registry_factory
=
140 new HttpAuthHandlerRegistryFactory();
141 if (IsSupportedScheme(supported_schemes
, "basic"))
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
);
152 ntlm_factory
->set_sspi_library(new SSPILibraryDefault());
154 registry_factory
->RegisterSchemeFactory("ntlm", ntlm_factory
);
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());
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
);
175 #endif // defined(USE_KERBEROS)
177 return registry_factory
;
180 int HttpAuthHandlerRegistryFactory::CreateAuthHandler(
181 HttpAuthChallengeTokenizer
* challenge
,
182 HttpAuth::Target target
,
185 int digest_nonce_count
,
186 const BoundNetLog
& net_log
,
187 scoped_ptr
<HttpAuthHandler
>* handler
) {
188 std::string scheme
= challenge
->scheme();
189 if (scheme
.empty()) {
191 return ERR_INVALID_RESPONSE
;
193 std::string lower_scheme
= base::ToLowerASCII(scheme
);
194 FactoryMap::iterator it
= factory_map_
.find(lower_scheme
);
195 if (it
== factory_map_
.end()) {
197 return ERR_UNSUPPORTED_AUTH_SCHEME
;
200 return it
->second
->CreateAuthHandler(challenge
, target
, origin
, reason
,
201 digest_nonce_count
, net_log
, handler
);