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

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc

Issue 23068005: Convert UserPolicySigninService to use OAuth2TokenService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed code review feedback. Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" 5 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/signin/oauth2_token_service.h"
14 #include "google_apis/gaia/gaia_constants.h" 15 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h" 16 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h" 17 #include "google_apis/gaia/google_service_auth_error.h"
17 18
18 #if defined(OS_ANDROID) 19 #if defined(OS_ANDROID)
19 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" 20 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/oauth2_token_service.h"
21 #else 21 #else
22 #include "google_apis/gaia/oauth2_access_token_consumer.h" 22 #include "google_apis/gaia/oauth2_access_token_consumer.h"
23 #include "google_apis/gaia/oauth2_access_token_fetcher.h" 23 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
24 #endif 24 #endif
25 25
26 namespace policy { 26 namespace policy {
27 27
28 class CloudPolicyClientRegistrationHelper::TokenHelper {
Mattias Nissler (ping if slow) 2013/08/19 14:03:22 Empty base class? Why?
Andrew T Wilson (Slow) 2013/08/20 09:28:35 Because on Desktop we need either a TokenServiceHe
29 public:
30 TokenHelper() {}
31 virtual ~TokenHelper() {}
32 };
33
28 namespace { 34 namespace {
29 35
30 // OAuth2 scope for the userinfo service. 36 // OAuth2 scope for the userinfo service.
31 const char kServiceScopeGetUserInfo[] = 37 const char kServiceScopeGetUserInfo[] =
32 "https://www.googleapis.com/auth/userinfo.email"; 38 "https://www.googleapis.com/auth/userinfo.email";
33 39
34 // The key under which the hosted-domain value is stored in the UserInfo 40 // The key under which the hosted-domain value is stored in the UserInfo
35 // response. 41 // response.
36 const char kGetHostedDomainKey[] = "hd"; 42 const char kGetHostedDomainKey[] = "hd";
37 43
38 typedef base::Callback<void(const std::string&)> StringCallback; 44 typedef base::Callback<void(const std::string&)> StringCallback;
39 45
40 } // namespace 46 // This class fetches an OAuth2 token scoped for the userinfo and DM services.
47 // On Android, we use a special API to allow us to fetch a token for an account
48 // that is not yet logged in to allow fetching the token before the sign-in
49 // process is finished.
50 class TokenServiceHelper
51 : public CloudPolicyClientRegistrationHelper::TokenHelper,
52 public OAuth2TokenService::Consumer {
53 public:
54 TokenServiceHelper();
41 55
56 void FetchAccessToken(
42 #if defined(OS_ANDROID) 57 #if defined(OS_ANDROID)
43 58 // TODO(atwilson): Remove this when StartRequestForUsername() is merged
44 // This class fetches an OAuth2 token scoped for the userinfo and DM services. 59 // into the base OAuth2TokenService class.
45 // The AccountManager is used to mint the token on the Java side, given the 60 AndroidProfileOAuth2TokenService* token_service,
46 // username of an account that is known to exist on the device. 61 #else
47 // This allows fetching the token before the sign-in process is finished. 62 OAuth2TokenService* token_service,
48 class CloudPolicyClientRegistrationHelper::TokenHelperAndroid 63 #endif
49 : public OAuth2TokenService::Consumer { 64 const std::string& username,
50 public: 65 const StringCallback& callback);
51 TokenHelperAndroid();
52
53 void FetchAccessToken(AndroidProfileOAuth2TokenService* token_service,
54 const std::string& username,
55 const StringCallback& callback);
56 66
57 private: 67 private:
58 // OAuth2TokenService::Consumer implementation: 68 // OAuth2TokenService::Consumer implementation:
59 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 69 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
60 const std::string& access_token, 70 const std::string& access_token,
61 const base::Time& expiration_time) OVERRIDE; 71 const base::Time& expiration_time) OVERRIDE;
62 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 72 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
63 const GoogleServiceAuthError& error) OVERRIDE; 73 const GoogleServiceAuthError& error) OVERRIDE;
64 74
65 StringCallback callback_; 75 StringCallback callback_;
66 scoped_ptr<OAuth2TokenService::Request> token_request_; 76 scoped_ptr<OAuth2TokenService::Request> token_request_;
67 }; 77 };
68 78
69 CloudPolicyClientRegistrationHelper::TokenHelperAndroid::TokenHelperAndroid() {} 79 TokenServiceHelper::TokenServiceHelper() {}
70 80
71 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::FetchAccessToken( 81 void TokenServiceHelper::FetchAccessToken(
82 #if defined(OS_ANDROID)
72 AndroidProfileOAuth2TokenService* token_service, 83 AndroidProfileOAuth2TokenService* token_service,
84 #else
85 OAuth2TokenService* token_service,
86 #endif
73 const std::string& username, 87 const std::string& username,
74 const StringCallback& callback) { 88 const StringCallback& callback) {
89 DCHECK(!token_request_);
90 // Either the caller must supply a username, or the user must be signed in
91 // already.
92 DCHECK(!username.empty() || token_service->RefreshTokenIsAvailable());
75 callback_ = callback; 93 callback_ = callback;
76 94
77 OAuth2TokenService::ScopeSet scopes; 95 OAuth2TokenService::ScopeSet scopes;
78 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); 96 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth);
79 scopes.insert(kServiceScopeGetUserInfo); 97 scopes.insert(kServiceScopeGetUserInfo);
80 98
99 #if defined(OS_ANDROID)
81 token_request_ = 100 token_request_ =
82 token_service->StartRequestForUsername(username, scopes, this); 101 token_service->StartRequestForUsername(username, scopes, this);
102 #else
103 token_request_ = token_service->StartRequest(scopes, this);
104 #endif
83 } 105 }
84 106
85 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess( 107 void TokenServiceHelper::OnGetTokenSuccess(
86 const OAuth2TokenService::Request* request, 108 const OAuth2TokenService::Request* request,
87 const std::string& access_token, 109 const std::string& access_token,
88 const base::Time& expiration_time) { 110 const base::Time& expiration_time) {
89 DCHECK_EQ(token_request_.get(), request); 111 DCHECK_EQ(token_request_.get(), request);
90 callback_.Run(access_token); 112 callback_.Run(access_token);
91 } 113 }
92 114
93 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenFailure( 115 void TokenServiceHelper::OnGetTokenFailure(
94 const OAuth2TokenService::Request* request, 116 const OAuth2TokenService::Request* request,
95 const GoogleServiceAuthError& error) { 117 const GoogleServiceAuthError& error) {
96 DCHECK_EQ(token_request_.get(), request); 118 DCHECK_EQ(token_request_.get(), request);
97 callback_.Run(""); 119 callback_.Run("");
98 } 120 }
99 121
100 #else 122 #if !defined(OS_ANDROID)
101
102 // This class fetches the OAuth2 token scoped for the userinfo and DM services. 123 // This class fetches the OAuth2 token scoped for the userinfo and DM services.
103 // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token 124 // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token
104 // that can be used to authorize that request. 125 // that can be used to authorize that request. This class is not needed on
105 class CloudPolicyClientRegistrationHelper::TokenHelper 126 // Android because we can use OAuth2TokenService to fetch tokens for accounts
106 : public OAuth2AccessTokenConsumer { 127 // even before they are signed in.
128 class LoginTokenHelper
129 : public CloudPolicyClientRegistrationHelper::TokenHelper,
130 public OAuth2AccessTokenConsumer {
107 public: 131 public:
108 TokenHelper(); 132 LoginTokenHelper();
109 133
110 void FetchAccessToken(const std::string& login_refresh_token, 134 void FetchAccessToken(const std::string& login_refresh_token,
111 net::URLRequestContextGetter* context, 135 net::URLRequestContextGetter* context,
112 const StringCallback& callback); 136 const StringCallback& callback);
113 137
114 private: 138 private:
115 // OAuth2AccessTokenConsumer implementation: 139 // OAuth2AccessTokenConsumer implementation:
116 virtual void OnGetTokenSuccess(const std::string& access_token, 140 virtual void OnGetTokenSuccess(const std::string& access_token,
117 const base::Time& expiration_time) OVERRIDE; 141 const base::Time& expiration_time) OVERRIDE;
118 virtual void OnGetTokenFailure( 142 virtual void OnGetTokenFailure(
119 const GoogleServiceAuthError& error) OVERRIDE; 143 const GoogleServiceAuthError& error) OVERRIDE;
120 144
121 StringCallback callback_; 145 StringCallback callback_;
122 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; 146 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
123 }; 147 };
124 148
125 CloudPolicyClientRegistrationHelper::TokenHelper::TokenHelper() {} 149 LoginTokenHelper::LoginTokenHelper() {}
126 150
127 void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken( 151 void LoginTokenHelper::FetchAccessToken(
128 const std::string& login_refresh_token, 152 const std::string& login_refresh_token,
129 net::URLRequestContextGetter* context, 153 net::URLRequestContextGetter* context,
130 const StringCallback& callback) { 154 const StringCallback& callback) {
155 DCHECK(!oauth2_access_token_fetcher_);
131 callback_ = callback; 156 callback_ = callback;
132 157
133 // Start fetching an OAuth2 access token for the device management and 158 // Start fetching an OAuth2 access token for the device management and
134 // userinfo services. 159 // userinfo services.
135 oauth2_access_token_fetcher_.reset( 160 oauth2_access_token_fetcher_.reset(
136 new OAuth2AccessTokenFetcher(this, context)); 161 new OAuth2AccessTokenFetcher(this, context));
137 std::vector<std::string> scopes; 162 std::vector<std::string> scopes;
138 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); 163 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth);
139 scopes.push_back(kServiceScopeGetUserInfo); 164 scopes.push_back(kServiceScopeGetUserInfo);
140 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); 165 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
141 oauth2_access_token_fetcher_->Start( 166 oauth2_access_token_fetcher_->Start(
142 gaia_urls->oauth2_chrome_client_id(), 167 gaia_urls->oauth2_chrome_client_id(),
143 gaia_urls->oauth2_chrome_client_secret(), 168 gaia_urls->oauth2_chrome_client_secret(),
144 login_refresh_token, 169 login_refresh_token,
145 scopes); 170 scopes);
146 } 171 }
147 172
148 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenSuccess( 173 void LoginTokenHelper::OnGetTokenSuccess(const std::string& access_token,
149 const std::string& access_token, 174 const base::Time& expiration_time) {
150 const base::Time& expiration_time) {
151 callback_.Run(access_token); 175 callback_.Run(access_token);
152 } 176 }
153 177
154 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenFailure( 178 void LoginTokenHelper::OnGetTokenFailure(const GoogleServiceAuthError& error) {
155 const GoogleServiceAuthError& error) {
156 callback_.Run(""); 179 callback_.Run("");
157 } 180 }
158 181
159 #endif 182 #endif
160 183
184 } // namespace
185
161 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper( 186 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper(
162 net::URLRequestContextGetter* context, 187 net::URLRequestContextGetter* context,
163 CloudPolicyClient* client, 188 CloudPolicyClient* client,
164 bool should_force_load_policy, 189 bool should_force_load_policy,
165 enterprise_management::DeviceRegisterRequest::Type registration_type) 190 enterprise_management::DeviceRegisterRequest::Type registration_type)
166 : context_(context), 191 : context_(context),
167 client_(client), 192 client_(client),
168 should_force_load_policy_(should_force_load_policy), 193 should_force_load_policy_(should_force_load_policy),
169 registration_type_(registration_type) { 194 registration_type_(registration_type) {
170 DCHECK(context_); 195 DCHECK(context_);
171 DCHECK(client_); 196 DCHECK(client_);
172 } 197 }
173 198
174 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { 199 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() {
175 // Clean up any pending observers in case the browser is shutdown while 200 // Clean up any pending observers in case the browser is shutdown while
176 // trying to register for policy. 201 // trying to register for policy.
177 if (client_) 202 if (client_)
178 client_->RemoveObserver(this); 203 client_->RemoveObserver(this);
179 } 204 }
180 205
181 #if defined(OS_ANDROID)
182 206
183 void CloudPolicyClientRegistrationHelper::StartRegistration( 207 void CloudPolicyClientRegistrationHelper::StartRegistration(
208 #if defined(OS_ANDROID)
184 AndroidProfileOAuth2TokenService* token_service, 209 AndroidProfileOAuth2TokenService* token_service,
210 #else
211 OAuth2TokenService* token_service,
212 #endif
185 const std::string& username, 213 const std::string& username,
186 const base::Closure& callback) { 214 const base::Closure& callback) {
187 DVLOG(1) << "Starting registration process with username"; 215 DVLOG(1) << "Starting registration process with username";
188 DCHECK(!client_->is_registered()); 216 DCHECK(!client_->is_registered());
189 callback_ = callback; 217 callback_ = callback;
190 client_->AddObserver(this); 218 client_->AddObserver(this);
191 219
192 token_helper_.reset(new TokenHelperAndroid()); 220 scoped_ptr<TokenServiceHelper> token_service_helper(new TokenServiceHelper());
193 token_helper_->FetchAccessToken( 221 token_service_helper->FetchAccessToken(
194 token_service, 222 token_service,
195 username, 223 username,
196 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, 224 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
197 base::Unretained(this))); 225 base::Unretained(this)));
226 token_helper_ = token_service_helper.PassAs<TokenHelper>();
198 } 227 }
199 228
200 #else 229 #if !defined(OS_ANDROID)
201
202 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( 230 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
203 const std::string& login_refresh_token, 231 const std::string& login_refresh_token,
204 const base::Closure& callback) { 232 const base::Closure& callback) {
205 DVLOG(1) << "Starting registration process with login token"; 233 DVLOG(1) << "Starting registration process with login token";
206 DCHECK(!client_->is_registered()); 234 DCHECK(!client_->is_registered());
207 callback_ = callback; 235 callback_ = callback;
208 client_->AddObserver(this); 236 client_->AddObserver(this);
209 237
210 token_helper_.reset(new TokenHelper()); 238 scoped_ptr<LoginTokenHelper> login_token_helper(new LoginTokenHelper());
211 token_helper_->FetchAccessToken( 239 login_token_helper->FetchAccessToken(
212 login_refresh_token, 240 login_refresh_token,
213 context_, 241 context_,
214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, 242 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
215 base::Unretained(this))); 243 base::Unretained(this)));
244 token_helper_ = login_token_helper.Pass();
216 } 245 }
217
218 #endif 246 #endif
219 247
220 void CloudPolicyClientRegistrationHelper::OnTokenFetched( 248 void CloudPolicyClientRegistrationHelper::OnTokenFetched(
221 const std::string& access_token) { 249 const std::string& access_token) {
222 token_helper_.reset(); 250 token_helper_.reset();
223 251
224 if (access_token.empty()) { 252 if (access_token.empty()) {
225 DLOG(WARNING) << "Could not fetch access token for " 253 DLOG(WARNING) << "Could not fetch access token for "
226 << GaiaConstants::kDeviceManagementServiceOAuth; 254 << GaiaConstants::kDeviceManagementServiceOAuth;
227 RequestCompleted(); 255 RequestCompleted();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 void CloudPolicyClientRegistrationHelper::RequestCompleted() { 319 void CloudPolicyClientRegistrationHelper::RequestCompleted() {
292 if (client_) { 320 if (client_) {
293 client_->RemoveObserver(this); 321 client_->RemoveObserver(this);
294 // |client_| may be freed by the callback so clear it now. 322 // |client_| may be freed by the callback so clear it now.
295 client_ = NULL; 323 client_ = NULL;
296 callback_.Run(); 324 callback_.Run();
297 } 325 }
298 } 326 }
299 327
300 } // namespace policy 328 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698