Chromium Code Reviews| Index: remoting/host/gaia_oauth_client.cc |
| diff --git a/remoting/host/gaia_oauth_client.cc b/remoting/host/gaia_oauth_client.cc |
| index d9d049f50549516a9b6114efa1f48211ae2cb269..0c50342d2971c77a17305e94c01f8f026aa4c733 100644 |
| --- a/remoting/host/gaia_oauth_client.cc |
| +++ b/remoting/host/gaia_oauth_client.cc |
| @@ -17,9 +17,14 @@ |
| #include "remoting/host/url_fetcher.h" |
| namespace { |
| + |
| +const char kUserInfoUrl[] = "https://www.googleapis.com/oauth2/v1/userinfo"; |
|
Wez
2012/05/16 23:00:07
It seems wrong for the GaiaOAuth2Url to be a param
Sergey Ulanov
2012/05/17 00:38:11
Made them both configurable.
|
| + |
| const char kAccessTokenValue[] = "access_token"; |
| const char kRefreshTokenValue[] = "refresh_token"; |
| const char kExpiresInValue[] = "expires_in"; |
| +const char kEmailValue[] = "email"; |
|
Wez
2012/05/16 23:00:07
nit: Keep this definition with the URL definition,
Sergey Ulanov
2012/05/17 00:38:11
Done.
|
| + |
| } // namespace |
| namespace remoting { |
| @@ -42,14 +47,21 @@ class GaiaOAuthClient::Core |
| friend class base::RefCountedThreadSafe<Core>; |
| virtual ~Core() {} |
| - void OnUrlFetchComplete(const net::URLRequestStatus& status, |
| - int response_code, |
| - const std::string& response); |
| + void OnAuthTokenFetchComplete(const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response); |
| + void FetchUserInfo(); |
| + void OnUserInfoFetchComplete(const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response); |
| GURL gaia_url_; |
| scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| GaiaOAuthClient::Delegate* delegate_; |
| scoped_ptr<UrlFetcher> request_; |
| + |
| + std::string access_token_; |
| + int expires_in_seconds_; |
| }; |
| void GaiaOAuthClient::Core::RefreshToken( |
| @@ -58,6 +70,11 @@ void GaiaOAuthClient::Core::RefreshToken( |
| GaiaOAuthClient::Delegate* delegate) { |
| DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
| + delegate_ = delegate; |
| + |
| + access_token_.clear(); |
| + expires_in_seconds_ = 0; |
| + |
| std::string post_body = |
| "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + |
| "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, |
| @@ -65,14 +82,14 @@ void GaiaOAuthClient::Core::RefreshToken( |
| "&client_secret=" + |
| net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + |
| "&grant_type=refresh_token"; |
| - delegate_ = delegate; |
| request_.reset(new UrlFetcher(gaia_url_, UrlFetcher::POST)); |
| request_->SetRequestContext(request_context_getter_); |
| request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
| - request_->Start(base::Bind(&GaiaOAuthClient::Core::OnUrlFetchComplete, this)); |
| + request_->Start( |
| + base::Bind(&GaiaOAuthClient::Core::OnAuthTokenFetchComplete, this)); |
| } |
| -void GaiaOAuthClient::Core::OnUrlFetchComplete( |
| +void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( |
| const net::URLRequestStatus& status, |
| int response_code, |
| const std::string& response) { |
| @@ -90,31 +107,56 @@ void GaiaOAuthClient::Core::OnUrlFetchComplete( |
| return; |
| } |
| - std::string access_token; |
| - std::string refresh_token; |
| - int expires_in_seconds = 0; |
| if (response_code == net::HTTP_OK) { |
| scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |
| if (message_value.get() && |
| message_value->IsType(Value::TYPE_DICTIONARY)) { |
| scoped_ptr<DictionaryValue> response_dict( |
| static_cast<DictionaryValue*>(message_value.release())); |
| - response_dict->GetString(kAccessTokenValue, &access_token); |
| - response_dict->GetString(kRefreshTokenValue, &refresh_token); |
| - response_dict->GetInteger(kExpiresInValue, &expires_in_seconds); |
| + response_dict->GetString(kAccessTokenValue, &access_token_); |
| + response_dict->GetInteger(kExpiresInValue, &expires_in_seconds_); |
| } |
| - VLOG(1) << "Gaia response: acess_token='" << access_token |
| - << "', refresh_token='" << refresh_token |
| - << "', expires in " << expires_in_seconds << " second(s)"; |
| + VLOG(1) << "Gaia response: acess_token='" << access_token_ |
| + << "', expires in " << expires_in_seconds_ << " second(s)"; |
| } else { |
| LOG(ERROR) << "Gaia response: response code=" << response_code; |
| } |
| - if (access_token.empty()) { |
| + if (access_token_.empty()) { |
| + delegate_->OnNetworkError(response_code); |
| + } else { |
| + FetchUserInfo(); |
| + } |
| +} |
| + |
| +void GaiaOAuthClient::Core::FetchUserInfo() { |
|
Wez
2012/05/16 23:00:07
Since this fetches the UserInfo and then notifies
Sergey Ulanov
2012/05/17 00:38:11
Renamed it to FetchUserInfoAndInvokeCallback(), bu
|
| + request_.reset(new UrlFetcher(GURL(kUserInfoUrl), UrlFetcher::GET)); |
| + request_->SetRequestContext(request_context_getter_); |
| + request_->SetHeader("Authorization", "Bearer " + access_token_); |
| + request_->Start( |
| + base::Bind(&GaiaOAuthClient::Core::OnUserInfoFetchComplete, this)); |
| +} |
| + |
| +void GaiaOAuthClient::Core::OnUserInfoFetchComplete( |
| + const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response) { |
| + std::string email; |
| + if (response_code == net::HTTP_OK) { |
| + scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |
|
Wez
2012/05/16 23:00:07
nit: Indentation.
Sergey Ulanov
2012/05/17 00:38:11
Done.
|
| + if (message_value.get() && |
| + message_value->IsType(Value::TYPE_DICTIONARY)) { |
| + scoped_ptr<DictionaryValue> response_dict( |
| + static_cast<DictionaryValue*>(message_value.release())); |
|
Wez
2012/05/16 23:00:07
Can't use AsDictionaryValue() here?
Sergey Ulanov
2012/05/17 00:38:11
Yes, I can, but it makes this code shorter only wh
|
| + response_dict->GetString(kEmailValue, &email); |
| + } |
| + } |
| + |
| + if (email.empty()) { |
| delegate_->OnNetworkError(response_code); |
| - } else if (refresh_token.empty()) { |
| - // If we only have an access token, then this was a refresh request. |
| - delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds); |
| + } else { |
| + delegate_->OnRefreshTokenResponse( |
| + email, access_token_, expires_in_seconds_); |
| } |
| } |