| 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 { |
| 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 url_fetcher_type_(URL_FETCHER_NONE) { |
| 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); |
| 59 | 63 |
| 64 // net::URLFetcherDelegate interface |
| 65 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 66 |
| 60 private: | 67 private: |
| 61 friend class base::RefCountedThreadSafe<Core>; | 68 friend class base::RefCountedThreadSafe<Core>; |
| 69 |
| 70 enum URLFetcherType { |
| 71 URL_FETCHER_NONE, |
| 72 URL_FETCHER_REFRESH_TOKEN, |
| 73 URL_FETCHER_GET_USER_INFO |
| 74 }; |
| 75 |
| 62 virtual ~Core() {} | 76 virtual ~Core() {} |
| 63 | 77 |
| 64 void OnAuthTokenFetchComplete(const net::URLRequestStatus& status, | 78 void OnAuthTokenFetchComplete(const net::URLRequestStatus& status, |
| 65 int response_code, | 79 int response_code, |
| 66 const std::string& response); | 80 const std::string& response); |
| 67 void FetchUserInfoAndInvokeCallback(); | 81 void FetchUserInfoAndInvokeCallback(); |
| 68 void OnUserInfoFetchComplete(const net::URLRequestStatus& status, | 82 void OnUserInfoFetchComplete(const net::URLRequestStatus& status, |
| 69 int response_code, | 83 int response_code, |
| 70 const std::string& response); | 84 const std::string& response); |
| 71 | 85 |
| 72 OAuthProviderInfo provider_info_; | 86 OAuthProviderInfo provider_info_; |
| 73 | 87 |
| 74 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 88 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 75 GaiaOAuthClient::Delegate* delegate_; | 89 GaiaOAuthClient::Delegate* delegate_; |
| 76 scoped_ptr<UrlFetcher> request_; | 90 scoped_ptr<net::URLFetcher> request_; |
| 91 URLFetcherType url_fetcher_type_; |
| 77 | 92 |
| 78 std::string access_token_; | 93 std::string access_token_; |
| 79 int expires_in_seconds_; | 94 int expires_in_seconds_; |
| 80 }; | 95 }; |
| 81 | 96 |
| 82 void GaiaOAuthClient::Core::RefreshToken( | 97 void GaiaOAuthClient::Core::RefreshToken( |
| 83 const OAuthClientInfo& oauth_client_info, | 98 const OAuthClientInfo& oauth_client_info, |
| 84 const std::string& refresh_token, | 99 const std::string& refresh_token, |
| 85 GaiaOAuthClient::Delegate* delegate) { | 100 GaiaOAuthClient::Delegate* delegate) { |
| 86 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; | 101 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
| 87 | 102 |
| 88 delegate_ = delegate; | 103 delegate_ = delegate; |
| 89 | 104 |
| 90 access_token_.clear(); | 105 access_token_.clear(); |
| 91 expires_in_seconds_ = 0; | 106 expires_in_seconds_ = 0; |
| 92 | 107 |
| 93 std::string post_body = | 108 std::string post_body = |
| 94 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + | 109 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + |
| 95 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, | 110 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, |
| 96 true) + | 111 true) + |
| 97 "&client_secret=" + | 112 "&client_secret=" + |
| 98 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + | 113 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + |
| 99 "&grant_type=refresh_token"; | 114 "&grant_type=refresh_token"; |
| 100 request_.reset(new UrlFetcher(GURL(provider_info_.access_token_url), | 115 request_.reset(net::URLFetcher::Create( |
| 101 UrlFetcher::POST)); | 116 GURL(provider_info_.access_token_url), net::URLFetcher::POST, this)); |
| 102 request_->SetRequestContext(request_context_getter_); | 117 request_->SetRequestContext(request_context_getter_); |
| 103 request_->SetUploadData("application/x-www-form-urlencoded", post_body); | 118 request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
| 104 request_->Start( | 119 url_fetcher_type_ = URL_FETCHER_REFRESH_TOKEN; |
| 105 base::Bind(&GaiaOAuthClient::Core::OnAuthTokenFetchComplete, this)); | 120 request_->Start(); |
| 121 } |
| 122 |
| 123 void GaiaOAuthClient::Core::OnURLFetchComplete( |
| 124 const net::URLFetcher* source) { |
| 125 std::string response_string; |
| 126 source->GetResponseAsString(&response_string); |
| 127 switch (url_fetcher_type_) { |
| 128 case URL_FETCHER_REFRESH_TOKEN: |
| 129 OnAuthTokenFetchComplete(source->GetStatus(), |
| 130 source->GetResponseCode(), |
| 131 response_string); |
| 132 break; |
| 133 case URL_FETCHER_GET_USER_INFO: |
| 134 OnUserInfoFetchComplete(source->GetStatus(), |
| 135 source->GetResponseCode(), |
| 136 response_string); |
| 137 break; |
| 138 default: |
| 139 LOG(ERROR) << "Unrecognised URLFetcher type: " << url_fetcher_type_; |
| 140 } |
| 106 } | 141 } |
| 107 | 142 |
| 108 void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( | 143 void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( |
| 109 const net::URLRequestStatus& status, | 144 const net::URLRequestStatus& status, |
| 110 int response_code, | 145 int response_code, |
| 111 const std::string& response) { | 146 const std::string& response) { |
| 112 request_.reset(); | 147 request_.reset(); |
| 113 | 148 |
| 114 if (!status.is_success()) { | 149 if (!status.is_success()) { |
| 115 delegate_->OnNetworkError(response_code); | 150 delegate_->OnNetworkError(response_code); |
| 116 return; | 151 return; |
| 117 } | 152 } |
| 118 | 153 |
| 119 // HTTP_BAD_REQUEST means the arguments are invalid. | 154 // HTTP_BAD_REQUEST means the arguments are invalid. |
| 120 if (response_code == net::HTTP_BAD_REQUEST) { | 155 if (response_code == net::HTTP_BAD_REQUEST) { |
| 121 LOG(ERROR) << "Gaia response: response code=net::HTTP_BAD_REQUEST."; | 156 LOG(ERROR) << "Gaia response: response code=net::HTTP_BAD_REQUEST."; |
| 122 delegate_->OnOAuthError(); | 157 delegate_->OnOAuthError(); |
| 123 return; | 158 return; |
| 124 } | 159 } |
| 125 | 160 |
| 126 if (response_code == net::HTTP_OK) { | 161 if (response_code == net::HTTP_OK) { |
| 127 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); | 162 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |
| 128 if (message_value.get() && | 163 if (message_value.get() && |
| 129 message_value->IsType(Value::TYPE_DICTIONARY)) { | 164 message_value->IsType(Value::TYPE_DICTIONARY)) { |
| 130 scoped_ptr<DictionaryValue> response_dict( | 165 scoped_ptr<DictionaryValue> response_dict( |
| 131 static_cast<DictionaryValue*>(message_value.release())); | 166 static_cast<DictionaryValue*>(message_value.release())); |
| 132 response_dict->GetString(kAccessTokenValue, &access_token_); | 167 std::string access_token; |
| 168 response_dict->GetString(kAccessTokenValue, &access_token); |
| 169 if (access_token.find("\r\n") != std::string::npos) { |
| 170 LOG(ERROR) << "Gaia response: access token include CRLF"; |
| 171 delegate_->OnOAuthError(); |
| 172 return; |
| 173 } |
| 174 access_token_ = access_token; |
| 133 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds_); | 175 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds_); |
| 134 } | 176 } |
| 135 VLOG(1) << "Gaia response: acess_token='" << access_token_ | 177 VLOG(1) << "Gaia response: acess_token='" << access_token_ |
| 136 << "', expires in " << expires_in_seconds_ << " second(s)"; | 178 << "', expires in " << expires_in_seconds_ << " second(s)"; |
| 137 } else { | 179 } else { |
| 138 LOG(ERROR) << "Gaia response: response code=" << response_code; | 180 LOG(ERROR) << "Gaia response: response code=" << response_code; |
| 139 } | 181 } |
| 140 | 182 |
| 141 if (access_token_.empty()) { | 183 if (access_token_.empty()) { |
| 142 delegate_->OnNetworkError(response_code); | 184 delegate_->OnNetworkError(response_code); |
| 143 } else { | 185 } else { |
| 144 FetchUserInfoAndInvokeCallback(); | 186 FetchUserInfoAndInvokeCallback(); |
| 145 } | 187 } |
| 146 } | 188 } |
| 147 | 189 |
| 148 void GaiaOAuthClient::Core::FetchUserInfoAndInvokeCallback() { | 190 void GaiaOAuthClient::Core::FetchUserInfoAndInvokeCallback() { |
| 149 request_.reset(new UrlFetcher( | 191 request_.reset(net::URLFetcher::Create( |
| 150 GURL(provider_info_.user_info_url), UrlFetcher::GET)); | 192 GURL(provider_info_.user_info_url), net::URLFetcher::GET, this)); |
| 151 request_->SetRequestContext(request_context_getter_); | 193 request_->SetRequestContext(request_context_getter_); |
| 152 request_->SetHeader("Authorization", "Bearer " + access_token_); | 194 request_->AddExtraRequestHeader("Authorization: Bearer " + access_token_); |
| 153 request_->Start( | 195 url_fetcher_type_ = URL_FETCHER_GET_USER_INFO; |
| 154 base::Bind(&GaiaOAuthClient::Core::OnUserInfoFetchComplete, this)); | 196 request_->Start(); |
| 155 } | 197 } |
| 156 | 198 |
| 157 void GaiaOAuthClient::Core::OnUserInfoFetchComplete( | 199 void GaiaOAuthClient::Core::OnUserInfoFetchComplete( |
| 158 const net::URLRequestStatus& status, | 200 const net::URLRequestStatus& status, |
| 159 int response_code, | 201 int response_code, |
| 160 const std::string& response) { | 202 const std::string& response) { |
| 203 request_.reset(); |
| 204 url_fetcher_type_ = URL_FETCHER_NONE; |
| 161 std::string email; | 205 std::string email; |
| 162 if (response_code == net::HTTP_OK) { | 206 if (response_code == net::HTTP_OK) { |
| 163 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); | 207 scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |
| 164 if (message_value.get() && | 208 if (message_value.get() && |
| 165 message_value->IsType(Value::TYPE_DICTIONARY)) { | 209 message_value->IsType(Value::TYPE_DICTIONARY)) { |
| 166 scoped_ptr<DictionaryValue> response_dict( | 210 scoped_ptr<DictionaryValue> response_dict( |
| 167 static_cast<DictionaryValue*>(message_value.release())); | 211 static_cast<DictionaryValue*>(message_value.release())); |
| 168 response_dict->GetString(kEmailValue, &email); | 212 response_dict->GetString(kEmailValue, &email); |
| 169 } | 213 } |
| 170 } | 214 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 187 | 231 |
| 188 void GaiaOAuthClient::RefreshToken(const OAuthClientInfo& oauth_client_info, | 232 void GaiaOAuthClient::RefreshToken(const OAuthClientInfo& oauth_client_info, |
| 189 const std::string& refresh_token, | 233 const std::string& refresh_token, |
| 190 Delegate* delegate) { | 234 Delegate* delegate) { |
| 191 return core_->RefreshToken(oauth_client_info, | 235 return core_->RefreshToken(oauth_client_info, |
| 192 refresh_token, | 236 refresh_token, |
| 193 delegate); | 237 delegate); |
| 194 } | 238 } |
| 195 | 239 |
| 196 } // namespace remoting | 240 } // namespace remoting |
| OLD | NEW |