OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/policy/policy_oauth2_token_fetcher.h" | 5 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 namespace policy { | 22 namespace policy { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Max retry count for token fetching requests. | 26 // Max retry count for token fetching requests. |
27 const int kMaxRequestAttemptCount = 5; | 27 const int kMaxRequestAttemptCount = 5; |
28 | 28 |
29 // OAuth token request retry delay in milliseconds. | 29 // OAuth token request retry delay in milliseconds. |
30 const int kRequestRestartDelay = 3000; | 30 const int kRequestRestartDelay = 3000; |
31 | 31 |
32 } // namespace | 32 class PolicyOAuth2TokenFetcherImpl |
33 : public PolicyOAuth2TokenFetcher, | |
34 public base::SupportsWeakPtr<PolicyOAuth2TokenFetcherImpl>, | |
achuithb
2016/03/08 08:11:44
switch to using WeakPtrFactory instead.
jdufault
2016/03/08 21:37:08
Done.
| |
35 public GaiaAuthConsumer, | |
36 public OAuth2AccessTokenConsumer { | |
37 public: | |
38 PolicyOAuth2TokenFetcherImpl(); | |
39 ~PolicyOAuth2TokenFetcherImpl() override; | |
achuithb
2016/03/08 08:11:44
protected ctor + dtor?
jdufault
2016/03/08 21:37:08
ctor needs to be public so the base class can inst
achuithb
2016/03/08 22:13:26
Acknowledged.
| |
33 | 40 |
34 PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher() { | 41 // Fetches the device management service's oauth2 token. This may be fetched |
achuithb
2016/03/08 08:11:44
don't need this comment, but you need
// PolicyOau
jdufault
2016/03/08 21:37:08
Done.
| |
35 } | 42 // via signin context, auth code, or oauth2 refresh token. |
43 void StartWithSigninContext( | |
achuithb
2016/03/08 08:11:44
make the overriden methods private?
jdufault
2016/03/08 21:37:08
Done.
| |
44 net::URLRequestContextGetter* auth_context_getter, | |
45 net::URLRequestContextGetter* system_context_getter, | |
46 const TokenCallback& callback) override; | |
47 void StartWithAuthCode(const std::string& auth_code, | |
48 net::URLRequestContextGetter* system_context_getter, | |
49 const TokenCallback& callback) override; | |
50 void StartWithRefreshToken( | |
51 const std::string& oauth2_refresh_token, | |
52 net::URLRequestContextGetter* system_context_getter, | |
53 const TokenCallback& callback) override; | |
36 | 54 |
37 PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() { | 55 // Returns true if we have previously attempted to fetch tokens with this |
38 } | 56 // class and failed. |
57 bool failed() const override { return failed_; } | |
39 | 58 |
40 void PolicyOAuth2TokenFetcher::StartWithSigninContext( | 59 const std::string& oauth2_refresh_token() const override { |
60 return oauth2_refresh_token_; | |
61 } | |
62 const std::string& oauth2_access_token() const override { | |
63 return oauth2_access_token_; | |
64 } | |
65 | |
66 private: | |
67 // GaiaAuthConsumer overrides. | |
68 void OnClientOAuthSuccess( | |
69 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) override; | |
70 void OnClientOAuthFailure(const GoogleServiceAuthError& error) override; | |
71 | |
72 // OAuth2AccessTokenConsumer overrides. | |
73 void OnGetTokenSuccess(const std::string& access_token, | |
74 const base::Time& expiration_time) override; | |
75 void OnGetTokenFailure(const GoogleServiceAuthError& error) override; | |
76 | |
77 // Starts fetching OAuth2 refresh token. | |
78 void StartFetchingRefreshToken(); | |
79 | |
80 // Starts fetching OAuth2 access token for the device management service. | |
81 void StartFetchingAccessToken(); | |
82 | |
83 // Decides how to proceed on GAIA |error|. If the error looks temporary, | |
84 // retries |task| until max retry count is reached. | |
85 // If retry count runs out, or error condition is unrecoverable, it calls | |
86 // Delegate::OnOAuth2TokenFetchFailed(). | |
87 void RetryOnError(const GoogleServiceAuthError& error, | |
88 const base::Closure& task); | |
89 | |
90 // Passes |token| and |error| to the |callback_|. | |
91 void ForwardPolicyToken(const std::string& token, | |
92 const GoogleServiceAuthError& error); | |
93 | |
94 // Auth code which is used to retreive a refresh token. | |
95 std::string auth_code_; | |
96 | |
97 scoped_refptr<net::URLRequestContextGetter> auth_context_getter_; | |
98 scoped_refptr<net::URLRequestContextGetter> system_context_getter_; | |
99 scoped_ptr<GaiaAuthFetcher> refresh_token_fetcher_; | |
100 scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_; | |
101 | |
102 // OAuth2 refresh token. Could come either from the outside or through | |
103 // refresh token fetching flow within this class. | |
104 std::string oauth2_refresh_token_; | |
105 | |
106 // OAuth2 access token. | |
107 std::string oauth2_access_token_; | |
108 | |
109 // The retry counter. Increment this only when failure happened. | |
110 int retry_count_ = 0; | |
111 | |
112 // True if we have already failed to fetch the policy. | |
113 bool failed_ = false; | |
114 | |
115 // The callback to invoke when done. | |
116 TokenCallback callback_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcherImpl); | |
119 }; | |
120 | |
121 PolicyOAuth2TokenFetcherImpl::PolicyOAuth2TokenFetcherImpl() {} | |
122 | |
123 PolicyOAuth2TokenFetcherImpl::~PolicyOAuth2TokenFetcherImpl() {} | |
124 | |
125 void PolicyOAuth2TokenFetcherImpl::StartWithSigninContext( | |
41 net::URLRequestContextGetter* auth_context_getter, | 126 net::URLRequestContextGetter* auth_context_getter, |
42 net::URLRequestContextGetter* system_context_getter, | 127 net::URLRequestContextGetter* system_context_getter, |
43 const TokenCallback& callback) { | 128 const TokenCallback& callback) { |
44 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); | 129 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); |
45 | 130 |
46 auth_context_getter_ = auth_context_getter; | 131 auth_context_getter_ = auth_context_getter; |
47 system_context_getter_ = system_context_getter; | 132 system_context_getter_ = system_context_getter; |
48 callback_ = callback; | 133 callback_ = callback; |
134 | |
49 StartFetchingRefreshToken(); | 135 StartFetchingRefreshToken(); |
50 } | 136 } |
51 | 137 |
52 void PolicyOAuth2TokenFetcher::StartWithAuthCode( | 138 void PolicyOAuth2TokenFetcherImpl::StartWithAuthCode( |
53 const std::string& auth_code, | 139 const std::string& auth_code, |
54 net::URLRequestContextGetter* system_context_getter, | 140 net::URLRequestContextGetter* system_context_getter, |
55 const TokenCallback& callback) { | 141 const TokenCallback& callback) { |
56 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); | 142 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); |
57 | 143 |
58 auth_code_ = auth_code; | 144 auth_code_ = auth_code; |
59 system_context_getter_ = system_context_getter; | 145 system_context_getter_ = system_context_getter; |
60 callback_ = callback; | 146 callback_ = callback; |
147 | |
61 StartFetchingRefreshToken(); | 148 StartFetchingRefreshToken(); |
62 } | 149 } |
63 | 150 |
64 void PolicyOAuth2TokenFetcher::StartWithRefreshToken( | 151 void PolicyOAuth2TokenFetcherImpl::StartWithRefreshToken( |
65 const std::string& oauth2_refresh_token, | 152 const std::string& oauth2_refresh_token, |
66 net::URLRequestContextGetter* system_context_getter, | 153 net::URLRequestContextGetter* system_context_getter, |
67 const TokenCallback& callback) { | 154 const TokenCallback& callback) { |
68 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); | 155 DCHECK(!refresh_token_fetcher_ && !access_token_fetcher_); |
69 | 156 |
70 oauth2_refresh_token_ = oauth2_refresh_token; | 157 oauth2_refresh_token_ = oauth2_refresh_token; |
71 system_context_getter_ = system_context_getter; | 158 system_context_getter_ = system_context_getter; |
72 callback_ = callback; | 159 callback_ = callback; |
160 | |
73 StartFetchingAccessToken(); | 161 StartFetchingAccessToken(); |
74 } | 162 } |
75 | 163 |
76 void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() { | 164 void PolicyOAuth2TokenFetcherImpl::StartFetchingRefreshToken() { |
77 if (auth_code_.empty()) { | 165 if (auth_code_.empty()) { |
78 refresh_token_fetcher_.reset(new GaiaAuthFetcher( | 166 refresh_token_fetcher_.reset(new GaiaAuthFetcher( |
79 this, GaiaConstants::kChromeSource, auth_context_getter_.get())); | 167 this, GaiaConstants::kChromeSource, auth_context_getter_.get())); |
80 refresh_token_fetcher_->StartCookieForOAuthLoginTokenExchange( | 168 refresh_token_fetcher_->StartCookieForOAuthLoginTokenExchange( |
81 std::string()); | 169 std::string()); |
82 } else { | 170 } else { |
83 refresh_token_fetcher_.reset(new GaiaAuthFetcher( | 171 refresh_token_fetcher_.reset(new GaiaAuthFetcher( |
84 this, GaiaConstants::kChromeSource, system_context_getter_.get())); | 172 this, GaiaConstants::kChromeSource, system_context_getter_.get())); |
85 refresh_token_fetcher_->StartAuthCodeForOAuth2TokenExchange(auth_code_); | 173 refresh_token_fetcher_->StartAuthCodeForOAuth2TokenExchange(auth_code_); |
86 } | 174 } |
87 } | 175 } |
88 | 176 |
89 void PolicyOAuth2TokenFetcher::StartFetchingAccessToken() { | 177 void PolicyOAuth2TokenFetcherImpl::StartFetchingAccessToken() { |
90 std::vector<std::string> scopes; | 178 std::vector<std::string> scopes; |
91 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); | 179 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth); |
92 scopes.push_back(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | 180 scopes.push_back(GaiaConstants::kOAuthWrapBridgeUserInfoScope); |
93 access_token_fetcher_.reset( | 181 access_token_fetcher_.reset( |
94 new OAuth2AccessTokenFetcherImpl(this, | 182 new OAuth2AccessTokenFetcherImpl(this, |
95 system_context_getter_.get(), | 183 system_context_getter_.get(), |
96 oauth2_refresh_token_)); | 184 oauth2_refresh_token_)); |
97 access_token_fetcher_->Start( | 185 access_token_fetcher_->Start( |
98 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 186 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
99 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 187 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
100 scopes); | 188 scopes); |
101 } | 189 } |
102 | 190 |
103 void PolicyOAuth2TokenFetcher::OnClientOAuthSuccess( | 191 void PolicyOAuth2TokenFetcherImpl::OnClientOAuthSuccess( |
104 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { | 192 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
105 VLOG(1) << "OAuth2 tokens for policy fetching succeeded."; | 193 VLOG(1) << "OAuth2 tokens for policy fetching succeeded."; |
106 oauth2_refresh_token_ = oauth2_tokens.refresh_token; | 194 oauth2_refresh_token_ = oauth2_tokens.refresh_token; |
107 retry_count_ = 0; | 195 retry_count_ = 0; |
108 StartFetchingAccessToken(); | 196 StartFetchingAccessToken(); |
109 } | 197 } |
110 | 198 |
111 void PolicyOAuth2TokenFetcher::OnClientOAuthFailure( | 199 void PolicyOAuth2TokenFetcherImpl::OnClientOAuthFailure( |
112 const GoogleServiceAuthError& error) { | 200 const GoogleServiceAuthError& error) { |
113 VLOG(1) << "OAuth2 tokens fetch for policy fetch failed! (error = " | 201 VLOG(1) << "OAuth2 tokens fetch for policy fetch failed! (error = " |
114 << error.state() << ")"; | 202 << error.state() << ")"; |
115 RetryOnError(error, | 203 RetryOnError( |
116 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingRefreshToken, | 204 error, |
117 AsWeakPtr())); | 205 base::Bind(&PolicyOAuth2TokenFetcherImpl::StartFetchingRefreshToken, |
206 AsWeakPtr())); | |
118 } | 207 } |
119 | 208 |
120 void PolicyOAuth2TokenFetcher::OnGetTokenSuccess( | 209 void PolicyOAuth2TokenFetcherImpl::OnGetTokenSuccess( |
121 const std::string& access_token, | 210 const std::string& access_token, |
122 const base::Time& expiration_time) { | 211 const base::Time& expiration_time) { |
123 VLOG(1) << "OAuth2 access token (device management) fetching succeeded."; | 212 VLOG(1) << "OAuth2 access token (device management) fetching succeeded."; |
124 oauth2_access_token_ = access_token; | 213 oauth2_access_token_ = access_token; |
125 ForwardPolicyToken(access_token, | 214 ForwardPolicyToken(access_token, |
126 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 215 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
127 } | 216 } |
128 | 217 |
129 void PolicyOAuth2TokenFetcher::OnGetTokenFailure( | 218 void PolicyOAuth2TokenFetcherImpl::OnGetTokenFailure( |
130 const GoogleServiceAuthError& error) { | 219 const GoogleServiceAuthError& error) { |
131 LOG(ERROR) << "OAuth2 access token (device management) fetching failed!"; | 220 LOG(ERROR) << "OAuth2 access token (device management) fetching failed!"; |
132 RetryOnError(error, | 221 RetryOnError( |
133 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingAccessToken, | 222 error, base::Bind(&PolicyOAuth2TokenFetcherImpl::StartFetchingAccessToken, |
134 AsWeakPtr())); | 223 AsWeakPtr())); |
135 } | 224 } |
136 | 225 |
137 void PolicyOAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, | 226 void PolicyOAuth2TokenFetcherImpl::RetryOnError( |
138 const base::Closure& task) { | 227 const GoogleServiceAuthError& error, |
228 const base::Closure& task) { | |
139 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 229 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
140 if (error.IsTransientError() && retry_count_ < kMaxRequestAttemptCount) { | 230 if (error.IsTransientError() && retry_count_ < kMaxRequestAttemptCount) { |
141 retry_count_++; | 231 retry_count_++; |
142 BrowserThread::PostDelayedTask( | 232 BrowserThread::PostDelayedTask( |
143 BrowserThread::UI, FROM_HERE, task, | 233 BrowserThread::UI, FROM_HERE, task, |
144 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | 234 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
145 return; | 235 return; |
146 } | 236 } |
147 LOG(ERROR) << "Unrecoverable error or retry count max reached."; | 237 LOG(ERROR) << "Unrecoverable error or retry count max reached."; |
148 failed_ = true; | 238 failed_ = true; |
149 // Invoking the |callback_| signals to the owner of this object that it has | 239 // Invoking the |callback_| signals to the owner of this object that it has |
150 // completed, and the owner may delete this object on the callback method. | 240 // completed, and the owner may delete this object on the callback method. |
151 // So don't rely on |this| still being valid after ForwardPolicyToken() | 241 // So don't rely on |this| still being valid after ForwardPolicyToken() |
152 // returns i.e. don't write to |failed_| or other fields. | 242 // returns i.e. don't write to |failed_| or other fields. |
153 ForwardPolicyToken(std::string(), error); | 243 ForwardPolicyToken(std::string(), error); |
154 } | 244 } |
155 | 245 |
156 void PolicyOAuth2TokenFetcher::ForwardPolicyToken( | 246 void PolicyOAuth2TokenFetcherImpl::ForwardPolicyToken( |
157 const std::string& token, | 247 const std::string& token, |
158 const GoogleServiceAuthError& error) { | 248 const GoogleServiceAuthError& error) { |
159 if (!callback_.is_null()) | 249 if (!callback_.is_null()) |
160 callback_.Run(token, error); | 250 callback_.Run(token, error); |
161 } | 251 } |
162 | 252 |
253 // Fake token fetcher that immediately returns tokens without making network | |
254 // requests. | |
255 class PolicyOAuth2TokenFetcherFake : public PolicyOAuth2TokenFetcher { | |
256 public: | |
257 PolicyOAuth2TokenFetcherFake() {} | |
258 ~PolicyOAuth2TokenFetcherFake() override {} | |
achuithb
2016/03/08 08:11:44
make ctor/dtor protected
jdufault
2016/03/08 21:37:08
See previous comment.
achuithb
2016/03/08 22:13:26
Acknowledged.
| |
259 | |
260 void StartWithSigninContext( | |
achuithb
2016/03/08 08:11:44
Make all the rest private
jdufault
2016/03/08 21:37:08
Done.
| |
261 net::URLRequestContextGetter* auth_context_getter, | |
262 net::URLRequestContextGetter* system_context_getter, | |
263 const TokenCallback& callback) override { | |
264 ForwardPolicyToken(callback); | |
265 } | |
266 | |
267 void StartWithAuthCode(const std::string& auth_code, | |
268 net::URLRequestContextGetter* system_context_getter, | |
269 const TokenCallback& callback) override { | |
270 ForwardPolicyToken(callback); | |
271 } | |
272 | |
273 void StartWithRefreshToken( | |
274 const std::string& oauth2_refresh_token, | |
275 net::URLRequestContextGetter* system_context_getter, | |
276 const TokenCallback& callback) override { | |
277 ForwardPolicyToken(callback); | |
278 } | |
279 | |
280 bool failed() const override { return false; } | |
281 const std::string& oauth2_refresh_token() const override { | |
282 return refresh_token_; | |
283 } | |
284 const std::string& oauth2_access_token() const override { | |
285 return access_token_; | |
286 } | |
287 | |
288 private: | |
289 std::string refresh_token_ = "fake_refresh_token"; | |
290 std::string access_token_ = "fake_access_token"; | |
291 | |
292 void ForwardPolicyToken(const TokenCallback& callback) { | |
achuithb
2016/03/08 08:11:44
methods should precede data members, so this shoul
jdufault
2016/03/08 21:37:08
Done.
| |
293 if (!callback.is_null()) | |
294 callback.Run(access_token_, GoogleServiceAuthError::AuthErrorNone()); | |
295 } | |
296 }; | |
achuithb
2016/03/08 08:11:44
DISABLE_COPY_AND_ASSIGN
jdufault
2016/03/08 21:37:08
Done.
| |
297 | |
298 } // namespace | |
299 | |
300 // static | |
301 bool PolicyOAuth2TokenFetcher::use_fake_tokens_for_testing_ = false; | |
achuithb
2016/03/08 08:11:44
move this to anon namespace?
jdufault
2016/03/08 21:37:08
Done.
| |
302 | |
303 // static | |
304 void PolicyOAuth2TokenFetcher::UseFakeTokensForTesting() { | |
305 use_fake_tokens_for_testing_ = true; | |
306 } | |
307 | |
308 // static | |
309 PolicyOAuth2TokenFetcher* PolicyOAuth2TokenFetcher::CreateInstance() { | |
310 if (use_fake_tokens_for_testing_) | |
311 return new PolicyOAuth2TokenFetcherFake(); | |
312 return new PolicyOAuth2TokenFetcherImpl(); | |
313 } | |
314 | |
315 PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher() {} | |
316 | |
317 PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() {} | |
318 | |
163 } // namespace policy | 319 } // namespace policy |
OLD | NEW |