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