[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [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 | |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 5 | #ifndef NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_ |
| 6 | #define NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 8 | |
| 9 | #include <map> |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 10 | #include <string> |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 11 | #include <vector> |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 12 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 14 | #include "net/http/http_auth.h" |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 15 | #include "net/http/url_security_manager.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 16 | |
| 17 | class GURL; |
| 18 | |
| 19 | namespace net { |
| 20 | |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 21 | class BoundNetLog; |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 22 | class HostResolver; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 23 | class HttpAuthHandler; |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 24 | class HttpAuthHandlerRegistryFactory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 25 | |
| 26 | // An HttpAuthHandlerFactory is used to create HttpAuthHandler objects. |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 27 | // The HttpAuthHandlerFactory object _must_ outlive any of the HttpAuthHandler |
| 28 | // objects that it creates. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 29 | class HttpAuthHandlerFactory { |
| 30 | public: |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 31 | enum CreateReason { |
| 32 | CREATE_CHALLENGE, // Create a handler in response to a challenge. |
| 33 | CREATE_PREEMPTIVE, // Create a handler preemptively. |
| 34 | }; |
| 35 | |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 36 | HttpAuthHandlerFactory() : url_security_manager_(NULL) {} |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 37 | virtual ~HttpAuthHandlerFactory() {} |
| 38 | |
[email protected] | 797334f6 | 2010-08-18 18:42:17 | [diff] [blame] | 39 | // Sets an URL security manager. HttpAuthHandlerFactory doesn't own the URL |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 40 | // security manager, and the URL security manager should outlive this object. |
[email protected] | dc8b223 | 2010-05-20 19:41:58 | [diff] [blame] | 41 | void set_url_security_manager(URLSecurityManager* url_security_manager) { |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 42 | url_security_manager_ = url_security_manager; |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 45 | // Retrieves the associated URL security manager. |
[email protected] | dc8b223 | 2010-05-20 19:41:58 | [diff] [blame] | 46 | URLSecurityManager* url_security_manager() { |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 47 | return url_security_manager_; |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 50 | // 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] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 54 | // If an HttpAuthHandler object is successfully created it is passed back to |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 55 | // 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] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 63 | // |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] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 70 | // 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] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 75 | // |*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] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 79 | CreateReason create_reason, |
| 80 | int digest_nonce_count, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 81 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 82 | scoped_ptr<HttpAuthHandler>* handler) = 0; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 83 | |
| 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] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 92 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 93 | scoped_ptr<HttpAuthHandler>* handler); |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 94 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 95 | // 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] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 105 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 106 | scoped_ptr<HttpAuthHandler>* handler); |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 107 | |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 108 | // 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] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 111 | // |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 112 | // |resolver| is used by the Negotiate authentication handler to perform |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 113 | // CNAME lookups to generate a Kerberos SPN for the server. It must be |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 114 | // non-NULL. |resolver| must remain valid for the lifetime of the |
| 115 | // HttpAuthHandlerRegistryFactory and any HttpAuthHandlers created by said |
| 116 | // factory. |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 117 | static HttpAuthHandlerRegistryFactory* CreateDefault(HostResolver* resolver); |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 118 | |
| 119 | private: |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 120 | // The URL security manager |
[email protected] | dc8b223 | 2010-05-20 19:41:58 | [diff] [blame] | 121 | URLSecurityManager* url_security_manager_; |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 122 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(HttpAuthHandlerFactory); |
| 124 | }; |
| 125 | |
| 126 | // The HttpAuthHandlerRegistryFactory dispatches create requests out |
| 127 | // to other factories based on the auth scheme. |
| 128 | class HttpAuthHandlerRegistryFactory : public HttpAuthHandlerFactory { |
| 129 | public: |
| 130 | HttpAuthHandlerRegistryFactory(); |
| 131 | virtual ~HttpAuthHandlerRegistryFactory(); |
| 132 | |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 133 | // Sets an URL security manager into the factory associated with |scheme|. |
| 134 | void SetURLSecurityManager(const std::string& scheme, |
[email protected] | dc8b223 | 2010-05-20 19:41:58 | [diff] [blame] | 135 | URLSecurityManager* url_security_manager); |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 136 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 137 | // 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] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 148 | // 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] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 155 | // 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] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 169 | // |gssapi_library_name| specifies the name of the GSSAPI library that will |
| 170 | // be loaded on all platforms except Windows. |
| 171 | // |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 172 | // |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] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 178 | const std::string& gssapi_library_name, |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 179 | bool negotiate_disable_cname_lookup, |
| 180 | bool negotiate_enable_port); |
| 181 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 182 | // 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] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 192 | 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] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 201 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_FACTORY_H_ |