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> |
11 | 11 |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/app_mode/app_mode_utils.h" | 17 #include "chrome/browser/app_mode/app_mode_utils.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
20 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 20 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
21 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 23 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
25 #include "chrome/browser/signin/signin_global_error.h" | 25 #include "chrome/browser/signin/signin_global_error.h" |
| 26 #include "chrome/browser/signin/signin_manager.h" |
| 27 #include "chrome/browser/signin/signin_manager_factory.h" |
26 #include "chrome/common/extensions/api/identity.h" | 28 #include "chrome/common/extensions/api/identity.h" |
27 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" | 29 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" |
28 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
29 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
30 #include "extensions/browser/event_router.h" | 32 #include "extensions/browser/event_router.h" |
31 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
32 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
33 #include "google_apis/gaia/gaia_urls.h" | 35 #include "google_apis/gaia/gaia_urls.h" |
34 #include "url/gurl.h" | 36 #include "url/gurl.h" |
35 | 37 |
(...skipping 20 matching lines...) Expand all Loading... |
56 const char kPageLoadFailure[] = "Authorization page could not be loaded."; | 58 const char kPageLoadFailure[] = "Authorization page could not be loaded."; |
57 | 59 |
58 const int kCachedIssueAdviceTTLSeconds = 1; | 60 const int kCachedIssueAdviceTTLSeconds = 1; |
59 } // namespace identity_constants | 61 } // namespace identity_constants |
60 | 62 |
61 namespace { | 63 namespace { |
62 | 64 |
63 static const char kChromiumDomainRedirectUrlPattern[] = | 65 static const char kChromiumDomainRedirectUrlPattern[] = |
64 "https://%s.chromiumapp.org/"; | 66 "https://%s.chromiumapp.org/"; |
65 | 67 |
| 68 std::string GetPrimaryAccountId(Profile* profile) { |
| 69 SigninManagerBase* signin_manager = |
| 70 SigninManagerFactory::GetForProfile(profile); |
| 71 return signin_manager->GetAuthenticatedAccountId(); |
| 72 } |
| 73 |
66 } // namespace | 74 } // namespace |
67 | 75 |
68 namespace identity = api::identity; | 76 namespace identity = api::identity; |
69 | 77 |
70 IdentityGetAuthTokenFunction::IdentityGetAuthTokenFunction() | 78 IdentityGetAuthTokenFunction::IdentityGetAuthTokenFunction() |
71 : OAuth2TokenService::Consumer("extensions_identity_api"), | 79 : OAuth2TokenService::Consumer("extensions_identity_api"), |
72 should_prompt_for_scopes_(false), | 80 should_prompt_for_scopes_(false), |
73 should_prompt_for_signin_(false) {} | 81 should_prompt_for_signin_(false) {} |
74 | 82 |
75 IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {} | 83 IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {} |
(...skipping 21 matching lines...) Expand all Loading... |
97 if (oauth2_client_id_.empty()) { | 105 if (oauth2_client_id_.empty()) { |
98 error_ = identity_constants::kInvalidClientId; | 106 error_ = identity_constants::kInvalidClientId; |
99 return false; | 107 return false; |
100 } | 108 } |
101 | 109 |
102 if (oauth2_info.scopes.size() == 0) { | 110 if (oauth2_info.scopes.size() == 0) { |
103 error_ = identity_constants::kInvalidScopes; | 111 error_ = identity_constants::kInvalidScopes; |
104 return false; | 112 return false; |
105 } | 113 } |
106 | 114 |
107 ProfileOAuth2TokenService* token_service = | |
108 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | |
109 | |
110 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 115 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
111 oauth2_info.scopes.end()); | 116 oauth2_info.scopes.end()); |
112 token_key_.reset(new ExtensionTokenKey( | 117 token_key_.reset(new ExtensionTokenKey( |
113 GetExtension()->id(), token_service->GetPrimaryAccountId(), scopes)); | 118 GetExtension()->id(), GetPrimaryAccountId(GetProfile()), scopes)); |
114 | 119 |
115 // Balanced in CompleteFunctionWithResult|CompleteFunctionWithError | 120 // Balanced in CompleteFunctionWithResult|CompleteFunctionWithError |
116 AddRef(); | 121 AddRef(); |
117 | 122 |
118 #if defined(OS_CHROMEOS) | 123 #if defined(OS_CHROMEOS) |
119 policy::BrowserPolicyConnectorChromeOS* connector = | 124 policy::BrowserPolicyConnectorChromeOS* connector = |
120 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 125 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
121 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp() && | 126 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp() && |
122 connector->IsEnterpriseManaged()) { | 127 connector->IsEnterpriseManaged()) { |
123 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE); | 128 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE); |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 login_token_request_ = | 426 login_token_request_ = |
422 service->StartRequest(service->GetRobotAccountId(), | 427 service->StartRequest(service->GetRobotAccountId(), |
423 scopes, | 428 scopes, |
424 this); | 429 this); |
425 } | 430 } |
426 #endif | 431 #endif |
427 | 432 |
428 void IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest() { | 433 void IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest() { |
429 ProfileOAuth2TokenService* service = | 434 ProfileOAuth2TokenService* service = |
430 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 435 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); |
| 436 const std::string primary_account_id = GetPrimaryAccountId(GetProfile()); |
431 #if defined(OS_CHROMEOS) | 437 #if defined(OS_CHROMEOS) |
432 if (chrome::IsRunningInForcedAppMode()) { | 438 if (chrome::IsRunningInForcedAppMode()) { |
433 std::string app_client_id; | 439 std::string app_client_id; |
434 std::string app_client_secret; | 440 std::string app_client_secret; |
435 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( | 441 if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo( |
436 &app_client_id, &app_client_secret)) { | 442 &app_client_id, &app_client_secret)) { |
437 login_token_request_ = | 443 login_token_request_ = |
438 service->StartRequestForClient(service->GetPrimaryAccountId(), | 444 service->StartRequestForClient(primary_account_id, |
439 app_client_id, | 445 app_client_id, |
440 app_client_secret, | 446 app_client_secret, |
441 OAuth2TokenService::ScopeSet(), | 447 OAuth2TokenService::ScopeSet(), |
442 this); | 448 this); |
443 return; | 449 return; |
444 } | 450 } |
445 } | 451 } |
446 #endif | 452 #endif |
447 login_token_request_ = service->StartRequest( | 453 login_token_request_ = service->StartRequest( |
448 service->GetPrimaryAccountId(), OAuth2TokenService::ScopeSet(), this); | 454 primary_account_id, OAuth2TokenService::ScopeSet(), this); |
449 } | 455 } |
450 | 456 |
451 void IdentityGetAuthTokenFunction::StartGaiaRequest( | 457 void IdentityGetAuthTokenFunction::StartGaiaRequest( |
452 const std::string& login_access_token) { | 458 const std::string& login_access_token) { |
453 DCHECK(!login_access_token.empty()); | 459 DCHECK(!login_access_token.empty()); |
454 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); | 460 mint_token_flow_.reset(CreateMintTokenFlow(login_access_token)); |
455 mint_token_flow_->Start(); | 461 mint_token_flow_->Start(); |
456 } | 462 } |
457 | 463 |
458 void IdentityGetAuthTokenFunction::ShowLoginPopup() { | 464 void IdentityGetAuthTokenFunction::ShowLoginPopup() { |
(...skipping 24 matching lines...) Expand all Loading... |
483 oauth2_client_id_, | 489 oauth2_client_id_, |
484 oauth2_info.scopes, | 490 oauth2_info.scopes, |
485 gaia_mint_token_mode_)); | 491 gaia_mint_token_mode_)); |
486 return mint_token_flow; | 492 return mint_token_flow; |
487 } | 493 } |
488 | 494 |
489 bool IdentityGetAuthTokenFunction::HasLoginToken() const { | 495 bool IdentityGetAuthTokenFunction::HasLoginToken() const { |
490 ProfileOAuth2TokenService* token_service = | 496 ProfileOAuth2TokenService* token_service = |
491 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 497 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); |
492 return token_service->RefreshTokenIsAvailable( | 498 return token_service->RefreshTokenIsAvailable( |
493 token_service->GetPrimaryAccountId()); | 499 GetPrimaryAccountId(GetProfile())); |
494 } | 500 } |
495 | 501 |
496 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( | 502 std::string IdentityGetAuthTokenFunction::MapOAuth2ErrorToDescription( |
497 const std::string& error) { | 503 const std::string& error) { |
498 const char kOAuth2ErrorAccessDenied[] = "access_denied"; | 504 const char kOAuth2ErrorAccessDenied[] = "access_denied"; |
499 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; | 505 const char kOAuth2ErrorInvalidScope[] = "invalid_scope"; |
500 | 506 |
501 if (error == kOAuth2ErrorAccessDenied) | 507 if (error == kOAuth2ErrorAccessDenied) |
502 return std::string(identity_constants::kUserRejected); | 508 return std::string(identity_constants::kUserRejected); |
503 else if (error == kOAuth2ErrorInvalidScope) | 509 else if (error == kOAuth2ErrorInvalidScope) |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken( | 718 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken( |
713 const ExtensionTokenKey& key) { | 719 const ExtensionTokenKey& key) { |
714 return token_cache_[key]; | 720 return token_cache_[key]; |
715 } | 721 } |
716 | 722 |
717 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { | 723 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { |
718 return token_cache_; | 724 return token_cache_; |
719 } | 725 } |
720 | 726 |
721 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { | 727 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { |
722 ProfileOAuth2TokenService* token_service = | 728 account_tracker_.ReportAuthError(GetPrimaryAccountId(profile_), error); |
723 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
724 account_tracker_.ReportAuthError(token_service->GetPrimaryAccountId(), error); | |
725 } | 729 } |
726 | 730 |
727 void IdentityAPI::Shutdown() { | 731 void IdentityAPI::Shutdown() { |
728 account_tracker_.RemoveObserver(this); | 732 account_tracker_.RemoveObserver(this); |
729 account_tracker_.Shutdown(); | 733 account_tracker_.Shutdown(); |
730 } | 734 } |
731 | 735 |
732 static base::LazyInstance<ProfileKeyedAPIFactory<IdentityAPI> > | 736 static base::LazyInstance<ProfileKeyedAPIFactory<IdentityAPI> > |
733 g_factory = LAZY_INSTANCE_INITIALIZER; | 737 g_factory = LAZY_INSTANCE_INITIALIZER; |
734 | 738 |
(...skipping 19 matching lines...) Expand all Loading... |
754 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 758 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); |
755 } | 759 } |
756 | 760 |
757 template <> | 761 template <> |
758 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { | 762 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { |
759 DependsOn(ExtensionSystemFactory::GetInstance()); | 763 DependsOn(ExtensionSystemFactory::GetInstance()); |
760 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 764 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
761 } | 765 } |
762 | 766 |
763 } // namespace extensions | 767 } // namespace extensions |
OLD | NEW |