Chromium Code Reviews| 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.h" | 5 #include "remoting/host/oauth_token_getter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "google_apis/google_api_keys.h" | 10 #include "google_apis/google_api_keys.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 const std::string& login, | 27 const std::string& login, |
| 28 const std::string& refresh_token, | 28 const std::string& refresh_token, |
| 29 bool is_service_account) | 29 bool is_service_account) |
| 30 : login(login), | 30 : login(login), |
| 31 refresh_token(refresh_token), | 31 refresh_token(refresh_token), |
| 32 is_service_account(is_service_account) { | 32 is_service_account(is_service_account) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 OAuthTokenGetter::OAuthTokenGetter( | 35 OAuthTokenGetter::OAuthTokenGetter( |
| 36 scoped_ptr<OAuthCredentials> oauth_credentials, | 36 scoped_ptr<OAuthCredentials> oauth_credentials, |
| 37 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 37 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 38 bool auto_refresh) | |
| 38 : oauth_credentials_(oauth_credentials.Pass()), | 39 : oauth_credentials_(oauth_credentials.Pass()), |
| 39 gaia_oauth_client_( | 40 gaia_oauth_client_( |
| 40 new gaia::GaiaOAuthClient(url_request_context_getter)), | 41 new gaia::GaiaOAuthClient(url_request_context_getter)), |
| 41 url_request_context_getter_(url_request_context_getter), | 42 url_request_context_getter_(url_request_context_getter), |
| 42 refreshing_oauth_token_(false) { | 43 refreshing_oauth_token_(false) { |
| 44 if (auto_refresh) { | |
| 45 refresh_timer_.reset(new base::OneShotTimer<OAuthTokenGetter>()); | |
| 46 } | |
| 43 } | 47 } |
| 44 | 48 |
| 45 OAuthTokenGetter::~OAuthTokenGetter() {} | 49 OAuthTokenGetter::~OAuthTokenGetter() {} |
| 46 | 50 |
| 47 void OAuthTokenGetter::OnGetTokensResponse(const std::string& user_email, | 51 void OAuthTokenGetter::OnGetTokensResponse(const std::string& user_email, |
| 48 const std::string& access_token, | 52 const std::string& access_token, |
| 49 int expires_seconds) { | 53 int expires_seconds) { |
| 50 NOTREACHED(); | 54 NOTREACHED(); |
| 51 } | 55 } |
| 52 | 56 |
| 53 void OAuthTokenGetter::OnRefreshTokenResponse( | 57 void OAuthTokenGetter::OnRefreshTokenResponse( |
| 54 const std::string& access_token, | 58 const std::string& access_token, |
| 55 int expires_seconds) { | 59 int expires_seconds) { |
| 56 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
| 57 DCHECK(oauth_credentials_.get()); | 61 DCHECK(oauth_credentials_.get()); |
| 58 HOST_LOG << "Received OAuth token."; | 62 HOST_LOG << "Received OAuth token."; |
| 59 | 63 |
| 60 oauth_access_token_ = access_token; | 64 oauth_access_token_ = access_token; |
| 61 auth_token_expiry_time_ = base::Time::Now() + | 65 base::TimeDelta token_expiration = |
| 62 base::TimeDelta::FromSeconds(expires_seconds) - | 66 base::TimeDelta::FromSeconds(expires_seconds) - |
| 63 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); | 67 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); |
| 68 auth_token_expiry_time_ = base::Time::Now() + token_expiration; | |
| 64 | 69 |
| 65 gaia_oauth_client_->GetUserEmail(access_token, kMaxRetries, this); | 70 if (refresh_timer_) { |
| 71 refresh_timer_->Stop(); | |
| 72 refresh_timer_->Start(FROM_HERE, token_expiration, this, | |
| 73 &OAuthTokenGetter::RefreshOAuthToken); | |
| 74 } | |
| 75 | |
| 76 if (verified_email_.empty()) { | |
| 77 gaia_oauth_client_->GetUserEmail(access_token, kMaxRetries, this); | |
| 78 } else { | |
| 79 refreshing_oauth_token_ = false; | |
| 80 NotifyCallbacks( | |
| 81 OAuthTokenGetter::SUCCESS, verified_email_, oauth_access_token_); | |
| 82 } | |
| 66 } | 83 } |
| 67 | 84 |
| 68 void OAuthTokenGetter::OnGetUserEmailResponse(const std::string& user_email) { | 85 void OAuthTokenGetter::OnGetUserEmailResponse(const std::string& user_email) { |
| 69 DCHECK(CalledOnValidThread()); | 86 DCHECK(CalledOnValidThread()); |
| 70 DCHECK(oauth_credentials_.get()); | 87 DCHECK(oauth_credentials_.get()); |
| 71 HOST_LOG << "Received user info."; | 88 HOST_LOG << "Received user info."; |
| 72 | 89 |
| 73 if (user_email != oauth_credentials_->login) { | 90 if (user_email != oauth_credentials_->login) { |
| 74 LOG(ERROR) << "OAuth token and email address do not refer to " | 91 LOG(ERROR) << "OAuth token and email address do not refer to " |
| 75 "the same account."; | 92 "the same account."; |
| 76 OnOAuthError(); | 93 OnOAuthError(); |
| 77 return; | 94 return; |
| 78 } | 95 } |
| 79 | 96 |
| 97 verified_email_ = user_email; | |
| 80 refreshing_oauth_token_ = false; | 98 refreshing_oauth_token_ = false; |
| 81 | 99 |
| 82 // Now that we've refreshed the token and verified that it's for the correct | 100 // Now that we've refreshed the token and verified that it's for the correct |
| 83 // user account, try to connect using the new token. | 101 // user account, try to connect using the new token. |
| 84 NotifyCallbacks(OAuthTokenGetter::SUCCESS, user_email, oauth_access_token_); | 102 NotifyCallbacks(OAuthTokenGetter::SUCCESS, user_email, oauth_access_token_); |
| 85 } | 103 } |
| 86 | 104 |
| 87 void OAuthTokenGetter::NotifyCallbacks(Status status, | 105 void OAuthTokenGetter::NotifyCallbacks(Status status, |
| 88 const std::string& user_email, | 106 const std::string& user_email, |
| 89 const std::string& access_token) { | 107 const std::string& access_token) { |
| 90 std::queue<TokenCallback> callbacks(pending_callbacks_); | 108 std::queue<TokenCallback> callbacks(pending_callbacks_); |
| 91 pending_callbacks_ = std::queue<TokenCallback>(); | 109 pending_callbacks_ = std::queue<TokenCallback>(); |
| 92 | 110 |
| 93 while (!callbacks.empty()) { | 111 while (!callbacks.empty()) { |
| 94 callbacks.front().Run(status, user_email, access_token); | 112 callbacks.front().Run(status, user_email, access_token); |
| 95 callbacks.pop(); | 113 callbacks.pop(); |
| 96 } | 114 } |
| 97 } | 115 } |
| 98 | 116 |
| 99 void OAuthTokenGetter::OnOAuthError() { | 117 void OAuthTokenGetter::OnOAuthError() { |
| 100 DCHECK(CalledOnValidThread()); | 118 DCHECK(CalledOnValidThread()); |
| 101 LOG(ERROR) << "OAuth: invalid credentials."; | 119 LOG(ERROR) << "OAuth: invalid credentials."; |
| 102 refreshing_oauth_token_ = false; | 120 refreshing_oauth_token_ = false; |
| 121 | |
| 122 // Throw away invalid credentials and force a refresh. | |
| 123 oauth_access_token_.clear(); | |
| 124 auth_token_expiry_time_ = base::Time(); | |
| 125 verified_email_.clear(); | |
| 126 | |
| 103 NotifyCallbacks(OAuthTokenGetter::AUTH_ERROR, std::string(), std::string()); | 127 NotifyCallbacks(OAuthTokenGetter::AUTH_ERROR, std::string(), std::string()); |
| 104 } | 128 } |
| 105 | 129 |
| 106 void OAuthTokenGetter::OnNetworkError(int response_code) { | 130 void OAuthTokenGetter::OnNetworkError(int response_code) { |
| 107 DCHECK(CalledOnValidThread()); | 131 DCHECK(CalledOnValidThread()); |
| 108 LOG(ERROR) << "Network error when trying to update OAuth token: " | 132 LOG(ERROR) << "Network error when trying to update OAuth token: " |
| 109 << response_code; | 133 << response_code; |
| 110 refreshing_oauth_token_ = false; | 134 refreshing_oauth_token_ = false; |
| 111 NotifyCallbacks( | 135 NotifyCallbacks( |
| 112 OAuthTokenGetter::NETWORK_ERROR, std::string(), std::string()); | 136 OAuthTokenGetter::NETWORK_ERROR, std::string(), std::string()); |
| 113 } | 137 } |
| 114 | 138 |
| 115 void OAuthTokenGetter::CallWithToken(const TokenCallback& on_access_token) { | 139 void OAuthTokenGetter::CallWithToken(const TokenCallback& on_access_token) { |
| 116 DCHECK(CalledOnValidThread()); | 140 DCHECK(CalledOnValidThread()); |
| 117 bool need_new_auth_token = oauth_credentials_.get() && | 141 bool need_new_auth_token = auth_token_expiry_time_.is_null() |
| 118 (auth_token_expiry_time_.is_null() || | 142 || base::Time::Now() >= auth_token_expiry_time_ |
|
Sergey Ulanov
2014/05/19 22:33:46
nit: || should not be wrapped.
rmsousa
2014/05/19 22:46:53
it's the opposite in Java, I always get confused w
| |
| 119 base::Time::Now() >= auth_token_expiry_time_); | 143 || verified_email_.empty(); |
| 144 | |
| 120 if (need_new_auth_token) { | 145 if (need_new_auth_token) { |
| 121 pending_callbacks_.push(on_access_token); | 146 pending_callbacks_.push(on_access_token); |
| 122 if (!refreshing_oauth_token_) | 147 if (!refreshing_oauth_token_) |
| 123 RefreshOAuthToken(); | 148 RefreshOAuthToken(); |
| 124 } else { | 149 } else { |
| 125 on_access_token.Run( | 150 on_access_token.Run( |
| 126 SUCCESS, oauth_credentials_->login, oauth_access_token_); | 151 SUCCESS, oauth_credentials_->login, oauth_access_token_); |
| 127 } | 152 } |
| 128 } | 153 } |
| 129 | 154 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 146 }; | 171 }; |
| 147 | 172 |
| 148 refreshing_oauth_token_ = true; | 173 refreshing_oauth_token_ = true; |
| 149 std::vector<std::string> empty_scope_list; // Use scope from refresh token. | 174 std::vector<std::string> empty_scope_list; // Use scope from refresh token. |
| 150 gaia_oauth_client_->RefreshToken( | 175 gaia_oauth_client_->RefreshToken( |
| 151 client_info, oauth_credentials_->refresh_token, empty_scope_list, | 176 client_info, oauth_credentials_->refresh_token, empty_scope_list, |
| 152 kMaxRetries, this); | 177 kMaxRetries, this); |
| 153 } | 178 } |
| 154 | 179 |
| 155 } // namespace remoting | 180 } // namespace remoting |
| OLD | NEW |