| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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. This class cannot be used on its own becaue it relies | 6 // by the Gaia servers. This class cannot be used on its own becaue it relies |
| 7 // on a subclass to provide the virtual Post and GetBackoffDelaySeconds methods. | 7 // on a subclass to provide the virtual Post and GetBackoffDelaySeconds methods. |
| 8 // | 8 // |
| 9 // Sample usage: | 9 // Sample usage: |
| 10 // class ActualGaiaAuthenticator : public gaia::GaiaAuthenticator { | 10 // class ActualGaiaAuthenticator : public gaia::GaiaAuthenticator { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // artifacts of the sync code which needs to be cleaned up. | 27 // artifacts of the sync code which needs to be cleaned up. |
| 28 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 28 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 29 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 29 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 30 #pragma once | 30 #pragma once |
| 31 | 31 |
| 32 #include <string> | 32 #include <string> |
| 33 | 33 |
| 34 #include "base/basictypes.h" | 34 #include "base/basictypes.h" |
| 35 #include "base/gtest_prod_util.h" | 35 #include "base/gtest_prod_util.h" |
| 36 #include "base/message_loop.h" | 36 #include "base/message_loop.h" |
| 37 #include "chrome/common/deprecated/event_sys.h" | |
| 38 #include "googleurl/src/gurl.h" | 37 #include "googleurl/src/gurl.h" |
| 39 | 38 |
| 40 namespace gaia { | 39 namespace gaia { |
| 41 | 40 |
| 42 // Error codes from Gaia. These will be set correctly for both Gaia V1 | 41 // Error codes from Gaia. These will be set correctly for both Gaia V1 |
| 43 // (/ClientAuth) and V2 (/ClientLogin) | 42 // (/ClientAuth) and V2 (/ClientLogin) |
| 44 enum AuthenticationError { | 43 enum AuthenticationError { |
| 45 None = 0, | 44 None = 0, |
| 46 BadAuthentication = 1, | 45 BadAuthentication = 1, |
| 47 NotVerified = 2, | 46 NotVerified = 2, |
| 48 TermsNotAgreed = 3, | 47 TermsNotAgreed = 3, |
| 49 Unknown = 4, | 48 Unknown = 4, |
| 50 AccountDeleted = 5, | 49 AccountDeleted = 5, |
| 51 AccountDisabled = 6, | 50 AccountDisabled = 6, |
| 52 CaptchaRequired = 7, | 51 CaptchaRequired = 7, |
| 53 ServiceUnavailable = 8, | 52 ServiceUnavailable = 8, |
| 54 // Errors generated by this class not Gaia. | 53 // Errors generated by this class not Gaia. |
| 55 CredentialsNotSet = 9, | 54 CredentialsNotSet = 9, |
| 56 ConnectionUnavailable = 10 | 55 ConnectionUnavailable = 10 |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 class GaiaAuthenticator; | 58 class GaiaAuthenticator; |
| 60 | 59 |
| 61 struct GaiaAuthEvent { | |
| 62 enum { | |
| 63 GAIA_AUTH_FAILED, | |
| 64 GAIA_AUTH_SUCCEEDED, | |
| 65 GAIA_AUTHENTICATOR_DESTROYED | |
| 66 } | |
| 67 what_happened; | |
| 68 AuthenticationError error; | |
| 69 const GaiaAuthenticator* authenticator; | |
| 70 | |
| 71 // Lets us use GaiaAuthEvent as its own traits type in hookups. | |
| 72 typedef GaiaAuthEvent EventType; | |
| 73 static inline bool IsChannelShutdownEvent(const GaiaAuthEvent& event) { | |
| 74 return event.what_happened == GAIA_AUTHENTICATOR_DESTROYED; | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 // GaiaAuthenticator can be used to pass user credentials to Gaia and obtain | 60 // GaiaAuthenticator can be used to pass user credentials to Gaia and obtain |
| 79 // cookies set by the Gaia servers. | 61 // cookies set by the Gaia servers. |
| 80 class GaiaAuthenticator { | 62 class GaiaAuthenticator { |
| 81 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticatorTest, | 63 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticatorTest, |
| 82 TestNewlineAtEndOfAuthTokenRemoved); | 64 TestNewlineAtEndOfAuthTokenRemoved); |
| 83 public: | 65 public: |
| 84 | 66 |
| 85 // Since GaiaAuthenticator can be used for any service, or by any client, you | 67 // Since GaiaAuthenticator can be used for any service, or by any client, you |
| 86 // must include a user-agent and a service-id when creating one. The | 68 // must include a user-agent and a service-id when creating one. The |
| 87 // user_agent is a short string used for simple log analysis. gaia_url is used | 69 // user_agent is a short string used for simple log analysis. gaia_url is used |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 inline std::string captcha_url() const { | 222 inline std::string captcha_url() const { |
| 241 DCHECK_EQ(MessageLoop::current(), message_loop_); | 223 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 242 return auth_results_.captcha_url; | 224 return auth_results_.captcha_url; |
| 243 } | 225 } |
| 244 | 226 |
| 245 inline AuthResults results() const { | 227 inline AuthResults results() const { |
| 246 DCHECK_EQ(MessageLoop::current(), message_loop_); | 228 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 247 return auth_results_; | 229 return auth_results_; |
| 248 } | 230 } |
| 249 | 231 |
| 250 typedef EventChannel<GaiaAuthEvent, base::Lock> Channel; | |
| 251 | |
| 252 inline Channel* channel() const { | |
| 253 return channel_; | |
| 254 } | |
| 255 | |
| 256 private: | 232 private: |
| 257 bool IssueAuthToken(AuthResults* results, const std::string& service_id); | 233 bool IssueAuthToken(AuthResults* results, const std::string& service_id); |
| 258 | 234 |
| 259 // Helper method to parse response when authentication succeeds. | 235 // Helper method to parse response when authentication succeeds. |
| 260 void ExtractTokensFrom(const std::string& response, AuthResults* results); | 236 void ExtractTokensFrom(const std::string& response, AuthResults* results); |
| 261 // Helper method to parse response when authentication fails. | 237 // Helper method to parse response when authentication fails. |
| 262 void ExtractAuthErrorFrom(const std::string& response, AuthResults* results); | 238 void ExtractAuthErrorFrom(const std::string& response, AuthResults* results); |
| 263 | 239 |
| 264 // Fields for the obvious data items. | 240 // Fields for the obvious data items. |
| 265 const std::string user_agent_; | 241 const std::string user_agent_; |
| 266 const std::string service_id_; | 242 const std::string service_id_; |
| 267 const std::string gaia_url_; | 243 const std::string gaia_url_; |
| 268 | 244 |
| 269 AuthResults auth_results_; | 245 AuthResults auth_results_; |
| 270 | 246 |
| 271 // When multiple async requests are running, only the one that started most | 247 // When multiple async requests are running, only the one that started most |
| 272 // recently updates the values. | 248 // recently updates the values. |
| 273 // | 249 // |
| 274 // Note that even though this code was written to handle multiple requests | 250 // Note that even though this code was written to handle multiple requests |
| 275 // simultaneously, the sync code issues auth requests one at a time. | 251 // simultaneously, the sync code issues auth requests one at a time. |
| 276 uint32 request_count_; | 252 uint32 request_count_; |
| 277 | 253 |
| 278 Channel* channel_; | |
| 279 | |
| 280 // Used to compute backoff time for next allowed authentication. | 254 // Used to compute backoff time for next allowed authentication. |
| 281 int delay_; // In seconds. | 255 int delay_; // In seconds. |
| 282 // On Windows, time_t is 64-bit by default. Even though we have defined the | 256 // On Windows, time_t is 64-bit by default. Even though we have defined the |
| 283 // _USE_32BIT_TIME_T preprocessor flag, other libraries including this header | 257 // _USE_32BIT_TIME_T preprocessor flag, other libraries including this header |
| 284 // may not have that preprocessor flag defined resulting in mismatched class | 258 // may not have that preprocessor flag defined resulting in mismatched class |
| 285 // sizes. So we explicitly define it as 32-bit on Windows. | 259 // sizes. So we explicitly define it as 32-bit on Windows. |
| 286 // TODO(sanjeevr): Change this to to use base::Time | 260 // TODO(sanjeevr): Change this to to use base::Time |
| 287 #if defined(OS_WIN) | 261 #if defined(OS_WIN) |
| 288 __time32_t next_allowed_auth_attempt_time_; | 262 __time32_t next_allowed_auth_attempt_time_; |
| 289 #else // defined(OS_WIN) | 263 #else // defined(OS_WIN) |
| 290 time_t next_allowed_auth_attempt_time_; | 264 time_t next_allowed_auth_attempt_time_; |
| 291 #endif // defined(OS_WIN) | 265 #endif // defined(OS_WIN) |
| 292 int early_auth_attempt_count_; | 266 int early_auth_attempt_count_; |
| 293 | 267 |
| 294 // The message loop all our methods are invoked on. | 268 // The message loop all our methods are invoked on. |
| 295 const MessageLoop* message_loop_; | 269 const MessageLoop* message_loop_; |
| 296 }; | 270 }; |
| 297 | 271 |
| 298 } // namespace gaia | 272 } // namespace gaia |
| 299 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 273 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 300 | |
| OLD | NEW |