| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Use this class to authenticate users with Gaia and access cookies sent | 5 // Use this class to authenticate users with Gaia and access cookies sent |
| 6 // by the Gaia servers. | 6 // by the Gaia servers. This class lives on the SyncEngine_AuthWatcherThread. |
| 7 // | 7 // |
| 8 // Sample usage: | 8 // Sample usage: |
| 9 // GaiaAuthenticator gaia_auth("User-Agent", SYNC_SERVICE_NAME, | 9 // GaiaAuthenticator gaia_auth("User-Agent", SYNC_SERVICE_NAME, |
| 10 // browser_sync::kExternalGaiaUrl); | 10 // browser_sync::kExternalGaiaUrl); |
| 11 // if (gaia_auth.Authenticate("email", "passwd", SAVE_IN_MEMORY_ONLY, | 11 // if (gaia_auth.Authenticate("email", "passwd", SAVE_IN_MEMORY_ONLY, |
| 12 // true)) { // Synchronous | 12 // true)) { // Synchronous |
| 13 // // Do something with: gaia_auth.auth_token(), or gaia_auth.sid(), | 13 // // Do something with: gaia_auth.auth_token(), or gaia_auth.sid(), |
| 14 // // or gaia_auth.lsid() | 14 // // or gaia_auth.lsid() |
| 15 // } | 15 // } |
| 16 // | 16 // |
| 17 // Sample asynchonous usage: | |
| 18 // GaiaAuthenticator gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
| 19 // browser_sync::kExternalGaiaUrl); | |
| 20 // EventListenerHookup* hookup = NewListenerHookup(gaia_auth.channel(), | |
| 21 // this, &OnAuthenticate); | |
| 22 // gaia_auth.Authenticate("email", "passwd", true, false); | |
| 23 // // OnAuthenticate() will get called with result; | |
| 24 // | |
| 25 // Credentials can also be preserved for subsequent requests, though these are | 17 // Credentials can also be preserved for subsequent requests, though these are |
| 26 // saved in plain-text in memory, and not very secure on client systems. The | 18 // saved in plain-text in memory, and not very secure on client systems. The |
| 27 // email address associated with the Gaia account can be read; the password is | 19 // email address associated with the Gaia account can be read; the password is |
| 28 // write-only. | 20 // write-only. |
| 29 | 21 |
| 30 #ifndef CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ | 22 #ifndef CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ |
| 31 #define CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ | 23 #define CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ |
| 32 | 24 |
| 33 #include <string> | 25 #include <string> |
| 34 | 26 |
| 35 #include "base/basictypes.h" | 27 #include "base/basictypes.h" |
| 28 #include "base/message_loop.h" |
| 36 #include "chrome/browser/sync/engine/net/http_return.h" | 29 #include "chrome/browser/sync/engine/net/http_return.h" |
| 37 #include "chrome/browser/sync/util/event_sys.h" | 30 #include "chrome/browser/sync/util/event_sys.h" |
| 38 #include "chrome/browser/sync/util/pthread_helpers.h" | |
| 39 #include "chrome/browser/sync/util/signin.h" | 31 #include "chrome/browser/sync/util/signin.h" |
| 40 #include "googleurl/src/gurl.h" | 32 #include "googleurl/src/gurl.h" |
| 41 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 33 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 42 | 34 |
| 43 namespace browser_sync { | 35 namespace browser_sync { |
| 44 | 36 |
| 45 static const char kGaiaUrl[] = | 37 static const char kGaiaUrl[] = |
| 46 "https://www.google.com:443/accounts/ClientLogin"; | 38 "https://www.google.com:443/accounts/ClientLogin"; |
| 47 | 39 |
| 48 // Use of the following enum is odd. GaiaAuthenticator only looks at | 40 // Use of the following enum is odd. GaiaAuthenticator only looks at |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // must include a user-agent and a service-id when creating one. The | 96 // must include a user-agent and a service-id when creating one. The |
| 105 // user_agent is a short string used for simple log analysis. gaia_url is used | 97 // user_agent is a short string used for simple log analysis. gaia_url is used |
| 106 // to choose the server to authenticate with (e.g. | 98 // to choose the server to authenticate with (e.g. |
| 107 // http://www.google.com/accounts/ClientLogin). | 99 // http://www.google.com/accounts/ClientLogin). |
| 108 GaiaAuthenticator(const std::string& user_agent, | 100 GaiaAuthenticator(const std::string& user_agent, |
| 109 const std::string& service_id, | 101 const std::string& service_id, |
| 110 const std::string& gaia_url); | 102 const std::string& gaia_url); |
| 111 | 103 |
| 112 virtual ~GaiaAuthenticator(); | 104 virtual ~GaiaAuthenticator(); |
| 113 | 105 |
| 106 // This object should only be invoked from the AuthWatcherThread message |
| 107 // loop, which is injected here. |
| 108 void set_message_loop(MessageLoop* loop) { |
| 109 message_loop_ = loop; |
| 110 } |
| 111 |
| 114 // Pass credentials to authenticate with, or use saved credentials via an | 112 // Pass credentials to authenticate with, or use saved credentials via an |
| 115 // overload. If authentication succeeds, you can retrieve the authentication | 113 // overload. If authentication succeeds, you can retrieve the authentication |
| 116 // token via the respective accessors. Returns a boolean indicating whether | 114 // token via the respective accessors. Returns a boolean indicating whether |
| 117 // authentication succeeded or not. | 115 // authentication succeeded or not. |
| 118 bool Authenticate(const std::string& user_name, const std::string& password, | 116 bool Authenticate(const std::string& user_name, const std::string& password, |
| 119 SaveCredentials should_save_credentials, bool synchronous, | 117 SaveCredentials should_save_credentials, |
| 120 const std::string& captcha_token, | 118 const std::string& captcha_token, |
| 121 const std::string& captcha_value, | 119 const std::string& captcha_value, |
| 122 SignIn try_first); | 120 SignIn try_first); |
| 123 | 121 |
| 124 bool Authenticate(const std::string& user_name, const std::string& password, | 122 bool Authenticate(const std::string& user_name, const std::string& password, |
| 125 SaveCredentials should_save_credentials, bool synchronous, | 123 SaveCredentials should_save_credentials, |
| 126 SignIn try_first); | 124 SignIn try_first); |
| 127 | 125 |
| 128 bool AuthenticateService(const std::string& service_id, | |
| 129 const std::string& sid, | |
| 130 const std::string& lsid, | |
| 131 std::string* other_service_cookie); | |
| 132 | |
| 133 // Resets all stored cookies to their default values. | 126 // Resets all stored cookies to their default values. |
| 134 void ResetCredentials(); | 127 void ResetCredentials(); |
| 135 | 128 |
| 136 void SetUsernamePassword(const std::string& username, | 129 void SetUsernamePassword(const std::string& username, |
| 137 const std::string& password); | 130 const std::string& password); |
| 138 | 131 |
| 139 void SetUsername(const std::string& username); | 132 void SetUsername(const std::string& username); |
| 140 | 133 |
| 141 void SetAuthToken(const std::string& auth_token, SaveCredentials); | 134 void SetAuthToken(const std::string& auth_token, SaveCredentials); |
| 142 | 135 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 const std::string& password, | 175 const std::string& password, |
| 183 SaveCredentials should_save_credentials, | 176 SaveCredentials should_save_credentials, |
| 184 const std::string& captcha_token, | 177 const std::string& captcha_token, |
| 185 const std::string& captcha_value, | 178 const std::string& captcha_value, |
| 186 SignIn try_first); | 179 SignIn try_first); |
| 187 | 180 |
| 188 // The real Authenticate implementations. | 181 // The real Authenticate implementations. |
| 189 bool AuthenticateImpl(const AuthParams& params); | 182 bool AuthenticateImpl(const AuthParams& params); |
| 190 bool AuthenticateImpl(const AuthParams& params, AuthResults* results); | 183 bool AuthenticateImpl(const AuthParams& params, AuthResults* results); |
| 191 bool PerformGaiaRequest(const AuthParams& params, AuthResults* results); | 184 bool PerformGaiaRequest(const AuthParams& params, AuthResults* results); |
| 192 bool LaunchAuthenticate(const AuthParams& params, bool synchronous); | |
| 193 static void *ThreadMain(void *arg); | |
| 194 | 185 |
| 195 // virtual for testing purposes. | 186 // virtual for testing purposes. |
| 196 virtual bool Post(const GURL& url, const std::string& post_body, | 187 virtual bool Post(const GURL& url, const std::string& post_body, |
| 197 unsigned long* response_code, std::string* response_body) { | 188 unsigned long* response_code, std::string* response_body) { |
| 198 return false; | 189 return false; |
| 199 } | 190 } |
| 200 | 191 |
| 201 // Caller should fill in results->LSID before calling. Result in | 192 // Caller should fill in results->LSID before calling. Result in |
| 202 // results->primary_email. | 193 // results->primary_email. |
| 203 bool LookupEmail(AuthResults* results); | 194 bool LookupEmail(AuthResults* results); |
| 204 | 195 |
| 205 public: | 196 public: |
| 206 // Retrieve email. | 197 // Retrieve email. |
| 207 inline std::string email() const { | 198 inline std::string email() const { |
| 208 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 199 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 209 return auth_results_.email; | 200 return auth_results_.email; |
| 210 } | 201 } |
| 211 | 202 |
| 212 // Retrieve password. | 203 // Retrieve password. |
| 213 inline std::string password() const { | 204 inline std::string password() const { |
| 214 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 205 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 215 return auth_results_.password; | 206 return auth_results_.password; |
| 216 } | 207 } |
| 217 | 208 |
| 218 // Retrieve AuthToken, if previously authenticated; otherwise returns "". | 209 // Retrieve AuthToken, if previously authenticated; otherwise returns "". |
| 219 inline std::string auth_token() const { | 210 inline std::string auth_token() const { |
| 220 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 211 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 221 return auth_results_.auth_token; | 212 return auth_results_.auth_token; |
| 222 } | 213 } |
| 223 | 214 |
| 224 // Retrieve SID cookie. For details, see the Google Accounts documentation. | 215 // Retrieve SID cookie. For details, see the Google Accounts documentation. |
| 225 inline std::string sid() const { | 216 inline std::string sid() const { |
| 226 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 217 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 227 return auth_results_.sid; | 218 return auth_results_.sid; |
| 228 } | 219 } |
| 229 | 220 |
| 230 // Retrieve LSID cookie. For details, see the Google Accounts documentation. | 221 // Retrieve LSID cookie. For details, see the Google Accounts documentation. |
| 231 inline std::string lsid() const { | 222 inline std::string lsid() const { |
| 232 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 223 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 233 return auth_results_.lsid; | 224 return auth_results_.lsid; |
| 234 } | 225 } |
| 235 | 226 |
| 236 // Get last authentication error. | 227 // Get last authentication error. |
| 237 inline enum AuthenticationError auth_error() const { | 228 inline enum AuthenticationError auth_error() const { |
| 238 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 229 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 239 return auth_results_.auth_error; | 230 return auth_results_.auth_error; |
| 240 } | 231 } |
| 241 | 232 |
| 242 inline std::string auth_error_url() const { | 233 inline std::string auth_error_url() const { |
| 243 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 234 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 244 return auth_results_.auth_error_url; | 235 return auth_results_.auth_error_url; |
| 245 } | 236 } |
| 246 | 237 |
| 247 inline std::string captcha_token() const { | 238 inline std::string captcha_token() const { |
| 248 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 239 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 249 return auth_results_.captcha_token; | 240 return auth_results_.captcha_token; |
| 250 } | 241 } |
| 251 | 242 |
| 252 inline std::string captcha_url() const { | 243 inline std::string captcha_url() const { |
| 253 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 244 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 254 return auth_results_.captcha_url; | 245 return auth_results_.captcha_url; |
| 255 } | 246 } |
| 256 | 247 |
| 257 inline AuthResults results() const { | 248 inline AuthResults results() const { |
| 258 PThreadScopedLock<PThreadMutex> enter(&mutex_); | 249 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 259 return auth_results_; | 250 return auth_results_; |
| 260 } | 251 } |
| 261 | 252 |
| 262 typedef EventChannel<GaiaAuthEvent, PThreadMutex> Channel; | 253 typedef EventChannel<GaiaAuthEvent, Lock> Channel; |
| 263 | 254 |
| 264 inline Channel* channel() const { | 255 inline Channel* channel() const { |
| 265 return channel_; | 256 return channel_; |
| 266 } | 257 } |
| 267 | 258 |
| 268 private: | 259 private: |
| 269 bool IssueAuthToken(AuthResults* results, const std::string& service_id, | 260 bool IssueAuthToken(AuthResults* results, const std::string& service_id, |
| 270 bool long_lived_token); | 261 bool long_lived_token); |
| 271 | 262 |
| 272 // Helper method to parse response when authentication succeeds. | 263 // Helper method to parse response when authentication succeeds. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 288 // simultaneously, the sync code issues auth requests one at a time. | 279 // simultaneously, the sync code issues auth requests one at a time. |
| 289 uint32 request_count_; | 280 uint32 request_count_; |
| 290 | 281 |
| 291 Channel* channel_; | 282 Channel* channel_; |
| 292 | 283 |
| 293 // Used to compute backoff time for next allowed authentication. | 284 // Used to compute backoff time for next allowed authentication. |
| 294 int delay_; // In seconds. | 285 int delay_; // In seconds. |
| 295 time_t next_allowed_auth_attempt_time_; | 286 time_t next_allowed_auth_attempt_time_; |
| 296 int early_auth_attempt_count_; | 287 int early_auth_attempt_count_; |
| 297 | 288 |
| 298 // Protects auth_results_, and request_count_. | 289 // The message loop all our methods are invoked on. Generally this is the |
| 299 mutable PThreadMutex mutex_; | 290 // SyncEngine_AuthWatcherThread's message loop. |
| 291 MessageLoop* message_loop_; |
| 300 }; | 292 }; |
| 301 | 293 |
| 302 } // namespace browser_sync | 294 } // namespace browser_sync |
| 303 | 295 |
| 304 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ | 296 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_GAIA_AUTHENTICATOR_H_ |
| OLD | NEW |