OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 5 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
15 #include "chrome/common/net/gaia/google_service_auth_error.h" | 15 #include "chrome/common/net/gaia/google_service_auth_error.h" |
16 #include "content/public/common/url_fetcher_delegate.h" | |
17 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" |
18 | 18 |
19 // Authenticate a user against the Google Accounts ClientLogin API | 19 // Authenticate a user against the Google Accounts ClientLogin API |
20 // with various capabilities and return results to a GaiaAuthConsumer. | 20 // with various capabilities and return results to a GaiaAuthConsumer. |
21 // | 21 // |
22 // In the future, we will also issue auth tokens from this class. | 22 // In the future, we will also issue auth tokens from this class. |
23 // This class should be used on a single thread, but it can be whichever thread | 23 // This class should be used on a single thread, but it can be whichever thread |
24 // that you like. | 24 // that you like. |
25 // | 25 // |
26 // This class can handle one request at a time on any thread. To parallelize | 26 // This class can handle one request at a time on any thread. To parallelize |
27 // requests, create multiple GaiaAuthFetcher's. | 27 // requests, create multiple GaiaAuthFetcher's. |
28 | 28 |
29 class GaiaAuthFetcherTest; | 29 class GaiaAuthFetcherTest; |
30 | 30 |
31 namespace net { | 31 namespace net { |
| 32 class URLFetcher; |
32 class URLRequestContextGetter; | 33 class URLRequestContextGetter; |
33 class URLRequestStatus; | 34 class URLRequestStatus; |
34 } | 35 } |
35 | 36 |
36 class GaiaAuthFetcher : public content::URLFetcherDelegate { | 37 class GaiaAuthFetcher : public net::URLFetcherDelegate { |
37 public: | 38 public: |
38 enum HostedAccountsSetting { | 39 enum HostedAccountsSetting { |
39 HostedAccountsAllowed, | 40 HostedAccountsAllowed, |
40 HostedAccountsNotAllowed | 41 HostedAccountsNotAllowed |
41 }; | 42 }; |
42 | 43 |
43 // Magic string indicating that, while a second factor is still | 44 // Magic string indicating that, while a second factor is still |
44 // needed to complete authentication, the user provided the right password. | 45 // needed to complete authentication, the user provided the right password. |
45 static const char kSecondFactor[]; | 46 static const char kSecondFactor[]; |
46 | 47 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 // Start a request to exchange an OAuthLogin-scoped oauth2 access token for a | 163 // Start a request to exchange an OAuthLogin-scoped oauth2 access token for a |
163 // ClientLogin-style service tokens. The response to this request is the | 164 // ClientLogin-style service tokens. The response to this request is the |
164 // same as the response to a ClientLogin request, except that captcha | 165 // same as the response to a ClientLogin request, except that captcha |
165 // challenges are never issued. | 166 // challenges are never issued. |
166 // | 167 // |
167 // Either OnClientLoginSuccess or OnClientLoginFailure will be | 168 // Either OnClientLoginSuccess or OnClientLoginFailure will be |
168 // called on the consumer on the original thread. | 169 // called on the consumer on the original thread. |
169 void StartOAuthLogin(const std::string& access_token, | 170 void StartOAuthLogin(const std::string& access_token, |
170 const std::string& service); | 171 const std::string& service); |
171 | 172 |
172 // Implementation of content::URLFetcherDelegate | 173 // Implementation of net::URLFetcherDelegate |
173 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 174 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
174 | 175 |
175 // StartClientLogin been called && results not back yet? | 176 // StartClientLogin been called && results not back yet? |
176 bool HasPendingFetch(); | 177 bool HasPendingFetch(); |
177 | 178 |
178 // Stop any URL fetches in progress. | 179 // Stop any URL fetches in progress. |
179 void CancelRequest(); | 180 void CancelRequest(); |
180 | 181 |
181 // From a URLFetcher result, generate an appropriate error. | 182 // From a URLFetcher result, generate an appropriate error. |
182 // From the API documentation, both IssueAuthToken and ClientLogin have | 183 // From the API documentation, both IssueAuthToken and ClientLogin have |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 364 |
364 static std::string MakeOAuthLoginBody(const std::string& service, | 365 static std::string MakeOAuthLoginBody(const std::string& service, |
365 const std::string& source); | 366 const std::string& source); |
366 | 367 |
367 void StartOAuth2TokenPairFetch(const std::string& auth_code); | 368 void StartOAuth2TokenPairFetch(const std::string& auth_code); |
368 | 369 |
369 // Create a fetcher usable for making any Gaia request. |body| is used | 370 // Create a fetcher usable for making any Gaia request. |body| is used |
370 // as the body of the POST request sent to GAIA. Any strings listed in | 371 // as the body of the POST request sent to GAIA. Any strings listed in |
371 // |headers| are added as extra HTTP headers in the request. | 372 // |headers| are added as extra HTTP headers in the request. |
372 // | 373 // |
373 // |load_flags| are passed to directly to content::URLFetcher::Create() when | 374 // |load_flags| are passed to directly to net::URLFetcher::Create() when |
374 // creating the URL fetcher. | 375 // creating the URL fetcher. |
375 static content::URLFetcher* CreateGaiaFetcher( | 376 static net::URLFetcher* CreateGaiaFetcher( |
376 net::URLRequestContextGetter* getter, | 377 net::URLRequestContextGetter* getter, |
377 const std::string& body, | 378 const std::string& body, |
378 const std::string& headers, | 379 const std::string& headers, |
379 const GURL& gaia_gurl, | 380 const GURL& gaia_gurl, |
380 int load_flags, | 381 int load_flags, |
381 content::URLFetcherDelegate* delegate); | 382 net::URLFetcherDelegate* delegate); |
382 | 383 |
383 // From a URLFetcher result, generate an appropriate error. | 384 // From a URLFetcher result, generate an appropriate error. |
384 // From the API documentation, both IssueAuthToken and ClientLogin have | 385 // From the API documentation, both IssueAuthToken and ClientLogin have |
385 // the same error returns. | 386 // the same error returns. |
386 static GoogleServiceAuthError GenerateAuthError( | 387 static GoogleServiceAuthError GenerateAuthError( |
387 const std::string& data, | 388 const std::string& data, |
388 const net::URLRequestStatus& status); | 389 const net::URLRequestStatus& status); |
389 | 390 |
390 // These fields are common to GaiaAuthFetcher, same every request | 391 // These fields are common to GaiaAuthFetcher, same every request |
391 GaiaAuthConsumer* const consumer_; | 392 GaiaAuthConsumer* const consumer_; |
392 net::URLRequestContextGetter* const getter_; | 393 net::URLRequestContextGetter* const getter_; |
393 std::string source_; | 394 std::string source_; |
394 const GURL client_login_gurl_; | 395 const GURL client_login_gurl_; |
395 const GURL issue_auth_token_gurl_; | 396 const GURL issue_auth_token_gurl_; |
396 const GURL oauth2_token_gurl_; | 397 const GURL oauth2_token_gurl_; |
397 const GURL get_user_info_gurl_; | 398 const GURL get_user_info_gurl_; |
398 const GURL token_auth_gurl_; | 399 const GURL token_auth_gurl_; |
399 const GURL merge_session_gurl_; | 400 const GURL merge_session_gurl_; |
400 const GURL uberauth_token_gurl_; | 401 const GURL uberauth_token_gurl_; |
401 const GURL client_oauth_gurl_; | 402 const GURL client_oauth_gurl_; |
402 const GURL oauth_login_gurl_; | 403 const GURL oauth_login_gurl_; |
403 | 404 |
404 // While a fetch is going on: | 405 // While a fetch is going on: |
405 scoped_ptr<content::URLFetcher> fetcher_; | 406 scoped_ptr<net::URLFetcher> fetcher_; |
406 GURL client_login_to_oauth2_gurl_; | 407 GURL client_login_to_oauth2_gurl_; |
407 std::string request_body_; | 408 std::string request_body_; |
408 std::string requested_service_; // Currently tracked for IssueAuthToken only. | 409 std::string requested_service_; // Currently tracked for IssueAuthToken only. |
409 bool fetch_pending_; | 410 bool fetch_pending_; |
410 | 411 |
411 friend class GaiaAuthFetcherTest; | 412 friend class GaiaAuthFetcherTest; |
412 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); | 413 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); |
413 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); | 414 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); |
414 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); | 415 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); |
415 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); | 416 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); |
416 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); | 417 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); |
417 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); | 418 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); |
418 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); | 419 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); |
419 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); | 420 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); |
420 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); | 421 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); |
421 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, | 422 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, |
422 ParseClientLoginToOAuth2Response); | 423 ParseClientLoginToOAuth2Response); |
423 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse); | 424 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse); |
424 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ClientOAuthSuccess); | 425 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ClientOAuthSuccess); |
425 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ClientOAuthChallengeSuccess); | 426 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ClientOAuthChallengeSuccess); |
426 | 427 |
427 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); | 428 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); |
428 }; | 429 }; |
429 | 430 |
430 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 431 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
OLD | NEW |