| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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/chromeos/login/oauth2_policy_fetcher.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/policy/browser_policy_connector.h" |
| 13 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 16 #include "google_apis/gaia/gaia_constants.h" |
| 17 #include "google_apis/gaia/gaia_urls.h" |
| 18 #include "google_apis/gaia/google_service_auth_error.h" |
| 19 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace chromeos { |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Max retry count for token fetching requests. |
| 28 const int kMaxRequestAttemptCount = 5; |
| 29 |
| 30 // OAuth token request retry delay in milliseconds. |
| 31 const int kRequestRestartDelay = 3000; |
| 32 |
| 33 const char kServiceScopeChromeOSDeviceManagement[] = |
| 34 "https://www.googleapis.com/auth/chromeosdevicemanagement"; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 OAuth2PolicyFetcher::OAuth2PolicyFetcher( |
| 39 net::URLRequestContextGetter* auth_context_getter, |
| 40 net::URLRequestContextGetter* system_context_getter) |
| 41 : refresh_token_fetcher_( |
| 42 new GaiaAuthFetcher(this, |
| 43 GaiaConstants::kChromeSource, |
| 44 auth_context_getter)), |
| 45 access_token_fetcher_( |
| 46 new OAuth2AccessTokenFetcher(this, system_context_getter)), |
| 47 retry_count_(0), |
| 48 failed_(false) { |
| 49 } |
| 50 |
| 51 OAuth2PolicyFetcher::OAuth2PolicyFetcher( |
| 52 net::URLRequestContextGetter* system_context_getter, |
| 53 const std::string& oauth2_refresh_token) |
| 54 : access_token_fetcher_( |
| 55 new OAuth2AccessTokenFetcher(this, system_context_getter)), |
| 56 oauth2_refresh_token_(oauth2_refresh_token), |
| 57 retry_count_(0), |
| 58 failed_(false) { |
| 59 } |
| 60 |
| 61 OAuth2PolicyFetcher::~OAuth2PolicyFetcher() { |
| 62 } |
| 63 |
| 64 void OAuth2PolicyFetcher::Start() { |
| 65 retry_count_ = 0; |
| 66 if (oauth2_refresh_token_.empty()) { |
| 67 StartFetchingRefreshToken(); |
| 68 } else { |
| 69 StartFetchingAccessToken(); |
| 70 } |
| 71 } |
| 72 |
| 73 void OAuth2PolicyFetcher::StartFetchingRefreshToken() { |
| 74 DCHECK(refresh_token_fetcher_.get()); |
| 75 refresh_token_fetcher_->StartCookieForOAuthLoginTokenExchange(EmptyString()); |
| 76 } |
| 77 |
| 78 void OAuth2PolicyFetcher::StartFetchingAccessToken() { |
| 79 std::vector<std::string> scopes; |
| 80 scopes.push_back(kServiceScopeChromeOSDeviceManagement); |
| 81 access_token_fetcher_->Start( |
| 82 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 83 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 84 oauth2_refresh_token_, |
| 85 scopes); |
| 86 } |
| 87 |
| 88 void OAuth2PolicyFetcher::OnClientOAuthSuccess( |
| 89 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
| 90 LOG(INFO) << "OAuth2 tokens for policy fetching succeeded."; |
| 91 oauth2_tokens_ = oauth2_tokens; |
| 92 oauth2_refresh_token_ = oauth2_tokens.refresh_token; |
| 93 retry_count_ = 0; |
| 94 StartFetchingAccessToken(); |
| 95 } |
| 96 |
| 97 void OAuth2PolicyFetcher::OnClientOAuthFailure( |
| 98 const GoogleServiceAuthError& error) { |
| 99 LOG(ERROR) << "OAuth2 tokens fetch for policy fetch failed!"; |
| 100 RetryOnError(error, |
| 101 base::Bind(&OAuth2PolicyFetcher::StartFetchingRefreshToken, |
| 102 AsWeakPtr())); |
| 103 } |
| 104 |
| 105 void OAuth2PolicyFetcher::OnGetTokenSuccess( |
| 106 const std::string& access_token, |
| 107 const base::Time& expiration_time) { |
| 108 LOG(INFO) << "OAuth2 access token (device management) fetching succeeded."; |
| 109 SetPolicyToken(access_token); |
| 110 } |
| 111 |
| 112 void OAuth2PolicyFetcher::OnGetTokenFailure( |
| 113 const GoogleServiceAuthError& error) { |
| 114 LOG(ERROR) << "OAuth2 access token (device management) fetching failed!"; |
| 115 RetryOnError(error, |
| 116 base::Bind(&OAuth2PolicyFetcher::StartFetchingAccessToken, |
| 117 AsWeakPtr())); |
| 118 } |
| 119 |
| 120 void OAuth2PolicyFetcher::RetryOnError(const GoogleServiceAuthError& error, |
| 121 const base::Closure& task) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 123 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| 124 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
| 125 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && |
| 126 retry_count_ < kMaxRequestAttemptCount) { |
| 127 retry_count_++; |
| 128 BrowserThread::PostDelayedTask( |
| 129 BrowserThread::UI, FROM_HERE, task, |
| 130 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| 131 return; |
| 132 } |
| 133 LOG(ERROR) << "Unrecoverable error or retry count max reached."; |
| 134 SetPolicyToken(EmptyString()); |
| 135 failed_ = true; |
| 136 } |
| 137 |
| 138 void OAuth2PolicyFetcher::SetPolicyToken(const std::string& token) { |
| 139 policy_token_ = token; |
| 140 |
| 141 policy::BrowserPolicyConnector* browser_policy_connector = |
| 142 g_browser_process->browser_policy_connector(); |
| 143 browser_policy_connector->RegisterForUserPolicy(token); |
| 144 |
| 145 policy::UserCloudPolicyManagerChromeOS* cloud_policy_manager = |
| 146 browser_policy_connector->GetUserCloudPolicyManager(); |
| 147 if (cloud_policy_manager) { |
| 148 if (token.empty()) |
| 149 cloud_policy_manager->CancelWaitForPolicyFetch(); |
| 150 else |
| 151 cloud_policy_manager->RegisterClient(token); |
| 152 } |
| 153 } |
| 154 |
| 155 } // namespace chromeos |
| OLD | NEW |