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 "remoting/host/token_validator_base.h" | 5 #include "remoting/host/token_validator_base.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 | 78 |
79 const GURL& TokenValidatorBase::token_url() const { | 79 const GURL& TokenValidatorBase::token_url() const { |
80 return third_party_auth_config_.token_url; | 80 return third_party_auth_config_.token_url; |
81 } | 81 } |
82 | 82 |
83 const std::string& TokenValidatorBase::token_scope() const { | 83 const std::string& TokenValidatorBase::token_scope() const { |
84 return token_scope_; | 84 return token_scope_; |
85 } | 85 } |
86 | 86 |
87 // URLFetcherDelegate interface. | 87 // URLFetcherDelegate interface. |
88 void TokenValidatorBase::OnResponseStarted(net::URLRequest* source) { | 88 void TokenValidatorBase::OnResponseStarted(net::URLRequest* source, |
89 int net_error) { | |
Sergey Ulanov
2016/09/06 17:43:23
nit: maybe call this parameter net_result - it's n
| |
90 DCHECK_NE(net::ERR_IO_PENDING, net_error); | |
Sergey Ulanov
2016/09/06 17:43:23
nit: please swap these two parameters to make code
maksims (do not use this acc)
2016/09/21 05:17:07
Well, I've always thought it should have been othe
Sergey Ulanov
2016/09/21 07:13:50
It matters for EXPECT_EQ/EXPECT_NE from gtest, whe
| |
89 DCHECK_EQ(request_.get(), source); | 91 DCHECK_EQ(request_.get(), source); |
90 | 92 |
91 int bytes_read = 0; | 93 int bytes_read = request_->Read(buffer_.get(), kBufferSize); |
Sergey Ulanov
2016/09/06 17:43:23
Should we even try reading the response when net_e
maksims (do not use this acc)
2016/09/21 05:17:07
Done.
| |
92 request_->Read(buffer_.get(), kBufferSize, &bytes_read); | |
93 OnReadCompleted(request_.get(), bytes_read); | 94 OnReadCompleted(request_.get(), bytes_read); |
94 } | 95 } |
95 | 96 |
96 void TokenValidatorBase::OnReadCompleted(net::URLRequest* source, | 97 void TokenValidatorBase::OnReadCompleted(net::URLRequest* source, |
97 int bytes_read) { | 98 int bytes_read) { |
Sergey Ulanov
2016/09/06 17:43:23
rename this parameter to result or net_result and
maksims (do not use this acc)
2016/09/21 05:17:07
Done.
| |
98 DCHECK_EQ(request_.get(), source); | 99 DCHECK_EQ(request_.get(), source); |
99 | 100 |
100 do { | 101 while (bytes_read > 0) { |
101 if (!request_->status().is_success() || bytes_read <= 0) | 102 data_.append(buffer_->data(), bytes_read); |
102 break; | 103 bytes_read = request_->Read(buffer_.get(), kBufferSize); |
104 } | |
103 | 105 |
104 data_.append(buffer_->data(), bytes_read); | 106 int net_error = bytes_read; |
105 } while (request_->Read(buffer_.get(), kBufferSize, &bytes_read)); | 107 if (net_error == net::ERR_IO_PENDING) |
108 return; | |
106 | 109 |
107 const net::URLRequestStatus status = request_->status(); | 110 retrying_request_ = false; |
108 | 111 std::string shared_token = ProcessResponse(net_error); |
109 if (!status.is_io_pending()) { | 112 request_.reset(); |
110 retrying_request_ = false; | 113 on_token_validated_.Run(shared_token); |
111 std::string shared_token = ProcessResponse(); | |
112 request_.reset(); | |
113 on_token_validated_.Run(shared_token); | |
114 } | |
115 } | 114 } |
116 | 115 |
117 void TokenValidatorBase::OnReceivedRedirect( | 116 void TokenValidatorBase::OnReceivedRedirect( |
118 net::URLRequest* request, | 117 net::URLRequest* request, |
119 const net::RedirectInfo& redirect_info, | 118 const net::RedirectInfo& redirect_info, |
120 bool* defer_redirect) { | 119 bool* defer_redirect) { |
121 if (!retrying_request_ && redirect_info.new_method == "GET" && | 120 if (!retrying_request_ && redirect_info.new_method == "GET" && |
122 redirect_info.new_url == third_party_auth_config_.token_validation_url) { | 121 redirect_info.new_url == third_party_auth_config_.token_validation_url) { |
123 // A sequence of redirects caused the original POST request to become a GET | 122 // A sequence of redirects caused the original POST request to become a GET |
124 // request for this URL. Cancel the request, and re-submit the POST request. | 123 // request for this URL. Cancel the request, and re-submit the POST request. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 } | 182 } |
184 request_->ContinueWithCertificate(nullptr, nullptr); | 183 request_->ContinueWithCertificate(nullptr, nullptr); |
185 } | 184 } |
186 } | 185 } |
187 | 186 |
188 bool TokenValidatorBase::IsValidScope(const std::string& token_scope) { | 187 bool TokenValidatorBase::IsValidScope(const std::string& token_scope) { |
189 // TODO(rmsousa): Deal with reordering/subsets/supersets/aliases/etc. | 188 // TODO(rmsousa): Deal with reordering/subsets/supersets/aliases/etc. |
190 return token_scope == token_scope_; | 189 return token_scope == token_scope_; |
191 } | 190 } |
192 | 191 |
193 std::string TokenValidatorBase::ProcessResponse() { | 192 std::string TokenValidatorBase::ProcessResponse(int net_error) { |
194 // Verify that we got a successful response. | 193 // Verify that we got a successful response. |
195 net::URLRequestStatus status = request_->status(); | 194 if (net_error != net::OK) { |
196 if (!status.is_success()) { | 195 LOG(ERROR) << "Error validating token, err=" << net_error; |
197 LOG(ERROR) << "Error validating token, status=" << status.status() | |
198 << " err=" << status.error(); | |
199 return std::string(); | 196 return std::string(); |
200 } | 197 } |
201 | 198 |
202 int response = request_->GetResponseCode(); | 199 int response = request_->GetResponseCode(); |
203 if (response != 200) { | 200 if (response != 200) { |
204 LOG(ERROR) | 201 LOG(ERROR) |
205 << "Error " << response << " validating token: '" << data_ << "'"; | 202 << "Error " << response << " validating token: '" << data_ << "'"; |
206 return std::string(); | 203 return std::string(); |
207 } | 204 } |
208 | 205 |
(...skipping 13 matching lines...) Expand all Loading... | |
222 return std::string(); | 219 return std::string(); |
223 } | 220 } |
224 | 221 |
225 std::string shared_secret; | 222 std::string shared_secret; |
226 // Everything is valid, so return the shared secret to the caller. | 223 // Everything is valid, so return the shared secret to the caller. |
227 dict->GetStringWithoutPathExpansion("access_token", &shared_secret); | 224 dict->GetStringWithoutPathExpansion("access_token", &shared_secret); |
228 return shared_secret; | 225 return shared_secret; |
229 } | 226 } |
230 | 227 |
231 } // namespace remoting | 228 } // namespace remoting |
OLD | NEW |