Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #ifndef CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 6 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 // startup. | 93 // startup. |
| 94 virtual void OnRefreshTokensLoaded() {} | 94 virtual void OnRefreshTokensLoaded() {} |
| 95 // Called after all refresh tokens are removed from OAuth2TokenService. | 95 // Called after all refresh tokens are removed from OAuth2TokenService. |
| 96 virtual void OnRefreshTokensCleared() {} | 96 virtual void OnRefreshTokensCleared() {} |
| 97 protected: | 97 protected: |
| 98 virtual ~Observer() {} | 98 virtual ~Observer() {} |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 // A set of scopes in OAuth2 authentication. | 101 // A set of scopes in OAuth2 authentication. |
| 102 typedef std::set<std::string> ScopeSet; | 102 typedef std::set<std::string> ScopeSet; |
| 103 typedef std::pair<std::string, ScopeSet> ClientScopeSet; | |
| 103 | 104 |
| 104 OAuth2TokenService(); | 105 OAuth2TokenService(); |
| 105 virtual ~OAuth2TokenService(); | 106 virtual ~OAuth2TokenService(); |
| 106 | 107 |
| 107 // Add or remove observers of this token service. | 108 // Add or remove observers of this token service. |
| 108 void AddObserver(Observer* observer); | 109 void AddObserver(Observer* observer); |
| 109 void RemoveObserver(Observer* observer); | 110 void RemoveObserver(Observer* observer); |
| 110 | 111 |
| 111 // Checks in the cache for a valid access token, and if not found starts | 112 // Checks in the cache for a valid access token, and if not found starts |
| 112 // a request for an OAuth2 access token using the OAuth2 refresh token | 113 // a request for an OAuth2 access token using the OAuth2 refresh token |
| 113 // maintained by this instance. The caller owns the returned Request. | 114 // maintained by this instance. The caller owns the returned Request. |
| 114 // |scopes| is the set of scopes to get an access token for, |consumer| is | 115 // |scopes| is the set of scopes to get an access token for, |consumer| is |
| 115 // the object that will be called back with results if the returned request | 116 // the object that will be called back with results if the returned request |
| 116 // is not deleted. | 117 // is not deleted. |
| 117 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 118 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
| 118 Consumer* consumer); | 119 Consumer* consumer); |
| 119 | 120 |
| 121 #if defined(OS_CHROMEOS) | |
| 120 // This method does the same as |StartRequest| except it uses |client_id| and | 122 // This method does the same as |StartRequest| except it uses |client_id| and |
| 121 // |client_secret| to identify OAuth client app instead of using | 123 // |client_secret| to identify OAuth client app instead of using |
| 122 // Chrome's default values. | 124 // Chrome's default values. |
| 123 virtual scoped_ptr<Request> StartRequestForClient( | 125 virtual scoped_ptr<Request> StartRequestForClient( |
| 124 const std::string& client_id, | 126 const std::string& client_id, |
| 125 const std::string& client_secret, | 127 const std::string& client_secret, |
| 126 const ScopeSet& scopes, | 128 const ScopeSet& scopes, |
| 127 Consumer* consumer); | 129 Consumer* consumer); |
| 130 #endif | |
|
(NOT FOR CODE REVIEWS)
2013/08/07 19:56:49
+Michael and Filip.
I think this should be in all
Michael Courage
2013/08/07 21:07:31
Our input to a token request is (extension_id, cli
zel
2013/08/08 01:34:24
To answer the first question, atwilson@ ask me to
| |
| 128 | 131 |
| 129 // This method does the same as |StartRequest| except it uses the request | 132 // This method does the same as |StartRequest| except it uses the request |
| 130 // context given by |getter| instead of using the one returned by | 133 // context given by |getter| instead of using the one returned by |
| 131 // |GetRequestContext| implemented by derived classes. | 134 // |GetRequestContext| implemented by derived classes. |
| 132 virtual scoped_ptr<Request> StartRequestWithContext( | 135 virtual scoped_ptr<Request> StartRequestWithContext( |
| 133 net::URLRequestContextGetter* getter, | 136 net::URLRequestContextGetter* getter, |
| 134 const ScopeSet& scopes, | 137 const ScopeSet& scopes, |
| 135 Consumer* consumer); | 138 Consumer* consumer); |
| 136 | 139 |
| 137 // Returns true if a refresh token exists. If false, calls to | 140 // Returns true if a refresh token exists. If false, calls to |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 // Subclasses should return the refresh token maintained. | 176 // Subclasses should return the refresh token maintained. |
| 174 // If no token is available, return an empty string. | 177 // If no token is available, return an empty string. |
| 175 virtual std::string GetRefreshToken() = 0; | 178 virtual std::string GetRefreshToken() = 0; |
| 176 | 179 |
| 177 // Subclasses can override if they want to report errors to the user. | 180 // Subclasses can override if they want to report errors to the user. |
| 178 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 181 virtual void UpdateAuthError(const GoogleServiceAuthError& error); |
| 179 | 182 |
| 180 // Add a new entry to the cache. | 183 // Add a new entry to the cache. |
| 181 // Subclasses can override if there are implementation-specific reasons | 184 // Subclasses can override if there are implementation-specific reasons |
| 182 // that an access token should ever not be cached. | 185 // that an access token should ever not be cached. |
| 183 virtual void RegisterCacheEntry(const std::string& refresh_token, | 186 virtual void RegisterCacheEntry(const std::string& client_id, |
| 187 const std::string& refresh_token, | |
| 184 const ScopeSet& scopes, | 188 const ScopeSet& scopes, |
| 185 const std::string& access_token, | 189 const std::string& access_token, |
| 186 const base::Time& expiration_date); | 190 const base::Time& expiration_date); |
| 187 | 191 |
| 188 // Returns true if GetCacheEntry would return a valid cache entry for the | 192 // Returns true if GetCacheEntry would return a valid cache entry for the |
| 189 // given scopes. | 193 // given scopes. |
| 190 bool HasCacheEntry(const ScopeSet& scopes); | 194 bool HasCacheEntry(const ClientScopeSet& client_scopes); |
| 191 | 195 |
| 192 // Posts a task to fire the Consumer callback with the cached token. Must | 196 // Posts a task to fire the Consumer callback with the cached token. Must |
| 193 // Must only be called if HasCacheEntry() returns true. | 197 // Must only be called if HasCacheEntry() returns true. |
| 194 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 198 scoped_ptr<Request> StartCacheLookupRequest( |
| 195 Consumer* consumer); | 199 const ClientScopeSet& client_scopes, |
| 200 Consumer* consumer); | |
| 196 | 201 |
| 197 // Clears the internal token cache. | 202 // Clears the internal token cache. |
| 198 void ClearCache(); | 203 void ClearCache(); |
| 199 | 204 |
| 200 // Cancels all requests that are currently in progress. | 205 // Cancels all requests that are currently in progress. |
| 201 void CancelAllRequests(); | 206 void CancelAllRequests(); |
| 202 | 207 |
| 203 // Cancels all requests related to a given refresh token. | 208 // Cancels all requests related to a given refresh token. |
| 204 void CancelRequestsForToken(const std::string& refresh_token); | 209 void CancelRequestsForToken(const std::string& refresh_token); |
| 205 | 210 |
| 206 // Called by subclasses to notify observers. | 211 // Called by subclasses to notify observers. |
| 207 void FireRefreshTokenAvailable(const std::string& account_id); | 212 void FireRefreshTokenAvailable(const std::string& account_id); |
| 208 void FireRefreshTokenRevoked(const std::string& account_id, | 213 void FireRefreshTokenRevoked(const std::string& account_id, |
| 209 const GoogleServiceAuthError& error); | 214 const GoogleServiceAuthError& error); |
| 210 void FireRefreshTokensLoaded(); | 215 void FireRefreshTokensLoaded(); |
| 211 void FireRefreshTokensCleared(); | 216 void FireRefreshTokensCleared(); |
| 212 | 217 |
| 213 private: | 218 private: |
| 214 // Derived classes must provide a request context used for fetching access | 219 // Derived classes must provide a request context used for fetching access |
| 215 // tokens with the |StartRequest| method. | 220 // tokens with the |StartRequest| method. |
| 216 virtual net::URLRequestContextGetter* GetRequestContext() = 0; | 221 virtual net::URLRequestContextGetter* GetRequestContext() = 0; |
| 217 | 222 |
| 218 // Class that fetches an OAuth2 access token for a given set of scopes and | 223 // Class that fetches an OAuth2 access token for a given set of scopes and |
| 219 // OAuth2 refresh token. | 224 // OAuth2 refresh token. |
|
(NOT FOR CODE REVIEWS)
2013/08/07 19:56:49
Add comment about client_id too?
| |
| 220 class Fetcher; | 225 class Fetcher; |
| 221 friend class Fetcher; | 226 friend class Fetcher; |
| 222 | 227 |
| 223 // Struct that contains the information of an OAuth2 access token. | 228 // Struct that contains the information of an OAuth2 access token. |
| 224 struct CacheEntry { | 229 struct CacheEntry { |
| 225 std::string access_token; | 230 std::string access_token; |
| 226 base::Time expiration_date; | 231 base::Time expiration_date; |
| 227 }; | 232 }; |
| 228 | 233 |
| 229 // This method does the same as |StartRequestWithContext| except it | 234 // This method does the same as |StartRequestWithContext| except it |
| 230 // uses |client_id| and |client_secret| to identify OAuth | 235 // uses |client_id| and |client_secret| to identify OAuth |
| 231 // client app instead of using Chrome's default values. | 236 // client app instead of using Chrome's default values. |
| 232 scoped_ptr<Request> StartRequestForClientWithContext( | 237 scoped_ptr<Request> StartRequestForClientWithContext( |
| 233 net::URLRequestContextGetter* getter, | 238 net::URLRequestContextGetter* getter, |
| 234 const std::string& client_id, | 239 const std::string& client_id, |
| 235 const std::string& client_secret, | 240 const std::string& client_secret, |
| 236 const ScopeSet& scopes, | 241 const ScopeSet& scopes, |
| 237 Consumer* consumer); | 242 Consumer* consumer); |
| 238 | 243 |
| 239 // Returns a currently valid OAuth2 access token for the given set of scopes, | 244 // Returns a currently valid OAuth2 access token for the given set of scopes, |
| 240 // or NULL if none have been cached. Note the user of this method should | 245 // or NULL if none have been cached. Note the user of this method should |
| 241 // ensure no entry with the same |scopes| is added before the usage of the | 246 // ensure no entry with the same |client_scopes| is added before the usage of |
| 242 // returned entry is done. | 247 // the returned entry is done. |
| 243 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); | 248 const CacheEntry* GetCacheEntry(const ClientScopeSet& client_scopes); |
| 244 | 249 |
| 245 | 250 |
| 246 // Removes an access token for the given set of scopes from the cache. | 251 // Removes an access token for the given set of scopes from the cache. |
| 247 // Returns true if the entry was removed, otherwise false. | 252 // Returns true if the entry was removed, otherwise false. |
| 248 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, | 253 bool RemoveCacheEntry(const ClientScopeSet& client_scopes, |
| 249 const std::string& token_to_remove); | 254 const std::string& token_to_remove); |
| 250 | 255 |
| 251 | 256 |
| 252 // Called when |fetcher| finishes fetching. | 257 // Called when |fetcher| finishes fetching. |
| 253 void OnFetchComplete(Fetcher* fetcher); | 258 void OnFetchComplete(Fetcher* fetcher); |
| 254 | 259 |
| 255 // Called when a number of fetchers need to be canceled. | 260 // Called when a number of fetchers need to be canceled. |
| 256 void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel); | 261 void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel); |
| 257 | 262 |
| 258 // The cache of currently valid tokens. | 263 // The cache of currently valid tokens. |
| 259 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 264 typedef std::map<ClientScopeSet, CacheEntry> TokenCache; |
| 260 TokenCache token_cache_; | 265 TokenCache token_cache_; |
| 261 | 266 |
| 262 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 267 // The parameters (client_id, refresh token and scope set) used to fetch an |
| 263 // token. | 268 // OAuth2 access token. |
| 264 typedef std::pair<std::string, ScopeSet> FetchParameters; | 269 typedef std::pair<std::pair<std::string /* client_id */, |
| 270 std::string /* refresh_token */>, | |
| 271 ScopeSet> FetchParameters; | |
|
(NOT FOR CODE REVIEWS)
2013/08/07 19:56:49
Would be clearer to declare a struct with 3 distin
fgorski
2013/08/07 21:36:59
Alternative solution would be to use ClientScopeSe
zel
2013/08/08 01:34:24
I've used more complex key now and made this a str
| |
| 265 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 272 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
| 266 // token using these parameters. | 273 // token using these parameters. |
| 267 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 274 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
| 268 | 275 |
| 269 // List of observers to notify when token availiability changes. | 276 // List of observers to notify when token availiability changes. |
| 270 // Makes sure list is empty on destruction. | 277 // Makes sure list is empty on destruction. |
| 271 ObserverList<Observer, true> observer_list_; | 278 ObserverList<Observer, true> observer_list_; |
| 272 | 279 |
| 273 // Maximum number of retries in fetching an OAuth2 access token. | 280 // Maximum number of retries in fetching an OAuth2 access token. |
| 274 static int max_fetch_retry_num_; | 281 static int max_fetch_retry_num_; |
| 275 | 282 |
| 276 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 283 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 277 }; | 284 }; |
| 278 | 285 |
| 279 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 286 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| OLD | NEW |