1 // Copyright 2014 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 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string16.h"
11 #include "base/time/time.h"
15 class SingleThreadTaskRunner;
20 class HttpRequestHeaders;
21 class HttpResponseHeaders;
26 namespace data_reduction_proxy {
28 #if defined(OS_ANDROID)
29 extern const char kAndroidWebViewProtocolVersion[];
32 extern const char kClientAndroidWebview[];
33 extern const char kClientChromeAndroid[];
34 extern const char kClientChromeIOS[];
36 class DataReductionProxyParams;
38 class DataReductionProxyAuthRequestHandler {
40 static bool IsKeySetOnCommandLine();
42 // Constructs a DataReductionProxyAuthRequestHandler object with the given
43 // client type, params, and network task runner.
44 DataReductionProxyAuthRequestHandler(
45 const std::string& client,
46 DataReductionProxyParams* params,
47 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
49 virtual ~DataReductionProxyAuthRequestHandler();
51 // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
52 // proxy authentication credentials. Only adds this header if the provided
53 // |proxy_server| is a data reduction proxy and not the data reduction proxy's
54 // CONNECT server. Must be called on the IO thread.
55 void MaybeAddRequestHeader(net::URLRequest* request,
56 const net::ProxyServer& proxy_server,
57 net::HttpRequestHeaders* request_headers);
59 // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
60 // proxy authentication credentials. Only adds this header if the provided
61 // |proxy_server| is the data reduction proxy's CONNECT server. Must be called
63 void MaybeAddProxyTunnelRequestHandler(
64 const net::HostPortPair& proxy_server,
65 net::HttpRequestHeaders* request_headers);
67 // Sets a new authentication key. This must be called for platforms that do
68 // not have a default key defined. See the constructor implementation for
69 // those platforms. Must be called on the UI thread.
70 void SetKeyOnUI(const std::string& key);
74 void InitAuthenticationOnUI(const std::string& key);
76 void AddAuthorizationHeader(net::HttpRequestHeaders* headers);
78 // Returns a UTF16 string that's the hash of the configured authentication
79 // |key| and |salt|. Returns an empty UTF16 string if no key is configured or
80 // the data reduction proxy feature isn't available.
81 static base::string16 AuthHashForSalt(int64 salt,
82 const std::string& key);
83 // Visible for testing.
84 virtual base::Time Now() const;
85 virtual void RandBytes(void* output, size_t length);
87 // Visible for testing.
88 virtual std::string GetDefaultKey() const;
90 // Visible for testing.
91 DataReductionProxyAuthRequestHandler(
92 const std::string& client,
93 const std::string& version,
94 DataReductionProxyParams* params,
95 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
98 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
100 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
101 AuthorizationBogusVersion);
102 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
105 // Returns the version of Chromium that is being used.
106 std::string ChromiumVersion() const;
108 // Returns the build and patch numbers of |version|. If |version| isn't of the
109 // form xx.xx.xx.xx build and patch are not modified.
110 void GetChromiumBuildAndPatch(const std::string& version,
112 std::string* patch) const;
114 // Stores the supplied key and sets up credentials suitable for authenticating
115 // with the data reduction proxy.
116 void InitAuthentication(const std::string& key);
118 // Generates a session ID and credentials suitable for authenticating with
119 // the data reduction proxy.
120 void ComputeCredentials(const base::Time& now,
121 std::string* session,
122 std::string* credentials);
124 // Adds authentication headers only if |expects_ssl| is true and
125 // |proxy_server| is a data reduction proxy used for ssl tunneling via
126 // HTTP CONNECT, or |expect_ssl| is false and |proxy_server| is a data
127 // reduction proxy for HTTP traffic.
128 void MaybeAddRequestHeaderImpl(const net::HostPortPair& proxy_server,
130 net::HttpRequestHeaders* request_headers);
132 // Authentication state.
135 // Lives on the IO thread.
136 std::string session_;
137 std::string credentials_;
139 // Name of the client and version of the data reduction proxy protocol to use.
140 // Both live on the IO thread.
142 std::string build_number_;
143 std::string patch_number_;
145 // The last time the session was updated. Used to ensure that a session is
146 // never used for more than twenty-four hours.
147 base::Time last_update_time_;
149 DataReductionProxyParams* data_reduction_proxy_params_;
151 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
153 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyAuthRequestHandler);
156 } // namespace data_reduction_proxy
157 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_