| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chrome/browser/signin/token_service.h" | 5 #include "chrome/browser/signin/token_service.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 // store tokens and fetch them. Move the key-value storage out of | 106 // store tokens and fetch them. Move the key-value storage out of |
| 107 // token_service, and leave the token fetching in token_service. | 107 // token_service, and leave the token fetching in token_service. |
| 108 | 108 |
| 109 void TokenService::AddAuthTokenManually(const std::string& service, | 109 void TokenService::AddAuthTokenManually(const std::string& service, |
| 110 const std::string& auth_token) { | 110 const std::string& auth_token) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 112 VLOG(1) << "Got an authorization token for " << service; | 112 VLOG(1) << "Got an authorization token for " << service; |
| 113 token_map_[service] = auth_token; | 113 token_map_[service] = auth_token; |
| 114 FireTokenAvailableNotification(service, auth_token); | 114 FireTokenAvailableNotification(service, auth_token); |
| 115 SaveAuthTokenToDB(service, auth_token); | 115 SaveAuthTokenToDB(service, auth_token); |
| 116 |
| 117 #if defined(OS_CHROMEOS) |
| 118 // We don't ever want to fetch OAuth2 tokens from LSO service token in case |
| 119 // when ChromeOS is in forced OAuth2 use mode. OAuth2 token should only |
| 120 // arrive into token service exclusively through UpdateCredentialsWithOAuth2. |
| 121 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceOAuth1)) |
| 122 return; |
| 123 #endif |
| 124 |
| 116 // If we got ClientLogin token for "lso" service, and we don't already have | 125 // If we got ClientLogin token for "lso" service, and we don't already have |
| 117 // OAuth2 tokens, start fetching OAuth2 login scoped token pair. | 126 // OAuth2 tokens, start fetching OAuth2 login scoped token pair. |
| 118 if (service == GaiaConstants::kLSOService && !HasOAuthLoginToken()) { | 127 if (service == GaiaConstants::kLSOService && !HasOAuthLoginToken()) { |
| 119 int index = GetServiceIndex(service); | 128 int index = GetServiceIndex(service); |
| 120 CHECK_GE(index, 0); | 129 CHECK_GE(index, 0); |
| 121 fetchers_[index]->StartLsoForOAuthLoginTokenExchange(auth_token); | 130 fetchers_[index]->StartLsoForOAuthLoginTokenExchange(auth_token); |
| 122 } | 131 } |
| 123 } | 132 } |
| 124 | 133 |
| 125 | 134 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 155 fetchers_[i].reset(); | 164 fetchers_[i].reset(); |
| 156 } | 165 } |
| 157 | 166 |
| 158 // Notify AboutSigninInternals that a new lsid and sid are available. | 167 // Notify AboutSigninInternals that a new lsid and sid are available. |
| 159 FOR_DIAGNOSTICS_OBSERVERS(NotifySigninValueChanged( | 168 FOR_DIAGNOSTICS_OBSERVERS(NotifySigninValueChanged( |
| 160 signin_internals_util::SID, credentials.sid)); | 169 signin_internals_util::SID, credentials.sid)); |
| 161 FOR_DIAGNOSTICS_OBSERVERS(NotifySigninValueChanged(LSID, credentials.lsid)); | 170 FOR_DIAGNOSTICS_OBSERVERS(NotifySigninValueChanged(LSID, credentials.lsid)); |
| 162 } | 171 } |
| 163 | 172 |
| 164 void TokenService::UpdateCredentialsWithOAuth2( | 173 void TokenService::UpdateCredentialsWithOAuth2( |
| 165 const GaiaAuthConsumer::ClientOAuthResult& credentials) { | 174 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
| 166 // Will be implemented once the ClientOAuth signin is complete. Not called | 175 SaveOAuth2Credentials(oauth2_tokens); |
| 167 // yet by any code. | |
| 168 NOTREACHED(); | |
| 169 } | 176 } |
| 170 | 177 |
| 171 void TokenService::LoadTokensFromDB() { | 178 void TokenService::LoadTokensFromDB() { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 if (web_data_service_.get()) | 180 if (web_data_service_.get()) |
| 174 token_loading_query_ = web_data_service_->GetAllTokens(this); | 181 token_loading_query_ = web_data_service_->GetAllTokens(this); |
| 175 } | 182 } |
| 176 | 183 |
| 177 void TokenService::SaveAuthTokenToDB(const std::string& service, | 184 void TokenService::SaveAuthTokenToDB(const std::string& service, |
| 178 const std::string& auth_token) { | 185 const std::string& auth_token) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 // Note map[key] is not const. | 256 // Note map[key] is not const. |
| 250 return (*token_map_.find(service)).second; | 257 return (*token_map_.find(service)).second; |
| 251 } | 258 } |
| 252 return EmptyString(); | 259 return EmptyString(); |
| 253 } | 260 } |
| 254 | 261 |
| 255 bool TokenService::HasOAuthLoginToken() const { | 262 bool TokenService::HasOAuthLoginToken() const { |
| 256 return HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken); | 263 return HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
| 257 } | 264 } |
| 258 | 265 |
| 266 bool TokenService::HasOAuthLoginAccessToken() const { |
| 267 return HasTokenForService(GaiaConstants::kGaiaOAuth2LoginAccessToken); |
| 268 } |
| 269 |
| 259 const std::string& TokenService::GetOAuth2LoginRefreshToken() const { | 270 const std::string& TokenService::GetOAuth2LoginRefreshToken() const { |
| 260 return GetTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken); | 271 return GetTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken); |
| 261 } | 272 } |
| 262 | 273 |
| 263 const std::string& TokenService::GetOAuth2LoginAccessToken() const { | 274 const std::string& TokenService::GetOAuth2LoginAccessToken() const { |
| 264 return GetTokenForService(GaiaConstants::kGaiaOAuth2LoginAccessToken); | 275 return GetTokenForService(GaiaConstants::kGaiaOAuth2LoginAccessToken); |
| 265 } | 276 } |
| 266 | 277 |
| 267 // static | 278 // static |
| 268 void TokenService::GetServiceNamesForTesting(std::vector<std::string>* names) { | 279 void TokenService::GetServiceNamesForTesting(std::vector<std::string>* names) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 LOG(WARNING) << "Auth token issuing failed for service:" << service | 347 LOG(WARNING) << "Auth token issuing failed for service:" << service |
| 337 << ", error: " << error.ToString(); | 348 << ", error: " << error.ToString(); |
| 338 FOR_DIAGNOSTICS_OBSERVERS( | 349 FOR_DIAGNOSTICS_OBSERVERS( |
| 339 NotifyTokenReceivedFailure(service, error.ToString())); | 350 NotifyTokenReceivedFailure(service, error.ToString())); |
| 340 FireTokenRequestFailedNotification(service, error); | 351 FireTokenRequestFailedNotification(service, error); |
| 341 } | 352 } |
| 342 | 353 |
| 343 void TokenService::OnClientOAuthSuccess(const ClientOAuthResult& result) { | 354 void TokenService::OnClientOAuthSuccess(const ClientOAuthResult& result) { |
| 344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 345 VLOG(1) << "Got OAuth2 login token pair"; | 356 VLOG(1) << "Got OAuth2 login token pair"; |
| 357 SaveOAuth2Credentials(result); |
| 358 } |
| 359 |
| 360 void TokenService::SaveOAuth2Credentials(const ClientOAuthResult& result) { |
| 346 token_map_[GaiaConstants::kGaiaOAuth2LoginRefreshToken] = | 361 token_map_[GaiaConstants::kGaiaOAuth2LoginRefreshToken] = |
| 347 result.refresh_token; | 362 result.refresh_token; |
| 348 token_map_[GaiaConstants::kGaiaOAuth2LoginAccessToken] = result.access_token; | 363 token_map_[GaiaConstants::kGaiaOAuth2LoginAccessToken] = result.access_token; |
| 364 // Save refresh token only since access token is transient anyway. |
| 349 SaveAuthTokenToDB(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 365 SaveAuthTokenToDB(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 350 result.refresh_token); | 366 result.refresh_token); |
| 351 SaveAuthTokenToDB(GaiaConstants::kGaiaOAuth2LoginAccessToken, | |
| 352 result.access_token); | |
| 353 // We don't save expiration information for now. | 367 // We don't save expiration information for now. |
| 354 | 368 |
| 355 FOR_DIAGNOSTICS_OBSERVERS( | 369 FOR_DIAGNOSTICS_OBSERVERS( |
| 356 NotifyTokenReceivedSuccess(GaiaConstants::kGaiaOAuth2LoginAccessToken, | 370 NotifyTokenReceivedSuccess(GaiaConstants::kGaiaOAuth2LoginAccessToken, |
| 357 result.access_token, true)); | 371 result.access_token, true)); |
| 358 FOR_DIAGNOSTICS_OBSERVERS( | 372 FOR_DIAGNOSTICS_OBSERVERS( |
| 359 NotifyTokenReceivedSuccess(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 373 NotifyTokenReceivedSuccess(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 360 result.refresh_token, true)); | 374 result.refresh_token, true)); |
| 361 | 375 |
| 362 FireTokenAvailableNotification(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 376 FireTokenAvailableNotification(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 | 485 |
| 472 void TokenService::AddSigninDiagnosticsObserver( | 486 void TokenService::AddSigninDiagnosticsObserver( |
| 473 SigninDiagnosticsObserver* observer) { | 487 SigninDiagnosticsObserver* observer) { |
| 474 signin_diagnostics_observers_.AddObserver(observer); | 488 signin_diagnostics_observers_.AddObserver(observer); |
| 475 } | 489 } |
| 476 | 490 |
| 477 void TokenService::RemoveSigninDiagnosticsObserver( | 491 void TokenService::RemoveSigninDiagnosticsObserver( |
| 478 SigninDiagnosticsObserver* observer) { | 492 SigninDiagnosticsObserver* observer) { |
| 479 signin_diagnostics_observers_.RemoveObserver(observer); | 493 signin_diagnostics_observers_.RemoveObserver(observer); |
| 480 } | 494 } |
| OLD | NEW |