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> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class Time; | 19 class Time; |
20 } | 20 } |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
24 } | 24 } |
25 | 25 |
26 class GoogleServiceAuthError; | 26 class GoogleServiceAuthError; |
27 | 27 |
28 // Abstract base class for a service that fetches and caches OAuth2 access | 28 // Abstract base class for a service that fetches and caches OAuth2 access |
29 // tokens. Concrete subclasses should implement GetRefreshToken to return | 29 // tokens. Concrete subclasses should implement StartGetRefreshToken to provide |
30 // the appropriate refresh token. | 30 // the appropriate refresh token. |
31 // | 31 // |
32 // All calls are expected from the UI thread. | 32 // All calls are expected from the UI thread. |
33 // | 33 // |
34 // To use this service, call StartRequest() with a given set of scopes and a | 34 // To use this service, call StartRequest() with a given set of scopes and a |
35 // consumer of the request results. The consumer is required to outlive the | 35 // consumer of the request results. The consumer is required to outlive the |
36 // request. The request can be deleted. The consumer may be called back | 36 // request. The request can be deleted. The consumer may be called back |
37 // asynchronously with the fetch results. | 37 // asynchronously with the fetch results. |
38 // | 38 // |
39 // - If the consumer is not called back before the request is deleted, it will | 39 // - If the consumer is not called back before the request is deleted, it will |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 | 80 |
81 // Checks in the cache for a valid access token, and if not found starts | 81 // Checks in the cache for a valid access token, and if not found starts |
82 // a request for an OAuth2 access token using the OAuth2 refresh token | 82 // a request for an OAuth2 access token using the OAuth2 refresh token |
83 // maintained by this instance. The caller owns the returned Request. | 83 // maintained by this instance. The caller owns the returned Request. |
84 // |scopes| is the set of scopes to get an access token for, |consumer| is | 84 // |scopes| is the set of scopes to get an access token for, |consumer| is |
85 // the object that will be called back with results if the returned request | 85 // the object that will be called back with results if the returned request |
86 // is not deleted. | 86 // is not deleted. |
87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
88 Consumer* consumer); | 88 Consumer* consumer); |
89 | 89 |
90 // Returns true if a refresh token exists. If false, calls to | |
91 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | |
92 bool RefreshTokenIsAvailable(); | |
93 | |
94 // Mark an OAuth2 access token as invalid. This should be done if the token | 90 // Mark an OAuth2 access token as invalid. This should be done if the token |
95 // was received from this class, but was not accepted by the server (e.g., | 91 // was received from this class, but was not accepted by the server (e.g., |
96 // the server returned 401 Unauthorized). The token will be removed from the | 92 // the server returned 401 Unauthorized). The token will be removed from the |
97 // cache for the given scopes. | 93 // cache for the given scopes. |
98 virtual void InvalidateToken(const ScopeSet& scopes, | 94 virtual void InvalidateToken(const ScopeSet& scopes, |
99 const std::string& invalid_token); | 95 const std::string& invalid_token); |
100 | 96 |
101 // Return the current number of entries in the cache. | 97 // Return the current number of entries in the cache. |
102 int cache_size_for_testing() const; | 98 int cache_size_for_testing() const; |
103 | 99 |
104 protected: | 100 protected: |
101 // Interface that StartGetRefreshToken calls back into. | |
102 class RefreshTokenValidationConsumer { | |
103 public: | |
104 virtual void OnRefreshTokenValidationComplete( | |
105 const std::string& refresh_token, | |
106 bool is_valid) = 0; | |
107 }; | |
108 | |
109 // Implements a cancelable |OAuth2TokenService::Request|, which should be | |
110 // operated on the UI thread. | |
111 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, | |
112 public Request { | |
113 public: | |
114 // |consumer| is required to outlive this. | |
115 explicit RequestImpl(Consumer* consumer); | |
116 virtual ~RequestImpl(); | |
117 | |
118 // Informs |consumer_| that this request is completed. | |
119 void InformConsumer(const GoogleServiceAuthError& error, | |
120 const std::string& access_token, | |
121 const base::Time& expiration_date); | |
122 | |
123 private: | |
124 // |consumer_| to call back when this request completes. | |
125 Consumer* const consumer_; | |
126 }; | |
127 | |
105 // Subclasses should return the refresh token maintained. | 128 // Subclasses should return the refresh token maintained. |
106 // If no token is available, return an empty string. | 129 // If no token is available, return an empty string. |
107 virtual std::string GetRefreshToken() = 0; | 130 virtual std::string GetRefreshToken() = 0; |
108 | 131 |
132 // Subclasses can optionally implement this method to validate the given | |
133 // refresh token. Return true if the validation is started and the | |
134 // callback should be expected, or false if validation is skipped and the | |
135 // token should be used directly. This method may be invoked in parallel; | |
136 // subclasses must ensure that all consumers are notified of the results. | |
137 virtual bool StartRefreshTokenValidation( | |
Mattias Nissler (ping if slow)
2013/06/17 05:34:17
I wonder whether making the concept of refresh tok
David Roche
2013/06/18 04:12:08
I started doing that initially, but unfortunately
| |
138 const std::string refresh_token, | |
139 RefreshTokenValidationConsumer* consumer); | |
140 | |
109 // Subclasses can override if they want to report errors to the user. | 141 // Subclasses can override if they want to report errors to the user. |
110 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 142 virtual void UpdateAuthError(const GoogleServiceAuthError& error); |
111 | 143 |
112 // Add a new entry to the cache. | 144 // Add a new entry to the cache. |
113 // Subclasses can override if there are implementation-specific reasons | 145 // Subclasses can override if there are implementation-specific reasons |
114 // that an access token should ever not be cached. | 146 // that an access token should ever not be cached. |
115 virtual void RegisterCacheEntry(const std::string& refresh_token, | 147 virtual void RegisterCacheEntry(const std::string& refresh_token, |
116 const ScopeSet& scopes, | 148 const ScopeSet& scopes, |
117 const std::string& access_token, | 149 const std::string& access_token, |
118 const base::Time& expiration_date); | 150 const base::Time& expiration_date); |
119 | 151 |
120 // Returns true if GetCacheEntry would return a valid cache entry for the | 152 // Returns true if GetCacheEntry would return a valid cache entry for the |
121 // given scopes. | 153 // given scopes. |
122 bool HasCacheEntry(const ScopeSet& scopes); | 154 bool HasCacheEntry(const ScopeSet& scopes); |
123 | 155 |
124 // Posts a task to fire the Consumer callback with the cached token. Must | 156 // Posts a task to fetch the cached token for the given in-flight Request. |
125 // only be called if HasCacheEntry() returns true. | 157 // Must only be called if HasCacheEntry() returns true. |
126 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 158 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, |
127 Consumer* consumer); | 159 scoped_ptr<RequestImpl> request); |
128 | 160 |
129 // Clears the internal token cache. | 161 // Clears the internal token cache. |
130 void ClearCache(); | 162 void ClearCache(); |
131 | 163 |
132 // Implements a cancelable |OAuth2TokenService::Request|, which should be | |
133 // operated on the UI thread. | |
134 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, | |
135 public Request { | |
136 public: | |
137 // |consumer| is required to outlive this. | |
138 explicit RequestImpl(Consumer* consumer); | |
139 virtual ~RequestImpl(); | |
140 | |
141 // Informs |consumer_| that this request is completed. | |
142 void InformConsumer(const GoogleServiceAuthError& error, | |
143 const std::string& access_token, | |
144 const base::Time& expiration_date); | |
145 | |
146 private: | |
147 // |consumer_| to call back when this request completes. | |
148 Consumer* const consumer_; | |
149 }; | |
150 | |
151 private: | 164 private: |
152 // Class that fetches an OAuth2 access token for a given set of scopes and | 165 // Class that fetches an OAuth2 access token for a given set of scopes and |
153 // OAuth2 refresh token. | 166 // OAuth2 refresh token. |
154 class Fetcher; | 167 class Fetcher; |
155 friend class Fetcher; | 168 friend class Fetcher; |
156 | 169 |
157 // Struct that contains the information of an OAuth2 access token. | 170 // Struct that contains the information of an OAuth2 access token. |
158 struct CacheEntry { | 171 struct CacheEntry { |
159 std::string access_token; | 172 std::string access_token; |
160 base::Time expiration_date; | 173 base::Time expiration_date; |
(...skipping 26 matching lines...) Expand all Loading... | |
187 // token. | 200 // token. |
188 typedef std::pair<std::string, ScopeSet> FetchParameters; | 201 typedef std::pair<std::string, ScopeSet> FetchParameters; |
189 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 202 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
190 // token using these parameters. | 203 // token using these parameters. |
191 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 204 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
192 | 205 |
193 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 206 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
194 }; | 207 }; |
195 | 208 |
196 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 209 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |