Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: services/authenticating_url_loader/authenticating_url_loader_impl.cc

Issue 1161603003: Have the authenticating_url_loader cache token per-origin. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "services/authenticating_url_loader/authenticating_url_loader_impl.h" 5 #include "services/authenticating_url_loader/authenticating_url_loader_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "mojo/services/network/public/interfaces/network_service.mojom.h" 9 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
10 10
11 namespace mojo { 11 namespace mojo {
12 12
13 AuthenticatingURLLoaderImpl::AuthenticatingURLLoaderImpl( 13 AuthenticatingURLLoaderImpl::AuthenticatingURLLoaderImpl(
14 InterfaceRequest<AuthenticatingURLLoader> request, 14 InterfaceRequest<AuthenticatingURLLoader> request,
15 authentication::AuthenticationService* authentication_service, 15 authentication::AuthenticationService* authentication_service,
16 NetworkService* network_service, 16 NetworkService* network_service,
17 std::map<GURL, std::string>* cached_tokens,
17 const Callback<void(AuthenticatingURLLoaderImpl*)>& 18 const Callback<void(AuthenticatingURLLoaderImpl*)>&
18 connection_error_callback) 19 connection_error_callback)
19 : binding_(this, request.Pass()), 20 : binding_(this, request.Pass()),
20 authentication_service_(authentication_service), 21 authentication_service_(authentication_service),
21 network_service_(network_service), 22 network_service_(network_service),
22 connection_error_callback_(connection_error_callback), 23 connection_error_callback_(connection_error_callback),
23 request_authorization_state_(REQUEST_NOT_AUTHORIZED) { 24 request_authorization_state_(REQUEST_NOT_AUTHORIZED),
25 cached_tokens_(cached_tokens) {
24 binding_.set_error_handler(this); 26 binding_.set_error_handler(this);
25 } 27 }
26 28
27 AuthenticatingURLLoaderImpl::~AuthenticatingURLLoaderImpl() { 29 AuthenticatingURLLoaderImpl::~AuthenticatingURLLoaderImpl() {
28 } 30 }
29 31
30 void AuthenticatingURLLoaderImpl::Start( 32 void AuthenticatingURLLoaderImpl::Start(
31 URLRequestPtr request, 33 URLRequestPtr request,
32 const Callback<void(URLResponsePtr)>& callback) { 34 const Callback<void(URLResponsePtr)>& callback) {
33 // TODO(blundell): If we need to handle requests with bodies, we'll need to 35 // TODO(blundell): If we need to handle requests with bodies, we'll need to
34 // do something here. 36 // do something here.
35 if (request->body) { 37 if (request->body) {
36 LOG(ERROR) 38 LOG(ERROR)
37 << "Cannot pass a request to AuthenticatingURLLoader that has a body"; 39 << "Cannot pass a request to AuthenticatingURLLoader that has a body";
38 callback.Run(nullptr); 40 callback.Run(nullptr);
39 return; 41 return;
40 } 42 }
41 url_ = request->url; 43 url_ = GURL(request->url);
42 auto_follow_redirects_ = request->auto_follow_redirects; 44 auto_follow_redirects_ = request->auto_follow_redirects;
43 bypass_cache_ = request->bypass_cache; 45 bypass_cache_ = request->bypass_cache;
44 headers_ = request->headers.Clone(); 46 headers_ = request->headers.Clone();
45 pending_start_callback_ = callback; 47 pending_start_callback_ = callback;
48 GURL url(request->url);
blundell 2015/05/27 15:43:44 nit: unneeded, just use |url_|.
qsr 2015/05/27 16:02:26 Missed one. Thanks.
49 if (cached_tokens_->find(url_.GetOrigin()) != cached_tokens_->end()) {
50 request->headers.push_back("Authorization: Bearer " +
51 (*cached_tokens_)[url.GetOrigin()]);
52 }
46 StartNetworkRequest(request.Pass()); 53 StartNetworkRequest(request.Pass());
47 } 54 }
48 55
49 void AuthenticatingURLLoaderImpl::StartNetworkRequest(URLRequestPtr request) { 56 void AuthenticatingURLLoaderImpl::StartNetworkRequest(URLRequestPtr request) {
50 network_service_->CreateURLLoader(mojo::GetProxy(&url_loader_)); 57 network_service_->CreateURLLoader(mojo::GetProxy(&url_loader_));
51 url_loader_->Start(request.Pass(), 58 url_loader_->Start(request.Pass(),
52 base::Bind(&AuthenticatingURLLoaderImpl::OnLoadComplete, 59 base::Bind(&AuthenticatingURLLoaderImpl::OnLoadComplete,
53 base::Unretained(this))); 60 base::Unretained(this)));
54 } 61 }
55 62
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 117
111 void AuthenticatingURLLoaderImpl::OnOAuth2TokenReceived(String token, 118 void AuthenticatingURLLoaderImpl::OnOAuth2TokenReceived(String token,
112 String error) { 119 String error) {
113 if (error) { 120 if (error) {
114 LOG(ERROR) << "Error (" << error << ") while getting token"; 121 LOG(ERROR) << "Error (" << error << ") while getting token";
115 pending_start_callback_.Run(pending_response_.Pass()); 122 pending_start_callback_.Run(pending_response_.Pass());
116 return; 123 return;
117 } 124 }
118 125
119 DCHECK(token); 126 DCHECK(token);
127 (*cached_tokens_)[url_.GetOrigin()] = token;
120 token_ = token; 128 token_ = token;
121 mojo::Array<mojo::String> headers(0); 129 mojo::Array<mojo::String> headers(0);
122 if (headers_) 130 if (headers_)
123 headers = headers_.Clone(); 131 headers = headers_.Clone();
124 headers.push_back("Authorization: Bearer " + token.get()); 132 headers.push_back("Authorization: Bearer " + token.get());
125 133
126 URLRequestPtr request(mojo::URLRequest::New()); 134 URLRequestPtr request(mojo::URLRequest::New());
127 request->url = url_; 135 request->url = url_.spec();
128 request->auto_follow_redirects = auto_follow_redirects_; 136 request->auto_follow_redirects = auto_follow_redirects_;
129 request->bypass_cache = bypass_cache_; 137 request->bypass_cache = bypass_cache_;
130 request->headers = headers.Pass(); 138 request->headers = headers.Pass();
131 139
132 StartNetworkRequest(request.Pass()); 140 StartNetworkRequest(request.Pass());
133 } 141 }
134 142
135 void AuthenticatingURLLoaderImpl::SetAuthenticationService( 143 void AuthenticatingURLLoaderImpl::SetAuthenticationService(
136 authentication::AuthenticationService* authentication_service) { 144 authentication::AuthenticationService* authentication_service) {
137 authentication_service_ = authentication_service; 145 authentication_service_ = authentication_service;
138 if (authentication_service || !pending_response_) 146 if (authentication_service || !pending_response_)
139 return; 147 return;
140 148
141 // We need authentication but have no AuthenticationService. 149 // We need authentication but have no AuthenticationService.
142 DCHECK(!pending_start_callback_.is_null()); 150 DCHECK(!pending_start_callback_.is_null());
143 pending_start_callback_.Run(pending_response_.Pass()); 151 pending_start_callback_.Run(pending_response_.Pass());
144 } 152 }
145 153
146 } // namespace mojo 154 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698