| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/oauth_token_getter_impl.h" | 5 #include "remoting/base/oauth_token_getter_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "google_apis/google_api_keys.h" | 12 #include "google_apis/google_api_keys.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "remoting/base/logging.h" | 14 #include "remoting/base/logging.h" |
| 15 | 15 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 45 const std::string& access_token, | 45 const std::string& access_token, |
| 46 int expires_seconds) { | 46 int expires_seconds) { |
| 47 NOTREACHED(); | 47 NOTREACHED(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void OAuthTokenGetterImpl::OnRefreshTokenResponse( | 50 void OAuthTokenGetterImpl::OnRefreshTokenResponse( |
| 51 const std::string& access_token, | 51 const std::string& access_token, |
| 52 int expires_seconds) { | 52 int expires_seconds) { |
| 53 DCHECK(CalledOnValidThread()); | 53 DCHECK(CalledOnValidThread()); |
| 54 DCHECK(oauth_credentials_.get()); | 54 DCHECK(oauth_credentials_.get()); |
| 55 HOST_LOG << "Received OAuth token."; | 55 CRD_LOG << "Received OAuth token."; |
| 56 | 56 |
| 57 oauth_access_token_ = access_token; | 57 oauth_access_token_ = access_token; |
| 58 base::TimeDelta token_expiration = | 58 base::TimeDelta token_expiration = |
| 59 base::TimeDelta::FromSeconds(expires_seconds) - | 59 base::TimeDelta::FromSeconds(expires_seconds) - |
| 60 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); | 60 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); |
| 61 auth_token_expiry_time_ = base::Time::Now() + token_expiration; | 61 auth_token_expiry_time_ = base::Time::Now() + token_expiration; |
| 62 | 62 |
| 63 if (refresh_timer_) { | 63 if (refresh_timer_) { |
| 64 refresh_timer_->Stop(); | 64 refresh_timer_->Stop(); |
| 65 refresh_timer_->Start(FROM_HERE, token_expiration, this, | 65 refresh_timer_->Start(FROM_HERE, token_expiration, this, |
| 66 &OAuthTokenGetterImpl::RefreshOAuthToken); | 66 &OAuthTokenGetterImpl::RefreshOAuthToken); |
| 67 } | 67 } |
| 68 | 68 |
| 69 if (!oauth_credentials_->is_service_account && !email_verified_) { | 69 if (!oauth_credentials_->is_service_account && !email_verified_) { |
| 70 gaia_oauth_client_->GetUserEmail(access_token, kMaxRetries, this); | 70 gaia_oauth_client_->GetUserEmail(access_token, kMaxRetries, this); |
| 71 } else { | 71 } else { |
| 72 refreshing_oauth_token_ = false; | 72 refreshing_oauth_token_ = false; |
| 73 NotifyCallbacks(OAuthTokenGetterImpl::SUCCESS, oauth_credentials_->login, | 73 NotifyCallbacks(OAuthTokenGetterImpl::SUCCESS, oauth_credentials_->login, |
| 74 oauth_access_token_); | 74 oauth_access_token_); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 void OAuthTokenGetterImpl::OnGetUserEmailResponse( | 78 void OAuthTokenGetterImpl::OnGetUserEmailResponse( |
| 79 const std::string& user_email) { | 79 const std::string& user_email) { |
| 80 DCHECK(CalledOnValidThread()); | 80 DCHECK(CalledOnValidThread()); |
| 81 DCHECK(oauth_credentials_.get()); | 81 DCHECK(oauth_credentials_.get()); |
| 82 HOST_LOG << "Received user info."; | 82 CRD_LOG << "Received user info."; |
| 83 | 83 |
| 84 if (user_email != oauth_credentials_->login) { | 84 if (user_email != oauth_credentials_->login) { |
| 85 LOG(ERROR) << "OAuth token and email address do not refer to " | 85 LOG(ERROR) << "OAuth token and email address do not refer to " |
| 86 "the same account."; | 86 "the same account."; |
| 87 OnOAuthError(); | 87 OnOAuthError(); |
| 88 return; | 88 return; |
| 89 } | 89 } |
| 90 | 90 |
| 91 email_verified_ = true; | 91 email_verified_ = true; |
| 92 refreshing_oauth_token_ = false; | 92 refreshing_oauth_token_ = false; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 DCHECK(CalledOnValidThread()); | 127 DCHECK(CalledOnValidThread()); |
| 128 LOG(ERROR) << "Network error when trying to update OAuth token: " | 128 LOG(ERROR) << "Network error when trying to update OAuth token: " |
| 129 << response_code; | 129 << response_code; |
| 130 refreshing_oauth_token_ = false; | 130 refreshing_oauth_token_ = false; |
| 131 NotifyCallbacks(OAuthTokenGetterImpl::NETWORK_ERROR, std::string(), | 131 NotifyCallbacks(OAuthTokenGetterImpl::NETWORK_ERROR, std::string(), |
| 132 std::string()); | 132 std::string()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void OAuthTokenGetterImpl::CallWithToken(const TokenCallback& on_access_token) { | 135 void OAuthTokenGetterImpl::CallWithToken(const TokenCallback& on_access_token) { |
| 136 DCHECK(CalledOnValidThread()); | 136 DCHECK(CalledOnValidThread()); |
| 137 bool need_new_auth_token = auth_token_expiry_time_.is_null() || | 137 bool need_new_auth_token = |
| 138 base::Time::Now() >= auth_token_expiry_time_ || | 138 auth_token_expiry_time_.is_null() || |
| 139 (!oauth_credentials_->is_service_account && | 139 base::Time::Now() >= auth_token_expiry_time_ || |
| 140 !email_verified_); | 140 (!oauth_credentials_->is_service_account && !email_verified_); |
| 141 | 141 |
| 142 if (need_new_auth_token) { | 142 if (need_new_auth_token) { |
| 143 pending_callbacks_.push(on_access_token); | 143 pending_callbacks_.push(on_access_token); |
| 144 if (!refreshing_oauth_token_) | 144 if (!refreshing_oauth_token_) |
| 145 RefreshOAuthToken(); | 145 RefreshOAuthToken(); |
| 146 } else { | 146 } else { |
| 147 on_access_token.Run(SUCCESS, oauth_credentials_->login, | 147 on_access_token.Run(SUCCESS, oauth_credentials_->login, |
| 148 oauth_access_token_); | 148 oauth_access_token_); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 void OAuthTokenGetterImpl::InvalidateCache() { | 152 void OAuthTokenGetterImpl::InvalidateCache() { |
| 153 DCHECK(CalledOnValidThread()); | 153 DCHECK(CalledOnValidThread()); |
| 154 auth_token_expiry_time_ = base::Time(); | 154 auth_token_expiry_time_ = base::Time(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void OAuthTokenGetterImpl::RefreshOAuthToken() { | 157 void OAuthTokenGetterImpl::RefreshOAuthToken() { |
| 158 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
| 159 HOST_LOG << "Refreshing OAuth token."; | 159 CRD_LOG << "Refreshing OAuth token."; |
| 160 DCHECK(!refreshing_oauth_token_); | 160 DCHECK(!refreshing_oauth_token_); |
| 161 | 161 |
| 162 // Service accounts use different API keys, as they use the client app flow. | 162 // Service accounts use different API keys, as they use the client app flow. |
| 163 google_apis::OAuth2Client oauth2_client = | 163 google_apis::OAuth2Client oauth2_client = |
| 164 oauth_credentials_->is_service_account ? google_apis::CLIENT_REMOTING_HOST | 164 oauth_credentials_->is_service_account ? google_apis::CLIENT_REMOTING_HOST |
| 165 : google_apis::CLIENT_REMOTING; | 165 : google_apis::CLIENT_REMOTING; |
| 166 | 166 |
| 167 gaia::OAuthClientInfo client_info = { | 167 gaia::OAuthClientInfo client_info = { |
| 168 google_apis::GetOAuth2ClientID(oauth2_client), | 168 google_apis::GetOAuth2ClientID(oauth2_client), |
| 169 google_apis::GetOAuth2ClientSecret(oauth2_client), | 169 google_apis::GetOAuth2ClientSecret(oauth2_client), |
| 170 // Redirect URL is only used when getting tokens from auth code. It | 170 // Redirect URL is only used when getting tokens from auth code. It |
| 171 // is not required when getting access tokens. | 171 // is not required when getting access tokens. |
| 172 ""}; | 172 ""}; |
| 173 | 173 |
| 174 refreshing_oauth_token_ = true; | 174 refreshing_oauth_token_ = true; |
| 175 std::vector<std::string> empty_scope_list; // Use scope from refresh token. | 175 std::vector<std::string> empty_scope_list; // Use scope from refresh token. |
| 176 gaia_oauth_client_->RefreshToken(client_info, | 176 gaia_oauth_client_->RefreshToken(client_info, |
| 177 oauth_credentials_->refresh_token, | 177 oauth_credentials_->refresh_token, |
| 178 empty_scope_list, kMaxRetries, this); | 178 empty_scope_list, kMaxRetries, this); |
| 179 } | 179 } |
| 180 | 180 |
| 181 } // namespace remoting | 181 } // namespace remoting |
| OLD | NEW |