| 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/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return; | 92 return; |
| 93 if (data_reduction_proxy_params_ && | 93 if (data_reduction_proxy_params_ && |
| 94 data_reduction_proxy_params_->IsDataReductionProxy( | 94 data_reduction_proxy_params_->IsDataReductionProxy( |
| 95 proxy_server.host_port_pair(), NULL)) { | 95 proxy_server.host_port_pair(), NULL)) { |
| 96 AddAuthorizationHeader(request_headers); | 96 AddAuthorizationHeader(request_headers); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( | 100 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( |
| 101 net::HttpRequestHeaders* headers) { | 101 net::HttpRequestHeaders* headers) { |
| 102 base::Time now = Now(); |
| 103 if (now - last_update_time_ > base::TimeDelta::FromHours(24)) { |
| 104 last_update_time_ = now; |
| 105 ComputeCredentials(last_update_time_, &session_, &credentials_); |
| 106 } |
| 102 const char kChromeProxyHeader[] = "Chrome-Proxy"; | 107 const char kChromeProxyHeader[] = "Chrome-Proxy"; |
| 103 std::string header_value; | 108 std::string header_value; |
| 104 if (headers->HasHeader(kChromeProxyHeader)) { | 109 if (headers->HasHeader(kChromeProxyHeader)) { |
| 105 headers->GetHeader(kChromeProxyHeader, &header_value); | 110 headers->GetHeader(kChromeProxyHeader, &header_value); |
| 106 headers->RemoveHeader(kChromeProxyHeader); | 111 headers->RemoveHeader(kChromeProxyHeader); |
| 107 header_value += ", "; | 112 header_value += ", "; |
| 108 } | 113 } |
| 109 header_value += | 114 header_value += |
| 110 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; | 115 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; |
| 111 if (!client_.empty()) | 116 if (!client_.empty()) |
| 112 header_value += ", c=" + client_; | 117 header_value += ", c=" + client_; |
| 113 headers->SetHeader(kChromeProxyHeader, header_value); | 118 headers->SetHeader(kChromeProxyHeader, header_value); |
| 114 } | 119 } |
| 115 | 120 |
| 116 void DataReductionProxyAuthRequestHandler::InitAuthenticationOnUI( | 121 void DataReductionProxyAuthRequestHandler::InitAuthenticationOnUI( |
| 117 const std::string& key) { | 122 const std::string& key) { |
| 118 key_ = key; | 123 network_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 124 &DataReductionProxyAuthRequestHandler::InitAuthentication, |
| 125 base::Unretained(this), |
| 126 key)); |
| 127 } |
| 128 |
| 129 void DataReductionProxyAuthRequestHandler::ComputeCredentials( |
| 130 const base::Time& now, |
| 131 std::string* session, |
| 132 std::string* credentials) { |
| 133 DCHECK(session); |
| 134 DCHECK(credentials); |
| 119 int64 timestamp = | 135 int64 timestamp = |
| 120 (Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | 136 (now - base::Time::UnixEpoch()).InMilliseconds() / 1000; |
| 121 | 137 |
| 122 int32 rand[3]; | 138 int32 rand[3]; |
| 123 RandBytes(rand, 3 * sizeof(rand[0])); | 139 RandBytes(rand, 3 * sizeof(rand[0])); |
| 124 std::string session = base::StringPrintf("%lld-%u-%u-%u", | 140 *session = base::StringPrintf("%lld-%u-%u-%u", |
| 125 static_cast<long long>(timestamp), | 141 static_cast<long long>(timestamp), |
| 126 rand[0], | 142 rand[0], |
| 127 rand[1], | 143 rand[1], |
| 128 rand[2]); | 144 rand[2]); |
| 129 std::string credentials = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); | 145 *credentials = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); |
| 130 | 146 |
| 131 DVLOG(1) << "session: [" << session << "] " | 147 DVLOG(1) << "session: [" << *session << "] " |
| 132 << "password: [" << credentials << "]"; | 148 << "password: [" << *credentials << "]"; |
| 133 network_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 134 &DataReductionProxyAuthRequestHandler::InitAuthentication, | |
| 135 base::Unretained(this), | |
| 136 session, | |
| 137 credentials)); | |
| 138 } | 149 } |
| 139 | 150 |
| 140 void DataReductionProxyAuthRequestHandler::InitAuthentication( | 151 void DataReductionProxyAuthRequestHandler::InitAuthentication( |
| 141 const std::string& session, | 152 const std::string& key) { |
| 142 const std::string& credentials) { | |
| 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 153 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 144 session_ = session; | 154 key_ = key; |
| 145 credentials_ = credentials; | 155 last_update_time_ = Now(); |
| 156 ComputeCredentials(last_update_time_, &session_, &credentials_); |
| 146 } | 157 } |
| 147 | 158 |
| 148 void DataReductionProxyAuthRequestHandler::SetKeyOnUI(const std::string& key) { | 159 void DataReductionProxyAuthRequestHandler::SetKeyOnUI(const std::string& key) { |
| 149 if (!key.empty()) | 160 if (!key.empty()) |
| 150 InitAuthenticationOnUI(key); | 161 InitAuthenticationOnUI(key); |
| 151 } | 162 } |
| 152 | 163 |
| 153 | 164 |
| 154 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { | 165 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { |
| 155 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 166 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 156 std::string key = | 167 std::string key = |
| 157 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); | 168 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); |
| 158 #if defined(SPDY_PROXY_AUTH_VALUE) | 169 #if defined(SPDY_PROXY_AUTH_VALUE) |
| 159 if (key.empty()) | 170 if (key.empty()) |
| 160 key = SPDY_PROXY_AUTH_VALUE; | 171 key = SPDY_PROXY_AUTH_VALUE; |
| 161 #endif | 172 #endif |
| 162 return key; | 173 return key; |
| 163 } | 174 } |
| 164 | 175 |
| 165 } // namespace data_reduction_proxy | 176 } // namespace data_reduction_proxy |
| OLD | NEW |