| 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. 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 { |
| 11 // Provides actual implementation of Post and GetBackoffDelaySeconds. | 11 // Provides actual implementation of Post and GetBackoffDelaySeconds. |
| 12 // }; | 12 // }; |
| 13 // ActualGaiaAuthenticator gaia_auth("User-Agent", SERVICE_NAME, kGaiaUrl); | 13 // ActualGaiaAuthenticator gaia_auth("User-Agent", SERVICE_NAME, kGaiaUrl); |
| 14 // if (gaia_auth.Authenticate("email", "passwd", SAVE_IN_MEMORY_ONLY, | 14 // if (gaia_auth.Authenticate("email", "passwd", SAVE_IN_MEMORY_ONLY, |
| 15 // true)) { // Synchronous | 15 // true)) { // Synchronous |
| 16 // // Do something with: gaia_auth.auth_token(), or gaia_auth.sid(), | 16 // // Do something with: gaia_auth.auth_token(), or gaia_auth.sid(), |
| 17 // // or gaia_auth.lsid() | 17 // // or gaia_auth.lsid() |
| 18 // } | 18 // } |
| 19 // | 19 // |
| 20 // Credentials can also be preserved for subsequent requests, though these are | 20 // Credentials can also be preserved for subsequent requests, though these are |
| 21 // saved in plain-text in memory, and not very secure on client systems. The | 21 // saved in plain-text in memory, and not very secure on client systems. The |
| 22 // email address associated with the Gaia account can be read; the password is | 22 // email address associated with the Gaia account can be read; the password is |
| 23 // write-only. | 23 // write-only. |
| 24 | 24 |
| 25 // TODO(sanjeevr): This class has been moved here from the bookmarks sync code. | 25 // TODO(sanjeevr): This class has been moved here from the bookmarks sync code. |
| 26 // While it is a generic class that handles GAIA authentication, there are some | 26 // While it is a generic class that handles GAIA authentication, there are some |
| 27 // artifacts of the sync code such as the SaveCredentials enum which needs to | 27 // artifacts of the sync code which needs to be cleaned up. |
| 28 // be cleaned up. | |
| 29 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 28 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 30 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 29 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 31 | 30 |
| 32 #include <string> | 31 #include <string> |
| 33 | 32 |
| 34 #include "base/basictypes.h" | 33 #include "base/basictypes.h" |
| 35 #include "base/message_loop.h" | 34 #include "base/message_loop.h" |
| 36 #include "chrome/common/net/gaia/signin.h" | |
| 37 #include "chrome/common/deprecated/event_sys.h" | 35 #include "chrome/common/deprecated/event_sys.h" |
| 38 #include "googleurl/src/gurl.h" | 36 #include "googleurl/src/gurl.h" |
| 39 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 37 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 40 | 38 |
| 41 namespace gaia { | 39 namespace gaia { |
| 42 | 40 |
| 43 static const char kGaiaUrl[] = | 41 static const char kGaiaUrl[] = |
| 44 "https://www.google.com:443/accounts/ClientLogin"; | 42 "https://www.google.com:443/accounts/ClientLogin"; |
| 45 | 43 |
| 46 // Use of the following enum is odd. GaiaAuthenticator only looks at | |
| 47 // and DONT_SAVE_CREDENTIALS and SAVE_IN_MEMORY_ONLY (PERSIST_TO_DISK is == to | |
| 48 // SAVE_IN_MEMORY_ONLY for GaiaAuthenticator). | |
| 49 | |
| 50 enum SaveCredentials { | |
| 51 DONT_SAVE_CREDENTIALS, | |
| 52 SAVE_IN_MEMORY_ONLY, | |
| 53 PERSIST_TO_DISK // Saved in both memory and disk | |
| 54 }; | |
| 55 | |
| 56 // Error codes from Gaia. These will be set correctly for both Gaia V1 | 44 // Error codes from Gaia. These will be set correctly for both Gaia V1 |
| 57 // (/ClientAuth) and V2 (/ClientLogin) | 45 // (/ClientAuth) and V2 (/ClientLogin) |
| 58 enum AuthenticationError { | 46 enum AuthenticationError { |
| 59 None = 0, | 47 None = 0, |
| 60 BadAuthentication = 1, | 48 BadAuthentication = 1, |
| 61 NotVerified = 2, | 49 NotVerified = 2, |
| 62 TermsNotAgreed = 3, | 50 TermsNotAgreed = 3, |
| 63 Unknown = 4, | 51 Unknown = 4, |
| 64 AccountDeleted = 5, | 52 AccountDeleted = 5, |
| 65 AccountDisabled = 6, | 53 AccountDisabled = 6, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // loop, which is injected here. | 98 // loop, which is injected here. |
| 111 void set_message_loop(const MessageLoop* loop) { | 99 void set_message_loop(const MessageLoop* loop) { |
| 112 message_loop_ = loop; | 100 message_loop_ = loop; |
| 113 } | 101 } |
| 114 | 102 |
| 115 // Pass credentials to authenticate with, or use saved credentials via an | 103 // Pass credentials to authenticate with, or use saved credentials via an |
| 116 // overload. If authentication succeeds, you can retrieve the authentication | 104 // overload. If authentication succeeds, you can retrieve the authentication |
| 117 // token via the respective accessors. Returns a boolean indicating whether | 105 // token via the respective accessors. Returns a boolean indicating whether |
| 118 // authentication succeeded or not. | 106 // authentication succeeded or not. |
| 119 bool Authenticate(const std::string& user_name, const std::string& password, | 107 bool Authenticate(const std::string& user_name, const std::string& password, |
| 120 SaveCredentials should_save_credentials, | |
| 121 const std::string& captcha_token, | 108 const std::string& captcha_token, |
| 122 const std::string& captcha_value, | 109 const std::string& captcha_value); |
| 123 SignIn try_first); | |
| 124 | 110 |
| 125 bool Authenticate(const std::string& user_name, const std::string& password, | 111 bool Authenticate(const std::string& user_name, const std::string& password); |
| 126 SaveCredentials should_save_credentials, | |
| 127 SignIn try_first); | |
| 128 | 112 |
| 129 // Pass the LSID to authenticate with. If the authentication succeeds, you can | 113 // Pass the LSID to authenticate with. If the authentication succeeds, you can |
| 130 // retrieve the authetication token via the respective accessors. Returns a | 114 // retrieve the authetication token via the respective accessors. Returns a |
| 131 // boolean indicating whether authentication succeeded or not. | 115 // boolean indicating whether authentication succeeded or not. |
| 132 bool AuthenticateWithLsid(const std::string& lsid, bool long_lived_token); | 116 // Always returns a long lived token. |
| 117 bool AuthenticateWithLsid(const std::string& lsid); |
| 133 | 118 |
| 134 // Resets all stored cookies to their default values. | 119 // Resets all stored cookies to their default values. |
| 135 void ResetCredentials(); | 120 void ResetCredentials(); |
| 136 | 121 |
| 137 void SetUsernamePassword(const std::string& username, | 122 void SetUsernamePassword(const std::string& username, |
| 138 const std::string& password); | 123 const std::string& password); |
| 139 | 124 |
| 140 void SetUsername(const std::string& username); | 125 void SetUsername(const std::string& username); |
| 141 | 126 |
| 142 // Virtual for testing | 127 // Virtual for testing |
| 143 virtual void RenewAuthToken(const std::string& auth_token); | 128 virtual void RenewAuthToken(const std::string& auth_token); |
| 144 void SetAuthToken(const std::string& auth_token, SaveCredentials); | 129 void SetAuthToken(const std::string& auth_token); |
| 145 | 130 |
| 146 struct AuthResults { | 131 struct AuthResults { |
| 147 SaveCredentials credentials_saved; | |
| 148 std::string email; | 132 std::string email; |
| 149 std::string password; | 133 std::string password; |
| 150 | 134 |
| 151 // Fields that store various cookies. | 135 // Fields that store various cookies. |
| 152 std::string sid; | 136 std::string sid; |
| 153 std::string lsid; | 137 std::string lsid; |
| 154 std::string auth_token; | 138 std::string auth_token; |
| 155 | 139 |
| 156 std::string primary_email; | 140 std::string primary_email; |
| 157 | 141 |
| 158 // Fields for items returned when authentication fails. | 142 // Fields for items returned when authentication fails. |
| 159 std::string error_msg; | 143 std::string error_msg; |
| 160 enum AuthenticationError auth_error; | 144 enum AuthenticationError auth_error; |
| 161 std::string auth_error_url; | 145 std::string auth_error_url; |
| 162 std::string captcha_token; | 146 std::string captcha_token; |
| 163 std::string captcha_url; | 147 std::string captcha_url; |
| 164 SignIn signin; | |
| 165 | 148 |
| 166 // TODO(skrul): When auth fails, the "signin" field of the results | 149 AuthResults() : auth_error(None) {} |
| 167 // struct never gets set, which causes valgrind to complain. Give | |
| 168 // this field a value here so the error is suppressed. It turns | |
| 169 // out that the signin field has only one possible value, so the | |
| 170 // correct fix here would be to to remove it entirely. | |
| 171 AuthResults() : credentials_saved(DONT_SAVE_CREDENTIALS), | |
| 172 auth_error(None), | |
| 173 signin(GMAIL_SIGNIN) { } | |
| 174 }; | 150 }; |
| 175 | 151 |
| 176 protected: | 152 protected: |
| 177 | 153 |
| 178 struct AuthParams { | 154 struct AuthParams { |
| 179 GaiaAuthenticator* authenticator; | 155 GaiaAuthenticator* authenticator; |
| 180 uint32 request_id; | 156 uint32 request_id; |
| 181 SaveCredentials should_save_credentials; | |
| 182 std::string email; | 157 std::string email; |
| 183 std::string password; | 158 std::string password; |
| 184 std::string captcha_token; | 159 std::string captcha_token; |
| 185 std::string captcha_value; | 160 std::string captcha_value; |
| 186 SignIn try_first; | |
| 187 }; | 161 }; |
| 188 | 162 |
| 189 // mutex_ must be entered before calling this function. | 163 // mutex_ must be entered before calling this function. |
| 190 AuthParams MakeParams(const std::string& user_name, | 164 AuthParams MakeParams(const std::string& user_name, |
| 191 const std::string& password, | 165 const std::string& password, |
| 192 SaveCredentials should_save_credentials, | |
| 193 const std::string& captcha_token, | 166 const std::string& captcha_token, |
| 194 const std::string& captcha_value, | 167 const std::string& captcha_value); |
| 195 SignIn try_first); | |
| 196 | 168 |
| 197 // The real Authenticate implementations. | 169 // The real Authenticate implementations. |
| 198 bool AuthenticateImpl(const AuthParams& params); | 170 bool AuthenticateImpl(const AuthParams& params); |
| 199 bool AuthenticateImpl(const AuthParams& params, AuthResults* results); | 171 bool AuthenticateImpl(const AuthParams& params, AuthResults* results); |
| 200 | 172 |
| 201 // virtual for testing purposes. | 173 // virtual for testing purposes. |
| 202 virtual bool PerformGaiaRequest(const AuthParams& params, | 174 virtual bool PerformGaiaRequest(const AuthParams& params, |
| 203 AuthResults* results); | 175 AuthResults* results); |
| 204 virtual bool Post(const GURL& url, const std::string& post_body, | 176 virtual bool Post(const GURL& url, const std::string& post_body, |
| 205 unsigned long* response_code, std::string* response_body) { | 177 unsigned long* response_code, std::string* response_body) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 return auth_results_; | 249 return auth_results_; |
| 278 } | 250 } |
| 279 | 251 |
| 280 typedef EventChannel<GaiaAuthEvent, Lock> Channel; | 252 typedef EventChannel<GaiaAuthEvent, Lock> Channel; |
| 281 | 253 |
| 282 inline Channel* channel() const { | 254 inline Channel* channel() const { |
| 283 return channel_; | 255 return channel_; |
| 284 } | 256 } |
| 285 | 257 |
| 286 private: | 258 private: |
| 287 bool IssueAuthToken(AuthResults* results, const std::string& service_id, | 259 bool IssueAuthToken(AuthResults* results, const std::string& service_id); |
| 288 bool long_lived_token); | |
| 289 | 260 |
| 290 // Helper method to parse response when authentication succeeds. | 261 // Helper method to parse response when authentication succeeds. |
| 291 void ExtractTokensFrom(const std::string& response, AuthResults* results); | 262 void ExtractTokensFrom(const std::string& response, AuthResults* results); |
| 292 // Helper method to parse response when authentication fails. | 263 // Helper method to parse response when authentication fails. |
| 293 void ExtractAuthErrorFrom(const std::string& response, AuthResults* results); | 264 void ExtractAuthErrorFrom(const std::string& response, AuthResults* results); |
| 294 | 265 |
| 295 // Fields for the obvious data items. | 266 // Fields for the obvious data items. |
| 296 const std::string user_agent_; | 267 const std::string user_agent_; |
| 297 const std::string service_id_; | 268 const std::string service_id_; |
| 298 const std::string gaia_url_; | 269 const std::string gaia_url_; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 322 #endif // defined(OS_WIN) | 293 #endif // defined(OS_WIN) |
| 323 int early_auth_attempt_count_; | 294 int early_auth_attempt_count_; |
| 324 | 295 |
| 325 // The message loop all our methods are invoked on. | 296 // The message loop all our methods are invoked on. |
| 326 const MessageLoop* message_loop_; | 297 const MessageLoop* message_loop_; |
| 327 }; | 298 }; |
| 328 | 299 |
| 329 } // namespace gaia | 300 } // namespace gaia |
| 330 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ | 301 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR_H_ |
| 331 | 302 |
| OLD | NEW |