summaryrefslogtreecommitdiffstats
path: root/google_apis
diff options
context:
space:
mode:
authorzelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-04 23:53:25 +0000
committerzelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-04 23:53:25 +0000
commit2a0ff28378173300b81b2874736c30a6c48d0029 (patch)
tree0eb6c0bab63a475a355f6ae73cea78219a789504 /google_apis
parent64c15a3b3ac83e94f23c4e569e28f31bf95175ca (diff)
downloadchromium_src-2a0ff28378173300b81b2874736c30a6c48d0029.zip
chromium_src-2a0ff28378173300b81b2874736c30a6c48d0029.tar.gz
chromium_src-2a0ff28378173300b81b2874736c30a6c48d0029.tar.bz2
Handling of multiple concurrent requests from different clients in OAuth2TokenService
BUG=268937 TEST=OAuth2TokenServiceTest.SameScopesRequestedForDifferentClients TBR=tim (for chrome/browser/sync) Review URL: https://chromiumcodereview.appspot.com/22581003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221303 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r--google_apis/gaia/oauth2_access_token_fetcher.cc13
-rw-r--r--google_apis/gaia/oauth2_access_token_fetcher.h12
-rw-r--r--google_apis/gaia/oauth2_token_service.cc182
-rw-r--r--google_apis/gaia/oauth2_token_service.h75
-rw-r--r--google_apis/gaia/oauth2_token_service_unittest.cc155
5 files changed, 348 insertions, 89 deletions
diff --git a/google_apis/gaia/oauth2_access_token_fetcher.cc b/google_apis/gaia/oauth2_access_token_fetcher.cc
index 456251f..9fd46d7 100644
--- a/google_apis/gaia/oauth2_access_token_fetcher.cc
+++ b/google_apis/gaia/oauth2_access_token_fetcher.cc
@@ -56,13 +56,14 @@ static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) {
}
}
-static URLFetcher* CreateFetcher(URLRequestContextGetter* getter,
+static URLFetcher* CreateFetcher(int id,
+ URLRequestContextGetter* getter,
const GURL& url,
const std::string& body,
URLFetcherDelegate* delegate) {
bool empty_body = body.empty();
URLFetcher* result = net::URLFetcher::Create(
- 0, url,
+ id, url,
empty_body ? URLFetcher::GET : URLFetcher::POST,
delegate);
@@ -82,6 +83,8 @@ static URLFetcher* CreateFetcher(URLRequestContextGetter* getter,
}
} // namespace
+int OAuth2AccessTokenFetcher::last_fetcher_id_ = 0;
+
OAuth2AccessTokenFetcher::OAuth2AccessTokenFetcher(
OAuth2AccessTokenConsumer* consumer,
URLRequestContextGetter* getter)
@@ -110,6 +113,7 @@ void OAuth2AccessTokenFetcher::StartGetAccessToken() {
CHECK_EQ(INITIAL, state_);
state_ = GET_ACCESS_TOKEN_STARTED;
fetcher_.reset(CreateFetcher(
+ last_fetcher_id_++,
getter_,
MakeGetAccessTokenUrl(),
MakeGetAccessTokenBody(
@@ -231,3 +235,8 @@ bool OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse(
return dict->GetString(kAccessTokenKey, access_token) &&
dict->GetInteger(kExpiresInKey, expires_in);
}
+
+// static
+void OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest() {
+ last_fetcher_id_ = 0;
+}
diff --git a/google_apis/gaia/oauth2_access_token_fetcher.h b/google_apis/gaia/oauth2_access_token_fetcher.h
index 11ac6ea..e9e1520 100644
--- a/google_apis/gaia/oauth2_access_token_fetcher.h
+++ b/google_apis/gaia/oauth2_access_token_fetcher.h
@@ -26,6 +26,10 @@ class URLRequestContextGetter;
class URLRequestStatus;
}
+namespace policy {
+ class UserPolicySigninServiceTest;
+}
+
// Abstracts the details to get OAuth2 access token token from
// OAuth2 refresh token.
// See "Using the Refresh Token" section in:
@@ -94,6 +98,9 @@ class OAuth2AccessTokenFetcher : public net::URLFetcherDelegate {
std::string* access_token,
int* expires_in);
+ // Resets |last_fetcher_id_| to 0.
+ static void ResetLastFetcherIdForTest();
+
// State that is set during construction.
OAuth2AccessTokenConsumer* const consumer_;
net::URLRequestContextGetter* const getter_;
@@ -106,7 +113,12 @@ class OAuth2AccessTokenFetcher : public net::URLFetcherDelegate {
std::string refresh_token_;
std::vector<std::string> scopes_;
+ // The last fetcher id.
+ static int last_fetcher_id_;
+
friend class OAuth2AccessTokenFetcherTest;
+ friend class OAuth2TokenServiceTest;
+ friend class policy::UserPolicySigninServiceTest;
FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest,
ParseGetAccessTokenResponse);
FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest,
diff --git a/google_apis/gaia/oauth2_token_service.cc b/google_apis/gaia/oauth2_token_service.cc
index 0834c60..828f4cc6 100644
--- a/google_apis/gaia/oauth2_token_service.cc
+++ b/google_apis/gaia/oauth2_token_service.cc
@@ -15,12 +15,57 @@
#include "base/timer/timer.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
-#include "google_apis/gaia/oauth2_access_token_consumer.h"
-#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
int OAuth2TokenService::max_fetch_retry_num_ = 5;
+OAuth2TokenService::ClientScopeSet::ClientScopeSet(
+ const std::string& client_id,
+ const ScopeSet& scopes)
+ : client_id(client_id),
+ scopes(scopes) {
+}
+
+OAuth2TokenService::ClientScopeSet::~ClientScopeSet() {
+}
+
+bool OAuth2TokenService::ClientScopeSet::operator<(
+ const ClientScopeSet& s) const {
+ if (client_id < s.client_id)
+ return true;
+ else if (s.client_id < client_id)
+ return false;
+
+ return scopes < s.scopes;
+}
+
+OAuth2TokenService::FetchParameters::FetchParameters(
+ const std::string& client_id,
+ const std::string& refresh_token,
+ const ScopeSet& scopes)
+ : client_id(client_id),
+ refresh_token(refresh_token),
+ scopes(scopes) {
+}
+
+OAuth2TokenService::FetchParameters::~FetchParameters() {
+}
+
+bool OAuth2TokenService::FetchParameters::operator<(
+ const FetchParameters& p) const {
+ if (client_id < p.client_id)
+ return true;
+ else if (p.client_id < client_id)
+ return false;
+
+ if (refresh_token < p.refresh_token)
+ return true;
+ else if (p.refresh_token < refresh_token)
+ return false;
+
+ return scopes < p.scopes;
+}
+
OAuth2TokenService::RequestImpl::RequestImpl(
OAuth2TokenService::Consumer* consumer)
: consumer_(consumer) {
@@ -41,6 +86,9 @@ void OAuth2TokenService::RequestImpl::InformConsumer(
consumer_->OnGetTokenFailure(this, error);
}
+// Class that fetches an OAuth2 access token for a given set of scopes and
+// OAuth2 refresh token.
+
// Class that fetches OAuth2 access tokens for given scopes and refresh token.
//
// It aims to meet OAuth2TokenService's requirements on token fetching. Retry
@@ -57,17 +105,17 @@ void OAuth2TokenService::RequestImpl::InformConsumer(
//
// Requests that are waiting for the fetching results of this Fetcher can be
// added to the Fetcher by calling
-// OAuth2TokenService::Fetcher::AddWaitingRequest() before the Fetcher completes
-// fetching.
+// OAuth2TokenService::Fetcher::AddWaitingRequest() before the Fetcher
+// completes fetching.
//
-// The waiting requests are taken as weak pointers and they can be deleted. The
-// waiting requests will be called back with fetching results if they are not
-// deleted
+// The waiting requests are taken as weak pointers and they can be deleted.
+// The waiting requests will be called back with fetching results if they are
+// not deleted
// - when the Fetcher completes fetching, if the Fetcher is not destructed
// before it completes fetching, or
// - when the Fetcher is destructed if the Fetcher is destructed before it
-// completes fetching (in this case, the waiting requests will be called back
-// with error).
+// completes fetching (in this case, the waiting requests will be called
+// back with error).
class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
public:
// Creates a Fetcher and starts fetching an OAuth2 access token for
@@ -75,20 +123,24 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
// The given |oauth2_token_service| will be informed when fetching is done.
static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service,
net::URLRequestContextGetter* getter,
- const std::string& chrome_client_id,
- const std::string& chrome_client_secret,
+ const std::string& client_id,
+ const std::string& client_secret,
const std::string& refresh_token,
- const OAuth2TokenService::ScopeSet& scopes,
+ const ScopeSet& scopes,
base::WeakPtr<RequestImpl> waiting_request);
virtual ~Fetcher();
// Add a request that is waiting for the result of this Fetcher.
void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request);
+ // Returns count of waiting requests.
+ size_t GetWaitingRequestCount() const;
+
void Cancel();
- const OAuth2TokenService::ScopeSet& GetScopeSet() const;
+ const ScopeSet& GetScopeSet() const;
const std::string& GetRefreshToken() const;
+ const std::string& GetClientId() const;
// The error result from this fetcher.
const GoogleServiceAuthError& error() const { return error_; }
@@ -97,13 +149,14 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
// OAuth2AccessTokenConsumer
virtual void OnGetTokenSuccess(const std::string& access_token,
const base::Time& expiration_date) OVERRIDE;
- virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
+ virtual void OnGetTokenFailure(
+ const GoogleServiceAuthError& error) OVERRIDE;
private:
Fetcher(OAuth2TokenService* oauth2_token_service,
net::URLRequestContextGetter* getter,
- const std::string& chrome_client_id,
- const std::string& chrome_client_secret,
+ const std::string& client_id,
+ const std::string& client_secret,
const std::string& refresh_token,
const OAuth2TokenService::ScopeSet& scopes,
base::WeakPtr<RequestImpl> waiting_request);
@@ -120,11 +173,11 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
OAuth2TokenService* const oauth2_token_service_;
scoped_refptr<net::URLRequestContextGetter> getter_;
const std::string refresh_token_;
- const OAuth2TokenService::ScopeSet scopes_;
+ const ScopeSet scopes_;
std::vector<base::WeakPtr<RequestImpl> > waiting_requests_;
int retry_number_;
- base::OneShotTimer<OAuth2TokenService::Fetcher> retry_timer_;
+ base::OneShotTimer<Fetcher> retry_timer_;
scoped_ptr<OAuth2AccessTokenFetcher> fetcher_;
// Variables that store fetch results.
@@ -133,9 +186,10 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
GoogleServiceAuthError error_;
std::string access_token_;
base::Time expiration_date_;
+
// OAuth2 client id and secret.
- std::string chrome_client_id_;
- std::string chrome_client_secret_;
+ std::string client_id_;
+ std::string client_secret_;
DISALLOW_COPY_AND_ASSIGN(Fetcher);
};
@@ -144,16 +198,16 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer {
OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart(
OAuth2TokenService* oauth2_token_service,
net::URLRequestContextGetter* getter,
- const std::string& chrome_client_id,
- const std::string& chrome_client_secret,
+ const std::string& client_id,
+ const std::string& client_secret,
const std::string& refresh_token,
const OAuth2TokenService::ScopeSet& scopes,
base::WeakPtr<RequestImpl> waiting_request) {
OAuth2TokenService::Fetcher* fetcher = new Fetcher(
oauth2_token_service,
getter,
- chrome_client_id,
- chrome_client_secret,
+ client_id,
+ client_secret,
refresh_token,
scopes,
waiting_request);
@@ -164,8 +218,8 @@ OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart(
OAuth2TokenService::Fetcher::Fetcher(
OAuth2TokenService* oauth2_token_service,
net::URLRequestContextGetter* getter,
- const std::string& chrome_client_id,
- const std::string& chrome_client_secret,
+ const std::string& client_id,
+ const std::string& client_secret,
const std::string& refresh_token,
const OAuth2TokenService::ScopeSet& scopes,
base::WeakPtr<RequestImpl> waiting_request)
@@ -175,8 +229,8 @@ OAuth2TokenService::Fetcher::Fetcher(
scopes_(scopes),
retry_number_(0),
error_(GoogleServiceAuthError::SERVICE_UNAVAILABLE),
- chrome_client_id_(chrome_client_id),
- chrome_client_secret_(chrome_client_secret) {
+ client_id_(client_id),
+ client_secret_(client_secret) {
DCHECK(oauth2_token_service_);
DCHECK(getter_.get());
DCHECK(refresh_token_.length());
@@ -191,8 +245,8 @@ OAuth2TokenService::Fetcher::~Fetcher() {
void OAuth2TokenService::Fetcher::Start() {
fetcher_.reset(new OAuth2AccessTokenFetcher(this, getter_.get()));
- fetcher_->Start(chrome_client_id_,
- chrome_client_secret_,
+ fetcher_->Start(client_id_,
+ client_secret_,
refresh_token_,
std::vector<std::string>(scopes_.begin(), scopes_.end()));
retry_timer_.Stop();
@@ -212,7 +266,8 @@ void OAuth2TokenService::Fetcher::OnGetTokenSuccess(
// we still inform all waiting Consumers of a successful token fetch below.
// This is intentional -- some consumers may need the token for cleanup
// tasks. https://chromiumcodereview.appspot.com/11312124/
- oauth2_token_service_->RegisterCacheEntry(refresh_token_,
+ oauth2_token_service_->RegisterCacheEntry(client_id_,
+ refresh_token_,
scopes_,
access_token_,
expiration_date_);
@@ -281,6 +336,10 @@ void OAuth2TokenService::Fetcher::AddWaitingRequest(
waiting_requests_.push_back(waiting_request);
}
+size_t OAuth2TokenService::Fetcher::GetWaitingRequestCount() const {
+ return waiting_requests_.size();
+}
+
void OAuth2TokenService::Fetcher::Cancel() {
fetcher_.reset();
retry_timer_.Stop();
@@ -297,6 +356,10 @@ const std::string& OAuth2TokenService::Fetcher::GetRefreshToken() const {
return refresh_token_;
}
+const std::string& OAuth2TokenService::Fetcher::GetClientId() const {
+ return client_id_;
+}
+
OAuth2TokenService::Request::Request() {
}
@@ -389,8 +452,9 @@ OAuth2TokenService::StartRequestForClientWithContext(
return request.PassAs<Request>();
}
- if (HasCacheEntry(scopes)) {
- StartCacheLookupRequest(request.get(), scopes, consumer);
+ ClientScopeSet client_scopes(client_id, scopes);
+ if (HasCacheEntry(client_scopes)) {
+ StartCacheLookupRequest(request.get(), client_scopes, consumer);
} else {
FetchOAuth2Token(request.get(),
getter,
@@ -411,7 +475,9 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request,
// If there is already a pending fetcher for |scopes| and |refresh_token|,
// simply register this |request| for those results rather than starting
// a new fetcher.
- FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes);
+ FetchParameters fetch_parameters = FetchParameters(client_id,
+ refresh_token,
+ scopes);
std::map<FetchParameters, Fetcher*>::iterator iter =
pending_fetchers_.find(fetch_parameters);
if (iter != pending_fetchers_.end()) {
@@ -431,10 +497,10 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request,
void OAuth2TokenService::StartCacheLookupRequest(
RequestImpl* request,
- const OAuth2TokenService::ScopeSet& scopes,
+ const OAuth2TokenService::ClientScopeSet& client_scopes,
OAuth2TokenService::Consumer* consumer) {
- CHECK(HasCacheEntry(scopes));
- const CacheEntry* cache_entry = GetCacheEntry(scopes);
+ CHECK(HasCacheEntry(client_scopes));
+ const CacheEntry* cache_entry = GetCacheEntry(client_scopes);
base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
&RequestImpl::InformConsumer,
request->AsWeakPtr(),
@@ -446,7 +512,10 @@ void OAuth2TokenService::StartCacheLookupRequest(
void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes,
const std::string& invalid_token) {
DCHECK(CalledOnValidThread());
- RemoveCacheEntry(scopes, invalid_token);
+ RemoveCacheEntry(
+ ClientScopeSet(GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
+ scopes),
+ invalid_token);
}
void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) {
@@ -483,23 +552,25 @@ void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) {
// Then by (2), |fetcher| is recorded in |pending_fetchers_|.
// Then by (3), |fetcher_| is mapped to its refresh token and ScopeSet.
std::map<FetchParameters, Fetcher*>::iterator iter =
- pending_fetchers_.find(std::make_pair(
- fetcher->GetRefreshToken(), fetcher->GetScopeSet()));
+ pending_fetchers_.find(FetchParameters(
+ fetcher->GetClientId(),
+ fetcher->GetRefreshToken(),
+ fetcher->GetScopeSet()));
DCHECK(iter != pending_fetchers_.end());
DCHECK_EQ(fetcher, iter->second);
pending_fetchers_.erase(iter);
}
bool OAuth2TokenService::HasCacheEntry(
- const OAuth2TokenService::ScopeSet& scopes) {
- const CacheEntry* cache_entry = GetCacheEntry(scopes);
+ const ClientScopeSet& client_scopes) {
+ const CacheEntry* cache_entry = GetCacheEntry(client_scopes);
return cache_entry && cache_entry->access_token.length();
}
const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry(
- const OAuth2TokenService::ScopeSet& scopes) {
+ const ClientScopeSet& client_scopes) {
DCHECK(CalledOnValidThread());
- TokenCache::iterator token_iterator = token_cache_.find(scopes);
+ TokenCache::iterator token_iterator = token_cache_.find(client_scopes);
if (token_iterator == token_cache_.end())
return NULL;
if (token_iterator->second.expiration_date <= base::Time::Now()) {
@@ -510,10 +581,10 @@ const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry(
}
bool OAuth2TokenService::RemoveCacheEntry(
- const OAuth2TokenService::ScopeSet& scopes,
+ const ClientScopeSet& client_scopes,
const std::string& token_to_remove) {
DCHECK(CalledOnValidThread());
- TokenCache::iterator token_iterator = token_cache_.find(scopes);
+ TokenCache::iterator token_iterator = token_cache_.find(client_scopes);
if (token_iterator != token_cache_.end() &&
token_iterator->second.access_token == token_to_remove) {
token_cache_.erase(token_iterator);
@@ -523,13 +594,15 @@ bool OAuth2TokenService::RemoveCacheEntry(
}
void OAuth2TokenService::RegisterCacheEntry(
+ const std::string& client_id,
const std::string& refresh_token,
const OAuth2TokenService::ScopeSet& scopes,
const std::string& access_token,
const base::Time& expiration_date) {
DCHECK(CalledOnValidThread());
- CacheEntry& token = token_cache_[scopes];
+ CacheEntry& token = token_cache_[ClientScopeSet(client_id,
+ scopes)];
token.access_token = access_token;
token.expiration_date = expiration_date;
}
@@ -561,7 +634,7 @@ void OAuth2TokenService::CancelRequestsForToken(
pending_fetchers_.begin();
iter != pending_fetchers_.end();
++iter) {
- if (iter->first.first == refresh_token)
+ if (iter->first.refresh_token == refresh_token)
fetchers_to_cancel.push_back(iter->second);
}
CancelFetchers(fetchers_to_cancel);
@@ -606,3 +679,16 @@ void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing(
DCHECK(CalledOnValidThread());
max_fetch_retry_num_ = max_retries;
}
+
+size_t OAuth2TokenService::GetNumPendingRequestsForTesting(
+ const std::string& client_id,
+ const std::string& refresh_token,
+ const ScopeSet& scopes) const {
+ PendingFetcherMap::const_iterator iter = pending_fetchers_.find(
+ OAuth2TokenService::FetchParameters(
+ client_id,
+ refresh_token,
+ scopes));
+ return iter == pending_fetchers_.end() ?
+ 0 : iter->second->GetWaitingRequestCount();
+}
diff --git a/google_apis/gaia/oauth2_token_service.h b/google_apis/gaia/oauth2_token_service.h
index 967dc93..b58c563 100644
--- a/google_apis/gaia/oauth2_token_service.h
+++ b/google_apis/gaia/oauth2_token_service.h
@@ -10,12 +10,16 @@
#include <string>
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/non_thread_safe.h"
#include "base/time/time.h"
+#include "base/timer/timer.h"
#include "google_apis/gaia/google_service_auth_error.h"
+#include "google_apis/gaia/oauth2_access_token_consumer.h"
+#include "google_apis/gaia/oauth2_access_token_fetcher.h"
namespace net {
class URLRequestContextGetter;
@@ -145,8 +149,23 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Return the current number of entries in the cache.
int cache_size_for_testing() const;
void set_max_authorization_token_fetch_retries_for_testing(int max_retries);
+ // Returns the current number of pending fetchers matching given params.
+ size_t GetNumPendingRequestsForTesting(
+ const std::string& client_id,
+ const std::string& refresh_token,
+ const ScopeSet& scopes) const;
protected:
+ struct ClientScopeSet {
+ ClientScopeSet(const std::string& client_id,
+ const ScopeSet& scopes);
+ ~ClientScopeSet();
+ bool operator<(const ClientScopeSet& set) const;
+
+ std::string client_id;
+ ScopeSet scopes;
+ };
+
// Implements a cancelable |OAuth2TokenService::Request|, which should be
// operated on the UI thread.
// TODO(davidroche): move this out of header file.
@@ -178,19 +197,20 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Add a new entry to the cache.
// Subclasses can override if there are implementation-specific reasons
// that an access token should ever not be cached.
- virtual void RegisterCacheEntry(const std::string& refresh_token,
+ virtual void RegisterCacheEntry(const std::string& client_id,
+ const std::string& refresh_token,
const ScopeSet& scopes,
const std::string& access_token,
const base::Time& expiration_date);
// Returns true if GetCacheEntry would return a valid cache entry for the
// given scopes.
- bool HasCacheEntry(const ScopeSet& scopes);
+ bool HasCacheEntry(const ClientScopeSet& client_scopes);
// Posts a task to fire the Consumer callback with the cached token. Must
// Must only be called if HasCacheEntry() returns true.
void StartCacheLookupRequest(RequestImpl* request,
- const ScopeSet& scopes,
+ const ClientScopeSet& client_scopes,
Consumer* consumer);
// Clears the internal token cache.
@@ -208,10 +228,6 @@ class OAuth2TokenService : public base::NonThreadSafe {
void FireRefreshTokensLoaded();
void FireRefreshTokensCleared();
- // Derived classes must provide a request context used for fetching access
- // tokens with the |StartRequest| method.
- virtual net::URLRequestContextGetter* GetRequestContext() = 0;
-
// Fetches an OAuth token for the specified client/scopes. Virtual so it can
// be overridden for tests and for platform-specific behavior on Android.
virtual void FetchOAuth2Token(RequestImpl* request,
@@ -219,13 +235,32 @@ class OAuth2TokenService : public base::NonThreadSafe {
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes);
-
private:
- // Class that fetches an OAuth2 access token for a given set of scopes and
- // OAuth2 refresh token.
class Fetcher;
friend class Fetcher;
+ // The parameters used to fetch an OAuth2 access token.
+ struct FetchParameters {
+ FetchParameters(const std::string& client_id,
+ const std::string& refresh_token,
+ const ScopeSet& scopes);
+ ~FetchParameters();
+ bool operator<(const FetchParameters& params) const;
+
+ // OAuth2 client id.
+ std::string client_id;
+ // Refresh token used for minting access tokens within this request.
+ std::string refresh_token;
+ // URL scopes for the requested access token.
+ ScopeSet scopes;
+ };
+
+ typedef std::map<FetchParameters, Fetcher*> PendingFetcherMap;
+
+ // Derived classes must provide a request context used for fetching access
+ // tokens with the |StartRequest| method.
+ virtual net::URLRequestContextGetter* GetRequestContext() = 0;
+
// Struct that contains the information of an OAuth2 access token.
struct CacheEntry {
std::string access_token;
@@ -244,14 +279,14 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Returns a currently valid OAuth2 access token for the given set of scopes,
// or NULL if none have been cached. Note the user of this method should
- // ensure no entry with the same |scopes| is added before the usage of the
- // returned entry is done.
- const CacheEntry* GetCacheEntry(const ScopeSet& scopes);
+ // ensure no entry with the same |client_scopes| is added before the usage of
+ // the returned entry is done.
+ const CacheEntry* GetCacheEntry(const ClientScopeSet& client_scopes);
// Removes an access token for the given set of scopes from the cache.
// Returns true if the entry was removed, otherwise false.
- bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes,
+ bool RemoveCacheEntry(const ClientScopeSet& client_scopes,
const std::string& token_to_remove);
@@ -262,15 +297,12 @@ class OAuth2TokenService : public base::NonThreadSafe {
void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel);
// The cache of currently valid tokens.
- typedef std::map<ScopeSet, CacheEntry> TokenCache;
+ typedef std::map<ClientScopeSet, CacheEntry> TokenCache;
TokenCache token_cache_;
- // The parameters (refresh token and scope set) used to fetch an OAuth2 access
- // token.
- typedef std::pair<std::string, ScopeSet> FetchParameters;
// A map from fetch parameters to a fetcher that is fetching an OAuth2 access
// token using these parameters.
- std::map<FetchParameters, Fetcher*> pending_fetchers_;
+ PendingFetcherMap pending_fetchers_;
// List of observers to notify when token availability changes.
// Makes sure list is empty on destruction.
@@ -279,6 +311,11 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Maximum number of retries in fetching an OAuth2 access token.
static int max_fetch_retry_num_;
+ FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, ClientScopeSetOrderTest);
+ FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, FetchParametersOrderTest);
+ FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest,
+ SameScopesRequestedForDifferentClients);
+
DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
};
diff --git a/google_apis/gaia/oauth2_token_service_unittest.cc b/google_apis/gaia/oauth2_token_service_unittest.cc
index 9937268..8f4e922 100644
--- a/google_apis/gaia/oauth2_token_service_unittest.cc
+++ b/google_apis/gaia/oauth2_token_service_unittest.cc
@@ -9,6 +9,7 @@
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_consumer.h"
+#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "google_apis/gaia/oauth2_token_service.h"
#include "google_apis/gaia/oauth2_token_service_test_util.h"
#include "net/http/http_status_code.h"
@@ -78,6 +79,7 @@ class OAuth2TokenServiceTest : public testing::Test {
virtual void TearDown() OVERRIDE {
// Makes sure that all the clean up tasks are run.
base::RunLoop().RunUntilIdle();
+ OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest();
}
protected:
@@ -105,7 +107,7 @@ TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
fetcher->SetResponseString(std::string());
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -123,7 +125,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -152,7 +154,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -167,7 +169,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
base::RunLoop().RunUntilIdle();
// No new network fetcher.
- EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
+ EXPECT_EQ(1U, factory_.GetFetcherCount());
EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
EXPECT_EQ("token", consumer_.last_token_);
@@ -178,8 +180,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
- fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ fetcher = factory_.GetFetcherByID(1);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -198,7 +201,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 0));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -214,8 +217,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) {
EXPECT_EQ(0, consumer_.number_of_errors_);
// Network failure.
- fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ fetcher = factory_.GetFetcherByID(1);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
fetcher->SetResponseString(std::string());
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -233,7 +237,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 0));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -248,8 +252,9 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) {
EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
- fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ fetcher = factory_.GetFetcherByID(1);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("another token", 0));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -267,7 +272,7 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
request.reset();
@@ -285,6 +290,7 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) {
std::set<std::string>(), &consumer_));
base::RunLoop().RunUntilIdle();
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -312,6 +318,7 @@ TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) {
request.reset();
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -326,6 +333,7 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) {
std::set<std::string>(), &consumer_));
base::RunLoop().RunUntilIdle();
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -337,7 +345,7 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) {
oauth2_service_->set_refresh_token("");
request = oauth2_service_->StartRequest(std::set<std::string>(), &consumer_);
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
+ EXPECT_EQ(1U, factory_.GetFetcherCount());
EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
EXPECT_EQ(1, consumer_.number_of_errors_);
}
@@ -352,7 +360,9 @@ TEST_F(OAuth2TokenServiceTest,
scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
scopes, &consumer_));
base::RunLoop().RunUntilIdle();
+ ASSERT_EQ(1U, factory_.GetFetcherCount());
net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher1);
// Note |request| is still pending when the refresh token changes.
oauth2_service_->set_refresh_token("second refreshToken");
@@ -364,7 +374,8 @@ TEST_F(OAuth2TokenServiceTest,
oauth2_service_->StartRequest(scopes, &consumer2));
base::RunLoop().RunUntilIdle();
- net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(1);
fetcher2->set_response_code(net::HTTP_OK);
fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600));
fetcher2->delegate()->OnURLFetchComplete(fetcher2);
@@ -412,7 +423,8 @@ TEST_F(OAuth2TokenServiceTest, RetryingConsumer) {
EXPECT_EQ(0, consumer.number_of_successful_tokens_);
EXPECT_EQ(1, consumer.number_of_errors_);
- fetcher = factory_.GetFetcherByID(0);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ fetcher = factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
fetcher->SetResponseString(std::string());
@@ -433,7 +445,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -448,7 +460,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
base::RunLoop().RunUntilIdle();
// No new network fetcher.
- EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
+ ASSERT_EQ(1U, factory_.GetFetcherCount());
EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
EXPECT_EQ("token", consumer_.last_token_);
@@ -460,8 +472,9 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
EXPECT_EQ(0, consumer_.number_of_errors_);
- fetcher = factory_.GetFetcherByID(0);
- EXPECT_TRUE(fetcher);
+ ASSERT_EQ(2U, factory_.GetFetcherCount());
+ fetcher = factory_.GetFetcherByID(1);
+ ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
fetcher->delegate()->OnURLFetchComplete(fetcher);
@@ -520,3 +533,105 @@ TEST_F(OAuth2TokenServiceTest, CancelRequestsForToken) {
EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
EXPECT_EQ(3, consumer_.number_of_errors_);
}
+
+TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) {
+ std::string client_id_1("client1");
+ std::string client_secret_1("secret1");
+ std::string client_id_2("client2");
+ std::string client_secret_2("secret2");
+ std::set<std::string> scope_set;
+ scope_set.insert("scope1");
+ scope_set.insert("scope2");
+
+ std::string refresh_token("refreshToken");
+ oauth2_service_->set_refresh_token(refresh_token);
+
+ scoped_ptr<OAuth2TokenService::Request> request1(
+ oauth2_service_->StartRequestForClient(client_id_1,
+ client_secret_1,
+ scope_set,
+ &consumer_));
+ scoped_ptr<OAuth2TokenService::Request> request2(
+ oauth2_service_->StartRequestForClient(client_id_2,
+ client_secret_2,
+ scope_set,
+ &consumer_));
+ // Start a request that should be duplicate of |request1|.
+ scoped_ptr<OAuth2TokenService::Request> request3(
+ oauth2_service_->StartRequestForClient(client_id_1,
+ client_secret_1,
+ scope_set,
+ &consumer_));
+ base::RunLoop().RunUntilIdle();
+
+ ASSERT_EQ(2U,
+ oauth2_service_->GetNumPendingRequestsForTesting(
+ client_id_1,
+ refresh_token,
+ scope_set));
+ ASSERT_EQ(1U,
+ oauth2_service_->GetNumPendingRequestsForTesting(
+ client_id_2,
+ refresh_token,
+ scope_set));
+}
+
+TEST_F(OAuth2TokenServiceTest, ClientScopeSetOrderTest) {
+ OAuth2TokenService::ScopeSet set_0;
+ OAuth2TokenService::ScopeSet set_1;
+ set_1.insert("1");
+
+ OAuth2TokenService::ClientScopeSet sets[] = {
+ OAuth2TokenService::ClientScopeSet("0", set_0),
+ OAuth2TokenService::ClientScopeSet("0", set_1),
+ OAuth2TokenService::ClientScopeSet("1", set_0),
+ OAuth2TokenService::ClientScopeSet("1", set_1),
+ };
+
+ for (size_t i = 0; i < arraysize(sets); i++) {
+ for (size_t j = 0; j < arraysize(sets); j++) {
+ if (i == j) {
+ EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j;
+ } else if (i < j) {
+ EXPECT_TRUE(sets[i] < sets[j]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j;
+ } else {
+ EXPECT_TRUE(sets[j] < sets[i]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j;
+ }
+ }
+ }
+}
+
+TEST_F(OAuth2TokenServiceTest, FetchParametersOrderTest) {
+ OAuth2TokenService::ScopeSet set_0;
+ OAuth2TokenService::ScopeSet set_1;
+ set_1.insert("1");
+
+ OAuth2TokenService::FetchParameters params[] = {
+ OAuth2TokenService::FetchParameters("0", "0", set_0),
+ OAuth2TokenService::FetchParameters("0", "0", set_1),
+ OAuth2TokenService::FetchParameters("0", "1", set_0),
+ OAuth2TokenService::FetchParameters("0", "1", set_1),
+ OAuth2TokenService::FetchParameters("1", "0", set_0),
+ OAuth2TokenService::FetchParameters("1", "0", set_1),
+ OAuth2TokenService::FetchParameters("1", "1", set_0),
+ OAuth2TokenService::FetchParameters("1", "1", set_1),
+ };
+
+ for (size_t i = 0; i < arraysize(params); i++) {
+ for (size_t j = 0; j < arraysize(params); j++) {
+ if (i == j) {
+ EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
+ } else if (i < j) {
+ EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
+ } else {
+ EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j;
+ EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
+ }
+ }
+ }
+}