OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/signin/dice_response_handler.h" | 5 #include "chrome/browser/signin/dice_response_handler.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/signin/account_tracker_service_factory.h" | 10 #include "chrome/browser/signin/account_tracker_service_factory.h" |
10 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 11 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 12 #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_dependency_manager.h" |
13 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" | 14 #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/account_tracker_service.h" |
15 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 16 #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_client.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 Profile* profile = static_cast<Profile*>(context); | 58 Profile* profile = static_cast<Profile*>(context); |
58 return new DiceResponseHandler( | 59 return new DiceResponseHandler( |
59 ChromeSigninClientFactory::GetForProfile(profile), | 60 ChromeSigninClientFactory::GetForProfile(profile), |
60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 61 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
61 AccountTrackerServiceFactory::GetForProfile(profile)); | 62 AccountTrackerServiceFactory::GetForProfile(profile)); |
62 } | 63 } |
63 }; | 64 }; |
64 | 65 |
65 } // namespace | 66 } // namespace |
66 | 67 |
| 68 //////////////////////////////////////////////////////////////////////////////// |
| 69 // DiceTokenFetcher |
| 70 //////////////////////////////////////////////////////////////////////////////// |
| 71 |
| 72 DiceResponseHandler::DiceTokenFetcher::DiceTokenFetcher( |
| 73 const std::string& gaia_id, |
| 74 const std::string& email, |
| 75 const std::string& authorization_code, |
| 76 SigninClient* signin_client, |
| 77 DiceResponseHandler* dice_response_handler) |
| 78 : gaia_id_(gaia_id), |
| 79 email_(email), |
| 80 authorization_code_(authorization_code), |
| 81 dice_response_handler_(dice_response_handler) { |
| 82 gaia_auth_fetcher_ = signin_client->CreateGaiaAuthFetcher( |
| 83 this, GaiaConstants::kChromeSource, |
| 84 signin_client->GetURLRequestContext()); |
| 85 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code_); |
| 86 |
| 87 // TODO(droger): The token exchange must complete quickly or be cancelled. Add |
| 88 // a timeout logic. |
| 89 } |
| 90 |
| 91 DiceResponseHandler::DiceTokenFetcher::~DiceTokenFetcher() {} |
| 92 |
| 93 void DiceResponseHandler::DiceTokenFetcher::OnClientOAuthSuccess( |
| 94 const GaiaAuthConsumer::ClientOAuthResult& result) { |
| 95 dice_response_handler_->OnTokenExchangeSuccess(this, gaia_id_, email_, |
| 96 result); |
| 97 } |
| 98 |
| 99 void DiceResponseHandler::DiceTokenFetcher::OnClientOAuthFailure( |
| 100 const GoogleServiceAuthError& error) { |
| 101 dice_response_handler_->OnTokenExchangeFailure(this, error); |
| 102 } |
| 103 |
| 104 //////////////////////////////////////////////////////////////////////////////// |
| 105 // DiceResponseHandler |
| 106 //////////////////////////////////////////////////////////////////////////////// |
| 107 |
67 // static | 108 // static |
68 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) { | 109 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) { |
69 return DiceResponseHandlerFactory::GetForProfile(profile); | 110 return DiceResponseHandlerFactory::GetForProfile(profile); |
70 } | 111 } |
71 | 112 |
72 DiceResponseHandler::DiceResponseHandler( | 113 DiceResponseHandler::DiceResponseHandler( |
73 SigninClient* signin_client, | 114 SigninClient* signin_client, |
74 ProfileOAuth2TokenService* profile_oauth2_token_service, | 115 ProfileOAuth2TokenService* profile_oauth2_token_service, |
75 AccountTrackerService* account_tracker_service) | 116 AccountTrackerService* account_tracker_service) |
76 : signin_client_(signin_client), | 117 : signin_client_(signin_client), |
(...skipping 29 matching lines...) Expand all Loading... |
106 return; | 147 return; |
107 } | 148 } |
108 | 149 |
109 void DiceResponseHandler::ProcessDiceSigninHeader( | 150 void DiceResponseHandler::ProcessDiceSigninHeader( |
110 const std::string& gaia_id, | 151 const std::string& gaia_id, |
111 const std::string& email, | 152 const std::string& email, |
112 const std::string& authorization_code) { | 153 const std::string& authorization_code) { |
113 DCHECK(!gaia_id.empty()); | 154 DCHECK(!gaia_id.empty()); |
114 DCHECK(!email.empty()); | 155 DCHECK(!email.empty()); |
115 DCHECK(!authorization_code.empty()); | 156 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 | 157 |
126 // TODO(droger): The token exchange must complete quickly or be cancelled. Add | 158 for (auto it = token_fetchers_.begin(); it != token_fetchers_.end(); ++it) { |
127 // a timeout logic. | 159 if ((it->get()->gaia_id() == gaia_id) && (it->get()->email() == email) && |
| 160 (it->get()->authorization_code() == authorization_code)) { |
| 161 return; // There is already a request in flight with the same parameters. |
| 162 } |
| 163 } |
| 164 |
| 165 token_fetchers_.push_back(base::MakeUnique<DiceTokenFetcher>( |
| 166 gaia_id, email, authorization_code, signin_client_, this)); |
128 } | 167 } |
129 | 168 |
130 void DiceResponseHandler::OnClientOAuthSuccess( | 169 void DiceResponseHandler::DeleteTokenFetcher(DiceTokenFetcher* token_fetcher) { |
131 const ClientOAuthResult& result) { | 170 for (auto it = token_fetchers_.begin(); it != token_fetchers_.end(); ++it) { |
| 171 if (it->get() == token_fetcher) { |
| 172 token_fetchers_.erase(it); |
| 173 return; |
| 174 } |
| 175 } |
| 176 NOTREACHED(); |
| 177 } |
| 178 |
| 179 void DiceResponseHandler::OnTokenExchangeSuccess( |
| 180 DiceTokenFetcher* token_fetcher, |
| 181 const std::string& gaia_id, |
| 182 const std::string& email, |
| 183 const GaiaAuthConsumer::ClientOAuthResult& result) { |
132 std::string account_id = | 184 std::string account_id = |
133 account_tracker_service_->SeedAccountInfo(gaia_id_, email_); | 185 account_tracker_service_->SeedAccountInfo(gaia_id, email); |
134 VLOG(1) << "Dice OAuth success for account: " << account_id; | 186 VLOG(1) << "Dice OAuth success for account: " << account_id; |
135 token_service_->UpdateCredentials(account_id, result.refresh_token); | 187 token_service_->UpdateCredentials(account_id, result.refresh_token); |
136 gaia_id_.clear(); | 188 DeleteTokenFetcher(token_fetcher); |
137 email_.clear(); | |
138 gaia_auth_fetcher_.reset(); | |
139 } | 189 } |
140 | 190 |
141 void DiceResponseHandler::OnClientOAuthFailure( | 191 void DiceResponseHandler::OnTokenExchangeFailure( |
| 192 DiceTokenFetcher* token_fetcher, |
142 const GoogleServiceAuthError& error) { | 193 const GoogleServiceAuthError& error) { |
143 // TODO(droger): Handle authentication errors. | 194 // TODO(droger): Handle authentication errors. |
144 VLOG(1) << "Dice OAuth failed with error: " << error.ToString(); | 195 VLOG(1) << "Dice OAuth failed with error: " << error.ToString(); |
145 gaia_id_.clear(); | 196 DeleteTokenFetcher(token_fetcher); |
146 email_.clear(); | |
147 gaia_auth_fetcher_.reset(); | |
148 } | 197 } |
OLD | NEW |