Chromium Code Reviews| 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 // The TokenService will supply authentication tokens for any service that | 5 // The TokenService will supply authentication tokens for any service that |
| 6 // needs it, such as sync. Whenever the user logs in, a controller watching | 6 // needs it, such as sync. Whenever the user logs in, a controller watching |
| 7 // the token service is expected to call ClientLogin to derive a new SID and | 7 // the token service is expected either to call ClientLogin to derive a new |
| 8 // LSID. Whenever such credentials are available, the TokenService should be | 8 // SID and LSID, or to use GAIA OAuth requests to derive an OAuth1 access |
| 9 // updated with new credentials. The controller should then start fetching | 9 // token for the OAuthLogin scope. Whenever such credentials are available, |
| 10 // tokens, which will be written to the database after retrieval, as well as | 10 // the TokenService should be updated with new credentials. The controller |
| 11 // provided to listeners. | 11 // should then start fetching tokens, which will be written to the database |
| 12 // after retrieval, as well as provided to listeners. | |
| 12 // | 13 // |
| 13 // A token service controller like the ChromiumOS login is expected to: | 14 // A token service controller like the ChromiumOS login is expected to: |
| 14 // | 15 // |
| 15 // Initialize() // Soon as you can | 16 // Initialize() // Soon as you can |
| 16 // LoadTokensFromDB() // When it's OK to talk to the database | 17 // LoadTokensFromDB() // When it's OK to talk to the database |
| 17 // UpdateCredentials() // When user logs in | 18 // UpdateCredentials() // When user logs in |
| 18 // StartFetchingTokens() // When it's safe to start fetching | 19 // StartFetchingTokens() // When it's safe to start fetching |
| 19 // | 20 // |
| 20 // Typically a user of the TokenService is expected just to call: | 21 // Typically a user of the TokenService is expected just to call: |
| 21 // | 22 // |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 33 | 34 |
| 34 #ifndef CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 35 #ifndef CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
| 35 #define CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 36 #define CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
| 36 #pragma once | 37 #pragma once |
| 37 | 38 |
| 38 #include <map> | 39 #include <map> |
| 39 #include <string> | 40 #include <string> |
| 40 | 41 |
| 41 #include "base/gtest_prod_util.h" | 42 #include "base/gtest_prod_util.h" |
| 42 #include "base/memory/scoped_ptr.h" | 43 #include "base/memory/scoped_ptr.h" |
| 44 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h" | |
| 45 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" | |
| 43 #include "chrome/browser/webdata/web_data_service.h" | 46 #include "chrome/browser/webdata/web_data_service.h" |
| 44 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 47 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
| 45 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" | 48 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
| 46 #include "chrome/common/net/gaia/google_service_auth_error.h" | 49 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 47 #include "content/common/notification_observer.h" | 50 #include "content/common/notification_observer.h" |
| 48 #include "content/common/notification_registrar.h" | 51 #include "content/common/notification_registrar.h" |
| 49 | 52 |
| 50 class Profile; | 53 class Profile; |
| 51 | 54 |
| 52 namespace net { | 55 namespace net { |
| 53 class URLRequestContextGetter; | 56 class URLRequestContextGetter; |
| 54 } | 57 } |
| 55 | 58 |
| 56 // The TokenService is a Profile member, so all calls are expected | 59 // The TokenService is a Profile member, so all calls are expected |
| 57 // from the UI thread. | 60 // from the UI thread. |
| 58 class TokenService : public GaiaAuthConsumer, | 61 class TokenService : public GaiaAuthConsumer, |
| 62 public GaiaOAuthConsumer, | |
| 59 public WebDataServiceConsumer, | 63 public WebDataServiceConsumer, |
| 60 public NotificationObserver { | 64 public NotificationObserver { |
| 61 public: | 65 public: |
| 62 TokenService(); | 66 TokenService(); |
| 63 virtual ~TokenService(); | 67 virtual ~TokenService(); |
| 64 | 68 |
| 65 // Notification classes | 69 // Notification classes |
| 66 class TokenAvailableDetails { | 70 class TokenAvailableDetails { |
| 67 public: | 71 public: |
| 68 TokenAvailableDetails() {} | 72 TokenAvailableDetails() {} |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 91 }; | 95 }; |
| 92 | 96 |
| 93 // Initialize this token service with a request source | 97 // Initialize this token service with a request source |
| 94 // (usually from a GaiaAuthConsumer constant), and the profile. | 98 // (usually from a GaiaAuthConsumer constant), and the profile. |
| 95 // Typically you'd then update the credentials. | 99 // Typically you'd then update the credentials. |
| 96 void Initialize(const char* const source, Profile* profile); | 100 void Initialize(const char* const source, Profile* profile); |
| 97 | 101 |
| 98 // Used to determine whether Initialize() has been called. | 102 // Used to determine whether Initialize() has been called. |
| 99 bool Initialized() const { return !source_.empty(); } | 103 bool Initialized() const { return !source_.empty(); } |
| 100 | 104 |
| 101 // Update the credentials in the token service. | 105 // Update ClientLogin credentials in the token service. |
| 102 // Afterwards you can StartFetchingTokens. | 106 // Afterwards you can StartFetchingTokens. |
| 103 void UpdateCredentials( | 107 void UpdateCredentials( |
| 104 const GaiaAuthConsumer::ClientLoginResult& credentials); | 108 const GaiaAuthConsumer::ClientLoginResult& credentials); |
| 105 | 109 |
| 110 // Update OAuth credentials in the token service. | |
| 111 // Afterwards you can StartFetchingOAuthTokens. | |
| 112 void UpdateOAuthCredentials( | |
| 113 const std::string& oauth_token, | |
| 114 const std::string& oauth_secret); | |
| 115 | |
| 106 // Terminate any running requests and reset the TokenService to a clean | 116 // Terminate any running requests and reset the TokenService to a clean |
| 107 // slate. Resets in memory structures. Does not modify the DB. | 117 // slate. Resets in memory structures. Does not modify the DB. |
| 108 // When this is done, no tokens will be left in memory and no | 118 // When this is done, no tokens will be left in memory and no |
| 109 // user credentials will be left. Useful if a user is logging out. | 119 // user credentials will be left. Useful if a user is logging out. |
| 110 // Initialize doesn't need to be called again but UpdateCredentials does. | 120 // Initialize doesn't need to be called again but UpdateCredentials and |
| 121 // UpdateOAuthCredentials do. | |
| 111 void ResetCredentialsInMemory(); | 122 void ResetCredentialsInMemory(); |
| 112 | 123 |
| 113 // Async load all tokens for services we know of from the DB. | 124 // Async load all tokens for services we know of from the DB. |
| 114 // You should do this at startup. Optionally you can do it again | 125 // You should do this at startup. Optionally you can do it again |
| 115 // after you reset in memory credentials. | 126 // after you reset in memory credentials. |
| 116 void LoadTokensFromDB(); | 127 void LoadTokensFromDB(); |
| 117 | 128 |
| 118 // Clear all DB stored tokens for the current profile. Tokens may still be | 129 // Clear all DB stored tokens for the current profile. Tokens may still be |
| 119 // available in memory. If a DB load is pending it may still be serviced. | 130 // available in memory. If a DB load is pending it may still be serviced. |
| 120 void EraseTokensFromDB(); | 131 void EraseTokensFromDB(); |
| 121 | 132 |
| 122 // For legacy services with their own auth routines, they can just read | 133 // For legacy services with their own auth routines, they can just read |
| 123 // the LSID out directly. Deprecated. | 134 // the LSID out directly. Deprecated. |
| 124 bool HasLsid() const; | 135 bool HasLsid() const; |
| 125 const std::string& GetLsid() const; | 136 const std::string& GetLsid() const; |
| 126 // Did we get a proper LSID? | 137 // Did we get a proper LSID? |
| 127 bool AreCredentialsValid() const; | 138 bool AreCredentialsValid() const; |
| 139 // Do we have an OAuth access token and secret. | |
| 140 bool AreOAuthCredentialsValid() const; | |
| 128 | 141 |
| 129 // Tokens will be fetched for all services(sync, talk) in the background. | 142 // Tokens will be fetched for all services(sync, talk) in the background. |
| 130 // Results come back via event channel. Services can also poll before events | 143 // Results come back via event channel. Services can also poll before events |
| 131 // are issued. | 144 // are issued. |
| 132 void StartFetchingTokens(); | 145 void StartFetchingTokens(); |
| 146 void StartFetchingOAuthTokens(); | |
| 133 bool HasTokenForService(const char* const service) const; | 147 bool HasTokenForService(const char* const service) const; |
| 134 const std::string& GetTokenForService(const char* const service) const; | 148 const std::string& GetTokenForService(const char* const service) const; |
| 135 | 149 |
| 136 // For tests only. Doesn't save to the WebDB. | 150 // For tests only. Doesn't save to the WebDB. |
| 137 void IssueAuthTokenForTest(const std::string& service, | 151 void IssueAuthTokenForTest(const std::string& service, |
| 138 const std::string& auth_token); | 152 const std::string& auth_token); |
| 139 | 153 |
| 140 // GaiaAuthConsumer implementation. | 154 // GaiaAuthConsumer implementation. |
| 141 virtual void OnIssueAuthTokenSuccess(const std::string& service, | 155 virtual void OnIssueAuthTokenSuccess(const std::string& service, |
| 142 const std::string& auth_token); | 156 const std::string& auth_token) OVERRIDE; |
| 143 virtual void OnIssueAuthTokenFailure(const std::string& service, | 157 virtual void OnIssueAuthTokenFailure(const std::string& service, |
| 144 const GoogleServiceAuthError& error); | 158 const GoogleServiceAuthError& error) |
| 159 OVERRIDE; | |
| 160 | |
| 161 // GaiaOAuthConsumer implementation. | |
| 162 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, | |
| 163 const std::string& secret) OVERRIDE; | |
| 164 virtual void OnOAuthGetAccessTokenFailure( | |
| 165 const GoogleServiceAuthError& error) OVERRIDE; | |
| 166 | |
| 167 virtual void OnOAuthWrapBridgeSuccess(const std::string& service_scope, | |
| 168 const std::string& token, | |
| 169 const std::string& expires_in) OVERRIDE; | |
| 170 virtual void OnOAuthWrapBridgeFailure(const std::string& service_name, | |
| 171 const GoogleServiceAuthError& error) | |
| 172 OVERRIDE; | |
| 145 | 173 |
| 146 // WebDataServiceConsumer implementation. | 174 // WebDataServiceConsumer implementation. |
| 147 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, | 175 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, |
| 148 const WDTypedResult* result); | 176 const WDTypedResult* result); |
| 149 | 177 |
| 150 // NotificationObserver implementation. | 178 // NotificationObserver implementation. |
| 151 virtual void Observe(int type, | 179 virtual void Observe(int type, |
| 152 const NotificationSource& source, | 180 const NotificationSource& source, |
| 153 const NotificationDetails& details); | 181 const NotificationDetails& details); |
| 154 | 182 |
| 155 private: | 183 private: |
| 156 | 184 |
| 157 void FireTokenAvailableNotification(const std::string& service, | 185 void FireTokenAvailableNotification(const std::string& service, |
| 158 const std::string& auth_token); | 186 const std::string& auth_token); |
| 159 | 187 |
| 160 void FireTokenRequestFailedNotification(const std::string& service, | 188 void FireTokenRequestFailedNotification(const std::string& service, |
| 161 const GoogleServiceAuthError& error); | 189 const GoogleServiceAuthError& error); |
| 162 | 190 |
| 163 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, | 191 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, |
| 164 std::map<std::string, std::string>* out_toks); | 192 std::map<std::string, std::string>* out_toks); |
| 165 | 193 |
| 166 void SaveAuthTokenToDB(const std::string& service, | 194 void SaveAuthTokenToDB(const std::string& service, |
| 167 const std::string& auth_token); | 195 const std::string& auth_token); |
| 168 | 196 |
| 197 // The profile with which this instance was initialized, or NULL. | |
| 198 Profile* profile_; | |
|
Mattias Nissler (ping if slow)
2011/08/04 13:39:57
I kind of dislike this, since Profile is a super-h
Rick Campbell
2011/08/04 17:24:42
My current thinking is to leave things as they are
| |
| 199 | |
| 169 // Web data service to access tokens from. | 200 // Web data service to access tokens from. |
| 170 scoped_refptr<WebDataService> web_data_service_; | 201 scoped_refptr<WebDataService> web_data_service_; |
| 171 // Getter to use for fetchers. | 202 // Getter to use for fetchers. |
| 172 scoped_refptr<net::URLRequestContextGetter> getter_; | 203 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 173 // Request handle to load Gaia tokens from DB. | 204 // Request handle to load Gaia tokens from DB. |
| 174 WebDataService::Handle token_loading_query_; | 205 WebDataService::Handle token_loading_query_; |
| 175 | 206 |
| 176 // Gaia request source for Gaia accounting. | 207 // Gaia request source for Gaia accounting. |
| 177 std::string source_; | 208 std::string source_; |
| 178 // Credentials from ClientLogin for Issuing auth tokens. | 209 // Credentials from ClientLogin for Issuing auth tokens. |
| 179 GaiaAuthConsumer::ClientLoginResult credentials_; | 210 GaiaAuthConsumer::ClientLoginResult credentials_; |
| 211 // Credentials from Gaia OAuth (uber/login token) | |
| 212 std::string oauth_token_; | |
| 213 std::string oauth_secret_; | |
| 180 | 214 |
| 181 // Size of array of services (must be defined here). | 215 // Size of array of services capable of ClientLogin-based authentication. |
| 216 // This value must be defined here. | |
| 217 // NOTE: The use of --enable-sync-oauth does not affect this count. The | |
| 218 // TokenService can continue to do some degree of ClientLogin token | |
| 219 // management, mostly related to persistence while Sync and possibly other | |
| 220 // services are using OAuth-based authentication. | |
| 182 static const int kNumServices = 4; | 221 static const int kNumServices = 4; |
| 222 // List of services that we're performing operations for which are capable | |
|
Roger Tawa OOO till Jul 10th
2011/08/04 14:02:14
remove "for" ?
Rick Campbell
2011/08/04 17:24:42
Thanks. I went for "List of services that are cap
| |
| 223 // of ClientLogin-based authentication. | |
| 224 static const char* kServices[kNumServices]; | |
| 225 // A bunch of fetchers suitable for ClientLogin token issuing. We don't care | |
| 226 // about the ordering, nor do we care which is for which service. | |
| 227 scoped_ptr<GaiaAuthFetcher> fetchers_[kNumServices]; | |
| 228 | |
| 229 // Size of array of services capable of OAuth-based authentication. This | |
| 230 // value must be defined here. | |
| 231 // NOTE: The use of --enable-sync-oauth does not affect this count. The | |
| 232 // TokenService can continue to do some degree of OAuth token | |
| 233 // management, mostly related to persistence while Sync and possibly other | |
| 234 // services are using ClientLogin-based authentication. | |
| 235 static const int kNumOAuthServices = 1; | |
| 183 // List of services that we're performing operations for. | 236 // List of services that we're performing operations for. |
| 184 static const char* kServices[kNumServices]; | 237 static const char* kOAuthServices[kNumOAuthServices]; |
| 185 // A bunch of fetchers suitable for token issuing. We don't care about | 238 // A bunch of fetchers suitable for OAuth token issuing. We don't care about |
| 186 // the ordering, nor do we care which is for which service. | 239 // the ordering, nor do we care which is for which service. |
| 187 scoped_ptr<GaiaAuthFetcher> fetchers_[kNumServices]; | 240 scoped_ptr<GaiaOAuthFetcher> oauth_fetchers_[kNumOAuthServices]; |
| 241 | |
| 188 // Map from service to token. | 242 // Map from service to token. |
| 189 std::map<std::string, std::string> token_map_; | 243 std::map<std::string, std::string> token_map_; |
| 190 | 244 |
| 191 NotificationRegistrar registrar_; | 245 NotificationRegistrar registrar_; |
| 192 | 246 |
| 193 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); | 247 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); |
| 194 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); | 248 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); |
| 195 | 249 |
| 196 DISALLOW_COPY_AND_ASSIGN(TokenService); | 250 DISALLOW_COPY_AND_ASSIGN(TokenService); |
| 197 }; | 251 }; |
| 198 | 252 |
| 199 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 253 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
| OLD | NEW |