summaryrefslogtreecommitdiffstats
path: root/google_apis
diff options
context:
space:
mode:
authorcourage@chromium.org <courage@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-22 02:29:23 +0000
committercourage@chromium.org <courage@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-22 02:29:23 +0000
commitd2db51aa31ca57ce620723f5b6758f09a5ae593b (patch)
treecf76733442e6d2aafc85ddb8d21f03da583de413 /google_apis
parentd8b42e288d8fc5994163eed9c77ba87a5c16e489 (diff)
downloadchromium_src-d2db51aa31ca57ce620723f5b6758f09a5ae593b.zip
chromium_src-d2db51aa31ca57ce620723f5b6758f09a5ae593b.tar.gz
chromium_src-d2db51aa31ca57ce620723f5b6758f09a5ae593b.tar.bz2
Identity API: Add AccountTracker and attach it to identity.onSignInChanged
The AccountTracker class fetches gaia IDs for all accounts signed into Chrome, and calls its observers any time an account is added, removed, or changes sign-in state. The IdentityAPI class observes the AccountTracker and routes the events to listening apps and extensions. BUG=305830 Review URL: https://codereview.chromium.org/30503002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@230017 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r--google_apis/gaia/gaia_oauth_client.cc41
-rw-r--r--google_apis/gaia/gaia_oauth_client.h10
-rw-r--r--google_apis/gaia/gaia_oauth_client_unittest.cc16
3 files changed, 64 insertions, 3 deletions
diff --git a/google_apis/gaia/gaia_oauth_client.cc b/google_apis/gaia/gaia_oauth_client.cc
index 86064d9..dc83dbe 100644
--- a/google_apis/gaia/gaia_oauth_client.cc
+++ b/google_apis/gaia/gaia_oauth_client.cc
@@ -52,6 +52,9 @@ class GaiaOAuthClient::Core
void GetUserEmail(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
+ void GetUserId(const std::string& oauth_access_token,
+ int max_retries,
+ Delegate* delegate);
void GetTokenInfo(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
@@ -67,11 +70,15 @@ class GaiaOAuthClient::Core
TOKENS_FROM_AUTH_CODE,
REFRESH_TOKEN,
TOKEN_INFO,
- USER_INFO,
+ USER_EMAIL,
+ USER_ID,
};
virtual ~Core() {}
+ void GetUserInfo(const std::string& oauth_access_token,
+ int max_retries,
+ Delegate* delegate);
void MakeGaiaRequest(const GURL& url,
const std::string& post_body,
int max_retries,
@@ -136,7 +143,22 @@ void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token,
Delegate* delegate) {
DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
DCHECK(!request_.get());
- request_type_ = USER_INFO;
+ request_type_ = USER_EMAIL;
+ GetUserInfo(oauth_access_token, max_retries, delegate);
+}
+
+void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token,
+ int max_retries,
+ Delegate* delegate) {
+ DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
+ DCHECK(!request_.get());
+ request_type_ = USER_ID;
+ GetUserInfo(oauth_access_token, max_retries, delegate);
+}
+
+void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token,
+ int max_retries,
+ Delegate* delegate) {
delegate_ = delegate;
num_retries_ = 0;
request_.reset(net::URLFetcher::Create(
@@ -250,13 +272,20 @@ void GaiaOAuthClient::Core::HandleResponse(
request_type_ = NO_PENDING_REQUEST;
switch (type) {
- case USER_INFO: {
+ case USER_EMAIL: {
std::string email;
response_dict->GetString("email", &email);
delegate_->OnGetUserEmailResponse(email);
break;
}
+ case USER_ID: {
+ std::string id;
+ response_dict->GetString("id", &id);
+ delegate_->OnGetUserIdResponse(id);
+ break;
+ }
+
case TOKEN_INFO: {
delegate_->OnGetTokenInfoResponse(response_dict.Pass());
break;
@@ -328,6 +357,12 @@ void GaiaOAuthClient::GetUserEmail(const std::string& access_token,
return core_->GetUserEmail(access_token, max_retries, delegate);
}
+void GaiaOAuthClient::GetUserId(const std::string& access_token,
+ int max_retries,
+ Delegate* delegate) {
+ return core_->GetUserId(access_token, max_retries, delegate);
+}
+
void GaiaOAuthClient::GetTokenInfo(const std::string& access_token,
int max_retries,
Delegate* delegate) {
diff --git a/google_apis/gaia/gaia_oauth_client.h b/google_apis/gaia/gaia_oauth_client.h
index 94d3313..14a26a6 100644
--- a/google_apis/gaia/gaia_oauth_client.h
+++ b/google_apis/gaia/gaia_oauth_client.h
@@ -45,6 +45,8 @@ class GaiaOAuthClient {
int expires_in_seconds) {}
// Invoked on a successful response to the GetUserInfo request.
virtual void OnGetUserEmailResponse(const std::string& user_email) {}
+ // Invoked on a successful response to the GetUserId request.
+ virtual void OnGetUserIdResponse(const std::string& user_id) {}
// Invoked on a successful response to the GetTokenInfo request.
virtual void OnGetTokenInfoResponse(
scoped_ptr<DictionaryValue> token_info) {}
@@ -94,6 +96,14 @@ class GaiaOAuthClient {
int max_retries,
Delegate* delegate);
+ // Call the userinfo API, returning the user gaia ID associated
+ // with the given access token. The provided access token must have
+ // https://www.googleapis.com/auth/userinfo as one of its scopes.
+ // See |max_retries| docs above.
+ void GetUserId(const std::string& oauth_access_token,
+ int max_retries,
+ Delegate* delegate);
+
// Call the tokeninfo API, returning a dictionary of response values. The
// provided access token may have any scope, and basic results will be
// returned: issued_to, audience, scope, expires_in, access_type. In
diff --git a/google_apis/gaia/gaia_oauth_client_unittest.cc b/google_apis/gaia/gaia_oauth_client_unittest.cc
index cdeb483..32e7c66 100644
--- a/google_apis/gaia/gaia_oauth_client_unittest.cc
+++ b/google_apis/gaia/gaia_oauth_client_unittest.cc
@@ -130,6 +130,7 @@ const std::string kTestAccessToken = "1/fFAGRNJru1FTz70BzhT3Zg";
const std::string kTestRefreshToken =
"1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ";
const std::string kTestUserEmail = "a_user@gmail.com";
+const std::string kTestUserId = "8675309";
const int kTestExpiresIn = 3920;
const std::string kDummyGetTokensResult =
@@ -144,6 +145,9 @@ const std::string kDummyRefreshTokenResult =
const std::string kDummyUserInfoResult =
"{\"email\":\"" + kTestUserEmail + "\"}";
+const std::string kDummyUserIdResult =
+ "{\"id\":\"" + kTestUserId + "\"}";
+
const std::string kDummyTokenInfoResult =
"{\"issued_to\": \"1234567890.apps.googleusercontent.com\","
"\"audience\": \"1234567890.apps.googleusercontent.com\","
@@ -186,6 +190,7 @@ class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate {
MOCK_METHOD2(OnRefreshTokenResponse, void(const std::string& access_token,
int expires_in_seconds));
MOCK_METHOD1(OnGetUserEmailResponse, void(const std::string& user_email));
+ MOCK_METHOD1(OnGetUserIdResponse, void(const std::string& user_id));
MOCK_METHOD0(OnOAuthError, void());
MOCK_METHOD1(OnNetworkError, void(int response_code));
@@ -311,6 +316,17 @@ TEST_F(GaiaOAuthClientTest, GetUserEmail) {
auth.GetUserEmail("access_token", 1, &delegate);
}
+TEST_F(GaiaOAuthClientTest, GetUserId) {
+ MockGaiaOAuthClientDelegate delegate;
+ EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1);
+
+ MockOAuthFetcherFactory factory;
+ factory.set_results(kDummyUserIdResult);
+
+ GaiaOAuthClient auth(GetRequestContext());
+ auth.GetUserId("access_token", 1, &delegate);
+}
+
TEST_F(GaiaOAuthClientTest, GetTokenInfo) {
const DictionaryValue* captured_result;