1 // Copyright (c) 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.
5 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "google_apis/gaia/gaia_auth_fetcher.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_access_token_fetcher_impl.h"
18 #include "net/url_request/url_request_context_getter.h"
20 using content::BrowserThread
;
26 // Max retry count for token fetching requests.
27 const int kMaxRequestAttemptCount
= 5;
29 // OAuth token request retry delay in milliseconds.
30 const int kRequestRestartDelay
= 3000;
34 PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
35 net::URLRequestContextGetter
* auth_context_getter
,
36 net::URLRequestContextGetter
* system_context_getter
,
37 const TokenCallback
& callback
)
38 : auth_context_getter_(auth_context_getter
),
39 system_context_getter_(system_context_getter
),
42 callback_(callback
) {}
44 PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
45 const std::string
& auth_code
,
46 net::URLRequestContextGetter
* system_context_getter
,
47 const TokenCallback
& callback
)
48 : auth_code_(auth_code
),
49 system_context_getter_(system_context_getter
),
55 PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() {}
57 void PolicyOAuth2TokenFetcher::Start() {
59 StartFetchingRefreshToken();
62 void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() {
63 if (auth_code_
.empty()) {
64 refresh_token_fetcher_
.reset(new GaiaAuthFetcher(
65 this, GaiaConstants::kChromeSource
, auth_context_getter_
.get()));
66 refresh_token_fetcher_
->StartCookieForOAuthLoginTokenExchange(
69 refresh_token_fetcher_
.reset(new GaiaAuthFetcher(
70 this, GaiaConstants::kChromeSource
, system_context_getter_
.get()));
71 refresh_token_fetcher_
->StartAuthCodeForOAuth2TokenExchange(auth_code_
);
75 void PolicyOAuth2TokenFetcher::StartFetchingAccessToken() {
76 std::vector
<std::string
> scopes
;
77 scopes
.push_back(GaiaConstants::kDeviceManagementServiceOAuth
);
78 scopes
.push_back(GaiaConstants::kOAuthWrapBridgeUserInfoScope
);
79 access_token_fetcher_
.reset(
80 new OAuth2AccessTokenFetcherImpl(this,
81 system_context_getter_
.get(),
82 oauth2_refresh_token_
));
83 access_token_fetcher_
->Start(
84 GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
85 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
89 void PolicyOAuth2TokenFetcher::OnClientOAuthSuccess(
90 const GaiaAuthConsumer::ClientOAuthResult
& oauth2_tokens
) {
91 VLOG(1) << "OAuth2 tokens for policy fetching succeeded.";
92 oauth2_refresh_token_
= oauth2_tokens
.refresh_token
;
94 StartFetchingAccessToken();
97 void PolicyOAuth2TokenFetcher::OnClientOAuthFailure(
98 const GoogleServiceAuthError
& error
) {
99 VLOG(1) << "OAuth2 tokens fetch for policy fetch failed!";
101 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingRefreshToken
,
105 void PolicyOAuth2TokenFetcher::OnGetTokenSuccess(
106 const std::string
& access_token
,
107 const base::Time
& expiration_time
) {
108 VLOG(1) << "OAuth2 access token (device management) fetching succeeded.";
109 oauth2_access_token_
= access_token
;
110 ForwardPolicyToken(access_token
,
111 GoogleServiceAuthError(GoogleServiceAuthError::NONE
));
114 void PolicyOAuth2TokenFetcher::OnGetTokenFailure(
115 const GoogleServiceAuthError
& error
) {
116 LOG(ERROR
) << "OAuth2 access token (device management) fetching failed!";
118 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingAccessToken
,
122 void PolicyOAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError
& error
,
123 const base::Closure
& task
) {
124 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
125 if ((error
.state() == GoogleServiceAuthError::CONNECTION_FAILED
||
126 error
.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE
||
127 error
.state() == GoogleServiceAuthError::REQUEST_CANCELED
) &&
128 retry_count_
< kMaxRequestAttemptCount
) {
130 BrowserThread::PostDelayedTask(
131 BrowserThread::UI
, FROM_HERE
, task
,
132 base::TimeDelta::FromMilliseconds(kRequestRestartDelay
));
135 LOG(ERROR
) << "Unrecoverable error or retry count max reached.";
137 // Invoking the |callback_| signals to the owner of this object that it has
138 // completed, and the owner may delete this object on the callback method.
139 // So don't rely on |this| still being valid after ForwardPolicyToken()
140 // returns i.e. don't write to |failed_| or other fields.
141 ForwardPolicyToken(std::string(), error
);
144 void PolicyOAuth2TokenFetcher::ForwardPolicyToken(
145 const std::string
& token
,
146 const GoogleServiceAuthError
& error
) {
147 if (!callback_
.is_null())
148 callback_
.Run(token
, error
);
151 } // namespace policy