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 #if defined(USE_KERBEROS)
57 HttpAuthHandlerNegotiate::Factory
* negotiate_factory
=
58 new HttpAuthHandlerNegotiate::Factory();
60 negotiate_factory
->set_library(new GSSAPISharedLibrary(std::string()));
62 negotiate_factory
->set_library(new SSPILibraryDefault());
64 negotiate_factory
->set_host_resolver(host_resolver
);
65 registry_factory
->RegisterSchemeFactory("negotiate", negotiate_factory
);
66 #endif // defined(USE_KERBEROS)
68 HttpAuthHandlerNTLM::Factory
* ntlm_factory
=
69 new HttpAuthHandlerNTLM::Factory();
71 ntlm_factory
->set_sspi_library(new SSPILibraryDefault());
73 registry_factory
->RegisterSchemeFactory("ntlm", ntlm_factory
);
74 return registry_factory
;
79 bool IsSupportedScheme(const std::vector
<std::string
>& supported_schemes
,
80 const std::string
& scheme
) {
81 std::vector
<std::string
>::const_iterator it
= std::find(
82 supported_schemes
.begin(), supported_schemes
.end(), scheme
);
83 return it
!= supported_schemes
.end();
88 HttpAuthHandlerRegistryFactory::HttpAuthHandlerRegistryFactory() {
91 HttpAuthHandlerRegistryFactory::~HttpAuthHandlerRegistryFactory() {
92 STLDeleteContainerPairSecondPointers(factory_map_
.begin(),
96 void HttpAuthHandlerRegistryFactory::SetURLSecurityManager(
97 const std::string
& scheme
,
98 URLSecurityManager
* security_manager
) {
99 HttpAuthHandlerFactory
* factory
= GetSchemeFactory(scheme
);
101 factory
->set_url_security_manager(security_manager
);
104 void HttpAuthHandlerRegistryFactory::RegisterSchemeFactory(
105 const std::string
& scheme
,
106 HttpAuthHandlerFactory
* factory
) {
107 std::string lower_scheme
= base::StringToLowerASCII(scheme
);
108 FactoryMap::iterator it
= factory_map_
.find(lower_scheme
);
109 if (it
!= factory_map_
.end()) {
113 factory_map_
[lower_scheme
] = factory
;
115 factory_map_
.erase(it
);
118 HttpAuthHandlerFactory
* HttpAuthHandlerRegistryFactory::GetSchemeFactory(
119 const std::string
& scheme
) const {
120 std::string lower_scheme
= base::StringToLowerASCII(scheme
);
121 FactoryMap::const_iterator it
= factory_map_
.find(lower_scheme
);
122 if (it
== factory_map_
.end()) {
123 return NULL
; // |scheme| is not registered.
129 HttpAuthHandlerRegistryFactory
* HttpAuthHandlerRegistryFactory::Create(
130 const std::vector
<std::string
>& supported_schemes
,
131 URLSecurityManager
* security_manager
,
132 HostResolver
* host_resolver
,
133 const std::string
& gssapi_library_name
,
134 bool negotiate_disable_cname_lookup
,
135 bool negotiate_enable_port
) {
136 HttpAuthHandlerRegistryFactory
* registry_factory
=
137 new HttpAuthHandlerRegistryFactory();
138 if (IsSupportedScheme(supported_schemes
, "basic"))
139 registry_factory
->RegisterSchemeFactory(
140 "basic", new HttpAuthHandlerBasic::Factory());
141 if (IsSupportedScheme(supported_schemes
, "digest"))
142 registry_factory
->RegisterSchemeFactory(
143 "digest", new HttpAuthHandlerDigest::Factory());
144 if (IsSupportedScheme(supported_schemes
, "ntlm")) {
145 HttpAuthHandlerNTLM::Factory
* ntlm_factory
=
146 new HttpAuthHandlerNTLM::Factory();
147 ntlm_factory
->set_url_security_manager(security_manager
);
149 ntlm_factory
->set_sspi_library(new SSPILibraryDefault());
151 registry_factory
->RegisterSchemeFactory("ntlm", ntlm_factory
);
153 #if defined(USE_KERBEROS)
154 if (IsSupportedScheme(supported_schemes
, "negotiate")) {
155 HttpAuthHandlerNegotiate::Factory
* negotiate_factory
=
156 new HttpAuthHandlerNegotiate::Factory();
157 #if defined(OS_POSIX)
158 negotiate_factory
->set_library(
159 new GSSAPISharedLibrary(gssapi_library_name
));
160 #elif defined(OS_WIN)
161 negotiate_factory
->set_library(new SSPILibraryDefault());
163 negotiate_factory
->set_url_security_manager(security_manager
);
164 DCHECK(host_resolver
|| negotiate_disable_cname_lookup
);
165 negotiate_factory
->set_host_resolver(host_resolver
);
166 negotiate_factory
->set_disable_cname_lookup(negotiate_disable_cname_lookup
);
167 negotiate_factory
->set_use_port(negotiate_enable_port
);
168 registry_factory
->RegisterSchemeFactory("negotiate", negotiate_factory
);
170 #endif // defined(USE_KERBEROS)
172 return registry_factory
;
175 int HttpAuthHandlerRegistryFactory::CreateAuthHandler(
176 HttpAuthChallengeTokenizer
* challenge
,
177 HttpAuth::Target target
,
180 int digest_nonce_count
,
181 const BoundNetLog
& net_log
,
182 scoped_ptr
<HttpAuthHandler
>* handler
) {
183 std::string scheme
= challenge
->scheme();
184 if (scheme
.empty()) {
186 return ERR_INVALID_RESPONSE
;
188 std::string lower_scheme
= base::StringToLowerASCII(scheme
);
189 FactoryMap::iterator it
= factory_map_
.find(lower_scheme
);
190 if (it
== factory_map_
.end()) {
192 return ERR_UNSUPPORTED_AUTH_SCHEME
;
195 return it
->second
->CreateAuthHandler(challenge
, target
, origin
, reason
,
196 digest_nonce_count
, net_log
, handler
);