OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/signin/dice_response_handler.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 10 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 13 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| 14 #include "components/signin/core/browser/account_tracker_service.h" |
| 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 16 #include "components/signin/core/browser/signin_client.h" |
| 17 #include "components/signin/core/browser/signin_header_helper.h" |
| 18 #include "components/signin/core/common/profile_management_switches.h" |
| 19 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 20 #include "google_apis/gaia/gaia_constants.h" |
| 21 #include "google_apis/gaia/google_service_auth_error.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 class DiceResponseHandlerFactory : public BrowserContextKeyedServiceFactory { |
| 26 public: |
| 27 // Returns an instance of the factory singleton. |
| 28 static DiceResponseHandlerFactory* GetInstance() { |
| 29 return base::Singleton<DiceResponseHandlerFactory>::get(); |
| 30 } |
| 31 |
| 32 static DiceResponseHandler* GetForProfile(Profile* profile) { |
| 33 return static_cast<DiceResponseHandler*>( |
| 34 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 35 } |
| 36 |
| 37 private: |
| 38 friend struct base::DefaultSingletonTraits<DiceResponseHandlerFactory>; |
| 39 |
| 40 DiceResponseHandlerFactory() |
| 41 : BrowserContextKeyedServiceFactory( |
| 42 "DiceResponseHandler", |
| 43 BrowserContextDependencyManager::GetInstance()) { |
| 44 DependsOn(AccountTrackerServiceFactory::GetInstance()); |
| 45 DependsOn(ChromeSigninClientFactory::GetInstance()); |
| 46 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 47 } |
| 48 |
| 49 ~DiceResponseHandlerFactory() override {} |
| 50 |
| 51 // BrowserContextKeyedServiceFactory: |
| 52 KeyedService* BuildServiceInstanceFor( |
| 53 content::BrowserContext* context) const override { |
| 54 if (context->IsOffTheRecord()) |
| 55 return nullptr; |
| 56 |
| 57 Profile* profile = static_cast<Profile*>(context); |
| 58 return new DiceResponseHandler( |
| 59 ChromeSigninClientFactory::GetForProfile(profile), |
| 60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 61 AccountTrackerServiceFactory::GetForProfile(profile)); |
| 62 } |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 // static |
| 68 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) { |
| 69 return DiceResponseHandlerFactory::GetForProfile(profile); |
| 70 } |
| 71 |
| 72 DiceResponseHandler::DiceResponseHandler( |
| 73 SigninClient* signin_client, |
| 74 ProfileOAuth2TokenService* profile_oauth2_token_service, |
| 75 AccountTrackerService* account_tracker_service) |
| 76 : signin_client_(signin_client), |
| 77 token_service_(profile_oauth2_token_service), |
| 78 account_tracker_service_(account_tracker_service) { |
| 79 DCHECK(signin_client_); |
| 80 DCHECK(token_service_); |
| 81 DCHECK(account_tracker_service_); |
| 82 } |
| 83 |
| 84 DiceResponseHandler::~DiceResponseHandler() {} |
| 85 |
| 86 void DiceResponseHandler::ProcessDiceHeader( |
| 87 const signin::DiceResponseParams& dice_params) { |
| 88 DCHECK_EQ(switches::AccountConsistencyMethod::kDice, |
| 89 switches::GetAccountConsistencyMethod()); |
| 90 |
| 91 switch (dice_params.user_intention) { |
| 92 case signin::DiceAction::SIGNIN: |
| 93 ProcessDiceSigninHeader(dice_params.obfuscated_gaia_id, dice_params.email, |
| 94 dice_params.authorization_code); |
| 95 return; |
| 96 case signin::DiceAction::SIGNOUT: |
| 97 case signin::DiceAction::SINGLE_SESSION_SIGNOUT: |
| 98 LOG(ERROR) << "Signout through Dice is not implemented."; |
| 99 return; |
| 100 case signin::DiceAction::NONE: |
| 101 NOTREACHED() << "Invalid Dice response parameters."; |
| 102 return; |
| 103 } |
| 104 |
| 105 NOTREACHED(); |
| 106 return; |
| 107 } |
| 108 |
| 109 void DiceResponseHandler::ProcessDiceSigninHeader( |
| 110 const std::string& gaia_id, |
| 111 const std::string& email, |
| 112 const std::string& authorization_code) { |
| 113 DCHECK(!gaia_id.empty()); |
| 114 DCHECK(!email.empty()); |
| 115 DCHECK(!authorization_code.empty()); |
| 116 DCHECK(!gaia_auth_fetcher_); |
| 117 DCHECK(gaia_id_.empty()); |
| 118 DCHECK(email_.empty()); |
| 119 gaia_id_ = gaia_id; |
| 120 email_ = email; |
| 121 gaia_auth_fetcher_ = signin_client_->CreateGaiaAuthFetcher( |
| 122 this, GaiaConstants::kChromeSource, |
| 123 signin_client_->GetURLRequestContext()); |
| 124 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code); |
| 125 |
| 126 // TODO(droger): The token exchange must complete quickly or be cancelled. Add |
| 127 // a timeout logic. |
| 128 } |
| 129 |
| 130 void DiceResponseHandler::OnClientOAuthSuccess( |
| 131 const ClientOAuthResult& result) { |
| 132 std::string account_id = |
| 133 account_tracker_service_->SeedAccountInfo(gaia_id_, email_); |
| 134 VLOG(1) << "Dice OAuth success for account: " << account_id; |
| 135 token_service_->UpdateCredentials(account_id, result.refresh_token); |
| 136 gaia_id_.clear(); |
| 137 email_.clear(); |
| 138 gaia_auth_fetcher_.reset(); |
| 139 } |
| 140 |
| 141 void DiceResponseHandler::OnClientOAuthFailure( |
| 142 const GoogleServiceAuthError& error) { |
| 143 // TODO(droger): Handle authentication errors. |
| 144 VLOG(1) << "Dice OAuth failed with error: " << error.ToString(); |
| 145 gaia_id_.clear(); |
| 146 email_.clear(); |
| 147 gaia_auth_fetcher_.reset(); |
| 148 } |
OLD | NEW |