OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 5 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 } | 39 } |
40 | 40 |
41 std::string ApplyAccountIdPrefix(const std::string& account_id) { | 41 std::string ApplyAccountIdPrefix(const std::string& account_id) { |
42 return kAccountIdPrefix + account_id; | 42 return kAccountIdPrefix + account_id; |
43 } | 43 } |
44 | 44 |
45 std::string RemoveAccountIdPrefix(const std::string& prefixed_account_id) { | 45 std::string RemoveAccountIdPrefix(const std::string& prefixed_account_id) { |
46 return prefixed_account_id.substr(kAccountIdPrefixLength); | 46 return prefixed_account_id.substr(kAccountIdPrefixLength); |
47 } | 47 } |
48 | 48 |
49 std::string GetAccountId(Profile* profile) { | |
50 SigninManagerBase* signin_manager = | |
51 SigninManagerFactory::GetForProfileIfExists(profile); | |
52 return signin_manager ? signin_manager->GetAuthenticatedUsername() : | |
53 std::string(); | |
54 } | |
55 | |
56 } // namespace | 49 } // namespace |
57 | 50 |
58 ProfileOAuth2TokenService::ProfileOAuth2TokenService() | 51 ProfileOAuth2TokenService::ProfileOAuth2TokenService() |
59 : profile_(NULL), | 52 : profile_(NULL), |
60 web_data_service_request_(0), | 53 web_data_service_request_(0), |
61 last_auth_error_(GoogleServiceAuthError::NONE) { | 54 last_auth_error_(GoogleServiceAuthError::NONE) { |
62 } | 55 } |
63 | 56 |
64 ProfileOAuth2TokenService::~ProfileOAuth2TokenService() { | 57 ProfileOAuth2TokenService::~ProfileOAuth2TokenService() { |
65 DCHECK(!signin_global_error_.get()) << | 58 DCHECK(!signin_global_error_.get()) << |
(...skipping 26 matching lines...) Expand all Loading... |
92 | 85 |
93 void ProfileOAuth2TokenService::Shutdown() { | 86 void ProfileOAuth2TokenService::Shutdown() { |
94 DCHECK(profile_) << "Shutdown() called without matching call to Initialize()"; | 87 DCHECK(profile_) << "Shutdown() called without matching call to Initialize()"; |
95 CancelAllRequests(); | 88 CancelAllRequests(); |
96 signin_global_error_->RemoveProvider(this); | 89 signin_global_error_->RemoveProvider(this); |
97 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError( | 90 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError( |
98 signin_global_error_.get()); | 91 signin_global_error_.get()); |
99 signin_global_error_.reset(); | 92 signin_global_error_.reset(); |
100 } | 93 } |
101 | 94 |
102 std::string ProfileOAuth2TokenService::GetRefreshToken() { | 95 std::string ProfileOAuth2TokenService::GetRefreshToken( |
103 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 96 const std::string& account_id) { |
104 if (!token_service || !token_service->HasOAuthLoginToken()) { | 97 std::map<std::string, std::string>::const_iterator iter = |
105 return std::string(); | 98 refresh_tokens_.find(account_id); |
106 } | 99 if (iter != refresh_tokens_.end()) |
107 return token_service->GetOAuth2LoginRefreshToken(); | 100 return iter->second; |
| 101 return std::string(); |
108 } | 102 } |
109 | 103 |
110 net::URLRequestContextGetter* ProfileOAuth2TokenService::GetRequestContext() { | 104 net::URLRequestContextGetter* ProfileOAuth2TokenService::GetRequestContext() { |
111 return profile_->GetRequestContext(); | 105 return profile_->GetRequestContext(); |
112 } | 106 } |
113 | 107 |
114 void ProfileOAuth2TokenService::UpdateAuthError( | 108 void ProfileOAuth2TokenService::UpdateAuthError( |
| 109 const std::string& account_id, |
115 const GoogleServiceAuthError& error) { | 110 const GoogleServiceAuthError& error) { |
| 111 // TODO(fgorski): SigninGlobalError needs to be made multi-login aware. |
116 // Do not report connection errors as these are not actually auth errors. | 112 // Do not report connection errors as these are not actually auth errors. |
117 // We also want to avoid masking a "real" auth error just because we | 113 // We also want to avoid masking a "real" auth error just because we |
118 // subsequently get a transient network error. | 114 // subsequently get a transient network error. |
119 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) | 115 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) |
120 return; | 116 return; |
121 | 117 |
122 if (error.state() != last_auth_error_.state()) { | 118 if (error.state() != last_auth_error_.state()) { |
123 last_auth_error_ = error; | 119 last_auth_error_ = error; |
124 signin_global_error_->AuthStatusChanged(); | 120 signin_global_error_->AuthStatusChanged(); |
125 } | 121 } |
126 } | 122 } |
127 | 123 |
128 void ProfileOAuth2TokenService::Observe( | 124 void ProfileOAuth2TokenService::Observe( |
129 int type, | 125 int type, |
130 const content::NotificationSource& source, | 126 const content::NotificationSource& source, |
131 const content::NotificationDetails& details) { | 127 const content::NotificationDetails& details) { |
132 switch (type) { | 128 switch (type) { |
133 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | 129 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { |
134 TokenService::TokenAvailableDetails* tok_details = | 130 TokenService::TokenAvailableDetails* tok_details = |
135 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); | 131 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); |
136 if (tok_details->service() == | 132 if (tok_details->service() == |
137 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { | 133 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
138 // TODO(fgorski): Canceling all requests will not be correct in a | 134 // TODO(fgorski): Canceling all requests will not be correct in a |
139 // multi-login environment. We should cancel only the requests related | 135 // multi-login environment. We should cancel only the requests related |
140 // to the token being replaced (old token for the same account_id). | 136 // to the token being replaced (old token for the same account_id). |
141 // Previous refresh token is not available at this point, but since | 137 // Previous refresh token is not available at this point, but since |
142 // there are no other refresh tokens, we cancel all active requests. | 138 // there are no other refresh tokens, we cancel all active requests. |
143 CancelAllRequests(); | 139 CancelAllRequests(); |
144 ClearCache(); | 140 ClearCache(); |
145 UpdateAuthError(GoogleServiceAuthError::AuthErrorNone()); | 141 std::string account_id = GetPrimaryAccountId(); |
146 FireRefreshTokenAvailable(GetAccountId(profile_)); | 142 UpdateAuthError(account_id, GoogleServiceAuthError::AuthErrorNone()); |
| 143 refresh_tokens_[account_id] = tok_details->token(); |
| 144 FireRefreshTokenAvailable(account_id); |
147 } | 145 } |
148 break; | 146 break; |
149 } | 147 } |
150 case chrome::NOTIFICATION_TOKENS_CLEARED: { | 148 case chrome::NOTIFICATION_TOKENS_CLEARED: { |
151 CancelAllRequests(); | 149 CancelAllRequests(); |
152 ClearCache(); | 150 ClearCache(); |
153 UpdateAuthError(GoogleServiceAuthError::AuthErrorNone()); | 151 UpdateAuthError(GetPrimaryAccountId(), |
154 FireRefreshTokensCleared(); | 152 GoogleServiceAuthError::AuthErrorNone()); |
155 break; | 153 break; |
156 } | 154 } |
157 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: | 155 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: |
158 // During startup, if the user is signed in and the OAuth2 refresh token | 156 // During startup, if the user is signed in and the OAuth2 refresh token |
159 // is empty, flag it as an error by badging the menu. Otherwise, if the | 157 // is empty, flag it as an error by badging the menu. Otherwise, if the |
160 // user goes on to set up sync, they will have to make two attempts: | 158 // user goes on to set up sync, they will have to make two attempts: |
161 // One to surface the OAuth2 error, and a second one after signing in. | 159 // One to surface the OAuth2 error, and a second one after signing in. |
162 // See crbug.com/276650. | 160 // See crbug.com/276650. |
163 if (!GetAccountId(profile_).empty() && GetRefreshToken().empty()) { | 161 if (!GetPrimaryAccountId().empty() && |
164 UpdateAuthError(GoogleServiceAuthError( | 162 !RefreshTokenIsAvailable(GetPrimaryAccountId())) { |
| 163 UpdateAuthError(GetPrimaryAccountId(), GoogleServiceAuthError( |
165 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 164 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
166 } | 165 } |
167 FireRefreshTokensLoaded(); | 166 FireRefreshTokensLoaded(); |
168 break; | 167 break; |
169 default: | 168 default: |
170 NOTREACHED() << "Invalid notification type=" << type; | 169 NOTREACHED() << "Invalid notification type=" << type; |
171 break; | 170 break; |
172 } | 171 } |
173 } | 172 } |
174 | 173 |
175 GoogleServiceAuthError ProfileOAuth2TokenService::GetAuthStatus() const { | 174 GoogleServiceAuthError ProfileOAuth2TokenService::GetAuthStatus() const { |
176 return last_auth_error_; | 175 return last_auth_error_; |
177 } | 176 } |
178 | 177 |
179 void ProfileOAuth2TokenService::RegisterCacheEntry( | 178 void ProfileOAuth2TokenService::RegisterCacheEntry( |
180 const std::string& refresh_token, | 179 const std::string& refresh_token, |
181 const ScopeSet& scopes, | 180 const ScopeSet& scopes, |
182 const std::string& access_token, | 181 const std::string& access_token, |
183 const base::Time& expiration_date) { | 182 const base::Time& expiration_date) { |
184 if (ShouldCacheForRefreshToken(TokenServiceFactory::GetForProfile(profile_), | 183 if (ShouldCacheForRefreshToken(refresh_token)) { |
185 refresh_token)) { | |
186 OAuth2TokenService::RegisterCacheEntry(refresh_token, | 184 OAuth2TokenService::RegisterCacheEntry(refresh_token, |
187 scopes, | 185 scopes, |
188 access_token, | 186 access_token, |
189 expiration_date); | 187 expiration_date); |
190 } | 188 } |
191 } | 189 } |
192 | 190 |
193 bool ProfileOAuth2TokenService::ShouldCacheForRefreshToken( | 191 bool ProfileOAuth2TokenService::ShouldCacheForRefreshToken( |
194 TokenService *token_service, | |
195 const std::string& refresh_token) { | 192 const std::string& refresh_token) { |
196 if (!token_service || | 193 // Check below ensures that only refresh tokens belonging to one of the logged |
197 !token_service->HasOAuthLoginToken() || | 194 // in accounts will allow for the access tokens to be cached. |
198 token_service->GetOAuth2LoginRefreshToken().compare(refresh_token) != 0) { | 195 // TODO(fgorski): Convert to CHECK/DCHECK if it should not be possible. |
199 DLOG(INFO) << | 196 // Consider a re-auth scenario. |
200 "Received a token with a refresh token not maintained by TokenService."; | 197 for (std::map<std::string, std::string>::const_iterator iter = |
201 return false; | 198 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { |
| 199 if (iter->second == refresh_token) |
| 200 return true; |
202 } | 201 } |
203 return true; | 202 |
| 203 DLOG(INFO) << |
| 204 "Received a token with a refresh token not maintained by TokenService."; |
| 205 return false; |
| 206 } |
| 207 |
| 208 std::string ProfileOAuth2TokenService::GetPrimaryAccountId() { |
| 209 SigninManagerBase* signin_manager = |
| 210 SigninManagerFactory::GetForProfileIfExists(profile_); |
| 211 // TODO(fgorski): DCHECK(signin_manager) here - it may require update to test |
| 212 // code and the line above (SigninManager might not exist yet). |
| 213 return signin_manager ? signin_manager->GetAuthenticatedUsername() |
| 214 : std::string(); |
| 215 } |
| 216 |
| 217 std::vector<std::string> ProfileOAuth2TokenService::GetAccounts() { |
| 218 std::vector<std::string> account_ids; |
| 219 for (std::map<std::string, std::string>::const_iterator iter = |
| 220 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { |
| 221 account_ids.push_back(iter->first); |
| 222 } |
| 223 return account_ids; |
204 } | 224 } |
205 | 225 |
206 void ProfileOAuth2TokenService::UpdateCredentials( | 226 void ProfileOAuth2TokenService::UpdateCredentials( |
207 const std::string& account_id, | 227 const std::string& account_id, |
208 const std::string& refresh_token) { | 228 const std::string& refresh_token) { |
209 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 229 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
210 DCHECK(!refresh_token.empty()); | 230 DCHECK(!refresh_token.empty()); |
211 | 231 |
212 bool refresh_token_present = refresh_tokens_.count(account_id) > 0; | 232 bool refresh_token_present = refresh_tokens_.count(account_id) > 0; |
213 if (!refresh_token_present || | 233 if (!refresh_token_present || |
214 refresh_tokens_[account_id] != refresh_token) { | 234 refresh_tokens_[account_id] != refresh_token) { |
215 // If token present, and different from the new one, cancel its requests. | 235 // If token present, and different from the new one, cancel its requests, |
216 if (refresh_token_present) | 236 // and clear the entries in cache related to that account. |
| 237 if (refresh_token_present) { |
217 CancelRequestsForToken(refresh_tokens_[account_id]); | 238 CancelRequestsForToken(refresh_tokens_[account_id]); |
| 239 // ClearCacheForAccount(account_id); |
| 240 } |
218 | 241 |
219 // Save the token in memory and in persistent store. | 242 // Save the token in memory and in persistent store. |
220 refresh_tokens_[account_id] = refresh_token; | 243 refresh_tokens_[account_id] = refresh_token; |
221 scoped_refptr<TokenWebData> token_web_data = | 244 scoped_refptr<TokenWebData> token_web_data = |
222 TokenWebData::FromBrowserContext(profile_); | 245 TokenWebData::FromBrowserContext(profile_); |
223 if (token_web_data.get()) | 246 if (token_web_data.get()) |
224 token_web_data->SetTokenForService(ApplyAccountIdPrefix(account_id), | 247 token_web_data->SetTokenForService(ApplyAccountIdPrefix(account_id), |
225 refresh_token); | 248 refresh_token); |
226 | 249 |
| 250 UpdateAuthError(account_id, GoogleServiceAuthError::AuthErrorNone()); |
227 FireRefreshTokenAvailable(account_id); | 251 FireRefreshTokenAvailable(account_id); |
228 // TODO(fgorski): Notify diagnostic observers. | 252 // TODO(fgorski): Notify diagnostic observers. |
229 } | 253 } |
230 } | 254 } |
231 | 255 |
232 void ProfileOAuth2TokenService::RevokeCredentials( | 256 void ProfileOAuth2TokenService::RevokeCredentials( |
233 const std::string& account_id) { | 257 const std::string& account_id) { |
234 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 258 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
235 | 259 |
236 if (refresh_tokens_.count(account_id) > 0) { | 260 if (refresh_tokens_.count(account_id) > 0) { |
237 CancelRequestsForToken(refresh_tokens_[account_id]); | 261 CancelRequestsForToken(refresh_tokens_[account_id]); |
| 262 // TODO(fgorski): Call ClearCacheForAccount(account_id) from here. |
238 refresh_tokens_.erase(account_id); | 263 refresh_tokens_.erase(account_id); |
239 scoped_refptr<TokenWebData> token_web_data = | 264 scoped_refptr<TokenWebData> token_web_data = |
240 TokenWebData::FromBrowserContext(profile_); | 265 TokenWebData::FromBrowserContext(profile_); |
241 if (token_web_data.get()) | 266 if (token_web_data.get()) |
242 token_web_data->RemoveTokenForService(ApplyAccountIdPrefix(account_id)); | 267 token_web_data->RemoveTokenForService(ApplyAccountIdPrefix(account_id)); |
243 FireRefreshTokenRevoked(account_id); | 268 FireRefreshTokenRevoked(account_id); |
244 | 269 |
245 // TODO(fgorski): Notify diagnostic observers. | 270 // TODO(fgorski): Notify diagnostic observers. |
246 } | 271 } |
247 } | 272 } |
248 | 273 |
249 void ProfileOAuth2TokenService::RevokeAllCredentials() { | 274 void ProfileOAuth2TokenService::RevokeAllCredentials() { |
250 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 275 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
251 | 276 |
252 CancelAllRequests(); | 277 CancelAllRequests(); |
253 for (std::map<std::string, std::string>::const_iterator iter = | 278 for (std::map<std::string, std::string>::const_iterator iter = |
254 refresh_tokens_.begin(); | 279 refresh_tokens_.begin(); |
255 iter != refresh_tokens_.end(); | 280 iter != refresh_tokens_.end(); |
256 ++iter) { | 281 ++iter) { |
257 FireRefreshTokenRevoked(iter->first); | 282 FireRefreshTokenRevoked(iter->first); |
258 } | 283 } |
259 refresh_tokens_.clear(); | 284 refresh_tokens_.clear(); |
260 | 285 |
261 scoped_refptr<TokenWebData> token_web_data = | 286 scoped_refptr<TokenWebData> token_web_data = |
262 TokenWebData::FromBrowserContext(profile_); | 287 TokenWebData::FromBrowserContext(profile_); |
263 if (token_web_data.get()) | 288 if (token_web_data.get()) |
264 token_web_data->RemoveAllTokens(); | 289 token_web_data->RemoveAllTokens(); |
265 FireRefreshTokensCleared(); | |
266 | 290 |
267 // TODO(fgorski): Notify diagnostic observers. | 291 // TODO(fgorski): Notify diagnostic observers. |
268 } | 292 } |
269 | 293 |
270 void ProfileOAuth2TokenService::LoadCredentials() { | 294 void ProfileOAuth2TokenService::LoadCredentials() { |
271 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 295 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
272 DCHECK_EQ(0, web_data_service_request_); | 296 DCHECK_EQ(0, web_data_service_request_); |
273 | 297 |
274 CancelAllRequests(); | 298 CancelAllRequests(); |
275 refresh_tokens_.clear(); | 299 refresh_tokens_.clear(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } else { | 341 } else { |
318 DCHECK(!refresh_token.empty()); | 342 DCHECK(!refresh_token.empty()); |
319 std::string account_id = RemoveAccountIdPrefix(prefixed_account_id); | 343 std::string account_id = RemoveAccountIdPrefix(prefixed_account_id); |
320 refresh_tokens_[account_id] = refresh_token; | 344 refresh_tokens_[account_id] = refresh_token; |
321 FireRefreshTokenAvailable(account_id); | 345 FireRefreshTokenAvailable(account_id); |
322 // TODO(fgorski): Notify diagnostic observers. | 346 // TODO(fgorski): Notify diagnostic observers. |
323 } | 347 } |
324 } | 348 } |
325 | 349 |
326 if (!old_login_token.empty() && | 350 if (!old_login_token.empty() && |
327 refresh_tokens_.count(GetAccountId(profile_)) == 0) { | 351 refresh_tokens_.count(GetPrimaryAccountId()) == 0) { |
328 UpdateCredentials(GetAccountId(profile_), old_login_token); | 352 UpdateCredentials(GetPrimaryAccountId(), old_login_token); |
329 } | 353 } |
330 | 354 |
331 FireRefreshTokensLoaded(); | 355 FireRefreshTokensLoaded(); |
332 } | 356 } |
OLD | NEW |