1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// found in the LICENSE file. 4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_ 6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_ 7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/gtest_prod_util.h" 9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/ref_counted.h" 10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/strings/string16.h" 11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/time/time.h" 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "url/gurl.h" 13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace base { 156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class SingleThreadTaskRunner; 166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace net { 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass HostPortPair; 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass HttpRequestHeaders; 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass HttpResponseHeaders; 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ProxyServer; 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass URLRequest; 24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace data_reduction_proxy { 27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if defined(OS_ANDROID) 295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)extern const char kAndroidWebViewProtocolVersion[]; 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const char kClientAndroidWebview[]; 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const char kClientChromeAndroid[]; 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const char kClientChromeIOS[]; 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 36116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass DataReductionProxyParams; 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class DataReductionProxyAuthRequestHandler { 39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) public: 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static bool IsKeySetOnCommandLine(); 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Constructs a DataReductionProxyAuthRequestHandler object with the given 431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // client type, params, and network task runner. 445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) DataReductionProxyAuthRequestHandler( 455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const std::string& client, 466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) DataReductionProxyParams* params, 476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) scoped_refptr<base::SingleThreadTaskRunner> network_task_runner); 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual ~DataReductionProxyAuthRequestHandler(); 50010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // proxy authentication credentials. Only adds this header if the provided 531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |proxy_server| is a data reduction proxy and not the data reduction proxy's 541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // CONNECT server. Must be called on the IO thread. 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void MaybeAddRequestHeader(net::URLRequest* request, 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const net::ProxyServer& proxy_server, 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch net::HttpRequestHeaders* request_headers); 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction 601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // proxy authentication credentials. Only adds this header if the provided 611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |proxy_server| is the data reduction proxy's CONNECT server. Must be called 621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // on the IO thread. 631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void MaybeAddProxyTunnelRequestHandler( 641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const net::HostPortPair& proxy_server, 651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci net::HttpRequestHeaders* request_headers); 661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 6734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // Stores the supplied key and sets up credentials suitable for authenticating 6834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // with the data reduction proxy. 6934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // This can be called more than once. For example on a platform that does not 7034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // have a default key defined, this function will be called some time after 7134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // this class has been constructed. Android WebView is a platform that does 7234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // this. The caller needs to make sure |this| pointer is valid when 7334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // InitAuthentication is called. 7434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) void InitAuthentication(const std::string& key); 75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) protected: 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void Init(); 78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void AddAuthorizationHeader(net::HttpRequestHeaders* headers); 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Returns a UTF16 string that's the hash of the configured authentication 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |key| and |salt|. Returns an empty UTF16 string if no key is configured or 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the data reduction proxy feature isn't available. 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static base::string16 AuthHashForSalt(int64 salt, 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& key); 86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Visible for testing. 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual base::Time Now() const; 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void RandBytes(void* output, size_t length); 89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 90010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Visible for testing. 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual std::string GetDefaultKey() const; 92010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Visible for testing. 941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DataReductionProxyAuthRequestHandler( 951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const std::string& client, 961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const std::string& version, 971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DataReductionProxyParams* params, 981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_refptr<base::SingleThreadTaskRunner> network_task_runner); 991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) private: 101010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest, 10234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) AuthorizationOnIO); 10334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest, 10434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) AuthorizationIgnoresEmptyKey); 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest, 1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci AuthorizationBogusVersion); 1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest, 108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch AuthHashForSalt); 109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Returns the version of Chromium that is being used. 1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string ChromiumVersion() const; 1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Returns the build and patch numbers of |version|. If |version| isn't of the 1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // form xx.xx.xx.xx build and patch are not modified. 1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void GetChromiumBuildAndPatch(const std::string& version, 1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string* build, 1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string* patch) const; 1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Generates a session ID and credentials suitable for authenticating with 1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // the data reduction proxy. 1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) void ComputeCredentials(const base::Time& now, 1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) std::string* session, 1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) std::string* credentials); 1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Adds authentication headers only if |expects_ssl| is true and 1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |proxy_server| is a data reduction proxy used for ssl tunneling via 1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // HTTP CONNECT, or |expect_ssl| is false and |proxy_server| is a data 1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // reduction proxy for HTTP traffic. 1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void MaybeAddRequestHeaderImpl(const net::HostPortPair& proxy_server, 1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool expect_ssl, 1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci net::HttpRequestHeaders* request_headers); 1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Authentication state. 134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::string key_; 1356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Lives on the IO thread. 137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::string session_; 138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::string credentials_; 139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Name of the client and version of the data reduction proxy protocol to use. 1416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Both live on the IO thread. 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::string client_; 1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string build_number_; 1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string patch_number_; 145010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 1466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // The last time the session was updated. Used to ensure that a session is 1476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // never used for more than twenty-four hours. 1486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) base::Time last_update_time_; 1496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DataReductionProxyParams* data_reduction_proxy_params_; 151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(DataReductionProxyAuthRequestHandler); 155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}; 156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} // namespace data_reduction_proxy 158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_ 159