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/extensions/api/identity/identity_api.h" | 5 #include "chrome/browser/extensions/api/identity/identity_api.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken( | 162 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken( |
163 const ExtensionTokenKey& key) { | 163 const ExtensionTokenKey& key) { |
164 return token_cache_[key]; | 164 return token_cache_[key]; |
165 } | 165 } |
166 | 166 |
167 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { | 167 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { |
168 return token_cache_; | 168 return token_cache_; |
169 } | 169 } |
170 | 170 |
171 std::vector<std::string> IdentityAPI::GetAccounts() const { | 171 std::vector<std::string> IdentityAPI::GetAccounts() const { |
| 172 const std::string primary_account_id = GetPrimaryAccountId(browser_context_); |
172 const std::vector<AccountIds> ids = account_tracker_.GetAccounts(); | 173 const std::vector<AccountIds> ids = account_tracker_.GetAccounts(); |
173 std::vector<std::string> gaia_ids; | 174 std::vector<std::string> gaia_ids; |
174 | 175 |
175 if (switches::IsExtensionsMultiAccount()) { | 176 if (switches::IsExtensionsMultiAccount()) { |
176 for (std::vector<AccountIds>::const_iterator it = ids.begin(); | 177 for (std::vector<AccountIds>::const_iterator it = ids.begin(); |
177 it != ids.end(); | 178 it != ids.end(); |
178 ++it) { | 179 ++it) { |
179 gaia_ids.push_back(it->gaia); | 180 gaia_ids.push_back(it->gaia); |
180 } | 181 } |
181 } else if (ids.size() >= 1) { | 182 } else if (ids.size() >= 1) { |
182 gaia_ids.push_back(ids[0].gaia); | 183 gaia_ids.push_back(ids[0].gaia); |
183 } | 184 } |
184 | 185 |
185 return gaia_ids; | 186 return gaia_ids; |
186 } | 187 } |
187 | 188 |
| 189 std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { |
| 190 return account_tracker_.FindAccountKeyByGaiaId(gaia_id); |
| 191 } |
| 192 |
188 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { | 193 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { |
189 account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_), | 194 account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_), |
190 error); | 195 error); |
191 } | 196 } |
192 | 197 |
193 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { | 198 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { |
194 return account_tracker_.GetAuthStatus(); | 199 return account_tracker_.GetAuthStatus(); |
195 } | 200 } |
196 | 201 |
197 void IdentityAPI::Shutdown() { | 202 void IdentityAPI::Shutdown() { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 return false; | 315 return false; |
311 } | 316 } |
312 | 317 |
313 if (oauth2_info.scopes.size() == 0) { | 318 if (oauth2_info.scopes.size() == 0) { |
314 error_ = identity_constants::kInvalidScopes; | 319 error_ = identity_constants::kInvalidScopes; |
315 return false; | 320 return false; |
316 } | 321 } |
317 | 322 |
318 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 323 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
319 oauth2_info.scopes.end()); | 324 oauth2_info.scopes.end()); |
320 token_key_.reset(new ExtensionTokenKey( | 325 |
321 GetExtension()->id(), GetPrimaryAccountId(GetProfile()), scopes)); | 326 std::string account_key = GetPrimaryAccountId(GetProfile()); |
| 327 |
| 328 if (params->details->account.get()) { |
| 329 std::string detail_key = |
| 330 extensions::IdentityAPI::GetFactoryInstance() |
| 331 ->Get(GetProfile()) |
| 332 ->FindAccountKeyByGaiaId(params->details->account->id); |
| 333 |
| 334 if (detail_key != account_key) { |
| 335 if (detail_key.empty() || !switches::IsExtensionsMultiAccount()) { |
| 336 // TODO(courage): should this be a different error? |
| 337 error_ = identity_constants::kUserNotSignedIn; |
| 338 return false; |
| 339 } |
| 340 |
| 341 account_key = detail_key; |
| 342 } |
| 343 } |
| 344 |
| 345 token_key_.reset( |
| 346 new ExtensionTokenKey(GetExtension()->id(), account_key, scopes)); |
322 | 347 |
323 // From here on out, results must be returned asynchronously. | 348 // From here on out, results must be returned asynchronously. |
324 StartAsyncRun(); | 349 StartAsyncRun(); |
325 | 350 |
326 #if defined(OS_CHROMEOS) | 351 #if defined(OS_CHROMEOS) |
327 policy::BrowserPolicyConnectorChromeOS* connector = | 352 policy::BrowserPolicyConnectorChromeOS* connector = |
328 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 353 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
329 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp() && | 354 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp() && |
330 connector->IsEnterpriseManaged()) { | 355 connector->IsEnterpriseManaged()) { |
331 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE); | 356 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 login_token_request_ = | 663 login_token_request_ = |
639 service->StartRequest(service->GetRobotAccountId(), | 664 service->StartRequest(service->GetRobotAccountId(), |
640 scopes, | 665 scopes, |
641 this); | 666 this); |
642 } | 667 } |
643 #endif | 668 #endif |
644 | 669 |
645 void IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest() { | 670 void IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest() { |
646 ProfileOAuth2TokenService* service = | 671 ProfileOAuth2TokenService* service = |
647 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 672 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); |
648 const std::string primary_account_id = GetPrimaryAccountId(GetProfile()); | |
649 #if defined(OS_CHROMEOS) | 673 #if defined(OS_CHROMEOS) |
650 if (chrome::IsRunningInForcedAppMode()) { | 674 if (chrome::IsRunningInForcedAppMode()) { |
651 std::string app_client_id; | 675 std::string app_client_id; |
652 std::string app_client_secret; | 676 std::string app_client_secret; |
653 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( | 677 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( |
654 &app_client_id, &app_client_secret)) { | 678 &app_client_id, &app_client_secret)) { |
655 login_token_request_ = | 679 login_token_request_ = |
656 service->StartRequestForClient(primary_account_id, | 680 service->StartRequestForClient(token_key_->account_id, |
657 app_client_id, | 681 app_client_id, |
658 app_client_secret, | 682 app_client_secret, |
659 OAuth2TokenService::ScopeSet(), | 683 OAuth2TokenService::ScopeSet(), |
660 this); | 684 this); |
661 return; | 685 return; |
662 } | 686 } |
663 } | 687 } |
664 #endif | 688 #endif |
665 login_token_request_ = service->StartRequest( | 689 login_token_request_ = service->StartRequest( |
666 primary_account_id, OAuth2TokenService::ScopeSet(), this); | 690 token_key_->account_id, OAuth2TokenService::ScopeSet(), this); |
667 } | 691 } |
668 | 692 |
669 void IdentityGetAuthTokenFunction::StartGaiaRequest( | 693 void IdentityGetAuthTokenFunction::StartGaiaRequest( |
670 const std::string& login_access_token) { | 694 const std::string& login_access_token) { |
671 DCHECK(!login_access_token.empty()); | 695 DCHECK(!login_access_token.empty()); |
672 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); | 696 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); |
673 mint_token_flow_->Start(); | 697 mint_token_flow_->Start(); |
674 } | 698 } |
675 | 699 |
676 void IdentityGetAuthTokenFunction::ShowLoginPopup() { | 700 void IdentityGetAuthTokenFunction::ShowLoginPopup() { |
677 signin_flow_.reset(new IdentitySigninFlow(this, GetProfile())); | 701 signin_flow_.reset(new IdentitySigninFlow(this, GetProfile())); |
678 signin_flow_->Start(); | 702 signin_flow_->Start(); |
679 } | 703 } |
680 | 704 |
681 void IdentityGetAuthTokenFunction::ShowOAuthApprovalDialog( | 705 void IdentityGetAuthTokenFunction::ShowOAuthApprovalDialog( |
682 const IssueAdviceInfo& issue_advice) { | 706 const IssueAdviceInfo& issue_advice) { |
683 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 707 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
684 const std::string locale = g_browser_process->local_state()->GetString( | 708 const std::string locale = g_browser_process->local_state()->GetString( |
685 prefs::kApplicationLocale); | 709 prefs::kApplicationLocale); |
686 | 710 |
687 gaia_web_auth_flow_.reset(new GaiaWebAuthFlow( | 711 gaia_web_auth_flow_.reset(new GaiaWebAuthFlow(this, |
688 this, GetProfile(), GetExtension()->id(), oauth2_info, locale)); | 712 GetProfile(), |
| 713 token_key_->account_id, |
| 714 GetExtension()->id(), |
| 715 oauth2_info, |
| 716 locale)); |
689 gaia_web_auth_flow_->Start(); | 717 gaia_web_auth_flow_->Start(); |
690 } | 718 } |
691 | 719 |
692 OAuth2MintTokenFlow* IdentityGetAuthTokenFunction::CreateMintTokenFlow( | 720 OAuth2MintTokenFlow* IdentityGetAuthTokenFunction::CreateMintTokenFlow( |
693 const std::string& login_access_token) { | 721 const std::string& login_access_token) { |
694 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 722 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
695 | 723 |
696 OAuth2MintTokenFlow* mint_token_flow = new OAuth2MintTokenFlow( | 724 OAuth2MintTokenFlow* mint_token_flow = new OAuth2MintTokenFlow( |
697 GetProfile()->GetRequestContext(), | 725 GetProfile()->GetRequestContext(), |
698 this, | 726 this, |
699 OAuth2MintTokenFlow::Parameters(login_access_token, | 727 OAuth2MintTokenFlow::Parameters(login_access_token, |
700 GetExtension()->id(), | 728 GetExtension()->id(), |
701 oauth2_client_id_, | 729 oauth2_client_id_, |
702 oauth2_info.scopes, | 730 oauth2_info.scopes, |
703 gaia_mint_token_mode_)); | 731 gaia_mint_token_mode_)); |
704 return mint_token_flow; | 732 return mint_token_flow; |
705 } | 733 } |
706 | 734 |
707 bool IdentityGetAuthTokenFunction::HasLoginToken() const { | 735 bool IdentityGetAuthTokenFunction::HasLoginToken() const { |
708 ProfileOAuth2TokenService* token_service = | 736 ProfileOAuth2TokenService* token_service = |
709 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 737 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); |
710 return token_service->RefreshTokenIsAvailable( | 738 return token_service->RefreshTokenIsAvailable(token_key_->account_id); |
711 GetPrimaryAccountId(GetProfile())); | |
712 } | 739 } |
713 | 740 |
714 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( | 741 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( |
715 const std::string& error) { | 742 const std::string& error) { |
716 const char kOAuth2ErrorAccessDenied[] = "access_denied"; | 743 const char kOAuth2ErrorAccessDenied[] = "access_denied"; |
717 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; | 744 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; |
718 | 745 |
719 if (error == kOAuth2ErrorAccessDenied) | 746 if (error == kOAuth2ErrorAccessDenied) |
720 return std::string(identity_constants::kUserRejected); | 747 return std::string(identity_constants::kUserRejected); |
721 else if (error == kOAuth2ErrorInvalidScope) | 748 else if (error == kOAuth2ErrorInvalidScope) |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( | 855 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( |
829 const GURL& redirect_url) { | 856 const GURL& redirect_url) { |
830 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { | 857 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { |
831 SetResult(new base::StringValue(redirect_url.spec())); | 858 SetResult(new base::StringValue(redirect_url.spec())); |
832 SendResponse(true); | 859 SendResponse(true); |
833 Release(); // Balanced in RunAsync. | 860 Release(); // Balanced in RunAsync. |
834 } | 861 } |
835 } | 862 } |
836 | 863 |
837 } // namespace extensions | 864 } // namespace extensions |
OLD | NEW |