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