Home | History | Annotate | Download | only in login
      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 "chrome/browser/signin/oauth2_token_service.h"
     18 #include "google_apis/gaia/gaia_auth_consumer.h"
     19 #include "google_apis/gaia/gaia_auth_fetcher.h"
     20 #include "google_apis/gaia/oauth2_access_token_fetcher.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   class Delegate {
     32    public:
     33     virtual ~Delegate() {}
     34     // Invoked during exchange of OAuth2 refresh token for GAIA service token.
     35     virtual void OnOAuthLoginSuccess(
     36         const ClientLoginResult& gaia_credentials) = 0;
     37     // Invoked when provided OAuth2 refresh token is invalid.
     38     virtual void OnOAuthLoginFailure() = 0;
     39     // Invoked when cookie session is successfully merged.
     40     virtual void OnSessionMergeSuccess() = 0;
     41     // Invoked when cookie session can not be merged.
     42     virtual void OnSessionMergeFailure() = 0;
     43   };
     44 
     45   OAuth2LoginVerifier(OAuth2LoginVerifier::Delegate* delegate,
     46                       net::URLRequestContextGetter* system_request_context,
     47                       net::URLRequestContextGetter* user_request_context);
     48   virtual ~OAuth2LoginVerifier();
     49 
     50   // Attempts to restore session from OAuth2 refresh token minting all necesarry
     51   // tokens along the way (OAuth2 access token, SID/LSID, GAIA service token).
     52   void VerifyProfileTokens(Profile* profile);
     53 
     54  private:
     55   enum SessionRestoreType {
     56     RESTORE_UNDEFINED = 0,
     57     RESTORE_FROM_GAIA_TOKEN = 1,
     58     RESTORE_FROM_OAUTH2_REFRESH_TOKEN = 2,
     59   };
     60   // GaiaAuthConsumer overrides.
     61   virtual void OnUberAuthTokenSuccess(const std::string& token) OVERRIDE;
     62   virtual void OnUberAuthTokenFailure(
     63       const GoogleServiceAuthError& error) OVERRIDE;
     64   virtual void OnClientLoginSuccess(const ClientLoginResult& result) OVERRIDE;
     65   virtual void OnClientLoginFailure(
     66       const GoogleServiceAuthError& error) OVERRIDE;
     67   virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
     68   virtual void OnMergeSessionFailure(
     69       const GoogleServiceAuthError& error) OVERRIDE;
     70 
     71   // OAuth2TokenService::Consumer overrides.
     72   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
     73                                  const std::string& access_token,
     74                                  const base::Time& expiration_time) OVERRIDE;
     75   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
     76                                  const GoogleServiceAuthError& error) OVERRIDE;
     77 
     78   // Starts fetching OAuth1 access token for OAuthLogin call.
     79   void StartFetchingOAuthLoginAccessToken(Profile* profile);
     80 
     81   // Starts OAuthLogin request for GAIA uber-token.
     82   void StartOAuthLoginForUberToken();
     83 
     84   // Starts OAuthLogin request.
     85   void StartOAuthLoginForGaiaCredentials();
     86 
     87   // Attempts to merge session from present |gaia_token_|.
     88   void StartMergeSession();
     89 
     90   // Decides how to proceed on GAIA |error|. If the error looks temporary,
     91   // retries |task| after certain delay until max retry count is reached.
     92   void RetryOnError(const char* operation_id,
     93                     const GoogleServiceAuthError& error,
     94                     const base::Closure& task_to_retry,
     95                     const base::Closure& error_handler);
     96 
     97   OAuth2LoginVerifier::Delegate* delegate_;
     98   scoped_refptr<net::URLRequestContextGetter> system_request_context_;
     99   scoped_refptr<net::URLRequestContextGetter> user_request_context_;
    100   scoped_ptr<GaiaAuthFetcher> gaia_system_fetcher_;
    101   scoped_ptr<GaiaAuthFetcher> gaia_fetcher_;
    102   std::string access_token_;
    103   std::string gaia_token_;
    104   scoped_ptr<OAuth2TokenService::Request> login_token_request_;
    105   // The retry counter. Increment this only when failure happened.
    106   int retry_count_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(OAuth2LoginVerifier);
    109 };
    110 
    111 }  // namespace chromeos
    112 
    113 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_VERIFIER_H_
    114