| 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 class CloudPolicyClientRegistrationHelper::TokenHelper { |
| 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 DCHECK(token_service->RefreshTokenIsAvailable()); |
| 75 callback_ = callback; | 91 callback_ = callback; |
| 76 | 92 |
| 77 OAuth2TokenService::ScopeSet scopes; | 93 OAuth2TokenService::ScopeSet scopes; |
| 78 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 94 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
| 79 scopes.insert(kServiceScopeGetUserInfo); | 95 scopes.insert(kServiceScopeGetUserInfo); |
| 80 | 96 |
| 97 #if defined(OS_ANDROID) |
| 81 token_request_ = | 98 token_request_ = |
| 82 token_service->StartRequestForUsername(username, scopes, this); | 99 token_service->StartRequestForUsername(username, scopes, this); |
| 100 #else |
| 101 token_request_ = token_service->StartRequest(scopes, this); |
| 102 #endif |
| 83 } | 103 } |
| 84 | 104 |
| 85 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess( | 105 void TokenServiceHelper::OnGetTokenSuccess( |
| 86 const OAuth2TokenService::Request* request, | 106 const OAuth2TokenService::Request* request, |
| 87 const std::string& access_token, | 107 const std::string& access_token, |
| 88 const base::Time& expiration_time) { | 108 const base::Time& expiration_time) { |
| 89 DCHECK_EQ(token_request_.get(), request); | 109 DCHECK_EQ(token_request_.get(), request); |
| 90 callback_.Run(access_token); | 110 callback_.Run(access_token); |
| 91 } | 111 } |
| 92 | 112 |
| 93 void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenFailure( | 113 void TokenServiceHelper::OnGetTokenFailure( |
| 94 const OAuth2TokenService::Request* request, | 114 const OAuth2TokenService::Request* request, |
| 95 const GoogleServiceAuthError& error) { | 115 const GoogleServiceAuthError& error) { |
| 96 DCHECK_EQ(token_request_.get(), request); | 116 DCHECK_EQ(token_request_.get(), request); |
| 97 callback_.Run(""); | 117 callback_.Run(""); |
| 98 } | 118 } |
| 99 | 119 |
| 100 #else | 120 #if !defined(OS_ANDROID) |
| 101 | |
| 102 // This class fetches the OAuth2 token scoped for the userinfo and DM services. | 121 // 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 | 122 // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token |
| 104 // that can be used to authorize that request. | 123 // that can be used to authorize that request. This class is not needed on |
| 105 class CloudPolicyClientRegistrationHelper::TokenHelper | 124 // Android because we can use OAuth2TokenService to fetch tokens for accounts |
| 106 : public OAuth2AccessTokenConsumer { | 125 // even before they are signed in. |
| 126 class LoginTokenHelper |
| 127 : public CloudPolicyClientRegistrationHelper::TokenHelper, |
| 128 public OAuth2AccessTokenConsumer { |
| 107 public: | 129 public: |
| 108 TokenHelper(); | 130 LoginTokenHelper(); |
| 109 | 131 |
| 110 void FetchAccessToken(const std::string& login_refresh_token, | 132 void FetchAccessToken(const std::string& login_refresh_token, |
| 111 net::URLRequestContextGetter* context, | 133 net::URLRequestContextGetter* context, |
| 112 const StringCallback& callback); | 134 const StringCallback& callback); |
| 113 | 135 |
| 114 private: | 136 private: |
| 115 // OAuth2AccessTokenConsumer implementation: | 137 // OAuth2AccessTokenConsumer implementation: |
| 116 virtual void OnGetTokenSuccess(const std::string& access_token, | 138 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 117 const base::Time& expiration_time) OVERRIDE; | 139 const base::Time& expiration_time) OVERRIDE; |
| 118 virtual void OnGetTokenFailure( | 140 virtual void OnGetTokenFailure( |
| 119 const GoogleServiceAuthError& error) OVERRIDE; | 141 const GoogleServiceAuthError& error) OVERRIDE; |
| 120 | 142 |
| 121 StringCallback callback_; | 143 StringCallback callback_; |
| 122 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | 144 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; |
| 123 }; | 145 }; |
| 124 | 146 |
| 125 CloudPolicyClientRegistrationHelper::TokenHelper::TokenHelper() {} | 147 LoginTokenHelper::LoginTokenHelper() {} |
| 126 | 148 |
| 127 void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken( | 149 void LoginTokenHelper::FetchAccessToken( |
| 128 const std::string& login_refresh_token, | 150 const std::string& login_refresh_token, |
| 129 net::URLRequestContextGetter* context, | 151 net::URLRequestContextGetter* context, |
| 130 const StringCallback& callback) { | 152 const StringCallback& callback) { |
| 153 DCHECK(!oauth2_access_token_fetcher_); |
| 131 callback_ = callback; | 154 callback_ = callback; |
| 132 | 155 |
| 133 // Start fetching an OAuth2 access token for the device management and | 156 // Start fetching an OAuth2 access token for the device management and |
| 134 // userinfo services. | 157 // userinfo services. |
| 135 oauth2_access_token_fetcher_.reset( | 158 oauth2_access_token_fetcher_.reset( |
| 136 new OAuth2AccessTokenFetcher(this, context)); | 159 new OAuth2AccessTokenFetcher(this, context)); |
| 137 std::vector<std::string> scopes; | 160 std::vector<std::string> scopes; |
| 138 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); | 161 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); |
| 139 scopes.push_back(kServiceScopeGetUserInfo); | 162 scopes.push_back(kServiceScopeGetUserInfo); |
| 140 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 163 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
| 141 oauth2_access_token_fetcher_->Start( | 164 oauth2_access_token_fetcher_->Start( |
| 142 gaia_urls->oauth2_chrome_client_id(), | 165 gaia_urls->oauth2_chrome_client_id(), |
| 143 gaia_urls->oauth2_chrome_client_secret(), | 166 gaia_urls->oauth2_chrome_client_secret(), |
| 144 login_refresh_token, | 167 login_refresh_token, |
| 145 scopes); | 168 scopes); |
| 146 } | 169 } |
| 147 | 170 |
| 148 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenSuccess( | 171 void LoginTokenHelper::OnGetTokenSuccess(const std::string& access_token, |
| 149 const std::string& access_token, | 172 const base::Time& expiration_time) { |
| 150 const base::Time& expiration_time) { | |
| 151 callback_.Run(access_token); | 173 callback_.Run(access_token); |
| 152 } | 174 } |
| 153 | 175 |
| 154 void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenFailure( | 176 void LoginTokenHelper::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 155 const GoogleServiceAuthError& error) { | |
| 156 callback_.Run(""); | 177 callback_.Run(""); |
| 157 } | 178 } |
| 158 | 179 |
| 159 #endif | 180 #endif |
| 160 | 181 |
| 182 } // namespace |
| 183 |
| 161 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper( | 184 CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper( |
| 162 net::URLRequestContextGetter* context, | 185 net::URLRequestContextGetter* context, |
| 163 CloudPolicyClient* client, | 186 CloudPolicyClient* client, |
| 164 bool should_force_load_policy, | 187 bool should_force_load_policy, |
| 165 enterprise_management::DeviceRegisterRequest::Type registration_type) | 188 enterprise_management::DeviceRegisterRequest::Type registration_type) |
| 166 : context_(context), | 189 : context_(context), |
| 167 client_(client), | 190 client_(client), |
| 168 should_force_load_policy_(should_force_load_policy), | 191 should_force_load_policy_(should_force_load_policy), |
| 169 registration_type_(registration_type) { | 192 registration_type_(registration_type) { |
| 170 DCHECK(context_); | 193 DCHECK(context_); |
| 171 DCHECK(client_); | 194 DCHECK(client_); |
| 172 } | 195 } |
| 173 | 196 |
| 174 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { | 197 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { |
| 175 // Clean up any pending observers in case the browser is shutdown while | 198 // Clean up any pending observers in case the browser is shutdown while |
| 176 // trying to register for policy. | 199 // trying to register for policy. |
| 177 if (client_) | 200 if (client_) |
| 178 client_->RemoveObserver(this); | 201 client_->RemoveObserver(this); |
| 179 } | 202 } |
| 180 | 203 |
| 181 #if defined(OS_ANDROID) | |
| 182 | 204 |
| 183 void CloudPolicyClientRegistrationHelper::StartRegistration( | 205 void CloudPolicyClientRegistrationHelper::StartRegistration( |
| 206 #if defined(OS_ANDROID) |
| 184 AndroidProfileOAuth2TokenService* token_service, | 207 AndroidProfileOAuth2TokenService* token_service, |
| 208 #else |
| 209 OAuth2TokenService* token_service, |
| 210 #endif |
| 185 const std::string& username, | 211 const std::string& username, |
| 186 const base::Closure& callback) { | 212 const base::Closure& callback) { |
| 187 DVLOG(1) << "Starting registration process with username"; | 213 DVLOG(1) << "Starting registration process with username"; |
| 188 DCHECK(!client_->is_registered()); | 214 DCHECK(!client_->is_registered()); |
| 189 callback_ = callback; | 215 callback_ = callback; |
| 190 client_->AddObserver(this); | 216 client_->AddObserver(this); |
| 191 | 217 |
| 192 token_helper_.reset(new TokenHelperAndroid()); | 218 scoped_ptr<TokenServiceHelper> token_service_helper(new TokenServiceHelper()); |
| 193 token_helper_->FetchAccessToken( | 219 token_service_helper->FetchAccessToken( |
| 194 token_service, | 220 token_service, |
| 195 username, | 221 username, |
| 196 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 222 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
| 197 base::Unretained(this))); | 223 base::Unretained(this))); |
| 224 token_helper_ = token_service_helper.PassAs<TokenHelper>(); |
| 198 } | 225 } |
| 199 | 226 |
| 200 #else | 227 #if !defined(OS_ANDROID) |
| 201 | |
| 202 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( | 228 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( |
| 203 const std::string& login_refresh_token, | 229 const std::string& login_refresh_token, |
| 204 const base::Closure& callback) { | 230 const base::Closure& callback) { |
| 205 DVLOG(1) << "Starting registration process with login token"; | 231 DVLOG(1) << "Starting registration process with login token"; |
| 206 DCHECK(!client_->is_registered()); | 232 DCHECK(!client_->is_registered()); |
| 207 callback_ = callback; | 233 callback_ = callback; |
| 208 client_->AddObserver(this); | 234 client_->AddObserver(this); |
| 209 | 235 |
| 210 token_helper_.reset(new TokenHelper()); | 236 scoped_ptr<LoginTokenHelper> login_token_helper(new LoginTokenHelper()); |
| 211 token_helper_->FetchAccessToken( | 237 login_token_helper->FetchAccessToken( |
| 212 login_refresh_token, | 238 login_refresh_token, |
| 213 context_, | 239 context_, |
| 214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 240 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
| 215 base::Unretained(this))); | 241 base::Unretained(this))); |
| 242 token_helper_ = login_token_helper.PassAs<TokenHelper>(); |
| 216 } | 243 } |
| 217 | |
| 218 #endif | 244 #endif |
| 219 | 245 |
| 220 void CloudPolicyClientRegistrationHelper::OnTokenFetched( | 246 void CloudPolicyClientRegistrationHelper::OnTokenFetched( |
| 221 const std::string& access_token) { | 247 const std::string& access_token) { |
| 222 token_helper_.reset(); | 248 token_helper_.reset(); |
| 223 | 249 |
| 224 if (access_token.empty()) { | 250 if (access_token.empty()) { |
| 225 DLOG(WARNING) << "Could not fetch access token for " | 251 DLOG(WARNING) << "Could not fetch access token for " |
| 226 << GaiaConstants::kDeviceManagementServiceOAuth; | 252 << GaiaConstants::kDeviceManagementServiceOAuth; |
| 227 RequestCompleted(); | 253 RequestCompleted(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 void CloudPolicyClientRegistrationHelper::RequestCompleted() { | 317 void CloudPolicyClientRegistrationHelper::RequestCompleted() { |
| 292 if (client_) { | 318 if (client_) { |
| 293 client_->RemoveObserver(this); | 319 client_->RemoveObserver(this); |
| 294 // |client_| may be freed by the callback so clear it now. | 320 // |client_| may be freed by the callback so clear it now. |
| 295 client_ = NULL; | 321 client_ = NULL; |
| 296 callback_.Run(); | 322 callback_.Run(); |
| 297 } | 323 } |
| 298 } | 324 } |
| 299 | 325 |
| 300 } // namespace policy | 326 } // namespace policy |
| OLD | NEW |