diff options
author | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-14 03:16:51 +0000 |
---|---|---|
committer | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-14 03:18:08 +0000 |
commit | 6ed970c79e3fa0c33e0ea39f776e6f42dfa3922c (patch) | |
tree | 64d7427b18f55700707a2b578198e14e8ee5c931 /google_apis | |
parent | ea3936aec76800ad0281b01f562e5ca4c9790222 (diff) | |
download | chromium_src-6ed970c79e3fa0c33e0ea39f776e6f42dfa3922c.zip chromium_src-6ed970c79e3fa0c33e0ea39f776e6f42dfa3922c.tar.gz chromium_src-6ed970c79e3fa0c33e0ea39f776e6f42dfa3922c.tar.bz2 |
Expose full userinfo to callers of GaiaOAuthFetcher.
BUG=341408
Review URL: https://codereview.chromium.org/474473002
Cr-Commit-Position: refs/heads/master@{#289449}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289449 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gaia/gaia_constants.cc | 7 | ||||
-rw-r--r-- | google_apis/gaia/gaia_constants.h | 4 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client.cc | 47 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client.h | 13 | ||||
-rw-r--r-- | google_apis/gaia/gaia_oauth_client_unittest.cc | 58 |
5 files changed, 108 insertions, 21 deletions
diff --git a/google_apis/gaia/gaia_constants.cc b/google_apis/gaia/gaia_constants.cc index 95a5945..d3d0cd9 100644 --- a/google_apis/gaia/gaia_constants.cc +++ b/google_apis/gaia/gaia_constants.cc @@ -45,7 +45,12 @@ const char kChromeSyncSupervisedOAuth2Scope[] = const char kGoogleTalkOAuth2Scope[] = "https://www.googleapis.com/auth/googletalk"; -// Used to mint uber auth tokens when needed. +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"; const char kGaiaOAuthToken[] = "oauthToken"; diff --git a/google_apis/gaia/gaia_constants.h b/google_apis/gaia/gaia_constants.h index ea8cc0f..4786ac8 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 kChromeSyncSupervisedOAuth2Scope[]; 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..8e2d29b 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,15 @@ 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 GetUserInfoImpl(RequestType type, + const std::string& oauth_access_token, + int max_retries, + Delegate* delegate); void MakeGaiaRequest(const GURL& url, const std::string& post_body, int max_retries, @@ -142,24 +147,29 @@ void GaiaOAuthClient::Core::RefreshToken( void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, int max_retries, Delegate* delegate) { - DCHECK_EQ(request_type_, NO_PENDING_REQUEST); - DCHECK(!request_.get()); - request_type_ = USER_EMAIL; - GetUserInfo(oauth_access_token, max_retries, delegate); + GetUserInfoImpl(USER_EMAIL, 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); + GetUserInfoImpl(USER_ID, oauth_access_token, max_retries, delegate); } void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, - int max_retries, - Delegate* delegate) { + int max_retries, + Delegate* delegate) { + GetUserInfoImpl(USER_INFO, oauth_access_token, max_retries, delegate); +} + +void GaiaOAuthClient::Core::GetUserInfoImpl( + RequestType type, + const std::string& oauth_access_token, + int max_retries, + Delegate* delegate) { + DCHECK_EQ(request_type_, NO_PENDING_REQUEST); + DCHECK(!request_.get()); + request_type_ = type; delegate_ = delegate; num_retries_ = 0; request_.reset(net::URLFetcher::Create( @@ -295,6 +305,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 +387,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..68eb766 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) {} @@ -104,6 +107,16 @@ class GaiaOAuthClient { int max_retries, Delegate* delegate); + // Call the userinfo API, returning all the user info associated + // with the given access token. The provided access token must have + // https://www.googleapis.com/auth/userinfo.profile in its scopes. If + // email addresses are also to be retrieved, then + // https://www.googleapis.com/auth/userinfo.email must also be specified. + // 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..7ca60f3 100644 --- a/google_apis/gaia/gaia_oauth_client_unittest.cc +++ b/google_apis/gaia/gaia_oauth_client_unittest.cc @@ -7,6 +7,7 @@ #include <string> #include <vector> +#include "base/json/json_reader.h" #include "base/strings/string_number_conversions.h" #include "base/values.h" #include "google_apis/gaia/gaia_oauth_client.h" @@ -134,19 +135,31 @@ 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) + "}"; + "{\"access_token\":\"" + kTestAccessToken + "\"," + "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; const std::string kDummyUserInfoResult = - "{\"email\":\"" + kTestUserEmail + "\"}"; + "{\"email\":\"" + kTestUserEmail + "\"}"; const std::string kDummyUserIdResult = - "{\"id\":\"" + kTestUserId + "\"}"; + "{\"id\":\"" + kTestUserId + "\"}"; + +const std::string kDummyFullUserInfoResult = + "{" + "\"family_name\": \"Bar\", " + "\"name\": \"Foo Bar\", " + "\"picture\": \"https://lh4.googleusercontent.com/hash/photo.jpg\", " + "\"locale\": \"en\", " + "\"gender\": \"male\", " + "\"link\": \"https://plus.google.com/+FooBar\", " + "\"given_name\": \"Foo\", " + "\"id\": \"12345678901234567890\"" + "}"; const std::string kDummyTokenInfoResult = "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," @@ -198,6 +211,13 @@ class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate { // work-around is to create a mock method that takes a raw ptr, and // override the problematic method to call through to it. // https://groups.google.com/a/chromium.org/d/msg/chromium-dev/01sDxsJ1OYw/I_S0xCBRF2oJ + MOCK_METHOD1(OnGetUserInfoResponsePtr, + void(const base::DictionaryValue* user_info)); + virtual void OnGetUserInfoResponse( + scoped_ptr<base::DictionaryValue> user_info) OVERRIDE { + user_info_.reset(user_info.release()); + OnGetUserInfoResponsePtr(user_info_.get()); + } MOCK_METHOD1(OnGetTokenInfoResponsePtr, void(const base::DictionaryValue* token_info)); virtual void OnGetTokenInfoResponse( @@ -207,6 +227,7 @@ class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate { } private: + scoped_ptr<base::DictionaryValue> user_info_; scoped_ptr<base::DictionaryValue> token_info_; DISALLOW_COPY_AND_ASSIGN(MockGaiaOAuthClientDelegate); }; @@ -327,6 +348,29 @@ TEST_F(GaiaOAuthClientTest, GetUserId) { auth.GetUserId("access_token", 1, &delegate); } +TEST_F(GaiaOAuthClientTest, GetUserInfo) { + const base::DictionaryValue* captured_result; + + MockGaiaOAuthClientDelegate delegate; + EXPECT_CALL(delegate, OnGetUserInfoResponsePtr(_)) + .WillOnce(SaveArg<0>(&captured_result)); + + MockOAuthFetcherFactory factory; + factory.set_results(kDummyFullUserInfoResult); + + GaiaOAuthClient auth(GetRequestContext()); + auth.GetUserInfo("access_token", 1, &delegate); + + scoped_ptr<base::Value> value( + base::JSONReader::Read(kDummyFullUserInfoResult)); + DCHECK(value); + ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); + base::DictionaryValue* expected_result; + value->GetAsDictionary(&expected_result); + + ASSERT_TRUE(expected_result->Equals(captured_result)); +} + TEST_F(GaiaOAuthClientTest, GetTokenInfo) { const base::DictionaryValue* captured_result; |