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 either to call ClientLogin to derive a new | 7 // the token service is expected either to call ClientLogin to derive a new |
8 // SID and LSID, or to use GAIA OAuth requests to derive an OAuth1 access | 8 // SID and LSID, or to use GAIA OAuth requests to derive an OAuth1 access |
9 // token for the OAuthLogin scope. Whenever such credentials are available, | 9 // token for the OAuthLogin scope. Whenever such credentials are available, |
10 // the TokenService should be updated with new credentials. The controller | 10 // the TokenService should be updated with new credentials. The controller |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h" | 44 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h" |
45 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" | 45 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" |
46 #include "chrome/browser/webdata/web_data_service.h" | 46 #include "chrome/browser/webdata/web_data_service.h" |
47 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 47 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
48 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" | 48 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
49 #include "chrome/common/net/gaia/google_service_auth_error.h" | 49 #include "chrome/common/net/gaia/google_service_auth_error.h" |
50 #include "content/public/browser/notification_observer.h" | 50 #include "content/public/browser/notification_observer.h" |
51 #include "content/public/browser/notification_registrar.h" | 51 #include "content/public/browser/notification_registrar.h" |
52 | 52 |
53 class Profile; | 53 class Profile; |
| 54 class TokenServiceTest; |
54 | 55 |
55 namespace net { | 56 namespace net { |
56 class URLRequestContextGetter; | 57 class URLRequestContextGetter; |
57 } | 58 } |
58 | 59 |
59 // The TokenService is a Profile member, so all calls are expected | 60 // The TokenService is a Profile member, so all calls are expected |
60 // from the UI thread. | 61 // from the UI thread. |
61 class TokenService : public GaiaAuthConsumer, | 62 class TokenService : public GaiaAuthConsumer, |
62 public GaiaOAuthConsumer, | 63 public GaiaOAuthConsumer, |
63 public WebDataServiceConsumer, | 64 public WebDataServiceConsumer, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // are issued. | 149 // are issued. |
149 void StartFetchingTokens(); | 150 void StartFetchingTokens(); |
150 // Fetch tokens for only those services for which we are missing tokens. | 151 // Fetch tokens for only those services for which we are missing tokens. |
151 // This can happen when new services are added in new Chrome versions and the | 152 // This can happen when new services are added in new Chrome versions and the |
152 // user is already logged in. | 153 // user is already logged in. |
153 void StartFetchingMissingTokens(); | 154 void StartFetchingMissingTokens(); |
154 void StartFetchingOAuthTokens(); | 155 void StartFetchingOAuthTokens(); |
155 virtual bool HasTokenForService(const char* service) const; | 156 virtual bool HasTokenForService(const char* service) const; |
156 const std::string& GetTokenForService(const char* const service) const; | 157 const std::string& GetTokenForService(const char* const service) const; |
157 | 158 |
| 159 // OAuth login token is an all-powerful token that allows creating OAuth2 |
| 160 // tokens for any other scope (i.e. down-scoping). |
| 161 // Typical use is to create an OAuth2 token for appropriate scope and then |
| 162 // use that token to call a Google API. |
| 163 virtual bool HasOAuthLoginToken() const; |
| 164 const std::string& GetOAuth2LoginRefreshToken() const; |
| 165 const std::string& GetOAuth2LoginAccessToken() const; |
| 166 |
158 // For tests only. Doesn't save to the WebDB. | 167 // For tests only. Doesn't save to the WebDB. |
159 void IssueAuthTokenForTest(const std::string& service, | 168 void IssueAuthTokenForTest(const std::string& service, |
160 const std::string& auth_token); | 169 const std::string& auth_token); |
161 | 170 |
162 // GaiaAuthConsumer implementation. | 171 // GaiaAuthConsumer implementation. |
163 virtual void OnIssueAuthTokenSuccess(const std::string& service, | 172 virtual void OnIssueAuthTokenSuccess(const std::string& service, |
164 const std::string& auth_token) OVERRIDE; | 173 const std::string& auth_token) OVERRIDE; |
165 virtual void OnIssueAuthTokenFailure( | 174 virtual void OnIssueAuthTokenFailure( |
166 const std::string& service, | 175 const std::string& service, |
167 const GoogleServiceAuthError& error) OVERRIDE; | 176 const GoogleServiceAuthError& error) OVERRIDE; |
| 177 virtual void OnOAuthLoginTokenSuccess(const std::string& refresh_token, |
| 178 const std::string& access_token, |
| 179 int expires_in_secs) OVERRIDE; |
| 180 virtual void OnOAuthLoginTokenFailure(const GoogleServiceAuthError& error) |
| 181 OVERRIDE; |
168 | 182 |
169 // GaiaOAuthConsumer implementation. | 183 // GaiaOAuthConsumer implementation. |
170 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, | 184 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, |
171 const std::string& secret) OVERRIDE; | 185 const std::string& secret) OVERRIDE; |
172 virtual void OnOAuthGetAccessTokenFailure( | 186 virtual void OnOAuthGetAccessTokenFailure( |
173 const GoogleServiceAuthError& error) OVERRIDE; | 187 const GoogleServiceAuthError& error) OVERRIDE; |
174 | 188 |
175 virtual void OnOAuthWrapBridgeSuccess(const std::string& service_scope, | 189 virtual void OnOAuthWrapBridgeSuccess(const std::string& service_scope, |
176 const std::string& token, | 190 const std::string& token, |
177 const std::string& expires_in) OVERRIDE; | 191 const std::string& expires_in) OVERRIDE; |
(...skipping 12 matching lines...) Expand all Loading... |
190 const content::NotificationDetails& details) OVERRIDE; | 204 const content::NotificationDetails& details) OVERRIDE; |
191 | 205 |
192 private: | 206 private: |
193 | 207 |
194 void FireTokenAvailableNotification(const std::string& service, | 208 void FireTokenAvailableNotification(const std::string& service, |
195 const std::string& auth_token); | 209 const std::string& auth_token); |
196 | 210 |
197 void FireTokenRequestFailedNotification(const std::string& service, | 211 void FireTokenRequestFailedNotification(const std::string& service, |
198 const GoogleServiceAuthError& error); | 212 const GoogleServiceAuthError& error); |
199 | 213 |
200 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, | 214 void LoadTokensIntoMemory( |
201 std::map<std::string, std::string>* out_toks); | 215 const std::map<std::string, std::string>& db_tokens, |
| 216 std::map<std::string, std::string>* in_memory_tokens); |
| 217 void LoadSingleTokenIntoMemory( |
| 218 const std::map<std::string, std::string>& db_tokens, |
| 219 std::map<std::string, std::string>* in_memory_tokens, |
| 220 const std::string& service); |
202 | 221 |
203 void SaveAuthTokenToDB(const std::string& service, | 222 void SaveAuthTokenToDB(const std::string& service, |
204 const std::string& auth_token); | 223 const std::string& auth_token); |
205 | 224 |
| 225 // Returns the index of the given service. |
| 226 static int GetServiceIndex(const std::string& service); |
| 227 |
206 // The profile with which this instance was initialized, or NULL. | 228 // The profile with which this instance was initialized, or NULL. |
207 Profile* profile_; | 229 Profile* profile_; |
208 | 230 |
209 // Web data service to access tokens from. | 231 // Web data service to access tokens from. |
210 scoped_refptr<WebDataService> web_data_service_; | 232 scoped_refptr<WebDataService> web_data_service_; |
211 // Getter to use for fetchers. | 233 // Getter to use for fetchers. |
212 scoped_refptr<net::URLRequestContextGetter> getter_; | 234 scoped_refptr<net::URLRequestContextGetter> getter_; |
213 // Request handle to load Gaia tokens from DB. | 235 // Request handle to load Gaia tokens from DB. |
214 WebDataService::Handle token_loading_query_; | 236 WebDataService::Handle token_loading_query_; |
215 | 237 |
(...skipping 29 matching lines...) Expand all Loading... |
245 static const char* kOAuthServices[kNumOAuthServices]; | 267 static const char* kOAuthServices[kNumOAuthServices]; |
246 // A bunch of fetchers suitable for OAuth token issuing. We don't care about | 268 // A bunch of fetchers suitable for OAuth token issuing. We don't care about |
247 // the ordering, nor do we care which is for which service. | 269 // the ordering, nor do we care which is for which service. |
248 scoped_ptr<GaiaOAuthFetcher> oauth_fetchers_[kNumOAuthServices]; | 270 scoped_ptr<GaiaOAuthFetcher> oauth_fetchers_[kNumOAuthServices]; |
249 | 271 |
250 // Map from service to token. | 272 // Map from service to token. |
251 std::map<std::string, std::string> token_map_; | 273 std::map<std::string, std::string> token_map_; |
252 | 274 |
253 content::NotificationRegistrar registrar_; | 275 content::NotificationRegistrar registrar_; |
254 | 276 |
| 277 friend class TokenServiceTest; |
255 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); | 278 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); |
256 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); | 279 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); |
257 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, FullIntegrationNewServicesAdded); | 280 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, FullIntegrationNewServicesAdded); |
258 | 281 |
259 DISALLOW_COPY_AND_ASSIGN(TokenService); | 282 DISALLOW_COPY_AND_ASSIGN(TokenService); |
260 }; | 283 }; |
261 | 284 |
262 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 285 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
OLD | NEW |