Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/host/gaia_oauth_client.h" | 5 #include "remoting/host/gaia_oauth_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | |
| 8 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 14 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 16 #include "net/url_request/url_fetcher.h" | |
| 17 #include "net/url_request/url_fetcher_delegate.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 17 #include "remoting/host/url_fetcher.h" | |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 const char kDefaultOAuth2TokenUrl[] = | 23 const char kDefaultOAuth2TokenUrl[] = |
| 22 "https://accounts.google.com/o/oauth2/token"; | 24 "https://accounts.google.com/o/oauth2/token"; |
| 23 const char kDefaultOAuth2UserInfoUrl[] = | 25 const char kDefaultOAuth2UserInfoUrl[] = |
| 24 "https://www.googleapis.com/oauth2/v1/userinfo"; | 26 "https://www.googleapis.com/oauth2/v1/userinfo"; |
| 25 | 27 |
| 26 // Values used to parse token response. | 28 // Values used to parse token response. |
| 27 const char kAccessTokenValue[] = "access_token"; | 29 const char kAccessTokenValue[] = "access_token"; |
| 28 const char kRefreshTokenValue[] = "refresh_token"; | 30 const char kRefreshTokenValue[] = "refresh_token"; |
| 29 const char kExpiresInValue[] = "expires_in"; | 31 const char kExpiresInValue[] = "expires_in"; |
| 30 | 32 |
| 31 // Values used when parsing userinfo response. | 33 // Values used when parsing userinfo response. |
| 32 const char kEmailValue[] = "email"; | 34 const char kEmailValue[] = "email"; |
| 33 | 35 |
| 34 } // namespace | 36 } // namespace |
| 35 | 37 |
| 36 namespace remoting { | 38 namespace remoting { |
| 37 | 39 |
| 38 // static | 40 // static |
| 39 OAuthProviderInfo OAuthProviderInfo::GetDefault() { | 41 OAuthProviderInfo OAuthProviderInfo::GetDefault() { |
| 40 OAuthProviderInfo result; | 42 OAuthProviderInfo result; |
| 41 result.access_token_url = kDefaultOAuth2TokenUrl; | 43 result.access_token_url = kDefaultOAuth2TokenUrl; |
| 42 result.user_info_url = kDefaultOAuth2UserInfoUrl; | 44 result.user_info_url = kDefaultOAuth2UserInfoUrl; |
| 43 return result; | 45 return result; |
| 44 } | 46 } |
| 45 | 47 |
| 46 class GaiaOAuthClient::Core | 48 class GaiaOAuthClient::Core |
| 47 : public base::RefCountedThreadSafe<GaiaOAuthClient::Core> { | 49 : public base::RefCountedThreadSafe<GaiaOAuthClient::Core>, |
| 50 public net::URLFetcherDelegate { | |
|
Wez
2012/06/22 20:14:39
Do we actually need this implementation to be spli
Jamie
2012/06/22 22:42:02
This implementation is based on one used by cloud
Wez
2012/06/22 23:58:35
SGTM
| |
| 48 public: | 51 public: |
| 49 Core(const OAuthProviderInfo& info, | 52 Core(const OAuthProviderInfo& info, |
| 50 net::URLRequestContextGetter* request_context_getter) | 53 net::URLRequestContextGetter* request_context_getter) |
| 51 : provider_info_(info), | 54 : provider_info_(info), |
| 52 request_context_getter_(request_context_getter), | 55 request_context_getter_(request_context_getter), |
| 53 delegate_(NULL) { | 56 delegate_(NULL), |
| 57 request_is_oauth_refresh_(false) { | |
| 54 } | 58 } |
| 55 | 59 |
| 56 void RefreshToken(const OAuthClientInfo& oauth_client_info, | 60 void RefreshToken(const OAuthClientInfo& oauth_client_info, |
| 57 const std::string& refresh_token, | 61 const std::string& refresh_token, |
| 58 GaiaOAuthClient::Delegate* delegate); | 62 GaiaOAuthClient::Delegate* delegate); |
| 63 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
|
Wez
2012/06/22 20:14:39
nit: Prefix this with a blank line and comment "ne
Jamie
2012/06/22 22:42:02
Done.
| |
| 59 | 64 |
| 60 private: | 65 private: |
| 61 friend class base::RefCountedThreadSafe<Core>; | 66 friend class base::RefCountedThreadSafe<Core>; |
| 62 virtual ~Core() {} | 67 virtual ~Core() {} |
| 63 | 68 |
| 64 void OnAuthTokenFetchComplete(const net::URLRequestStatus& status, | 69 void OnAuthTokenFetchComplete(const net::URLRequestStatus& status, |
| 65 int response_code, | 70 int response_code, |
| 66 const std::string& response); | 71 const std::string& response); |
| 67 void FetchUserInfoAndInvokeCallback(); | 72 void FetchUserInfoAndInvokeCallback(); |
| 68 void OnUserInfoFetchComplete(const net::URLRequestStatus& status, | 73 void OnUserInfoFetchComplete(const net::URLRequestStatus& status, |
| 69 int response_code, | 74 int response_code, |
| 70 const std::string& response); | 75 const std::string& response); |
| 71 | 76 |
| 72 OAuthProviderInfo provider_info_; | 77 OAuthProviderInfo provider_info_; |
| 73 | 78 |
| 74 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 79 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 75 GaiaOAuthClient::Delegate* delegate_; | 80 GaiaOAuthClient::Delegate* delegate_; |
| 76 scoped_ptr<UrlFetcher> request_; | 81 scoped_ptr<net::URLFetcher> request_; |
| 82 bool request_is_oauth_refresh_; | |
|
Wez
2012/06/22 20:14:39
Looking at the rest of the class, I think it'd be
Jamie
2012/06/22 22:42:02
That's what I had at first, but we check that the
Wez
2012/06/22 23:58:35
I take your point, but you should really use a "st
Jamie
2012/06/23 00:20:23
Done.
| |
| 77 | 83 |
| 78 std::string access_token_; | 84 std::string access_token_; |
| 79 int expires_in_seconds_; | 85 int expires_in_seconds_; |
| 80 }; | 86 }; |
| 81 | 87 |
| 82 void GaiaOAuthClient::Core::RefreshToken( | 88 void GaiaOAuthClient::Core::RefreshToken( |
| 83 const OAuthClientInfo& oauth_client_info, | 89 const OAuthClientInfo& oauth_client_info, |
| 84 const std::string& refresh_token, | 90 const std::string& refresh_token, |
| 85 GaiaOAuthClient::Delegate* delegate) { | 91 GaiaOAuthClient::Delegate* delegate) { |
| 86 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; | 92 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
| 87 | 93 |
| 88 delegate_ = delegate; | 94 delegate_ = delegate; |
| 89 | 95 |
| 90 access_token_.clear(); | 96 access_token_.clear(); |
| 91 expires_in_seconds_ = 0; | 97 expires_in_seconds_ = 0; |
| 92 | 98 |
| 93 std::string post_body = | 99 std::string post_body = |
| 94 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + | 100 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + |
| 95 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, | 101 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, |
| 96 true) + | 102 true) + |
| 97 "&client_secret=" + | 103 "&client_secret=" + |
| 98 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + | 104 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + |
| 99 "&grant_type=refresh_token"; | 105 "&grant_type=refresh_token"; |
| 100 request_.reset(new UrlFetcher(GURL(provider_info_.access_token_url), | 106 request_.reset(net::URLFetcher::Create( |
| 101 UrlFetcher::POST)); | 107 GURL(provider_info_.access_token_url), net::URLFetcher::POST, this)); |
| 102 request_->SetRequestContext(request_context_getter_); | 108 request_->SetRequestContext(request_context_getter_); |
| 103 request_->SetUploadData("application/x-www-form-urlencoded", post_body); | 109 request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
| 104 request_->Start( | 110 request_is_oauth_refresh_ = true; |
| 105 base::Bind(&GaiaOAuthClient::Core::OnAuthTokenFetchComplete, this)); | 111 request_->Start(); |
| 112 } | |
| 113 | |
| 114 void GaiaOAuthClient::Core::OnURLFetchComplete( | |
| 115 const net::URLFetcher* source) { | |
| 116 std::string response_string; | |
| 117 source->GetResponseAsString(&response_string); | |
| 118 if (request_is_oauth_refresh_) { | |
| 119 OnAuthTokenFetchComplete(source->GetStatus(), | |
| 120 source->GetResponseCode(), | |
| 121 response_string); | |
| 122 } else { | |
| 123 OnUserInfoFetchComplete(source->GetStatus(), | |
| 124 source->GetResponseCode(), | |
| 125 response_string); | |
| 126 } | |
| 106 } | 127 } |
| 107 | 128 |
| 108 void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( | 129 void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( |
| 109 const net::URLRequestStatus& status, | 130 const net::URLRequestStatus& status, |
| 110 int response_code, | 131 int response_code, |
| 111 const std::string& response) { | 132 const std::string& response) { |
| 112 request_.reset(); | 133 request_.reset(); |
| 113 | 134 |
| 114 if (!status.is_success()) { | 135 if (!status.is_success()) { |
| 115 delegate_->OnNetworkError(response_code); | 136 delegate_->OnNetworkError(response_code); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 139 } | 160 } |
| 140 | 161 |
| 141 if (access_token_.empty()) { | 162 if (access_token_.empty()) { |
| 142 delegate_->OnNetworkError(response_code); | 163 delegate_->OnNetworkError(response_code); |
| 143 } else { | 164 } else { |
| 144 FetchUserInfoAndInvokeCallback(); | 165 FetchUserInfoAndInvokeCallback(); |
| 145 } | 166 } |
| 146 } | 167 } |
| 147 | 168 |
| 148 void GaiaOAuthClient::Core::FetchUserInfoAndInvokeCallback() { | 169 void GaiaOAuthClient::Core::FetchUserInfoAndInvokeCallback() { |
| 149 request_.reset(new UrlFetcher( | 170 request_.reset(net::URLFetcher::Create( |
| 150 GURL(provider_info_.user_info_url), UrlFetcher::GET)); | 171 GURL(provider_info_.user_info_url), net::URLFetcher::GET, this)); |
| 151 request_->SetRequestContext(request_context_getter_); | 172 request_->SetRequestContext(request_context_getter_); |
| 152 request_->SetHeader("Authorization", "Bearer " + access_token_); | 173 request_->AddExtraRequestHeader("Authorization: Bearer " + access_token_); |
|
Wez
2012/06/22 20:14:39
Do we need to sanitize access_token_, or will AddE
Jamie
2012/06/22 22:42:02
It DCHECKs that it doesn't contain CRLF. Something
| |
| 153 request_->Start( | 174 request_is_oauth_refresh_ = false; |
| 154 base::Bind(&GaiaOAuthClient::Core::OnUserInfoFetchComplete, this)); | 175 request_->Start(); |
| 155 } | 176 } |
| 156 | 177 |
| 157 void GaiaOAuthClient::Core::OnUserInfoFetchComplete( | 178 void GaiaOAuthClient::Core::OnUserInfoFetchComplete( |
| 158 const net::URLRequestStatus& status, | 179 const net::URLRequestStatus& status, |
| 159 int response_code, | 180 int response_code, |
| 160 const std::string& response) { | 181 const std::string& response) { |
| 182 request_.reset(); | |
| 161 std::string email; | 183 std::string email; |
| 162 if (response_code == net::HTTP_OK) { | 184 if (response_code == net::HTTP_OK) { |
| 163 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); | 185 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |
| 164 if (message_value.get() && | 186 if (message_value.get() && |
| 165 message_value->IsType(Value::TYPE_DICTIONARY)) { | 187 message_value->IsType(Value::TYPE_DICTIONARY)) { |
| 166 scoped_ptr<DictionaryValue> response_dict( | 188 scoped_ptr<DictionaryValue> response_dict( |
| 167 static_cast<DictionaryValue*>(message_value.release())); | 189 static_cast<DictionaryValue*>(message_value.release())); |
| 168 response_dict->GetString(kEmailValue, &email); | 190 response_dict->GetString(kEmailValue, &email); |
| 169 } | 191 } |
| 170 } | 192 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 187 | 209 |
| 188 void GaiaOAuthClient::RefreshToken(const OAuthClientInfo& oauth_client_info, | 210 void GaiaOAuthClient::RefreshToken(const OAuthClientInfo& oauth_client_info, |
| 189 const std::string& refresh_token, | 211 const std::string& refresh_token, |
| 190 Delegate* delegate) { | 212 Delegate* delegate) { |
| 191 return core_->RefreshToken(oauth_client_info, | 213 return core_->RefreshToken(oauth_client_info, |
| 192 refresh_token, | 214 refresh_token, |
| 193 delegate); | 215 delegate); |
| 194 } | 216 } |
| 195 | 217 |
| 196 } // namespace remoting | 218 } // namespace remoting |
| OLD | NEW |