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/chrome_signin_client_factory.h" | |
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
12 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" | |
13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
14 #include "components/signin/core/browser/signin_client.h" | |
15 #include "components/signin/core/browser/signin_header_helper.h" | |
16 #include "components/signin/core/common/profile_management_switches.h" | |
17 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
18 #include "google_apis/gaia/gaia_constants.h" | |
19 #include "google_apis/gaia/google_service_auth_error.h" | |
20 #include "google_apis/gaia/oauth2_token_service_delegate.h" | |
21 | |
22 namespace { | |
23 | |
24 class DiceResponseHandlerFactory : public BrowserContextKeyedServiceFactory { | |
25 public: | |
26 // Returns an instance of the factory singleton. | |
27 static DiceResponseHandlerFactory* GetInstance() { | |
28 return base::Singleton<DiceResponseHandlerFactory>::get(); | |
29 } | |
30 | |
31 static DiceResponseHandler* GetForProfile(Profile* profile) { | |
32 return static_cast<DiceResponseHandler*>( | |
33 GetInstance()->GetServiceForBrowserContext(profile, true)); | |
34 } | |
35 | |
36 private: | |
37 friend struct base::DefaultSingletonTraits<DiceResponseHandlerFactory>; | |
38 | |
39 DiceResponseHandlerFactory() | |
40 : BrowserContextKeyedServiceFactory( | |
41 "DiceResponseHandler", | |
42 BrowserContextDependencyManager::GetInstance()) { | |
43 DependsOn(ChromeSigninClientFactory::GetInstance()); | |
44 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | |
45 } | |
46 | |
47 ~DiceResponseHandlerFactory() override {} | |
48 | |
49 // BrowserContextKeyedServiceFactory: | |
50 KeyedService* BuildServiceInstanceFor( | |
51 content::BrowserContext* context) const override { | |
52 if (context->IsOffTheRecord()) | |
53 return nullptr; | |
54 | |
55 Profile* profile = static_cast<Profile*>(context); | |
56 return new DiceResponseHandler( | |
57 ChromeSigninClientFactory::GetForProfile(profile), | |
58 ProfileOAuth2TokenServiceFactory::GetForProfile(profile) | |
59 ->GetDelegate()); | |
60 } | |
61 }; | |
62 | |
63 } // namespace | |
64 | |
65 // static | |
66 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) { | |
67 return DiceResponseHandlerFactory::GetForProfile(profile); | |
68 } | |
69 | |
70 DiceResponseHandler::DiceResponseHandler( | |
71 SigninClient* signin_client, | |
72 OAuth2TokenServiceDelegate* oauth2_token_service_delegate) | |
73 : signin_client_(signin_client), | |
74 oauth2_token_service_delegate_(oauth2_token_service_delegate) { | |
75 DCHECK(signin_client_); | |
76 DCHECK(oauth2_token_service_delegate_); | |
77 } | |
78 | |
79 DiceResponseHandler::~DiceResponseHandler() {} | |
80 | |
81 void DiceResponseHandler::ProcessDiceHeader( | |
82 const signin::DiceResponseParams& dice_params) { | |
83 DCHECK_EQ(switches::AccountConsistencyMethod::kDice, | |
84 switches::GetAccountConsistencyMethod()); | |
85 | |
86 switch (dice_params.user_intention) { | |
87 case signin::DiceAction::SIGNIN: | |
msarda
2017/06/20 08:45:22
In Chrome there are multiple notions of account id
droger
2017/06/20 11:01:27
As discussed offline, I did not use PickAccountIdF
| |
88 ProcessDiceSigninHeader(dice_params.email, | |
89 dice_params.authorization_code); | |
90 return; | |
91 case signin::DiceAction::SIGNOUT: | |
92 case signin::DiceAction::SINGLE_SESSION_SIGNOUT: | |
93 NOTIMPLEMENTED() << "Signout through Dice is not implemented."; | |
msarda
2017/06/20 08:45:22
I think this should not be NOTIMPLEMENTED as the s
droger
2017/06/20 11:01:27
Done.
| |
94 return; | |
95 case signin::DiceAction::NONE: | |
96 NOTREACHED() << "Invalid Dice response parameters."; | |
97 return; | |
98 } | |
99 | |
100 NOTREACHED(); | |
101 return; | |
102 } | |
103 | |
104 void DiceResponseHandler::ProcessDiceSigninHeader( | |
105 const std::string& account_id, | |
106 const std::string& authorization_code) { | |
107 DCHECK(!gaia_auth_fetcher_); | |
108 DCHECK(account_id_for_signin_.empty()); | |
109 account_id_for_signin_ = account_id; | |
110 gaia_auth_fetcher_ = signin_client_->CreateGaiaAuthFetcher( | |
111 this, GaiaConstants::kChromeSource, | |
112 signin_client_->GetURLRequestContext()); | |
113 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code); | |
114 | |
115 // TODO(droger): The token exchange must complete quicly or be cancelled. Add | |
msarda
2017/06/20 08:45:23
s/quicly/quickly
droger
2017/06/20 11:01:27
Done.
| |
116 // a timeout logic. | |
117 } | |
118 | |
119 void DiceResponseHandler::OnClientOAuthSuccess( | |
120 const ClientOAuthResult& result) { | |
121 oauth2_token_service_delegate_->UpdateCredentials(account_id_for_signin_, | |
msarda
2017/06/20 08:45:22
Use the token service (not its delegate) to update
msarda
2017/06/20 08:45:22
Before calling UpdateCredentials, please also seed
droger
2017/06/20 11:01:28
Done.
| |
122 result.refresh_token); | |
123 account_id_for_signin_.clear(); | |
124 gaia_auth_fetcher_.reset(); | |
125 } | |
126 | |
127 void DiceResponseHandler::OnClientOAuthFailure( | |
128 const GoogleServiceAuthError& error) { | |
129 // TODO(droger): Handle authentication errors. | |
130 DLOG(ERROR) << "Dice OAuth failed: " << error.error_message(); | |
131 account_id_for_signin_.clear(); | |
132 gaia_auth_fetcher_.reset(); | |
133 } | |
OLD | NEW |