OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
8 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" | 14 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" |
13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" | 15 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" |
14 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | 16 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" |
15 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" | 17 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
16 #include "crypto/random.h" | 18 #include "crypto/random.h" |
17 #include "net/proxy/proxy_server.h" | 19 #include "net/proxy/proxy_server.h" |
(...skipping 11 matching lines...) Expand all Loading... |
29 const char kClientChromeIOS[] = "ios"; | 31 const char kClientChromeIOS[] = "ios"; |
30 | 32 |
31 // static | 33 // static |
32 bool DataReductionProxyAuthRequestHandler::IsKeySetOnCommandLine() { | 34 bool DataReductionProxyAuthRequestHandler::IsKeySetOnCommandLine() { |
33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 35 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
34 return command_line.HasSwitch( | 36 return command_line.HasSwitch( |
35 data_reduction_proxy::switches::kDataReductionProxyKey); | 37 data_reduction_proxy::switches::kDataReductionProxyKey); |
36 } | 38 } |
37 | 39 |
38 DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler( | 40 DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler( |
39 DataReductionProxyParams* params) | 41 DataReductionProxyParams* params, |
40 : data_reduction_proxy_params_(params) { | 42 base::MessageLoopProxy* io_thread_proxy) |
41 version_ = kProtocolVersion; | 43 : data_reduction_proxy_params_(params), |
42 #if defined(OS_ANDROID) | 44 io_thread_proxy_(io_thread_proxy) { |
43 client_ = kClientChromeAndroid; | |
44 #elif defined(OS_IOS) | |
45 client_ = kClientChromeIOS; | |
46 #endif | |
47 Init(); | 45 Init(); |
48 } | 46 } |
49 | 47 |
50 void DataReductionProxyAuthRequestHandler::Init() { | 48 void DataReductionProxyAuthRequestHandler::Init() { |
51 InitAuthentication(GetDefaultKey()); | 49 std::string client; |
| 50 #if defined(OS_ANDROID) |
| 51 client = kClientChromeAndroid; |
| 52 #elif defined(OS_IOS) |
| 53 client = kClientChromeIOS; |
| 54 #endif |
| 55 InitAuthentication(GetDefaultKey(), client, kProtocolVersion); |
52 } | 56 } |
53 | 57 |
54 | 58 |
55 DataReductionProxyAuthRequestHandler::~DataReductionProxyAuthRequestHandler() { | 59 DataReductionProxyAuthRequestHandler::~DataReductionProxyAuthRequestHandler() { |
56 } | 60 } |
57 | 61 |
58 // static | 62 // static |
59 base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt( | 63 base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt( |
60 int64 salt, | 64 int64 salt, |
61 const std::string& key) { | 65 const std::string& key) { |
(...skipping 13 matching lines...) Expand all Loading... |
75 | 79 |
76 void DataReductionProxyAuthRequestHandler::RandBytes( | 80 void DataReductionProxyAuthRequestHandler::RandBytes( |
77 void* output, size_t length) { | 81 void* output, size_t length) { |
78 crypto::RandBytes(output, length); | 82 crypto::RandBytes(output, length); |
79 } | 83 } |
80 | 84 |
81 void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader( | 85 void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader( |
82 net::URLRequest* request, | 86 net::URLRequest* request, |
83 const net::ProxyServer& proxy_server, | 87 const net::ProxyServer& proxy_server, |
84 net::HttpRequestHeaders* request_headers) { | 88 net::HttpRequestHeaders* request_headers) { |
| 89 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); |
85 if (!proxy_server.is_valid()) | 90 if (!proxy_server.is_valid()) |
86 return; | 91 return; |
87 if (data_reduction_proxy_params_ && | 92 if (data_reduction_proxy_params_ && |
88 data_reduction_proxy_params_->IsDataReductionProxy( | 93 data_reduction_proxy_params_->IsDataReductionProxy( |
89 proxy_server.host_port_pair(), NULL)) { | 94 proxy_server.host_port_pair(), NULL)) { |
90 AddAuthorizationHeader(request_headers); | 95 AddAuthorizationHeader(request_headers); |
91 } | 96 } |
92 } | 97 } |
93 | 98 |
94 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( | 99 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( |
95 net::HttpRequestHeaders* headers) { | 100 net::HttpRequestHeaders* headers) { |
96 const char kChromeProxyHeader[] = "Chrome-Proxy"; | 101 const char kChromeProxyHeader[] = "Chrome-Proxy"; |
97 std::string header_value; | 102 std::string header_value; |
98 if (headers->HasHeader(kChromeProxyHeader)) { | 103 if (headers->HasHeader(kChromeProxyHeader)) { |
99 headers->GetHeader(kChromeProxyHeader, &header_value); | 104 headers->GetHeader(kChromeProxyHeader, &header_value); |
100 headers->RemoveHeader(kChromeProxyHeader); | 105 headers->RemoveHeader(kChromeProxyHeader); |
101 header_value += ", "; | 106 header_value += ", "; |
102 } | 107 } |
103 header_value += | 108 header_value += |
104 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; | 109 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; |
105 if (!client_.empty()) | 110 if (!client_.empty()) |
106 header_value += ", c=" + client_; | 111 header_value += ", c=" + client_; |
107 headers->SetHeader(kChromeProxyHeader, header_value); | 112 headers->SetHeader(kChromeProxyHeader, header_value); |
108 } | 113 } |
109 | 114 |
110 void DataReductionProxyAuthRequestHandler::InitAuthentication( | 115 void DataReductionProxyAuthRequestHandler::InitAuthentication( |
111 const std::string& key) { | 116 const std::string& key, |
| 117 const std::string& client, |
| 118 const std::string& version) { |
112 key_ = key; | 119 key_ = key; |
113 int64 timestamp = | 120 int64 timestamp = |
114 (Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | 121 (Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; |
115 | 122 |
116 int32 rand[3]; | 123 int32 rand[3]; |
117 RandBytes(rand, 3 * sizeof(rand[0])); | 124 RandBytes(rand, 3 * sizeof(rand[0])); |
118 session_ = base::StringPrintf("%lld-%u-%u-%u", | 125 std::string session = base::StringPrintf("%lld-%u-%u-%u", |
119 static_cast<long long>(timestamp), | 126 static_cast<long long>(timestamp), |
120 rand[0], | 127 rand[0], |
121 rand[1], | 128 rand[1], |
122 rand[2]); | 129 rand[2]); |
123 credentials_ = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); | 130 std::string credentials = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); |
124 | 131 |
125 DVLOG(1) << "session: [" << session_ << "] " | 132 DVLOG(1) << "session: [" << session << "] " |
126 << "password: [" << credentials_ << "]"; | 133 << "password: [" << credentials << "]"; |
| 134 io_thread_proxy_->PostTask(FROM_HERE, base::Bind( |
| 135 &DataReductionProxyAuthRequestHandler::InitAuthenticationOnIOThread, |
| 136 base::Unretained(this), |
| 137 session, |
| 138 credentials, |
| 139 client, |
| 140 version)); |
| 141 } |
| 142 |
| 143 void DataReductionProxyAuthRequestHandler::InitAuthenticationOnIOThread( |
| 144 const std::string& session, |
| 145 const std::string& credentials, |
| 146 const std::string& client, |
| 147 const std::string& version) { |
| 148 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); |
| 149 session_ = session; |
| 150 credentials_ = credentials; |
| 151 client_ = client; |
| 152 version_ = version; |
127 } | 153 } |
128 | 154 |
129 void DataReductionProxyAuthRequestHandler::SetKey(const std::string& key, | 155 void DataReductionProxyAuthRequestHandler::SetKey(const std::string& key, |
130 const std::string& client, | 156 const std::string& client, |
131 const std::string& version) { | 157 const std::string& version) { |
132 client_ = client; | |
133 version_ = version; | |
134 if (!key.empty()) | 158 if (!key.empty()) |
135 InitAuthentication(key); | 159 InitAuthentication(key, client, version); |
136 } | 160 } |
137 | 161 |
138 | 162 |
139 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { | 163 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { |
140 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 164 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
141 std::string key = | 165 std::string key = |
142 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); | 166 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); |
143 #if defined(SPDY_PROXY_AUTH_VALUE) | 167 #if defined(SPDY_PROXY_AUTH_VALUE) |
144 if (key.empty()) | 168 if (key.empty()) |
145 key = SPDY_PROXY_AUTH_VALUE; | 169 key = SPDY_PROXY_AUTH_VALUE; |
146 #endif | 170 #endif |
147 return key; | 171 return key; |
148 } | 172 } |
149 | 173 |
150 } // namespace data_reduction_proxy | 174 } // namespace data_reduction_proxy |
OLD | NEW |