blob: c5cf98e773c3344cf323db3bdcd661f6bfcaa430 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]fa55e192010-02-15 14:25:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eb3cac72010-02-26 21:07:455#ifndef NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_
6#define NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]fa55e192010-02-15 14:25:508
9#include <map>
[email protected]eb3cac72010-02-26 21:07:4510#include <string>
[email protected]b7304162010-08-23 17:42:2911#include <vector>
[email protected]fa55e192010-02-15 14:25:5012
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
[email protected]fa55e192010-02-15 14:25:5014#include "net/http/http_auth.h"
[email protected]b4955e7d2010-04-16 20:22:3015#include "net/http/url_security_manager.h"
[email protected]fa55e192010-02-15 14:25:5016
17class GURL;
18
19namespace net {
20
[email protected]ac5c06e2010-05-27 15:07:3821class BoundNetLog;
[email protected]b7304162010-08-23 17:42:2922class HostResolver;
[email protected]fa55e192010-02-15 14:25:5023class HttpAuthHandler;
[email protected]eb3cac72010-02-26 21:07:4524class HttpAuthHandlerRegistryFactory;
[email protected]fa55e192010-02-15 14:25:5025
26// An HttpAuthHandlerFactory is used to create HttpAuthHandler objects.
[email protected]54fea2562010-11-17 14:40:4427// The HttpAuthHandlerFactory object _must_ outlive any of the HttpAuthHandler
28// objects that it creates.
[email protected]fa55e192010-02-15 14:25:5029class HttpAuthHandlerFactory {
30 public:
[email protected]ad74a592011-01-21 18:40:5531 enum CreateReason {
32 CREATE_CHALLENGE, // Create a handler in response to a challenge.
33 CREATE_PREEMPTIVE, // Create a handler preemptively.
34 };
35
[email protected]b4955e7d2010-04-16 20:22:3036 HttpAuthHandlerFactory() : url_security_manager_(NULL) {}
[email protected]fa55e192010-02-15 14:25:5037 virtual ~HttpAuthHandlerFactory() {}
38
[email protected]797334f62010-08-18 18:42:1739 // Sets an URL security manager. HttpAuthHandlerFactory doesn't own the URL
[email protected]b4955e7d2010-04-16 20:22:3040 // security manager, and the URL security manager should outlive this object.
[email protected]dc8b2232010-05-20 19:41:5841 void set_url_security_manager(URLSecurityManager* url_security_manager) {
[email protected]b4955e7d2010-04-16 20:22:3042 url_security_manager_ = url_security_manager;
[email protected]eb3cac72010-02-26 21:07:4543 }
44
[email protected]b4955e7d2010-04-16 20:22:3045 // Retrieves the associated URL security manager.
[email protected]dc8b2232010-05-20 19:41:5846 URLSecurityManager* url_security_manager() {
[email protected]b4955e7d2010-04-16 20:22:3047 return url_security_manager_;
[email protected]eb3cac72010-02-26 21:07:4548 }
49
[email protected]fa55e192010-02-15 14:25:5050 // Creates an HttpAuthHandler object based on the authentication
51 // challenge specified by |*challenge|. |challenge| must point to a valid
52 // non-NULL tokenizer.
53 //
[email protected]54fea2562010-11-17 14:40:4454 // If an HttpAuthHandler object is successfully created it is passed back to
[email protected]fa55e192010-02-15 14:25:5055 // the caller through |*handler| and OK is returned.
56 //
57 // If |*challenge| specifies an unsupported authentication scheme, |*handler|
58 // is set to NULL and ERR_UNSUPPORTED_AUTH_SCHEME is returned.
59 //
60 // If |*challenge| is improperly formed, |*handler| is set to NULL and
61 // ERR_INVALID_RESPONSE is returned.
62 //
[email protected]fa82f932010-05-20 11:09:2463 // |create_reason| indicates why the handler is being created. This is used
64 // since NTLM and Negotiate schemes do not support preemptive creation.
65 //
66 // |digest_nonce_count| is specifically intended for the Digest authentication
67 // scheme, and indicates the number of handlers generated for a particular
68 // server nonce challenge.
69 //
[email protected]eb3cac72010-02-26 21:07:4570 // For the NTLM and Negotiate handlers:
71 // If |origin| does not match the authentication method's filters for
72 // the specified |target|, ERR_INVALID_AUTH_CREDENTIALS is returned.
73 // NOTE: This will apply to ALL |origin| values if the filters are empty.
74 //
[email protected]fa55e192010-02-15 14:25:5075 // |*challenge| should not be reused after a call to |CreateAuthHandler()|,
76 virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* challenge,
77 HttpAuth::Target target,
78 const GURL& origin,
[email protected]fa82f932010-05-20 11:09:2479 CreateReason create_reason,
80 int digest_nonce_count,
[email protected]ac5c06e2010-05-27 15:07:3881 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:1482 scoped_ptr<HttpAuthHandler>* handler) = 0;
[email protected]fa55e192010-02-15 14:25:5083
84 // Creates an HTTP authentication handler based on the authentication
85 // challenge string |challenge|.
86 // This is a convenience function which creates a ChallengeTokenizer for
87 // |challenge| and calls |CreateAuthHandler|. See |CreateAuthHandler| for
88 // more details on return values.
89 int CreateAuthHandlerFromString(const std::string& challenge,
90 HttpAuth::Target target,
91 const GURL& origin,
[email protected]ac5c06e2010-05-27 15:07:3892 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:1493 scoped_ptr<HttpAuthHandler>* handler);
[email protected]fa55e192010-02-15 14:25:5094
[email protected]fa82f932010-05-20 11:09:2495 // Creates an HTTP authentication handler based on the authentication
96 // challenge string |challenge|.
97 // This is a convenience function which creates a ChallengeTokenizer for
98 // |challenge| and calls |CreateAuthHandler|. See |CreateAuthHandler| for
99 // more details on return values.
100 int CreatePreemptiveAuthHandlerFromString(
101 const std::string& challenge,
102 HttpAuth::Target target,
103 const GURL& origin,
104 int digest_nonce_count,
[email protected]ac5c06e2010-05-27 15:07:38105 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:14106 scoped_ptr<HttpAuthHandler>* handler);
[email protected]fa82f932010-05-20 11:09:24107
[email protected]eb3cac72010-02-26 21:07:45108 // Creates a standard HttpAuthHandlerRegistryFactory. The caller is
109 // responsible for deleting the factory.
110 // The default factory supports Basic, Digest, NTLM, and Negotiate schemes.
[email protected]f660e4b2010-09-29 14:20:08111 //
[email protected]73c45322010-10-01 23:57:54112 // |resolver| is used by the Negotiate authentication handler to perform
[email protected]f660e4b2010-09-29 14:20:08113 // CNAME lookups to generate a Kerberos SPN for the server. It must be
[email protected]73c45322010-10-01 23:57:54114 // non-NULL. |resolver| must remain valid for the lifetime of the
115 // HttpAuthHandlerRegistryFactory and any HttpAuthHandlers created by said
116 // factory.
[email protected]f660e4b2010-09-29 14:20:08117 static HttpAuthHandlerRegistryFactory* CreateDefault(HostResolver* resolver);
[email protected]fa55e192010-02-15 14:25:50118
119 private:
[email protected]b4955e7d2010-04-16 20:22:30120 // The URL security manager
[email protected]dc8b2232010-05-20 19:41:58121 URLSecurityManager* url_security_manager_;
[email protected]eb3cac72010-02-26 21:07:45122
[email protected]fa55e192010-02-15 14:25:50123 DISALLOW_COPY_AND_ASSIGN(HttpAuthHandlerFactory);
124};
125
126// The HttpAuthHandlerRegistryFactory dispatches create requests out
127// to other factories based on the auth scheme.
128class HttpAuthHandlerRegistryFactory : public HttpAuthHandlerFactory {
129 public:
130 HttpAuthHandlerRegistryFactory();
131 virtual ~HttpAuthHandlerRegistryFactory();
132
[email protected]b4955e7d2010-04-16 20:22:30133 // Sets an URL security manager into the factory associated with |scheme|.
134 void SetURLSecurityManager(const std::string& scheme,
[email protected]dc8b2232010-05-20 19:41:58135 URLSecurityManager* url_security_manager);
[email protected]eb3cac72010-02-26 21:07:45136
[email protected]fa55e192010-02-15 14:25:50137 // Registers a |factory| that will be used for a particular HTTP
138 // authentication scheme such as Basic, Digest, or Negotiate.
139 // The |*factory| object is assumed to be new-allocated, and its lifetime
140 // will be managed by this HttpAuthHandlerRegistryFactory object (including
141 // deleting it when it is no longer used.
142 // A NULL |factory| value means that HttpAuthHandlers's will not be created
143 // for |scheme|. If a factory object used to exist for |scheme|, it will be
144 // deleted.
145 void RegisterSchemeFactory(const std::string& scheme,
146 HttpAuthHandlerFactory* factory);
147
[email protected]e5ae96a2010-04-14 20:12:45148 // Retrieve the factory for the specified |scheme|. If no factory exists
149 // for the |scheme|, NULL is returned. The returned factory must not be
150 // deleted by the caller, and it is guaranteed to be valid until either
151 // a new factory is registered for the same scheme, or until this
152 // registry factory is destroyed.
153 HttpAuthHandlerFactory* GetSchemeFactory(const std::string& scheme) const;
154
[email protected]b7304162010-08-23 17:42:29155 // Creates an HttpAuthHandlerRegistryFactory.
156 //
157 // |supported_schemes| is a list of authentication schemes. Valid values
158 // include "basic", "digest", "ntlm", and "negotiate", where case matters.
159 //
160 // |security_manager| is used by the NTLM and Negotiate authenticators
161 // to determine which servers Integrated Authentication can be used with. If
162 // NULL, Integrated Authentication will not be used with any server.
163 //
164 // |host_resolver| is used by the Negotiate authentication handler to perform
165 // CNAME lookups to generate a Kerberos SPN for the server. If the "negotiate"
166 // scheme is used and |negotiate_disable_cname_lookup| is false,
167 // |host_resolver| must not be NULL.
168 //
[email protected]ac7f3fdb2010-11-12 12:47:05169 // |gssapi_library_name| specifies the name of the GSSAPI library that will
170 // be loaded on all platforms except Windows.
171 //
[email protected]b7304162010-08-23 17:42:29172 // |negotiate_disable_cname_lookup| and |negotiate_enable_port| both control
173 // how Negotiate does SPN generation, by default these should be false.
174 static HttpAuthHandlerRegistryFactory* Create(
175 const std::vector<std::string>& supported_schemes,
176 URLSecurityManager* security_manager,
177 HostResolver* host_resolver,
[email protected]ac7f3fdb2010-11-12 12:47:05178 const std::string& gssapi_library_name,
[email protected]b7304162010-08-23 17:42:29179 bool negotiate_disable_cname_lookup,
180 bool negotiate_enable_port);
181
[email protected]f48b9432011-01-11 07:25:40182 // Creates an auth handler by dispatching out to the registered factories
183 // based on the first token in |challenge|.
184 virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* challenge,
185 HttpAuth::Target target,
186 const GURL& origin,
187 CreateReason reason,
188 int digest_nonce_count,
189 const BoundNetLog& net_log,
190 scoped_ptr<HttpAuthHandler>* handler);
191
[email protected]fa55e192010-02-15 14:25:50192 private:
193 typedef std::map<std::string, HttpAuthHandlerFactory*> FactoryMap;
194
195 FactoryMap factory_map_;
196 DISALLOW_COPY_AND_ASSIGN(HttpAuthHandlerRegistryFactory);
197};
198
199} // namespace net
200
[email protected]eb3cac72010-02-26 21:07:45201#endif // NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_