diff options
author | khmel <khmel@chromium.org> | 2016-01-27 19:23:32 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-28 03:24:16 +0000 |
commit | 2a5da822dd88c4eb3c538c134fd4b81f1207b8fb (patch) | |
tree | 60c7e092ae56d88939e4f5bbc247ba8c0676bc1d /google_apis | |
parent | 5b685e2f6ec533187d6f93c6323ff90bd17c8a93 (diff) | |
download | chromium_src-2a5da822dd88c4eb3c538c134fd4b81f1207b8fb.zip chromium_src-2a5da822dd88c4eb3c538c134fd4b81f1207b8fb.tar.gz chromium_src-2a5da822dd88c4eb3c538c134fd4b81f1207b8fb.tar.bz2 |
Allow gaia auth fetcher to work with arbitrary clients.
BUG=581793
TEST=Extended GaiaAuthFetcherTest passes
TEST=Tested together https://codereview.chromium.org/1618193003/.
Auth code is returned for GMS Core client id and is consumed
at Android.
Review URL: https://codereview.chromium.org/1644713002
Cr-Commit-Position: refs/heads/master@{#371989}
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gaia/gaia_auth_consumer.h | 1 | ||||
-rw-r--r-- | google_apis/gaia/gaia_auth_fetcher.cc | 24 | ||||
-rw-r--r-- | google_apis/gaia/gaia_auth_fetcher.h | 21 | ||||
-rw-r--r-- | google_apis/gaia/gaia_auth_fetcher_unittest.cc | 33 |
4 files changed, 73 insertions, 6 deletions
diff --git a/google_apis/gaia/gaia_auth_consumer.h b/google_apis/gaia/gaia_auth_consumer.h index b8f70df..242e58d 100644 --- a/google_apis/gaia/gaia_auth_consumer.h +++ b/google_apis/gaia/gaia_auth_consumer.h @@ -69,6 +69,7 @@ class GaiaAuthConsumer { virtual void OnIssueAuthTokenFailure(const std::string& service, const GoogleServiceAuthError& error) {} + virtual void OnClientOAuthCode(const std::string& auth_code) {} virtual void OnClientOAuthSuccess(const ClientOAuthResult& result) {} virtual void OnClientOAuthFailure(const GoogleServiceAuthError& error) {} diff --git a/google_apis/gaia/gaia_auth_fetcher.cc b/google_apis/gaia/gaia_auth_fetcher.cc index c807e13..d614cd4 100644 --- a/google_apis/gaia/gaia_auth_fetcher.cc +++ b/google_apis/gaia/gaia_auth_fetcher.cc @@ -182,8 +182,7 @@ GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(source)), oauth2_iframe_url_(GaiaUrls::GetInstance()->oauth2_iframe_url()), client_login_to_oauth2_gurl_( - GaiaUrls::GetInstance()->client_login_to_oauth2_url()), - fetch_pending_(false) { + GaiaUrls::GetInstance()->client_login_to_oauth2_url()) { } GaiaAuthFetcher::~GaiaAuthFetcher() {} @@ -526,14 +525,25 @@ void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange( void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchangeWithDeviceId( const std::string& session_index, const std::string& device_id) { + StartCookieForOAuthLoginTokenExchange( + true, + session_index, + GaiaUrls::GetInstance()->oauth2_chrome_client_id(), + device_id); +} + +void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange( + bool fetch_token_from_auth_code, + const std::string& session_index, + const std::string& client_id, + const std::string& device_id) { DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; VLOG(1) << "Starting OAuth login token fetch with cookie jar"; std::string encoded_scope = net::EscapeUrlEncodedData( GaiaConstants::kOAuth1LoginScope, true); - std::string encoded_client_id = net::EscapeUrlEncodedData( - GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true); + std::string encoded_client_id = net::EscapeUrlEncodedData(client_id, true); std::string query_string = base::StringPrintf(kClientLoginToOAuth2URLFormat, encoded_scope.c_str(), encoded_client_id.c_str()); @@ -548,6 +558,7 @@ void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchangeWithDeviceId( base::StringPrintf(kDeviceIdHeaderFormat, device_id.c_str()); } + fetch_token_from_auth_code_ = fetch_token_from_auth_code; CreateAndStartGaiaFetcher(std::string(), device_id_header, client_login_to_oauth2_gurl_.Resolve(query_string), net::LOAD_NORMAL); @@ -738,7 +749,10 @@ void GaiaAuthFetcher::OnClientLoginToOAuth2Fetched( if (status.is_success() && response_code == net::HTTP_OK) { std::string auth_code; if (ParseClientLoginToOAuth2Response(cookies, &auth_code)) { - StartAuthCodeForOAuth2TokenExchange(auth_code); + if (fetch_token_from_auth_code_) + StartAuthCodeForOAuth2TokenExchange(auth_code); + else + consumer_->OnClientOAuthCode(auth_code); } else { GoogleServiceAuthError auth_error( GoogleServiceAuthError::FromUnexpectedServiceResponse( diff --git a/google_apis/gaia/gaia_auth_fetcher.h b/google_apis/gaia/gaia_auth_fetcher.h index 9c877b6..b477913 100644 --- a/google_apis/gaia/gaia_auth_fetcher.h +++ b/google_apis/gaia/gaia_auth_fetcher.h @@ -104,6 +104,24 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate { const std::string& session_index, const std::string& device_id); + // Start a request to exchange the cookies of a signed-in user session + // and for specified client for an OAuthLogin-scoped oauth2 token. Client is + // determined by its |client_id|. In the case of a session with multiple + // accounts signed in, |session_index| indicate the which of accounts + // within the session. If |fetch_token_from_auth_code| is not set fetching + // process stops after receiving an auth code and ClientOAuthSuccess won't be + // called. + // Resulting refresh token is annotated on the server with |device_id|. Format + // of device_id on the server is at most 64 unicode characters. + // + // Either OnClientOAuthCode or ClientOAuthSuccess or OnClientOAuthFailure + // will be called on the consumer on the original thread. + void StartCookieForOAuthLoginTokenExchange( + bool fetch_token_from_auth_code, + const std::string& session_index, + const std::string& client_id, + const std::string& device_id); + // Start a request to exchange the authorization code for an OAuthLogin-scoped // oauth2 token. // @@ -432,7 +450,8 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate { GURL client_login_to_oauth2_gurl_; std::string request_body_; std::string requested_service_; - bool fetch_pending_; + bool fetch_pending_ = false; + bool fetch_token_from_auth_code_ = false; // Headers used during the Logout call. std::string logout_headers_; diff --git a/google_apis/gaia/gaia_auth_fetcher_unittest.cc b/google_apis/gaia/gaia_auth_fetcher_unittest.cc index 19ae5df..f281f23 100644 --- a/google_apis/gaia/gaia_auth_fetcher_unittest.cc +++ b/google_apis/gaia/gaia_auth_fetcher_unittest.cc @@ -171,6 +171,7 @@ class MockGaiaConsumer : public GaiaAuthConsumer { MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, const std::string& token)); + MOCK_METHOD1(OnClientOAuthCode, void(const std::string& data)); MOCK_METHOD1(OnClientOAuthSuccess, void(const GaiaAuthConsumer::ClientOAuthResult& result)); MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); @@ -395,6 +396,7 @@ TEST_F(GaiaAuthFetcherTest, FullTokenFailure) { TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { MockGaiaConsumer consumer; + EXPECT_CALL(consumer, OnClientOAuthCode("test-code")).Times(0); EXPECT_CALL(consumer, OnClientOAuthSuccess( GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); @@ -429,6 +431,37 @@ TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { EXPECT_FALSE(auth.HasPendingFetch()); } +TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccessNoTokenFetch) { + MockGaiaConsumer consumer; + EXPECT_CALL(consumer, OnClientOAuthCode("test-code")).Times(1); + EXPECT_CALL(consumer, OnClientOAuthSuccess( + GaiaAuthConsumer::ClientOAuthResult("", "", 0))).Times(0); + + net::TestURLFetcherFactory factory; + GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); + auth.StartCookieForOAuthLoginTokenExchange( + false, "0", "ABCDE_12345", ""); + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); + EXPECT_TRUE(NULL != fetcher); + EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); + EXPECT_EQ(std::string::npos, + fetcher->GetOriginalURL().query().find("device_type=chrome")); + + net::ResponseCookies cookies; + cookies.push_back(kGetAuthCodeValidCookie); + EXPECT_TRUE(auth.HasPendingFetch()); + MockFetcher mock_fetcher1( + client_login_to_oauth2_source_, + net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), + net::HTTP_OK, + cookies, + std::string(), + net::URLFetcher::POST, + &auth); + auth.OnURLFetchComplete(&mock_fetcher1); + EXPECT_FALSE(auth.HasPendingFetch()); +} + TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) { MockGaiaConsumer consumer; net::TestURLFetcherFactory factory; |