| 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/single_thread_task_runner.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 16 matching lines...) Expand all Loading... |
| 34 // static | 36 // static |
| 35 bool DataReductionProxyAuthRequestHandler::IsKeySetOnCommandLine() { | 37 bool DataReductionProxyAuthRequestHandler::IsKeySetOnCommandLine() { |
| 36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 38 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 37 return command_line.HasSwitch( | 39 return command_line.HasSwitch( |
| 38 data_reduction_proxy::switches::kDataReductionProxyKey); | 40 data_reduction_proxy::switches::kDataReductionProxyKey); |
| 39 } | 41 } |
| 40 | 42 |
| 41 DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler( | 43 DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler( |
| 42 const std::string& client, | 44 const std::string& client, |
| 43 const std::string& version, | 45 const std::string& version, |
| 44 DataReductionProxyParams* params) | 46 DataReductionProxyParams* params, |
| 45 : data_reduction_proxy_params_(params) { | 47 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) |
| 48 : data_reduction_proxy_params_(params), |
| 49 network_task_runner_(network_task_runner) { |
| 46 client_ = client; | 50 client_ = client; |
| 47 version_ = version; | 51 version_ = version; |
| 48 Init(); | 52 Init(); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void DataReductionProxyAuthRequestHandler::Init() { | 55 void DataReductionProxyAuthRequestHandler::Init() { |
| 52 InitAuthentication(GetDefaultKey()); | 56 InitAuthenticationOnUI(GetDefaultKey()); |
| 53 } | 57 } |
| 54 | 58 |
| 55 | 59 |
| 56 DataReductionProxyAuthRequestHandler::~DataReductionProxyAuthRequestHandler() { | 60 DataReductionProxyAuthRequestHandler::~DataReductionProxyAuthRequestHandler() { |
| 57 } | 61 } |
| 58 | 62 |
| 59 // static | 63 // static |
| 60 base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt( | 64 base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt( |
| 61 int64 salt, | 65 int64 salt, |
| 62 const std::string& key) { | 66 const std::string& key) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 76 | 80 |
| 77 void DataReductionProxyAuthRequestHandler::RandBytes( | 81 void DataReductionProxyAuthRequestHandler::RandBytes( |
| 78 void* output, size_t length) { | 82 void* output, size_t length) { |
| 79 crypto::RandBytes(output, length); | 83 crypto::RandBytes(output, length); |
| 80 } | 84 } |
| 81 | 85 |
| 82 void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader( | 86 void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader( |
| 83 net::URLRequest* request, | 87 net::URLRequest* request, |
| 84 const net::ProxyServer& proxy_server, | 88 const net::ProxyServer& proxy_server, |
| 85 net::HttpRequestHeaders* request_headers) { | 89 net::HttpRequestHeaders* request_headers) { |
| 90 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 86 if (!proxy_server.is_valid()) | 91 if (!proxy_server.is_valid()) |
| 87 return; | 92 return; |
| 88 if (data_reduction_proxy_params_ && | 93 if (data_reduction_proxy_params_ && |
| 89 data_reduction_proxy_params_->IsDataReductionProxy( | 94 data_reduction_proxy_params_->IsDataReductionProxy( |
| 90 proxy_server.host_port_pair(), NULL)) { | 95 proxy_server.host_port_pair(), NULL)) { |
| 91 AddAuthorizationHeader(request_headers); | 96 AddAuthorizationHeader(request_headers); |
| 92 } | 97 } |
| 93 } | 98 } |
| 94 | 99 |
| 95 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( | 100 void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader( |
| 96 net::HttpRequestHeaders* headers) { | 101 net::HttpRequestHeaders* headers) { |
| 97 const char kChromeProxyHeader[] = "Chrome-Proxy"; | 102 const char kChromeProxyHeader[] = "Chrome-Proxy"; |
| 98 std::string header_value; | 103 std::string header_value; |
| 99 if (headers->HasHeader(kChromeProxyHeader)) { | 104 if (headers->HasHeader(kChromeProxyHeader)) { |
| 100 headers->GetHeader(kChromeProxyHeader, &header_value); | 105 headers->GetHeader(kChromeProxyHeader, &header_value); |
| 101 headers->RemoveHeader(kChromeProxyHeader); | 106 headers->RemoveHeader(kChromeProxyHeader); |
| 102 header_value += ", "; | 107 header_value += ", "; |
| 103 } | 108 } |
| 104 header_value += | 109 header_value += |
| 105 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; | 110 "ps=" + session_ + ", sid=" + credentials_ + ", v=" + version_; |
| 106 if (!client_.empty()) | 111 if (!client_.empty()) |
| 107 header_value += ", c=" + client_; | 112 header_value += ", c=" + client_; |
| 108 headers->SetHeader(kChromeProxyHeader, header_value); | 113 headers->SetHeader(kChromeProxyHeader, header_value); |
| 109 } | 114 } |
| 110 | 115 |
| 111 void DataReductionProxyAuthRequestHandler::InitAuthentication( | 116 void DataReductionProxyAuthRequestHandler::InitAuthenticationOnUI( |
| 112 const std::string& key) { | 117 const std::string& key) { |
| 113 key_ = key; | 118 key_ = key; |
| 114 int64 timestamp = | 119 int64 timestamp = |
| 115 (Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | 120 (Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; |
| 116 | 121 |
| 117 int32 rand[3]; | 122 int32 rand[3]; |
| 118 RandBytes(rand, 3 * sizeof(rand[0])); | 123 RandBytes(rand, 3 * sizeof(rand[0])); |
| 119 session_ = base::StringPrintf("%lld-%u-%u-%u", | 124 std::string session = base::StringPrintf("%lld-%u-%u-%u", |
| 120 static_cast<long long>(timestamp), | 125 static_cast<long long>(timestamp), |
| 121 rand[0], | 126 rand[0], |
| 122 rand[1], | 127 rand[1], |
| 123 rand[2]); | 128 rand[2]); |
| 124 credentials_ = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); | 129 std::string credentials = base::UTF16ToUTF8(AuthHashForSalt(timestamp, key_)); |
| 125 | 130 |
| 126 DVLOG(1) << "session: [" << session_ << "] " | 131 DVLOG(1) << "session: [" << session << "] " |
| 127 << "password: [" << credentials_ << "]"; | 132 << "password: [" << credentials << "]"; |
| 133 network_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 134 &DataReductionProxyAuthRequestHandler::InitAuthentication, |
| 135 base::Unretained(this), |
| 136 session, |
| 137 credentials)); |
| 128 } | 138 } |
| 129 | 139 |
| 130 void DataReductionProxyAuthRequestHandler::SetKey(const std::string& key) { | 140 void DataReductionProxyAuthRequestHandler::InitAuthentication( |
| 141 const std::string& session, |
| 142 const std::string& credentials) { |
| 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 144 session_ = session; |
| 145 credentials_ = credentials; |
| 146 } |
| 147 |
| 148 void DataReductionProxyAuthRequestHandler::SetKeyOnUI(const std::string& key) { |
| 131 if (!key.empty()) | 149 if (!key.empty()) |
| 132 InitAuthentication(key); | 150 InitAuthenticationOnUI(key); |
| 133 } | 151 } |
| 134 | 152 |
| 135 | 153 |
| 136 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { | 154 std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const { |
| 137 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 155 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 138 std::string key = | 156 std::string key = |
| 139 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); | 157 command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); |
| 140 #if defined(SPDY_PROXY_AUTH_VALUE) | 158 #if defined(SPDY_PROXY_AUTH_VALUE) |
| 141 if (key.empty()) | 159 if (key.empty()) |
| 142 key = SPDY_PROXY_AUTH_VALUE; | 160 key = SPDY_PROXY_AUTH_VALUE; |
| 143 #endif | 161 #endif |
| 144 return key; | 162 return key; |
| 145 } | 163 } |
| 146 | 164 |
| 147 } // namespace data_reduction_proxy | 165 } // namespace data_reduction_proxy |
| OLD | NEW |