OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 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 // AuthWatcher watches authentication events and user open and close |
| 6 // events and accordingly opens and closes shares. |
| 7 |
| 8 #ifndef CHROME_BROWSER_SYNC_ENGINE_AUTH_WATCHER_H_ |
| 9 #define CHROME_BROWSER_SYNC_ENGINE_AUTH_WATCHER_H_ |
| 10 |
| 11 #include <map> |
| 12 #include <string> |
| 13 |
| 14 #include "base/atomicops.h" |
| 15 #include "base/scoped_ptr.h" |
| 16 #include "chrome/browser/sync/engine/net/gaia_authenticator.h" |
| 17 #include "chrome/browser/sync/util/event_sys.h" |
| 18 #include "chrome/browser/sync/util/pthread_helpers.h" |
| 19 #include "chrome/browser/sync/util/sync_types.h" |
| 20 |
| 21 namespace syncable { |
| 22 struct DirectoryManagerEvent; |
| 23 class DirectoryManager; |
| 24 } |
| 25 |
| 26 namespace browser_sync { |
| 27 class AllStatus; |
| 28 class AuthWatcher; |
| 29 class ServerConnectionManager; |
| 30 class TalkMediator; |
| 31 class URLFactory; |
| 32 class UserSettings; |
| 33 struct ServerConnectionEvent; |
| 34 |
| 35 struct AuthWatcherEvent { |
| 36 enum WhatHappened { |
| 37 AUTHENTICATION_ATTEMPT_START, |
| 38 AUTHWATCHER_DESTROYED, |
| 39 AUTH_SUCCEEDED, |
| 40 GAIA_AUTH_FAILED, |
| 41 SERVICE_USER_NOT_SIGNED_UP, |
| 42 SERVICE_AUTH_FAILED, |
| 43 SERVICE_CONNECTION_FAILED, |
| 44 // Used in a safety check in AuthWatcher::AuthenticateWithToken() |
| 45 ILLEGAL_VALUE, |
| 46 }; |
| 47 WhatHappened what_happened; |
| 48 const GaiaAuthenticator::AuthResults* auth_results; |
| 49 // use AuthWatcherEvent as its own traits type in hookups. |
| 50 typedef AuthWatcherEvent EventType; |
| 51 static inline bool IsChannelShutdownEvent(const AuthWatcherEvent& event) { |
| 52 return event.what_happened == AUTHWATCHER_DESTROYED; |
| 53 } |
| 54 |
| 55 // Used for AUTH_SUCCEEDED notification |
| 56 std::string user_email; |
| 57 |
| 58 // How was this auth attempt initiated? |
| 59 enum AuthenticationTrigger { |
| 60 USER_INITIATED = 0, // default value. |
| 61 EXPIRED_CREDENTIALS, |
| 62 }; |
| 63 |
| 64 AuthenticationTrigger trigger; |
| 65 }; |
| 66 |
| 67 class AuthWatcher { |
| 68 public: |
| 69 // Normal progression is local -> gaia -> token |
| 70 enum Status { LOCALLY_AUTHENTICATED, GAIA_AUTHENTICATED, NOT_AUTHENTICATED }; |
| 71 typedef syncable::DirectoryManagerEvent DirectoryManagerEvent; |
| 72 typedef syncable::DirectoryManager DirectoryManager; |
| 73 typedef TalkMediator TalkMediator; |
| 74 |
| 75 AuthWatcher(DirectoryManager* dirman, |
| 76 ServerConnectionManager* scm, |
| 77 AllStatus* allstatus, |
| 78 const std::string& user_agent, |
| 79 const std::string& service_id, |
| 80 const std::string& gaia_url, |
| 81 UserSettings* user_settings, |
| 82 GaiaAuthenticator* gaia_auth, |
| 83 TalkMediator* talk_mediator); |
| 84 ~AuthWatcher(); |
| 85 |
| 86 // Returns true if the open share has gotten zero |
| 87 // updates from the sync server (initial sync complete.) |
| 88 bool LoadDirectoryListAndOpen(const PathString& login); |
| 89 |
| 90 typedef EventChannel<AuthWatcherEvent, PThreadMutex> Channel; |
| 91 |
| 92 inline Channel* channel() const { |
| 93 return channel_.get(); |
| 94 } |
| 95 |
| 96 void Authenticate(const std::string& email, const std::string& password, |
| 97 const std::string& captcha_token, const std::string& captcha_value, |
| 98 bool persist_creds_to_disk); |
| 99 |
| 100 void Authenticate(const std::string& email, const std::string& password, |
| 101 bool persist_creds_to_disk) { |
| 102 Authenticate(email, password, "", "", persist_creds_to_disk); |
| 103 } |
| 104 |
| 105 // Retrieves an auth token for a named service for which a long-lived token |
| 106 // was obtained at login time. Returns true if a long-lived token can be |
| 107 // found, false otherwise. |
| 108 bool GetAuthTokenForService(const std::string& service_name, |
| 109 std::string* service_token); |
| 110 |
| 111 std::string email() const; |
| 112 syncable::DirectoryManager* dirman() const { return dirman_; } |
| 113 ServerConnectionManager* scm() const { return scm_; } |
| 114 AllStatus* allstatus() const { return allstatus_; } |
| 115 UserSettings* settings() const { return user_settings_; } |
| 116 Status status() const { return (Status)status_; } |
| 117 |
| 118 void Logout(); |
| 119 |
| 120 // For synchronizing other destructors. |
| 121 void WaitForAuthThreadFinish(); |
| 122 |
| 123 protected: |
| 124 void Reset(); |
| 125 void ClearAuthenticationData(); |
| 126 |
| 127 void NotifyAuthSucceeded(const std::string& email); |
| 128 bool StartNewAuthAttempt(const std::string& email, |
| 129 const std::string& password, |
| 130 const std::string& auth_token, const std::string& captcha_token, |
| 131 const std::string& captcha_value, bool persist_creds_to_disk, |
| 132 AuthWatcherEvent::AuthenticationTrigger trigger); |
| 133 void HandleServerConnectionEvent(const ServerConnectionEvent& event); |
| 134 |
| 135 void SaveUserSettings(const std::string& username, |
| 136 const std::string& auth_token, |
| 137 const bool save_credentials); |
| 138 |
| 139 // These two helpers should only be called from the auth function. |
| 140 // returns false iff we had problems and should try GAIA_AUTH again. |
| 141 bool ProcessGaiaAuthSuccess(); |
| 142 void ProcessGaiaAuthFailure(); |
| 143 |
| 144 // Just checks that the user has at least one local share cache. |
| 145 bool AuthenticateLocally(std::string email); |
| 146 // Also checks the user's password against stored password hash. |
| 147 bool AuthenticateLocally(std::string email, const std::string& password); |
| 148 |
| 149 // Sets the trigger member of the event and sends the event on channel_. |
| 150 void NotifyListeners(AuthWatcherEvent* event); |
| 151 |
| 152 const std::string& sync_service_token() const { return sync_service_token_; } |
| 153 |
| 154 public: |
| 155 bool AuthenticateWithToken(const std::string& email, |
| 156 const std::string& auth_token); |
| 157 |
| 158 protected: |
| 159 typedef PThreadScopedLock<PThreadMutex> MutexLock; |
| 160 |
| 161 // Passed to newly created threads. |
| 162 struct ThreadParams { |
| 163 AuthWatcher* self; |
| 164 std::string email; |
| 165 std::string password; |
| 166 std::string auth_token; |
| 167 std::string captcha_token; |
| 168 std::string captcha_value; |
| 169 bool persist_creds_to_disk; |
| 170 AuthWatcherEvent::AuthenticationTrigger trigger; |
| 171 }; |
| 172 |
| 173 // Initial function passed to pthread_create. |
| 174 static void* AuthenticationThreadStartRoutine(void* arg); |
| 175 // Member function called by AuthenticationThreadStartRoutine. |
| 176 void* AuthenticationThreadMain(struct ThreadParams* arg); |
| 177 |
| 178 scoped_ptr<GaiaAuthenticator> const gaia_; |
| 179 syncable::DirectoryManager* const dirman_; |
| 180 ServerConnectionManager* const scm_; |
| 181 scoped_ptr<EventListenerHookup> connmgr_hookup_; |
| 182 AllStatus* const allstatus_; |
| 183 // TODO(chron): It is incorrect to make assignments to AtomicWord. |
| 184 volatile base::subtle::AtomicWord status_; |
| 185 UserSettings* user_settings_; |
| 186 TalkMediator* talk_mediator_; // Interface to the notifications engine. |
| 187 scoped_ptr<Channel> channel_; |
| 188 |
| 189 // We store our service token in memory as a workaround to the fact that we |
| 190 // don't persist it when the user unchecks "remember me". |
| 191 // We also include it on outgoing requests. |
| 192 std::string sync_service_token_; |
| 193 |
| 194 PThreadMutex mutex_; |
| 195 // All members below are protected by the above mutex |
| 196 pthread_t thread_; |
| 197 bool thread_handle_valid_; |
| 198 bool authenticating_now_; |
| 199 AuthWatcherEvent::AuthenticationTrigger current_attempt_trigger_; |
| 200 }; |
| 201 |
| 202 } // namespace browser_sync |
| 203 |
| 204 #endif // CHROME_BROWSER_SYNC_ENGINE_AUTH_WATCHER_H_ |
OLD | NEW |