OLD | NEW |
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 namespace { | |
29 | |
30 // OAuth2 scope for the userinfo service. | 28 // OAuth2 scope for the userinfo service. |
31 const char kServiceScopeGetUserInfo[] = | 29 const char kServiceScopeGetUserInfo[] = |
32 "https://www.googleapis.com/auth/userinfo.email"; | 30 "https://www.googleapis.com/auth/userinfo.email"; |
33 | 31 |
34 // The key under which the hosted-domain value is stored in the UserInfo | 32 // The key under which the hosted-domain value is stored in the UserInfo |
35 // response. | 33 // response. |
36 const char kGetHostedDomainKey[] = "hd"; | 34 const char kGetHostedDomainKey[] = "hd"; |
37 | 35 |
38 typedef base::Callback<void(const std::string&)> StringCallback; | 36 typedef base::Callback<void(const std::string&)> StringCallback; |
39 | 37 |
40 } // namespace | |
41 | |
42 #if defined(OS_ANDROID) | |
43 | |
44 // This class fetches an OAuth2 token scoped for the userinfo and DM services. | 38 // This class fetches an OAuth2 token scoped for the userinfo and DM services. |
45 // The AccountManager is used to mint the token on the Java side, given the | 39 // On Android, we use a special API to allow us to fetch a token for an account |
46 // username of an account that is known to exist on the device. | 40 // that is not yet logged in to allow fetching the token before the sign-in |
47 // This allows fetching the token before the sign-in process is finished. | 41 // process is finished. |
48 class CloudPolicyClientRegistrationHelper::TokenHelperAndroid | 42 class CloudPolicyClientRegistrationHelper::TokenServiceHelper |
49 : public OAuth2TokenService::Consumer { | 43 : public OAuth2TokenService::Consumer { |
50 public: | 44 public: |
51 TokenHelperAndroid(); | 45 TokenServiceHelper(); |
52 | 46 |
53 void FetchAccessToken(AndroidProfileOAuth2TokenService* token_service, | 47 void FetchAccessToken( |
54 const std::string& username, | 48 #if defined(OS_ANDROID) |
55 const StringCallback& callback); | 49 // TODO(atwilson): Remove this when StartRequestForUsername() is merged |
| 50 // into the base OAuth2TokenService class. |
| 51 AndroidProfileOAuth2TokenService* token_service, |
| 52 #else |
| 53 OAuth2TokenService* token_service, |
| 54 #endif |
| 55 const std::string& username, |
| 56 const StringCallback& callback); |
56 | 57 |
57 private: | 58 private: |
58 // OAuth2TokenService::Consumer implementation: | 59 // OAuth2TokenService::Consumer implementation: |
59 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 60 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
60 const std::string& access_token, | 61 const std::string& access_token, |
61 const base::Time& expiration_time) OVERRIDE; | 62 const base::Time& expiration_time) OVERRIDE; |
62 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 63 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
63 const GoogleServiceAuthError& error) OVERRIDE; | 64 const GoogleServiceAuthError& error) OVERRIDE; |
64 | 65 |
65 StringCallback callback_; | 66 StringCallback callback_; |
66 scoped_ptr<OAuth2TokenService::Request> token_request_; | 67 scoped_ptr<OAuth2TokenService::Request> token_request_; |
67 }; | 68 }; |
68 | 69 |
69 CloudPolicyClientRegistrationHelper::TokenHelperAndroid::TokenHelperAndroid() {} | 70 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {} |
70 | 71 |
71 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::FetchAccessToken( | 72 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken( |
| 73 #if defined(OS_ANDROID) |
72 AndroidProfileOAuth2TokenService* token_service, | 74 AndroidProfileOAuth2TokenService* token_service, |
| 75 #else |
| 76 OAuth2TokenService* token_service, |
| 77 #endif |
73 const std::string& username, | 78 const std::string& username, |
74 const StringCallback& callback) { | 79 const StringCallback& callback) { |
| 80 DCHECK(!token_request_); |
| 81 // Either the caller must supply a username, or the user must be signed in |
| 82 // already. |
| 83 DCHECK(!username.empty() || token_service->RefreshTokenIsAvailable()); |
75 callback_ = callback; | 84 callback_ = callback; |
76 | 85 |
77 OAuth2TokenService::ScopeSet scopes; | 86 OAuth2TokenService::ScopeSet scopes; |
78 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 87 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
79 scopes.insert(kServiceScopeGetUserInfo); | 88 scopes.insert(kServiceScopeGetUserInfo); |
80 | 89 |
| 90 #if defined(OS_ANDROID) |
81 token_request_ = | 91 token_request_ = |
82 token_service->StartRequestForUsername(username, scopes, this); | 92 token_service->StartRequestForUsername(username, scopes, this); |
| 93 #else |
| 94 token_request_ = token_service->StartRequest(scopes, this); |
| 95 #endif |
83 } | 96 } |
84 | 97 |
85 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess( | 98 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess( |
86 const OAuth2TokenService::Request* request, | 99 const OAuth2TokenService::Request* request, |
87 const std::string& access_token, | 100 const std::string& access_token, |
88 const base::Time& expiration_time) { | 101 const base::Time& expiration_time) { |
89 DCHECK_EQ(token_request_.get(), request); | 102 DCHECK_EQ(token_request_.get(), request); |
90 callback_.Run(access_token); | 103 callback_.Run(access_token); |
91 } | 104 } |
92 | 105 |
93 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenFailure( | 106 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenFailure( |
94 const OAuth2TokenService::Request* request, | 107 const OAuth2TokenService::Request* request, |
95 const GoogleServiceAuthError& error) { | 108 const GoogleServiceAuthError& error) { |
96 DCHECK_EQ(token_request_.get(), request); | 109 DCHECK_EQ(token_request_.get(), request); |
97 callback_.Run(""); | 110 callback_.Run(""); |
98 } | 111 } |
99 | 112 |
100 #else | 113 #if !defined(OS_ANDROID) |
101 | |
102 // This class fetches the OAuth2 token scoped for the userinfo and DM services. | 114 // 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 | 115 // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token |
104 // that can be used to authorize that request. | 116 // that can be used to authorize that request. This class is not needed on |
105 class CloudPolicyClientRegistrationHelper::TokenHelper | 117 // Android because we can use OAuth2TokenService to fetch tokens for accounts |
| 118 // even before they are signed in. |
| 119 class CloudPolicyClientRegistrationHelper::LoginTokenHelper |
106 : public OAuth2AccessTokenConsumer { | 120 : public OAuth2AccessTokenConsumer { |
107 public: | 121 public: |
108 TokenHelper(); | 122 LoginTokenHelper(); |
109 | 123 |
110 void FetchAccessToken(const std::string& login_refresh_token, | 124 void FetchAccessToken(const std::string& login_refresh_token, |
111 net::URLRequestContextGetter* context, | 125 net::URLRequestContextGetter* context, |
112 const StringCallback& callback); | 126 const StringCallback& callback); |
113 | 127 |
114 private: | 128 private: |
115 // OAuth2AccessTokenConsumer implementation: | 129 // OAuth2AccessTokenConsumer implementation: |
116 virtual void OnGetTokenSuccess(const std::string& access_token, | 130 virtual void OnGetTokenSuccess(const std::string& access_token, |
117 const base::Time& expiration_time) OVERRIDE; | 131 const base::Time& expiration_time) OVERRIDE; |
118 virtual void OnGetTokenFailure( | 132 virtual void OnGetTokenFailure( |
119 const GoogleServiceAuthError& error) OVERRIDE; | 133 const GoogleServiceAuthError& error) OVERRIDE; |
120 | 134 |
121 StringCallback callback_; | 135 StringCallback callback_; |
122 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | 136 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; |
123 }; | 137 }; |
124 | 138 |
125 CloudPolicyClientRegistrationHelper::TokenHelper::TokenHelper() {} | 139 CloudPolicyClientRegistrationHelper::LoginTokenHelper::LoginTokenHelper() {} |
126 | 140 |
127 void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken( | 141 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::FetchAccessToken( |
128 const std::string& login_refresh_token, | 142 const std::string& login_refresh_token, |
129 net::URLRequestContextGetter* context, | 143 net::URLRequestContextGetter* context, |
130 const StringCallback& callback) { | 144 const StringCallback& callback) { |
| 145 DCHECK(!oauth2_access_token_fetcher_); |
131 callback_ = callback; | 146 callback_ = callback; |
132 | 147 |
133 // Start fetching an OAuth2 access token for the device management and | 148 // Start fetching an OAuth2 access token for the device management and |
134 // userinfo services. | 149 // userinfo services. |
135 oauth2_access_token_fetcher_.reset( | 150 oauth2_access_token_fetcher_.reset( |
136 new OAuth2AccessTokenFetcher(this, context)); | 151 new OAuth2AccessTokenFetcher(this, context)); |
137 std::vector<std::string> scopes; | 152 std::vector<std::string> scopes; |
138 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); | 153 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); |
139 scopes.push_back(kServiceScopeGetUserInfo); | 154 scopes.push_back(kServiceScopeGetUserInfo); |
140 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 155 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
141 oauth2_access_token_fetcher_->Start( | 156 oauth2_access_token_fetcher_->Start( |
142 gaia_urls->oauth2_chrome_client_id(), | 157 gaia_urls->oauth2_chrome_client_id(), |
143 gaia_urls->oauth2_chrome_client_secret(), | 158 gaia_urls->oauth2_chrome_client_secret(), |
144 login_refresh_token, | 159 login_refresh_token, |
145 scopes); | 160 scopes); |
146 } | 161 } |
147 | 162 |
148 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenSuccess( | 163 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenSuccess( |
149 const std::string& access_token, | 164 const std::string& access_token, |
150 const base::Time& expiration_time) { | 165 const base::Time& expiration_time) { |
151 callback_.Run(access_token); | 166 callback_.Run(access_token); |
152 } | 167 } |
153 | 168 |
154 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenFailure( | 169 void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenFailure( |
155 const GoogleServiceAuthError& error) { | 170 const GoogleServiceAuthError& error) { |
156 callback_.Run(""); | 171 callback_.Run(""); |
157 } | 172 } |
158 | 173 |
159 #endif | 174 #endif |
160 | 175 |
161 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper( | 176 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper( |
162 net::URLRequestContextGetter* context, | 177 net::URLRequestContextGetter* context, |
163 CloudPolicyClient* client, | 178 CloudPolicyClient* client, |
164 bool should_force_load_policy, | 179 bool should_force_load_policy, |
165 enterprise_management::DeviceRegisterRequest::Type registration_type) | 180 enterprise_management::DeviceRegisterRequest::Type registration_type) |
166 : context_(context), | 181 : context_(context), |
167 client_(client), | 182 client_(client), |
168 should_force_load_policy_(should_force_load_policy), | 183 should_force_load_policy_(should_force_load_policy), |
169 registration_type_(registration_type) { | 184 registration_type_(registration_type) { |
170 DCHECK(context_); | 185 DCHECK(context_); |
171 DCHECK(client_); | 186 DCHECK(client_); |
172 } | 187 } |
173 | 188 |
174 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { | 189 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { |
175 // Clean up any pending observers in case the browser is shutdown while | 190 // Clean up any pending observers in case the browser is shutdown while |
176 // trying to register for policy. | 191 // trying to register for policy. |
177 if (client_) | 192 if (client_) |
178 client_->RemoveObserver(this); | 193 client_->RemoveObserver(this); |
179 } | 194 } |
180 | 195 |
181 #if defined(OS_ANDROID) | |
182 | 196 |
183 void CloudPolicyClientRegistrationHelper::StartRegistration( | 197 void CloudPolicyClientRegistrationHelper::StartRegistration( |
| 198 #if defined(OS_ANDROID) |
184 AndroidProfileOAuth2TokenService* token_service, | 199 AndroidProfileOAuth2TokenService* token_service, |
| 200 #else |
| 201 OAuth2TokenService* token_service, |
| 202 #endif |
185 const std::string& username, | 203 const std::string& username, |
186 const base::Closure& callback) { | 204 const base::Closure& callback) { |
187 DVLOG(1) << "Starting registration process with username"; | 205 DVLOG(1) << "Starting registration process with username"; |
188 DCHECK(!client_->is_registered()); | 206 DCHECK(!client_->is_registered()); |
189 callback_ = callback; | 207 callback_ = callback; |
190 client_->AddObserver(this); | 208 client_->AddObserver(this); |
191 | 209 |
192 token_helper_.reset(new TokenHelperAndroid()); | 210 token_service_helper_.reset(new TokenServiceHelper()); |
193 token_helper_->FetchAccessToken( | 211 token_service_helper_->FetchAccessToken( |
194 token_service, | 212 token_service, |
195 username, | 213 username, |
196 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
197 base::Unretained(this))); | 215 base::Unretained(this))); |
198 } | 216 } |
199 | 217 |
200 #else | 218 #if !defined(OS_ANDROID) |
201 | |
202 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( | 219 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( |
203 const std::string& login_refresh_token, | 220 const std::string& login_refresh_token, |
204 const base::Closure& callback) { | 221 const base::Closure& callback) { |
205 DVLOG(1) << "Starting registration process with login token"; | 222 DVLOG(1) << "Starting registration process with login token"; |
206 DCHECK(!client_->is_registered()); | 223 DCHECK(!client_->is_registered()); |
207 callback_ = callback; | 224 callback_ = callback; |
208 client_->AddObserver(this); | 225 client_->AddObserver(this); |
209 | 226 |
210 token_helper_.reset(new TokenHelper()); | 227 login_token_helper_.reset( |
211 token_helper_->FetchAccessToken( | 228 new CloudPolicyClientRegistrationHelper::LoginTokenHelper()); |
| 229 login_token_helper_->FetchAccessToken( |
212 login_refresh_token, | 230 login_refresh_token, |
213 context_, | 231 context_, |
214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 232 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
215 base::Unretained(this))); | 233 base::Unretained(this))); |
216 } | 234 } |
217 | |
218 #endif | 235 #endif |
219 | 236 |
220 void CloudPolicyClientRegistrationHelper::OnTokenFetched( | 237 void CloudPolicyClientRegistrationHelper::OnTokenFetched( |
221 const std::string& access_token) { | 238 const std::string& access_token) { |
222 token_helper_.reset(); | 239 #if !defined(OS_ANDROID) |
| 240 login_token_helper_.reset(); |
| 241 #endif |
| 242 token_service_helper_.reset(); |
223 | 243 |
224 if (access_token.empty()) { | 244 if (access_token.empty()) { |
225 DLOG(WARNING) << "Could not fetch access token for " | 245 DLOG(WARNING) << "Could not fetch access token for " |
226 << GaiaConstants::kDeviceManagementServiceOAuth; | 246 << GaiaConstants::kDeviceManagementServiceOAuth; |
227 RequestCompleted(); | 247 RequestCompleted(); |
228 return; | 248 return; |
229 } | 249 } |
230 | 250 |
231 // Cache the access token to be used after the GetUserInfo call. | 251 // Cache the access token to be used after the GetUserInfo call. |
232 oauth_access_token_ = access_token; | 252 oauth_access_token_ = access_token; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 void CloudPolicyClientRegistrationHelper::RequestCompleted() { | 311 void CloudPolicyClientRegistrationHelper::RequestCompleted() { |
292 if (client_) { | 312 if (client_) { |
293 client_->RemoveObserver(this); | 313 client_->RemoveObserver(this); |
294 // |client_| may be freed by the callback so clear it now. | 314 // |client_| may be freed by the callback so clear it now. |
295 client_ = NULL; | 315 client_ = NULL; |
296 callback_.Run(); | 316 callback_.Run(); |
297 } | 317 } |
298 } | 318 } |
299 | 319 |
300 } // namespace policy | 320 } // namespace policy |
OLD | NEW |