| 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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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()); | 673 const std::string primary_account_id = GetPrimaryAccountId(GetProfile()); |
| 649 #if defined(OS_CHROMEOS) | 674 #if defined(OS_CHROMEOS) |
| 650 if (chrome::IsRunningInForcedAppMode()) { | 675 if (chrome::IsRunningInForcedAppMode()) { |
| 651 std::string app_client_id; | 676 std::string app_client_id; |
| 652 std::string app_client_secret; | 677 std::string app_client_secret; |
| 653 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( | 678 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( |
| 654 &app_client_id, &app_client_secret)) { | 679 &app_client_id, &app_client_secret)) { |
| 680 // TODO(courage): figure out what account should be here |
| 655 login_token_request_ = | 681 login_token_request_ = |
| 656 service->StartRequestForClient(primary_account_id, | 682 service->StartRequestForClient(primary_account_id, |
| 657 app_client_id, | 683 app_client_id, |
| 658 app_client_secret, | 684 app_client_secret, |
| 659 OAuth2TokenService::ScopeSet(), | 685 OAuth2TokenService::ScopeSet(), |
| 660 this); | 686 this); |
| 661 return; | 687 return; |
| 662 } | 688 } |
| 663 } | 689 } |
| 664 #endif | 690 #endif |
| 665 login_token_request_ = service->StartRequest( | 691 login_token_request_ = service->StartRequest( |
| 666 primary_account_id, OAuth2TokenService::ScopeSet(), this); | 692 token_key_->account_id, OAuth2TokenService::ScopeSet(), this); |
| 667 } | 693 } |
| 668 | 694 |
| 669 void IdentityGetAuthTokenFunction::StartGaiaRequest( | 695 void IdentityGetAuthTokenFunction::StartGaiaRequest( |
| 670 const std::string& login_access_token) { | 696 const std::string& login_access_token) { |
| 671 DCHECK(!login_access_token.empty()); | 697 DCHECK(!login_access_token.empty()); |
| 672 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); | 698 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); |
| 673 mint_token_flow_->Start(); | 699 mint_token_flow_->Start(); |
| 674 } | 700 } |
| 675 | 701 |
| 676 void IdentityGetAuthTokenFunction::ShowLoginPopup() { | 702 void IdentityGetAuthTokenFunction::ShowLoginPopup() { |
| 677 signin_flow_.reset(new IdentitySigninFlow(this, GetProfile())); | 703 signin_flow_.reset(new IdentitySigninFlow(this, GetProfile())); |
| 678 signin_flow_->Start(); | 704 signin_flow_->Start(); |
| 679 } | 705 } |
| 680 | 706 |
| 681 void IdentityGetAuthTokenFunction::ShowOAuthApprovalDialog( | 707 void IdentityGetAuthTokenFunction::ShowOAuthApprovalDialog( |
| 682 const IssueAdviceInfo& issue_advice) { | 708 const IssueAdviceInfo& issue_advice) { |
| 683 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 709 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
| 684 const std::string locale = g_browser_process->local_state()->GetString( | 710 const std::string locale = g_browser_process->local_state()->GetString( |
| 685 prefs::kApplicationLocale); | 711 prefs::kApplicationLocale); |
| 686 | 712 |
| 687 gaia_web_auth_flow_.reset(new GaiaWebAuthFlow( | 713 gaia_web_auth_flow_.reset(new GaiaWebAuthFlow(this, |
| 688 this, GetProfile(), GetExtension()->id(), oauth2_info, locale)); | 714 GetProfile(), |
| 715 token_key_->account_id, |
| 716 GetExtension()->id(), |
| 717 oauth2_info, |
| 718 locale)); |
| 689 gaia_web_auth_flow_->Start(); | 719 gaia_web_auth_flow_->Start(); |
| 690 } | 720 } |
| 691 | 721 |
| 692 OAuth2MintTokenFlow* IdentityGetAuthTokenFunction::CreateMintTokenFlow( | 722 OAuth2MintTokenFlow* IdentityGetAuthTokenFunction::CreateMintTokenFlow( |
| 693 const std::string& login_access_token) { | 723 const std::string& login_access_token) { |
| 694 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 724 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
| 695 | 725 |
| 696 OAuth2MintTokenFlow* mint_token_flow = new OAuth2MintTokenFlow( | 726 OAuth2MintTokenFlow* mint_token_flow = new OAuth2MintTokenFlow( |
| 697 GetProfile()->GetRequestContext(), | 727 GetProfile()->GetRequestContext(), |
| 698 this, | 728 this, |
| 699 OAuth2MintTokenFlow::Parameters(login_access_token, | 729 OAuth2MintTokenFlow::Parameters(login_access_token, |
| 700 GetExtension()->id(), | 730 GetExtension()->id(), |
| 701 oauth2_client_id_, | 731 oauth2_client_id_, |
| 702 oauth2_info.scopes, | 732 oauth2_info.scopes, |
| 703 gaia_mint_token_mode_)); | 733 gaia_mint_token_mode_)); |
| 704 return mint_token_flow; | 734 return mint_token_flow; |
| 705 } | 735 } |
| 706 | 736 |
| 707 bool IdentityGetAuthTokenFunction::HasLoginToken() const { | 737 bool IdentityGetAuthTokenFunction::HasLoginToken() const { |
| 708 ProfileOAuth2TokenService* token_service = | 738 ProfileOAuth2TokenService* token_service = |
| 709 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 739 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); |
| 710 return token_service->RefreshTokenIsAvailable( | 740 return token_service->RefreshTokenIsAvailable(token_key_->account_id); |
| 711 GetPrimaryAccountId(GetProfile())); | |
| 712 } | 741 } |
| 713 | 742 |
| 714 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( | 743 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( |
| 715 const std::string& error) { | 744 const std::string& error) { |
| 716 const char kOAuth2ErrorAccessDenied[] = "access_denied"; | 745 const char kOAuth2ErrorAccessDenied[] = "access_denied"; |
| 717 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; | 746 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; |
| 718 | 747 |
| 719 if (error == kOAuth2ErrorAccessDenied) | 748 if (error == kOAuth2ErrorAccessDenied) |
| 720 return std::string(identity_constants::kUserRejected); | 749 return std::string(identity_constants::kUserRejected); |
| 721 else if (error == kOAuth2ErrorInvalidScope) | 750 else if (error == kOAuth2ErrorInvalidScope) |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( | 857 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( |
| 829 const GURL& redirect_url) { | 858 const GURL& redirect_url) { |
| 830 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { | 859 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { |
| 831 SetResult(new base::StringValue(redirect_url.spec())); | 860 SetResult(new base::StringValue(redirect_url.spec())); |
| 832 SendResponse(true); | 861 SendResponse(true); |
| 833 Release(); // Balanced in RunAsync. | 862 Release(); // Balanced in RunAsync. |
| 834 } | 863 } |
| 835 } | 864 } |
| 836 | 865 |
| 837 } // namespace extensions | 866 } // namespace extensions |
| OLD | NEW |