1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_VERIFIER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_VERIFIER_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/callback_forward.h" 12 #include "base/compiler_specific.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "chrome/browser/profiles/profile.h" 17 #include "google_apis/gaia/gaia_auth_consumer.h" 18 #include "google_apis/gaia/gaia_auth_fetcher.h" 19 #include "google_apis/gaia/oauth2_access_token_fetcher.h" 20 #include "google_apis/gaia/oauth2_token_service.h" 21 #include "net/url_request/url_request_context_getter.h" 22 23 namespace chromeos { 24 25 // Given the OAuth2 refresh token, this class will try to exchange it for GAIA 26 // credentials (SID+LSID) and populate current session's cookie jar. 27 class OAuth2LoginVerifier : public base::SupportsWeakPtr<OAuth2LoginVerifier>, 28 public GaiaAuthConsumer, 29 public OAuth2TokenService::Consumer { 30 public: 31 typedef base::Callback<void(bool connection_error)> ErrorHandler; 32 33 class Delegate { 34 public: 35 virtual ~Delegate() {} 36 // Invoked when cookie session is successfully merged. 37 virtual void OnSessionMergeSuccess() = 0; 38 39 // Invoked when cookie session can not be merged. 40 virtual void OnSessionMergeFailure(bool connection_error) = 0; 41 42 // Invoked when account list is retrieved during post-merge session 43 // verification. 44 virtual void OnListAccountsSuccess(const std::string& data) = 0; 45 46 // Invoked when post-merge session verification fails. 47 virtual void OnListAccountsFailure(bool connection_error) = 0; 48 }; 49 50 OAuth2LoginVerifier(OAuth2LoginVerifier::Delegate* delegate, 51 net::URLRequestContextGetter* system_request_context, 52 net::URLRequestContextGetter* user_request_context); 53 virtual ~OAuth2LoginVerifier(); 54 55 // Attempts to restore session from OAuth2 refresh token minting all necesarry 56 // tokens along the way (OAuth2 access token, SID/LSID, GAIA service token). 57 void VerifyProfileTokens(Profile* profile); 58 59 private: 60 enum SessionRestoreType { 61 RESTORE_UNDEFINED = 0, 62 RESTORE_FROM_GAIA_TOKEN = 1, 63 RESTORE_FROM_OAUTH2_REFRESH_TOKEN = 2, 64 }; 65 // GaiaAuthConsumer overrides. 66 virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE; 67 virtual void OnUberAuthTokenFailure( 68 const GoogleServiceAuthError& error) OVERRIDE; 69 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE; 70 virtual void OnMergeSessionFailure( 71 const GoogleServiceAuthError& error) OVERRIDE; 72 virtual void OnListAccountsSuccess(const std::string& data) OVERRIDE; 73 virtual void OnListAccountsFailure( 74 const GoogleServiceAuthError& error) OVERRIDE; 75 76 // OAuth2TokenService::Consumer overrides. 77 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 78 const std::string& access_token, 79 const base::Time& expiration_time) OVERRIDE; 80 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 81 const GoogleServiceAuthError& error) OVERRIDE; 82 83 // Starts fetching OAuth1 access token for OAuthLogin call. 84 void StartFetchingOAuthLoginAccessToken(Profile* profile); 85 86 // Starts OAuthLogin request for GAIA uber-token. 87 void StartOAuthLoginForUberToken(); 88 89 // Attempts to merge session from present |gaia_token_|. 90 void StartMergeSession(); 91 92 // Schedules post merge verification to ensure that browser session restore 93 // hasn't stumped over SID/LSID. 94 void SchedulePostMergeVerification(); 95 96 // Starts post merge request verification. 97 void StartPostRestoreVerification(); 98 99 // Decides how to proceed on GAIA |error|. If the error looks temporary, 100 // retries |task| after certain delay until max retry count is reached. 101 void RetryOnError(const char* operation_id, 102 const GoogleServiceAuthError& error, 103 const base::Closure& task_to_retry, 104 const ErrorHandler& error_handler); 105 106 OAuth2LoginVerifier::Delegate* delegate_; 107 scoped_refptr<net::URLRequestContextGetter> system_request_context_; 108 scoped_refptr<net::URLRequestContextGetter> user_request_context_; 109 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_; 110 std::string access_token_; 111 std::string gaia_token_; 112 scoped_ptr<OAuth2TokenService::Request> login_token_request_; 113 // The retry counter. Increment this only when failure happened. 114 int retry_count_; 115 116 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginVerifier); 117 }; 118 119 } // namespace chromeos 120 121 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_VERIFIER_H_ 122