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

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

Issue 109743002: Move policy code into components/policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: moar fixes Created 7 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/logging.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
18 #include "net/url_request/url_request_context_getter.h"
19
20 #if !defined(OS_ANDROID)
21 #include "google_apis/gaia/oauth2_access_token_consumer.h"
22 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
23 #endif
24
25 namespace policy {
26
27 // OAuth2 scope for the userinfo service.
28 const char kServiceScopeGetUserInfo[] =
29 "https://www.googleapis.com/auth/userinfo.email";
30
31 // The key under which the hosted-domain value is stored in the UserInfo
32 // response.
33 const char kGetHostedDomainKey[] = "hd";
34
35 typedef base::Callback<void(const std::string&)> StringCallback;
36
37 // This class fetches an OAuth2 token scoped for the userinfo and DM services.
38 // On Android, we use a special API to allow us to fetch a token for an account
39 // that is not yet logged in to allow fetching the token before the sign-in
40 // process is finished.
41 class CloudPolicyClientRegistrationHelper::TokenServiceHelper
42 : public OAuth2TokenService::Consumer {
43 public:
44 TokenServiceHelper();
45
46 void FetchAccessToken(
47 OAuth2TokenService* token_service,
48 const std::string& username,
49 const StringCallback& callback);
50
51 private:
52 // OAuth2TokenService::Consumer implementation:
53 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
54 const std::string& access_token,
55 const base::Time& expiration_time) OVERRIDE;
56 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
57 const GoogleServiceAuthError& error) OVERRIDE;
58
59 StringCallback callback_;
60 scoped_ptr<OAuth2TokenService::Request> token_request_;
61 };
62
63 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {}
64
65 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken(
66 OAuth2TokenService* token_service,
67 const std::string& account_id,
68 const StringCallback& callback) {
69 DCHECK(!token_request_);
70 // Either the caller must supply a username, or the user must be signed in
71 // already.
72 DCHECK(!account_id.empty());
73 DCHECK(token_service->RefreshTokenIsAvailable(account_id));
74
75 callback_ = callback;
76
77 OAuth2TokenService::ScopeSet scopes;
78 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth);
79 scopes.insert(kServiceScopeGetUserInfo);
80 token_request_ = token_service->StartRequest(account_id, scopes, this);
81 }
82
83 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess(
84 const OAuth2TokenService::Request* request,
85 const std::string& access_token,
86 const base::Time& expiration_time) {
87 DCHECK_EQ(token_request_.get(), request);
88 callback_.Run(access_token);
89 }
90
91 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenFailure(
92 const OAuth2TokenService::Request* request,
93 const GoogleServiceAuthError& error) {
94 DCHECK_EQ(token_request_.get(), request);
95 callback_.Run("");
96 }
97
98 #if !defined(OS_ANDROID)
99 // This class fetches the OAuth2 token scoped for the userinfo and DM services.
100 // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token
101 // that can be used to authorize that request. This class is not needed on
102 // Android because we can use OAuth2TokenService to fetch tokens for accounts
103 // even before they are signed in.
104 class CloudPolicyClientRegistrationHelper::LoginTokenHelper
105 : public OAuth2AccessTokenConsumer {
106 public:
107 LoginTokenHelper();
108
109 void FetchAccessToken(const std::string& login_refresh_token,
110 net::URLRequestContextGetter* context,
111 const StringCallback& callback);
112
113 private:
114 // OAuth2AccessTokenConsumer implementation:
115 virtual void OnGetTokenSuccess(const std::string& access_token,
116 const base::Time& expiration_time) OVERRIDE;
117 virtual void OnGetTokenFailure(
118 const GoogleServiceAuthError& error) OVERRIDE;
119
120 StringCallback callback_;
121 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
122 };
123
124 CloudPolicyClientRegistrationHelper::LoginTokenHelper::LoginTokenHelper() {}
125
126 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::FetchAccessToken(
127 const std::string& login_refresh_token,
128 net::URLRequestContextGetter* context,
129 const StringCallback& callback) {
130 DCHECK(!oauth2_access_token_fetcher_);
131 callback_ = callback;
132
133 // Start fetching an OAuth2 access token for the device management and
134 // userinfo services.
135 oauth2_access_token_fetcher_.reset(
136 new OAuth2AccessTokenFetcher(this, context));
137 std::vector<std::string> scopes;
138 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth);
139 scopes.push_back(kServiceScopeGetUserInfo);
140 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
141 oauth2_access_token_fetcher_->Start(
142 gaia_urls->oauth2_chrome_client_id(),
143 gaia_urls->oauth2_chrome_client_secret(),
144 login_refresh_token,
145 scopes);
146 }
147
148 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenSuccess(
149 const std::string& access_token,
150 const base::Time& expiration_time) {
151 callback_.Run(access_token);
152 }
153
154 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenFailure(
155 const GoogleServiceAuthError& error) {
156 callback_.Run("");
157 }
158
159 #endif
160
161 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper(
162 CloudPolicyClient* client,
163 bool should_force_load_policy,
164 enterprise_management::DeviceRegisterRequest::Type registration_type)
165 : context_(client->GetRequestContext()),
166 client_(client),
167 should_force_load_policy_(should_force_load_policy),
168 registration_type_(registration_type) {
169 DCHECK(context_);
170 DCHECK(client_);
171 }
172
173 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() {
174 // Clean up any pending observers in case the browser is shutdown while
175 // trying to register for policy.
176 if (client_)
177 client_->RemoveObserver(this);
178 }
179
180
181 void CloudPolicyClientRegistrationHelper::StartRegistration(
182 OAuth2TokenService* token_service,
183 const std::string& account_id,
184 const base::Closure& callback) {
185 DVLOG(1) << "Starting registration process with username";
186 DCHECK(!client_->is_registered());
187 callback_ = callback;
188 client_->AddObserver(this);
189
190 token_service_helper_.reset(new TokenServiceHelper());
191 token_service_helper_->FetchAccessToken(
192 token_service,
193 account_id,
194 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
195 base::Unretained(this)));
196 }
197
198 #if !defined(OS_ANDROID)
199 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
200 const std::string& login_refresh_token,
201 const base::Closure& callback) {
202 DVLOG(1) << "Starting registration process with login token";
203 DCHECK(!client_->is_registered());
204 callback_ = callback;
205 client_->AddObserver(this);
206
207 login_token_helper_.reset(
208 new CloudPolicyClientRegistrationHelper::LoginTokenHelper());
209 login_token_helper_->FetchAccessToken(
210 login_refresh_token,
211 context_,
212 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
213 base::Unretained(this)));
214 }
215 #endif
216
217 void CloudPolicyClientRegistrationHelper::OnTokenFetched(
218 const std::string& access_token) {
219 #if !defined(OS_ANDROID)
220 login_token_helper_.reset();
221 #endif
222 token_service_helper_.reset();
223
224 if (access_token.empty()) {
225 DLOG(WARNING) << "Could not fetch access token for "
226 << GaiaConstants::kDeviceManagementServiceOAuth;
227 RequestCompleted();
228 return;
229 }
230
231 // Cache the access token to be used after the GetUserInfo call.
232 oauth_access_token_ = access_token;
233 DVLOG(1) << "Fetched new scoped OAuth token:" << oauth_access_token_;
234 // Now we've gotten our access token - contact GAIA to see if this is a
235 // hosted domain.
236 user_info_fetcher_.reset(new UserInfoFetcher(this, context_));
237 user_info_fetcher_->Start(oauth_access_token_);
238 }
239
240 void CloudPolicyClientRegistrationHelper::OnGetUserInfoFailure(
241 const GoogleServiceAuthError& error) {
242 DVLOG(1) << "Failed to fetch user info from GAIA: " << error.state();
243 user_info_fetcher_.reset();
244 RequestCompleted();
245 }
246
247 void CloudPolicyClientRegistrationHelper::OnGetUserInfoSuccess(
248 const base::DictionaryValue* data) {
249 user_info_fetcher_.reset();
250 if (!data->HasKey(kGetHostedDomainKey) && !should_force_load_policy_) {
251 DVLOG(1) << "User not from a hosted domain - skipping registration";
252 RequestCompleted();
253 return;
254 }
255 DVLOG(1) << "Registering CloudPolicyClient for user from hosted domain";
256 // The user is from a hosted domain, so it's OK to register the
257 // CloudPolicyClient and make requests to DMServer.
258 if (client_->is_registered()) {
259 // Client should not be registered yet.
260 NOTREACHED();
261 RequestCompleted();
262 return;
263 }
264
265 // Kick off registration of the CloudPolicyClient with our newly minted
266 // oauth_access_token_.
267 client_->Register(registration_type_, oauth_access_token_,
268 std::string(), false, std::string());
269 }
270
271 void CloudPolicyClientRegistrationHelper::OnPolicyFetched(
272 CloudPolicyClient* client) {
273 // Ignored.
274 }
275
276 void CloudPolicyClientRegistrationHelper::OnRegistrationStateChanged(
277 CloudPolicyClient* client) {
278 DVLOG(1) << "Client registration succeeded";
279 DCHECK_EQ(client, client_);
280 DCHECK(client->is_registered());
281 RequestCompleted();
282 }
283
284 void CloudPolicyClientRegistrationHelper::OnClientError(
285 CloudPolicyClient* client) {
286 DVLOG(1) << "Client registration failed";
287 DCHECK_EQ(client, client_);
288 RequestCompleted();
289 }
290
291 void CloudPolicyClientRegistrationHelper::RequestCompleted() {
292 if (client_) {
293 client_->RemoveObserver(this);
294 // |client_| may be freed by the callback so clear it now.
295 client_ = NULL;
296 callback_.Run();
297 }
298 }
299
300 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698