Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: google_apis/gaia/ubertoken_fetcher.cc

Issue 597003002: UbertokenFecther should invalidate access token if it's invalid. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@z359700
Patch Set: merged Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « google_apis/gaia/ubertoken_fetcher.h ('k') | google_apis/gaia/ubertoken_fetcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "google_apis/gaia/ubertoken_fetcher.h" 5 #include "google_apis/gaia/ubertoken_fetcher.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/time/time.h"
10 #include "google_apis/gaia/gaia_auth_fetcher.h" 12 #include "google_apis/gaia/gaia_auth_fetcher.h"
11 #include "google_apis/gaia/gaia_constants.h" 13 #include "google_apis/gaia/gaia_constants.h"
12 #include "google_apis/gaia/google_service_auth_error.h" 14 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "google_apis/gaia/oauth2_token_service.h" 15 #include "google_apis/gaia/oauth2_token_service.h"
14 16
17 const int UbertokenFetcher::kMaxRetries = 3;
18
15 UbertokenFetcher::UbertokenFetcher( 19 UbertokenFetcher::UbertokenFetcher(
16 OAuth2TokenService* token_service, 20 OAuth2TokenService* token_service,
17 UbertokenConsumer* consumer, 21 UbertokenConsumer* consumer,
18 net::URLRequestContextGetter* request_context) 22 net::URLRequestContextGetter* request_context)
19 : OAuth2TokenService::Consumer("uber_token_fetcher"), 23 : OAuth2TokenService::Consumer("uber_token_fetcher"),
20 token_service_(token_service), 24 token_service_(token_service),
21 consumer_(consumer), 25 consumer_(consumer),
22 request_context_(request_context) { 26 request_context_(request_context),
27 retry_number_(0),
28 second_access_token_request_(false) {
23 DCHECK(token_service); 29 DCHECK(token_service);
24 DCHECK(consumer); 30 DCHECK(consumer);
25 DCHECK(request_context); 31 DCHECK(request_context);
26 } 32 }
27 33
28 UbertokenFetcher::~UbertokenFetcher() { 34 UbertokenFetcher::~UbertokenFetcher() {
29 } 35 }
30 36
31 void UbertokenFetcher::StartFetchingToken(const std::string& account_id) { 37 void UbertokenFetcher::StartFetchingToken(const std::string& account_id) {
32 OAuth2TokenService::ScopeSet scopes; 38 DCHECK(!account_id.empty());
33 scopes.insert(GaiaConstants::kOAuth1LoginScope); 39 account_id_ = account_id;
34 access_token_request_ = 40 retry_number_ = 0;
35 token_service_->StartRequest(account_id, scopes, this); 41 second_access_token_request_ = false;
42 gaia_auth_fetcher_.reset();
43 access_token_request_.reset();
44 retry_timer_.Stop();
45 RequestAccessToken();
36 } 46 }
37 47
38 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) { 48 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) {
39 consumer_->OnUbertokenSuccess(token); 49 consumer_->OnUbertokenSuccess(token);
40 } 50 }
41 51
42 void UbertokenFetcher::OnUberAuthTokenFailure( 52 void UbertokenFetcher::OnUberAuthTokenFailure(
43 const GoogleServiceAuthError& error) { 53 const GoogleServiceAuthError& error) {
54 // Retry only transient errors.
55 bool should_retry =
56 error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
57 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE;
58 if (should_retry) {
59 if (retry_number_ < kMaxRetries) {
60 // Calculate an exponential backoff with randomness of less than 1 sec.
61 double backoff = base::RandDouble() + (1 << retry_number_);
62 ++retry_number_;
63 retry_timer_.Stop();
64 retry_timer_.Start(FROM_HERE,
65 base::TimeDelta::FromSecondsD(backoff),
66 this,
67 &UbertokenFetcher::ExchangeTokens);
68 return;
69 }
70 } else {
guohui 2014/09/26 17:22:31 can we distinguish the cases 1) both refresh token
Roger Tawa OOO till Jul 10th 2014/09/26 17:49:55 Opened bug https://crbug.com/418158 to track this.
71 // The access token is invalid. Tell the token service.
72 OAuth2TokenService::ScopeSet scopes;
73 scopes.insert(GaiaConstants::kOAuth1LoginScope);
74 token_service_->InvalidateToken(account_id_, scopes, access_token_);
75
76 // In case the access was just stale, try one more time.
77 if (!second_access_token_request_) {
78 second_access_token_request_ = true;
79 RequestAccessToken();
80 return;
81 }
82 }
83
44 consumer_->OnUbertokenFailure(error); 84 consumer_->OnUbertokenFailure(error);
45 } 85 }
46 86
47 void UbertokenFetcher::OnGetTokenSuccess( 87 void UbertokenFetcher::OnGetTokenSuccess(
48 const OAuth2TokenService::Request* request, 88 const OAuth2TokenService::Request* request,
49 const std::string& access_token, 89 const std::string& access_token,
50 const base::Time& expiration_time) { 90 const base::Time& expiration_time) {
91 DCHECK(!access_token.empty());
92 access_token_ = access_token;
51 access_token_request_.reset(); 93 access_token_request_.reset();
52 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, 94 ExchangeTokens();
53 GaiaConstants::kChromeSource,
54 request_context_));
55 gaia_auth_fetcher_->StartTokenFetchForUberAuthExchange(access_token);
56 } 95 }
57 96
58 void UbertokenFetcher::OnGetTokenFailure( 97 void UbertokenFetcher::OnGetTokenFailure(
59 const OAuth2TokenService::Request* request, 98 const OAuth2TokenService::Request* request,
60 const GoogleServiceAuthError& error) { 99 const GoogleServiceAuthError& error) {
61 access_token_request_.reset(); 100 access_token_request_.reset();
62 consumer_->OnUbertokenFailure(error); 101 consumer_->OnUbertokenFailure(error);
63 } 102 }
103
104 void UbertokenFetcher::RequestAccessToken() {
105 OAuth2TokenService::ScopeSet scopes;
106 scopes.insert(GaiaConstants::kOAuth1LoginScope);
107 access_token_request_ =
108 token_service_->StartRequest(account_id_, scopes, this);
109 }
110
111 void UbertokenFetcher::ExchangeTokens() {
112 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this,
113 GaiaConstants::kChromeSource,
114 request_context_));
115 gaia_auth_fetcher_->StartTokenFetchForUberAuthExchange(access_token_);
116 }
OLDNEW
« no previous file with comments | « google_apis/gaia/ubertoken_fetcher.h ('k') | google_apis/gaia/ubertoken_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698