diff options
author | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 23:27:20 +0000 |
---|---|---|
committer | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 23:27:20 +0000 |
commit | 431a075a671f8c7f1f31a90282f76af6e0cc94ff (patch) | |
tree | 0d0f540ddb318688b1613408c72030fc6700d094 /google_apis | |
parent | fca61dc53df4dbb46c2ee4bf075f662632418d61 (diff) | |
download | chromium_src-431a075a671f8c7f1f31a90282f76af6e0cc94ff.zip chromium_src-431a075a671f8c7f1f31a90282f76af6e0cc94ff.tar.gz chromium_src-431a075a671f8c7f1f31a90282f76af6e0cc94ff.tar.bz2 |
Use new people.get api instead of oauth2/v1/userinfo.
Consolidate all uses into main helper class.
Details about new api: https://developers.google.com/+/api/latest/people/get
Format of returned response: https://developers.google.com/+/api/latest/people#resource
BUG=320354
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=267068
Review URL: https://codereview.chromium.org/257773002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267374 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gaia/gaia_constants.cc | 5 | ||||
-rw-r--r-- | google_apis/gaia/gaia_constants.h | 4 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client.cc | 60 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client.h | 18 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client_unittest.cc | 59 | ||||
-rw-r--r-- | google_apis/gaia/gaia_urls.cc | 10 | ||||
-rw-r--r-- | google_apis/gaia/gaia_urls.h | 4 |
7 files changed, 127 insertions, 33 deletions
diff --git a/google_apis/gaia/gaia_constants.cc b/google_apis/gaia/gaia_constants.cc index 60546bf..14a41c9 100644 --- a/google_apis/gaia/gaia_constants.cc +++ b/google_apis/gaia/gaia_constants.cc @@ -45,6 +45,11 @@ const char kChromeSyncManagedOAuth2Scope[] = const char kGoogleTalkOAuth2Scope[] = "https://www.googleapis.com/auth/googletalk"; +const char kGoogleUserInfoEmail[] = + "https://www.googleapis.com/auth/userinfo.email"; +const char kGoogleUserInfoProfile[] = + "https://www.googleapis.com/auth/userinfo.profile"; + // Used to mint uber auth tokens when needed. const char kGaiaSid[] = "sid"; const char kGaiaLsid[] = "lsid"; diff --git a/google_apis/gaia/gaia_constants.h b/google_apis/gaia/gaia_constants.h index 406298c..7adc130 100644 --- a/google_apis/gaia/gaia_constants.h +++ b/google_apis/gaia/gaia_constants.h @@ -18,6 +18,8 @@ extern const char kGaiaService[]; // uber token extern const char kPicasaService[]; extern const char kSyncService[]; extern const char kRemotingService[]; + +// OAuth2 scopes. extern const char kOAuth1LoginScope[]; extern const char kOAuthWrapBridgeUserInfoScope[]; extern const char kDeviceManagementServiceOAuth[]; @@ -25,6 +27,8 @@ extern const char kAnyApiOAuth2Scope[]; extern const char kChromeSyncOAuth2Scope[]; extern const char kChromeSyncManagedOAuth2Scope[]; extern const char kGoogleTalkOAuth2Scope[]; +extern const char kGoogleUserInfoEmail[]; +extern const char kGoogleUserInfoProfile[]; // Used with uber auth tokens when needed. extern const char kGaiaSid[]; diff --git a/google_apis/gaia/gaia_oauth_client.cc b/google_apis/gaia/gaia_oauth_client.cc index 1113ff6..1d5f9eb 100644 --- a/google_apis/gaia/gaia_oauth_client.cc +++ b/google_apis/gaia/gaia_oauth_client.cc @@ -56,6 +56,9 @@ class GaiaOAuthClient::Core void GetUserId(const std::string& oauth_access_token, int max_retries, Delegate* delegate); + void GetUserInfo(const std::string& oauth_access_token, + int max_retries, + Delegate* delegate); void GetTokenInfo(const std::string& oauth_access_token, int max_retries, Delegate* delegate); @@ -73,13 +76,14 @@ class GaiaOAuthClient::Core TOKEN_INFO, USER_EMAIL, USER_ID, + USER_INFO, }; virtual ~Core() {} - void GetUserInfo(const std::string& oauth_access_token, - int max_retries, - Delegate* delegate); + void PeopleGet(const std::string& oauth_access_token, + int max_retries, + Delegate* delegate); void MakeGaiaRequest(const GURL& url, const std::string& post_body, int max_retries, @@ -145,7 +149,7 @@ void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, DCHECK_EQ(request_type_, NO_PENDING_REQUEST); DCHECK(!request_.get()); request_type_ = USER_EMAIL; - GetUserInfo(oauth_access_token, max_retries, delegate); + PeopleGet(oauth_access_token, max_retries, delegate); } void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, @@ -154,16 +158,25 @@ void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, DCHECK_EQ(request_type_, NO_PENDING_REQUEST); DCHECK(!request_.get()); request_type_ = USER_ID; - GetUserInfo(oauth_access_token, max_retries, delegate); + PeopleGet(oauth_access_token, max_retries, delegate); } void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, int max_retries, Delegate* delegate) { + DCHECK_EQ(request_type_, NO_PENDING_REQUEST); + DCHECK(!request_.get()); + request_type_ = USER_INFO; + PeopleGet(oauth_access_token, max_retries, delegate); +} + +void GaiaOAuthClient::Core::PeopleGet(const std::string& oauth_access_token, + int max_retries, + Delegate* delegate) { delegate_ = delegate; num_retries_ = 0; request_.reset(net::URLFetcher::Create( - kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), + kUrlFetcherId, GURL(GaiaUrls::GetInstance()->people_get_url()), net::URLFetcher::GET, this)); request_->SetRequestContext(request_context_getter_.get()); request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); @@ -282,9 +295,27 @@ void GaiaOAuthClient::Core::HandleResponse( switch (type) { case USER_EMAIL: { - std::string email; - response_dict->GetString("email", &email); - delegate_->OnGetUserEmailResponse(email); + // Use first email of type "account" as the user's email. + const base::ListValue* emails_list; + bool email_found = false; + if (response_dict->GetList("emails", &emails_list)) { + for (size_t i = 0; i < emails_list->GetSize(); ++i) { + const base::DictionaryValue* email_dict; + if (emails_list->GetDictionary(i, &email_dict)) { + std::string email; + std::string type; + if (email_dict->GetString("type", &type) && + type == "account" && + email_dict->GetString("value", &email)) { + delegate_->OnGetUserEmailResponse(email); + email_found = true; + break; + } + } + } + } + if (!email_found) + delegate_->OnNetworkError(net::URLFetcher::RESPONSE_CODE_INVALID); break; } @@ -295,6 +326,11 @@ void GaiaOAuthClient::Core::HandleResponse( break; } + case USER_INFO: { + delegate_->OnGetUserInfoResponse(response_dict.Pass()); + break; + } + case TOKEN_INFO: { delegate_->OnGetTokenInfoResponse(response_dict.Pass()); break; @@ -372,6 +408,12 @@ void GaiaOAuthClient::GetUserId(const std::string& access_token, return core_->GetUserId(access_token, max_retries, delegate); } +void GaiaOAuthClient::GetUserInfo(const std::string& access_token, + int max_retries, + Delegate* delegate) { + return core_->GetUserInfo(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 8e01ef6..fd13f46 100644 --- a/google_apis/gaia/gaia_oauth_client.h +++ b/google_apis/gaia/gaia_oauth_client.h @@ -47,6 +47,9 @@ class GaiaOAuthClient { 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 GetUserInfo request. + virtual void OnGetUserInfoResponse( + scoped_ptr<base::DictionaryValue> user_info) {} // Invoked on a successful response to the GetTokenInfo request. virtual void OnGetTokenInfoResponse( scoped_ptr<base::DictionaryValue> token_info) {} @@ -88,7 +91,7 @@ class GaiaOAuthClient { int max_retries, Delegate* delegate); - // Call the userinfo API, returning the user email address associated + // Call the people.get API, returning the user email address associated // with the given access token. The provided access token must have // https://www.googleapis.com/auth/userinfo.email as one of its scopes. // See |max_retries| docs above. @@ -96,14 +99,23 @@ class GaiaOAuthClient { int max_retries, Delegate* delegate); - // Call the userinfo API, returning the user gaia ID associated + // Call the people.get 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. + // https://www.googleapis.com/auth/userinfo.profile 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 people.get API, returning the user info associated + // with the given access token. The provided access token must have + // https://www.googleapis.com/auth/userinfo.email and + // https://www.googleapis.com/auth/userinfo.profile as its scopes. + // See |max_retries| docs above. + void GetUserInfo(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 d4014f7..338578e 100644 --- a/google_apis/gaia/gaia_oauth_client_unittest.cc +++ b/google_apis/gaia/gaia_oauth_client_unittest.cc @@ -134,25 +134,22 @@ const std::string kTestUserId = "8675309"; const int kTestExpiresIn = 3920; const std::string kDummyGetTokensResult = - "{\"access_token\":\"" + kTestAccessToken + "\"," - "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "," - "\"refresh_token\":\"" + kTestRefreshToken + "\"}"; + "{\"access_token\":\"" + kTestAccessToken + "\"," + "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "," + "\"refresh_token\":\"" + kTestRefreshToken + "\"}"; const std::string kDummyRefreshTokenResult = - "{\"access_token\":\"" + kTestAccessToken + "\"," - "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; - -const std::string kDummyUserInfoResult = - "{\"email\":\"" + kTestUserEmail + "\"}"; + "{\"access_token\":\"" + kTestAccessToken + "\"," + "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; const std::string kDummyUserIdResult = - "{\"id\":\"" + kTestUserId + "\"}"; + "{\"id\":\"" + kTestUserId + "\"}"; const std::string kDummyTokenInfoResult = - "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," - "\"audience\": \"1234567890.apps.googleusercontent.com\"," - "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," - "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; + "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," + "\"audience\": \"1234567890.apps.googleusercontent.com\"," + "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," + "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; } namespace gaia { @@ -304,11 +301,45 @@ TEST_F(GaiaOAuthClientTest, RefreshTokenDownscopingSuccess) { factory.get_url_fetcher()->Finish(); } - TEST_F(GaiaOAuthClientTest, GetUserEmail) { MockGaiaOAuthClientDelegate delegate; EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); + const std::string kDummyUserInfoResult = + "{\"emails\": [{\"value\":\"" + kTestUserEmail + + "\", \"type\":\"account\"}]}"; + + MockOAuthFetcherFactory factory; + factory.set_results(kDummyUserInfoResult); + + GaiaOAuthClient auth(GetRequestContext()); + auth.GetUserEmail("access_token", 1, &delegate); +} + +TEST_F(GaiaOAuthClientTest, GetUserEmailSecondItemValid) { + MockGaiaOAuthClientDelegate delegate; + EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); + + const std::string kDummyUserInfoResult = + "{\"emails\": [{\"value\":\"foo\"}," + "{\"value\":\"" + kTestUserEmail + + "\", \"type\":\"account\"}]}"; + + MockOAuthFetcherFactory factory; + factory.set_results(kDummyUserInfoResult); + + GaiaOAuthClient auth(GetRequestContext()); + auth.GetUserEmail("access_token", 1, &delegate); +} + +TEST_F(GaiaOAuthClientTest, GetUserEmailNoValidItems) { + MockGaiaOAuthClientDelegate delegate; + EXPECT_CALL(delegate, OnNetworkError(_)).Times(1); + + const std::string kDummyUserInfoResult = + "{\"emails\": [{\"value\":\"" + kTestUserEmail + + "\", \"type\":\"foo\"}]}"; + MockOAuthFetcherFactory factory; factory.set_results(kDummyUserInfoResult); diff --git a/google_apis/gaia/gaia_urls.cc b/google_apis/gaia/gaia_urls.cc index 0b44a91..5abff9c 100644 --- a/google_apis/gaia/gaia_urls.cc +++ b/google_apis/gaia/gaia_urls.cc @@ -42,7 +42,7 @@ const char kOAuth2TokenUrlSuffix[] = "o/oauth2/token"; // API calls from www.googleapis.com const char kOAuth2IssueTokenUrlSuffix[] = "oauth2/v2/IssueToken"; const char kOAuth2TokenInfoUrlSuffix[] = "oauth2/v2/tokeninfo"; -const char kOAuthUserInfoUrlSuffix[] = "oauth2/v1/userinfo"; +const char kPeopleGetUrlSuffix[] = "plus/v1/people/me"; void GetSwitchValueWithDefault(const char* switch_value, const char* default_value, @@ -118,8 +118,8 @@ GaiaUrls::GaiaUrls() { google_apis_origin_url_.Resolve(kOAuth2IssueTokenUrlSuffix); oauth2_token_info_url_ = google_apis_origin_url_.Resolve(kOAuth2TokenInfoUrlSuffix); - oauth_user_info_url_ = - google_apis_origin_url_.Resolve(kOAuthUserInfoUrlSuffix); + people_get_url_ = + google_apis_origin_url_.Resolve(kPeopleGetUrlSuffix); gaia_login_form_realm_ = gaia_url_; } @@ -179,8 +179,8 @@ const GURL& GaiaUrls::oauth_wrap_bridge_url() const { return oauth_wrap_bridge_url_; } -const GURL& GaiaUrls::oauth_user_info_url() const { - return oauth_user_info_url_; +const GURL& GaiaUrls::people_get_url() const { + return people_get_url_; } const GURL& GaiaUrls::oauth_revoke_token_url() const { diff --git a/google_apis/gaia/gaia_urls.h b/google_apis/gaia/gaia_urls.h index f96e89c..7a41ebf 100644 --- a/google_apis/gaia/gaia_urls.h +++ b/google_apis/gaia/gaia_urls.h @@ -24,12 +24,12 @@ class GaiaUrls { const GURL& service_logout_url() const; const GURL& issue_auth_token_url() const; const GURL& get_user_info_url() const; + const GURL& people_get_url() const; const GURL& token_auth_url() const; const GURL& merge_session_url() const; const GURL& get_oauth_token_url() const; const GURL& oauth_get_access_token_url() const; const GURL& oauth_wrap_bridge_url() const; - const GURL& oauth_user_info_url() const; const GURL& oauth_revoke_token_url() const; const GURL& oauth1_login_url() const; const GURL& list_accounts_url() const; @@ -65,12 +65,12 @@ class GaiaUrls { GURL service_logout_url_; GURL issue_auth_token_url_; GURL get_user_info_url_; + GURL people_get_url_; GURL token_auth_url_; GURL merge_session_url_; GURL get_oauth_token_url_; GURL oauth_get_access_token_url_; GURL oauth_wrap_bridge_url_; - GURL oauth_user_info_url_; GURL oauth_revoke_token_url_; GURL oauth1_login_url_; GURL list_accounts_url_; |