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;
19 class HttpRequestHeaders;
20 class HttpResponseHeaders;
25 namespace data_reduction_proxy {
27 #if defined(OS_ANDROID)
28 extern const char kAndroidWebViewProtocolVersion[];
31 extern const char kClientAndroidWebview[];
32 extern const char kClientChromeAndroid[];
33 extern const char kClientChromeIOS[];
35 class DataReductionProxyParams;
37 class DataReductionProxyAuthRequestHandler {
39 static bool IsKeySetOnCommandLine();
41 // Constructs a DataReductionProxyAuthRequestHandler object with the given
42 // client type, params, and network task runner.
43 DataReductionProxyAuthRequestHandler(
44 const std::string& client,
45 DataReductionProxyParams* params,
46 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
48 virtual ~DataReductionProxyAuthRequestHandler();
50 // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
51 // proxy authentication credentials. Only adds this header if the provided
52 // |proxy_server| is a data reduction proxy. Must be called on the IO thread.
53 void MaybeAddRequestHeader(net::URLRequest* request,
54 const net::ProxyServer& proxy_server,
55 net::HttpRequestHeaders* request_headers);
57 // Sets a new authentication key. This must be called for platforms that do
58 // not have a default key defined. See the constructor implementation for
59 // those platforms. Must be called on the UI thread.
60 void SetKeyOnUI(const std::string& key);
64 void InitAuthenticationOnUI(const std::string& key);
66 void AddAuthorizationHeader(net::HttpRequestHeaders* headers);
68 // Returns a UTF16 string that's the hash of the configured authentication
69 // |key| and |salt|. Returns an empty UTF16 string if no key is configured or
70 // the data reduction proxy feature isn't available.
71 static base::string16 AuthHashForSalt(int64 salt,
72 const std::string& key);
73 // Visible for testing.
74 virtual base::Time Now() const;
75 virtual void RandBytes(void* output, size_t length);
77 // Visible for testing.
78 virtual std::string GetDefaultKey() const;
80 // Visible for testing.
81 DataReductionProxyAuthRequestHandler(
82 const std::string& client,
83 const std::string& version,
84 DataReductionProxyParams* params,
85 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
88 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
90 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
91 AuthorizationBogusVersion);
92 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
95 // Returns the version of Chromium that is being used.
96 std::string ChromiumVersion() const;
98 // Returns the build and patch numbers of |version|. If |version| isn't of the
99 // form xx.xx.xx.xx build and patch are not modified.
100 void GetChromiumBuildAndPatch(const std::string& version,
102 std::string* patch) const;
104 // Stores the supplied key and sets up credentials suitable for authenticating
105 // with the data reduction proxy.
106 void InitAuthentication(const std::string& key);
108 // Generates a session ID and credentials suitable for authenticating with
109 // the data reduction proxy.
110 void ComputeCredentials(const base::Time& now,
111 std::string* session,
112 std::string* credentials);
114 // Authentication state.
117 // Lives on the IO thread.
118 std::string session_;
119 std::string credentials_;
121 // Name of the client and version of the data reduction proxy protocol to use.
122 // Both live on the IO thread.
124 std::string build_number_;
125 std::string patch_number_;
127 // The last time the session was updated. Used to ensure that a session is
128 // never used for more than twenty-four hours.
129 base::Time last_update_time_;
131 DataReductionProxyParams* data_reduction_proxy_params_;
133 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
135 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyAuthRequestHandler);
138 } // namespace data_reduction_proxy
139 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_