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_OAUTH_LOGIN_MANAGER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH_LOGIN_MANAGER_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "net/url_request/url_request_context_getter.h" 12 13 class Profile; 14 15 namespace chromeos { 16 17 // This class is responsible for restoring authenticated web sessions out of 18 // OAuth tokens or vice versa. 19 class OAuthLoginManager { 20 public: 21 enum SessionRestoreState { 22 // Session restore is not started. 23 SESSION_RESTORE_NOT_STARTED, 24 // Session restore is in progress. We are currently issuing calls to verify 25 // stored OAuth tokens and populate cookie jar with GAIA credentials. 26 SESSION_RESTORE_IN_PROGRESS, 27 // Session restore is completed. 28 SESSION_RESTORE_DONE, 29 }; 30 31 // Session restore strategy. 32 enum SessionRestoreStrategy { 33 // Generate OAuth2 refresh token from authentication profile's cookie jar. 34 // Restore session from generated OAuth2 refresh token. 35 RESTORE_FROM_COOKIE_JAR, 36 // Restore session from saved OAuth2 refresh token from TokenServices. 37 RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN, 38 // Restore session from OAuth2 refresh token passed via command line. 39 RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN, 40 // Restore session from authentication code passed via command line. 41 RESTORE_FROM_AUTH_CODE, 42 }; 43 44 class Delegate { 45 public: 46 virtual ~Delegate() {} 47 48 // Raised when merge session is completed. 49 virtual void OnCompletedMergeSession() = 0; 50 51 // Raised when cookie jar authentication is successfully completed. 52 virtual void OnCompletedAuthentication(Profile* user_profile) = 0; 53 54 // Raised when stored OAuth(1|2) tokens are found and authentication 55 // profile is no longer needed. 56 virtual void OnFoundStoredTokens() = 0; 57 58 // Raised when a new OAuth2 refresh token is avaialble. 59 virtual void OnNewRefreshTokenAvaiable(Profile* user_profile) {} 60 }; 61 62 // Factory method. 63 static OAuthLoginManager* Create(OAuthLoginManager::Delegate* delegate); 64 65 explicit OAuthLoginManager(OAuthLoginManager::Delegate* delegate); 66 virtual ~OAuthLoginManager(); 67 68 // Restores and verifies OAuth tokens either following specified 69 // |restore_strategy|. For |restore_strategy| with values 70 // RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN or 71 // RESTORE_FROM_AUTH_CODE, respectively 72 // parameters |oauth2_refresh_token| or |auth_code| need to have non-empty 73 // value. 74 virtual void RestoreSession( 75 Profile* user_profile, 76 net::URLRequestContextGetter* auth_request_context, 77 SessionRestoreStrategy restore_strategy, 78 const std::string& oauth2_refresh_token, 79 const std::string& auth_code) = 0; 80 81 // Continues session restore after transient network errors. 82 virtual void ContinueSessionRestore() = 0; 83 84 // Stops all background authentication requests. 85 virtual void Stop() = 0; 86 87 // Returns session restore state. 88 SessionRestoreState state() { return state_; } 89 90 protected: 91 // Signals delegate that authentication is completed, kicks off token fetching 92 // process in TokenService. 93 void CompleteAuthentication(); 94 95 OAuthLoginManager::Delegate* delegate_; 96 Profile* user_profile_; 97 scoped_refptr<net::URLRequestContextGetter> auth_request_context_; 98 SessionRestoreStrategy restore_strategy_; 99 SessionRestoreState state_; 100 101 DISALLOW_COPY_AND_ASSIGN(OAuthLoginManager); 102 }; 103 104 } // namespace chromeos 105 106 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH_LOGIN_MANAGER_H_ 107