OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // The AuthenticationService will supply authentication tokens for any service |
| 6 // that needs it, such as sync. Whenever the user logs in, a controller |
| 7 // watching the token service is expected to call OAuth to derive a new SID |
| 8 // and LSID. Whenever such credentials are available, the |
| 9 // AuthenticationService should be updated with new credentials. The |
| 10 // controller should then start fetching tokens, which will be written to the |
| 11 // database after retrieval, as well as provided to listeners. |
| 12 |
| 13 // |
| 14 // A token service controller like the ChromiumOS login is expected to: |
| 15 // |
| 16 // Initialize() // Soon as you can |
| 17 // LoadTokensFromDB() // When it's OK to talk to the database |
| 18 // UpdateCredentials() // When user logs in |
| 19 // StartFetchingTokens() // When it's safe to start fetching |
| 20 // |
| 21 // Typically a user of the AuthenticationService is expected just to call: |
| 22 // |
| 23 // if (token_service.HasTokenForService(servicename)) { |
| 24 // SetMyToken(token_service.GetTokenForService(servicename)); |
| 25 // } |
| 26 // RegisterSomeObserver(token_service); |
| 27 // |
| 28 // Whenever a token update occurs: |
| 29 // OnTokenAvailable(...) { |
| 30 // if (IsServiceICareAbout(notification.service())) { |
| 31 // SetMyToken(notification.token()) |
| 32 // } |
| 33 // } |
| 34 |
| 35 #ifndef CHROME_BROWSER_NET_GAIA_AUTHENTICATION_SERVICE_H_ |
| 36 #define CHROME_BROWSER_NET_GAIA_AUTHENTICATION_SERVICE_H_ |
| 37 #pragma once |
| 38 |
| 39 #include <map> |
| 40 #include <string> |
| 41 |
| 42 #include "base/gtest_prod_util.h" |
| 43 #include "base/memory/scoped_ptr.h" |
| 44 #include "chrome/browser/webdata/web_data_service.h" |
| 45 #include "chrome/common/net/gaia/authentication_consumer.h" |
| 46 #include "chrome/common/net/gaia/authentication_fetcher.h" |
| 47 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 48 #include "content/common/notification_observer.h" |
| 49 #include "content/common/notification_registrar.h" |
| 50 |
| 51 class Profile; |
| 52 |
| 53 namespace net { |
| 54 class URLRequestContextGetter; |
| 55 } |
| 56 |
| 57 // The AuthenticationService is a Profile member, so all calls are expected |
| 58 // from the UI thread. |
| 59 class AuthenticationService : public AuthenticationConsumer, |
| 60 public WebDataServiceConsumer, |
| 61 public NotificationObserver { |
| 62 public: |
| 63 static const char kClientLoginVariant[]; |
| 64 static const char kOAuthVariant[]; |
| 65 |
| 66 AuthenticationService(); |
| 67 virtual ~AuthenticationService(); |
| 68 |
| 69 // Notification classes |
| 70 class TokenAvailableDetails { |
| 71 public: |
| 72 TokenAvailableDetails() {} |
| 73 TokenAvailableDetails(const std::string& service, |
| 74 const std::string& token) |
| 75 : service_(service), token_(token) {} |
| 76 const std::string& service() const { return service_; } |
| 77 const std::string& token() const { return token_; } |
| 78 private: |
| 79 std::string service_; |
| 80 std::string token_; |
| 81 }; |
| 82 |
| 83 class TokenRequestFailedDetails { |
| 84 public: |
| 85 TokenRequestFailedDetails() |
| 86 : error_(GoogleServiceAuthError::NONE) {} |
| 87 TokenRequestFailedDetails(const std::string& service, |
| 88 const GoogleServiceAuthError& error) |
| 89 : service_(service), error_(error) {} |
| 90 const std::string& service() const { return service_; } |
| 91 const GoogleServiceAuthError& error() const { return error_; } |
| 92 private: |
| 93 std::string service_; |
| 94 GoogleServiceAuthError error_; |
| 95 }; |
| 96 |
| 97 // Initialize this token service with a request source |
| 98 // (usually from a AuthenticationConsumer constant), and the profile. |
| 99 // Typically you'd then update the credentials. |
| 100 void Initialize(const char* const source, Profile* profile); |
| 101 |
| 102 // Update the credentials in the token service. |
| 103 // Afterwards you can StartFetchingTokens. |
| 104 void UpdateCredentials( |
| 105 AuthenticationConsumer::AuthenticationResult* credentials); |
| 106 |
| 107 // Terminate any running requests and reset the AuthenticationService to a |
| 108 // clean slate. Resets in memory structures. Does not modify the DB. |
| 109 // When this is done, no tokens will be left in memory and no |
| 110 // user credentials will be left. Useful if a user is logging out. |
| 111 // Initialize doesn't need to be called again but UpdateCredentials does. |
| 112 void ResetCredentialsInMemory(); |
| 113 |
| 114 // Async load all tokens for services we know of from the DB. |
| 115 // You should do this at startup. Optionally you can do it again |
| 116 // after you reset in memory credentials. |
| 117 void LoadTokensFromDB(); |
| 118 |
| 119 // Clear all DB stored tokens for the current profile. Tokens may still be |
| 120 // available in memory. If a DB load is pending it may still be serviced. |
| 121 void EraseTokensFromDB(); |
| 122 |
| 123 // For legacy services with their own auth routines, they can just read |
| 124 // the LSID out directly. Deprecated. |
| 125 bool HasLsid() const; |
| 126 const std::string& GetLsid() const; |
| 127 // Did we get a proper LSID? |
| 128 bool AreCredentialsValid() const; |
| 129 |
| 130 // Tokens will be fetched for all services(sync, talk) in the background. |
| 131 // Results come back via event channel. Services can also poll before events |
| 132 // are issued. |
| 133 void StartFetchingTokens(); |
| 134 bool HasTokenForService(const char* const service) const; |
| 135 const std::string& GetTokenForService(const char* const service) const; |
| 136 |
| 137 // For tests only. Doesn't save to the WebDB. |
| 138 void IssueAuthTokenForTest(const std::string& service, |
| 139 const std::string& auth_token); |
| 140 |
| 141 // AuthenticationConsumer implementation. |
| 142 virtual void OnIssueAuthTokenSuccess(const std::string& service, |
| 143 const std::string& auth_token); |
| 144 virtual void OnIssueAuthTokenFailure(const std::string& service, |
| 145 const GoogleServiceAuthError& error); |
| 146 |
| 147 // WebDataServiceConsumer implementation. |
| 148 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, |
| 149 const WDTypedResult* result); |
| 150 |
| 151 // NotificationObserver implementation. |
| 152 virtual void Observe(NotificationType type, |
| 153 const NotificationSource& source, |
| 154 const NotificationDetails& details); |
| 155 |
| 156 private: |
| 157 |
| 158 void FireTokenAvailableNotification(const std::string& service, |
| 159 const std::string& auth_token); |
| 160 |
| 161 void FireTokenRequestFailedNotification(const std::string& service, |
| 162 const GoogleServiceAuthError& error); |
| 163 |
| 164 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, |
| 165 std::map<std::string, std::string>* out_toks); |
| 166 |
| 167 void SaveAuthTokenToDB(const std::string& service, |
| 168 const std::string& auth_token); |
| 169 |
| 170 // Web data service to access tokens from. |
| 171 scoped_refptr<WebDataService> web_data_service_; |
| 172 // Getter to use for fetchers. |
| 173 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 174 // Request handle to load Gaia tokens from DB. |
| 175 WebDataService::Handle token_loading_query_; |
| 176 |
| 177 // Gaia request source for Gaia accounting. |
| 178 std::string source_; |
| 179 // Credentials from authentication for Issuing auth tokens. |
| 180 scoped_ptr<AuthenticationConsumer::AuthenticationResult> credentials_; |
| 181 |
| 182 // Size of array of services (must be defined here). |
| 183 static const int kNumServices = 4; |
| 184 // List of services that we're performing operations for. |
| 185 static const char* kServices[kNumServices]; |
| 186 // A bunch of fetchers suitable for token issuing. We don't care about |
| 187 // the ordering, nor do we care which is for which service. |
| 188 scoped_ptr<AuthenticationFetcher> fetchers_[kNumServices]; |
| 189 // Map from service to token. |
| 190 std::map<std::string, std::string> token_map_; |
| 191 |
| 192 NotificationRegistrar registrar_; |
| 193 |
| 194 FRIEND_TEST_ALL_PREFIXES(AuthenticationServiceTest, |
| 195 LoadTokensIntoMemoryBasic); |
| 196 FRIEND_TEST_ALL_PREFIXES(AuthenticationServiceTest, |
| 197 LoadTokensIntoMemoryAdvanced); |
| 198 |
| 199 DISALLOW_COPY_AND_ASSIGN(AuthenticationService); |
| 200 }; |
| 201 |
| 202 #endif // CHROME_BROWSER_NET_GAIA_AUTHENTICATION_SERVICE_H_ |
OLD | NEW |