Chromium Code Reviews| 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 profile_oauth2_token_service_(profile_oauth2_token_service), | |
| 78 account_tracker_service_(account_tracker_service) { | |
| 79 DCHECK(signin_client_); | |
| 80 DCHECK(profile_oauth2_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) { | |
|
msarda
2017/06/22 09:11:31
Let's DCHECK that gaia_id, email and authorization
droger
2017/06/22 12:39:07
Done.
| |
| 113 DCHECK(!gaia_auth_fetcher_); | |
| 114 DCHECK(gaia_id_.empty()); | |
| 115 DCHECK(email_.empty()); | |
| 116 gaia_id_ = gaia_id; | |
| 117 email_ = email; | |
| 118 gaia_auth_fetcher_ = signin_client_->CreateGaiaAuthFetcher( | |
| 119 this, GaiaConstants::kChromeSource, | |
| 120 signin_client_->GetURLRequestContext()); | |
| 121 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code); | |
| 122 | |
| 123 // TODO(droger): The token exchange must complete quickly or be cancelled. Add | |
| 124 // a timeout logic. | |
| 125 } | |
| 126 | |
| 127 void DiceResponseHandler::OnClientOAuthSuccess( | |
| 128 const ClientOAuthResult& result) { | |
|
msarda
2017/06/22 09:11:31
I would also add a VLOG(1) here.
droger
2017/06/22 12:39:07
Done.
| |
| 129 std::string account_id = | |
| 130 account_tracker_service_->SeedAccountInfo(gaia_id_, email_); | |
| 131 profile_oauth2_token_service_->UpdateCredentials(account_id, | |
| 132 result.refresh_token); | |
| 133 gaia_id_.clear(); | |
| 134 email_.clear(); | |
| 135 gaia_auth_fetcher_.reset(); | |
| 136 } | |
| 137 | |
| 138 void DiceResponseHandler::OnClientOAuthFailure( | |
| 139 const GoogleServiceAuthError& error) { | |
| 140 // TODO(droger): Handle authentication errors. | |
| 141 DLOG(ERROR) << "Dice OAuth failed: " << error.error_message(); | |
|
msarda
2017/06/22 09:11:31
Let's use VLOG in this case as it may be useful to
droger
2017/06/22 12:39:07
Done.
| |
| 142 gaia_id_.clear(); | |
| 143 email_.clear(); | |
| 144 gaia_auth_fetcher_.reset(); | |
| 145 } | |
| OLD | NEW |