| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" | 5 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 static const char kCodeKey[] = "code"; | 46 static const char kCodeKey[] = "code"; |
| 47 | 47 |
| 48 class ManagedUserRefreshTokenFetcherImpl | 48 class ManagedUserRefreshTokenFetcherImpl |
| 49 : public ManagedUserRefreshTokenFetcher, | 49 : public ManagedUserRefreshTokenFetcher, |
| 50 public OAuth2TokenService::Consumer, | 50 public OAuth2TokenService::Consumer, |
| 51 public URLFetcherDelegate, | 51 public URLFetcherDelegate, |
| 52 public GaiaOAuthClient::Delegate { | 52 public GaiaOAuthClient::Delegate { |
| 53 public: | 53 public: |
| 54 ManagedUserRefreshTokenFetcherImpl(OAuth2TokenService* oauth2_token_service, | 54 ManagedUserRefreshTokenFetcherImpl(OAuth2TokenService* oauth2_token_service, |
| 55 URLRequestContextGetter* context); | 55 const std::string& account_id, |
| 56 URLRequestContextGetter* context); |
| 56 virtual ~ManagedUserRefreshTokenFetcherImpl(); | 57 virtual ~ManagedUserRefreshTokenFetcherImpl(); |
| 57 | 58 |
| 58 // ManagedUserRefreshTokenFetcher implementation: | 59 // ManagedUserRefreshTokenFetcher implementation: |
| 59 virtual void Start(const std::string& managed_user_id, | 60 virtual void Start(const std::string& managed_user_id, |
| 60 const std::string& device_name, | 61 const std::string& device_name, |
| 61 const TokenCallback& callback) OVERRIDE; | 62 const TokenCallback& callback) OVERRIDE; |
| 62 | 63 |
| 63 protected: | 64 protected: |
| 64 // OAuth2TokenService::Consumer implementation: | 65 // OAuth2TokenService::Consumer implementation: |
| 65 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 66 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 // Requests an access token, which is the first thing we need. This is where | 85 // Requests an access token, which is the first thing we need. This is where |
| 85 // we restart when the returned access token has expired. | 86 // we restart when the returned access token has expired. |
| 86 void StartFetching(); | 87 void StartFetching(); |
| 87 | 88 |
| 88 void DispatchNetworkError(int error_code); | 89 void DispatchNetworkError(int error_code); |
| 89 void DispatchGoogleServiceAuthError(const GoogleServiceAuthError& error, | 90 void DispatchGoogleServiceAuthError(const GoogleServiceAuthError& error, |
| 90 const std::string& token); | 91 const std::string& token); |
| 91 OAuth2TokenService* oauth2_token_service_; | 92 OAuth2TokenService* oauth2_token_service_; |
| 93 std::string account_id_; |
| 92 URLRequestContextGetter* context_; | 94 URLRequestContextGetter* context_; |
| 93 | 95 |
| 94 std::string device_name_; | 96 std::string device_name_; |
| 95 std::string managed_user_id_; | 97 std::string managed_user_id_; |
| 96 TokenCallback callback_; | 98 TokenCallback callback_; |
| 97 | 99 |
| 98 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | 100 scoped_ptr<OAuth2TokenService::Request> access_token_request_; |
| 99 std::string access_token_; | 101 std::string access_token_; |
| 100 bool access_token_expired_; | 102 bool access_token_expired_; |
| 101 scoped_ptr<URLFetcher> url_fetcher_; | 103 scoped_ptr<URLFetcher> url_fetcher_; |
| 102 scoped_ptr<GaiaOAuthClient> gaia_oauth_client_; | 104 scoped_ptr<GaiaOAuthClient> gaia_oauth_client_; |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 ManagedUserRefreshTokenFetcherImpl::ManagedUserRefreshTokenFetcherImpl( | 107 ManagedUserRefreshTokenFetcherImpl::ManagedUserRefreshTokenFetcherImpl( |
| 106 OAuth2TokenService* oauth2_token_service, | 108 OAuth2TokenService* oauth2_token_service, |
| 109 const std::string& account_id, |
| 107 URLRequestContextGetter* context) | 110 URLRequestContextGetter* context) |
| 108 : oauth2_token_service_(oauth2_token_service), | 111 : oauth2_token_service_(oauth2_token_service), |
| 112 account_id_(account_id), |
| 109 context_(context), | 113 context_(context), |
| 110 access_token_expired_(false) {} | 114 access_token_expired_(false) {} |
| 111 | 115 |
| 112 ManagedUserRefreshTokenFetcherImpl::~ManagedUserRefreshTokenFetcherImpl() {} | 116 ManagedUserRefreshTokenFetcherImpl::~ManagedUserRefreshTokenFetcherImpl() {} |
| 113 | 117 |
| 114 void ManagedUserRefreshTokenFetcherImpl::Start( | 118 void ManagedUserRefreshTokenFetcherImpl::Start( |
| 115 const std::string& managed_user_id, | 119 const std::string& managed_user_id, |
| 116 const std::string& device_name, | 120 const std::string& device_name, |
| 117 const TokenCallback& callback) { | 121 const TokenCallback& callback) { |
| 118 DCHECK(callback_.is_null()); | 122 DCHECK(callback_.is_null()); |
| 119 managed_user_id_ = managed_user_id; | 123 managed_user_id_ = managed_user_id; |
| 120 device_name_ = device_name; | 124 device_name_ = device_name; |
| 121 callback_ = callback; | 125 callback_ = callback; |
| 122 StartFetching(); | 126 StartFetching(); |
| 123 } | 127 } |
| 124 | 128 |
| 125 void ManagedUserRefreshTokenFetcherImpl::StartFetching() { | 129 void ManagedUserRefreshTokenFetcherImpl::StartFetching() { |
| 126 OAuth2TokenService::ScopeSet scopes; | 130 OAuth2TokenService::ScopeSet scopes; |
| 127 scopes.insert(GaiaUrls::GetInstance()->oauth1_login_scope()); | 131 scopes.insert(GaiaUrls::GetInstance()->oauth1_login_scope()); |
| 128 access_token_request_ = oauth2_token_service_->StartRequest(scopes, this); | 132 access_token_request_ = oauth2_token_service_->StartRequest( |
| 133 account_id_, scopes, this); |
| 129 } | 134 } |
| 130 | 135 |
| 131 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenSuccess( | 136 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenSuccess( |
| 132 const OAuth2TokenService::Request* request, | 137 const OAuth2TokenService::Request* request, |
| 133 const std::string& access_token, | 138 const std::string& access_token, |
| 134 const Time& expiration_time) { | 139 const Time& expiration_time) { |
| 135 DCHECK_EQ(access_token_request_.get(), request); | 140 DCHECK_EQ(access_token_request_.get(), request); |
| 136 access_token_ = access_token; | 141 access_token_ = access_token; |
| 137 | 142 |
| 138 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url()); | 143 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 const URLFetcher* source) { | 178 const URLFetcher* source) { |
| 174 const net::URLRequestStatus& status = source->GetStatus(); | 179 const net::URLRequestStatus& status = source->GetStatus(); |
| 175 if (!status.is_success()) { | 180 if (!status.is_success()) { |
| 176 DispatchNetworkError(status.error()); | 181 DispatchNetworkError(status.error()); |
| 177 return; | 182 return; |
| 178 } | 183 } |
| 179 | 184 |
| 180 int response_code = source->GetResponseCode(); | 185 int response_code = source->GetResponseCode(); |
| 181 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | 186 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { |
| 182 access_token_expired_ = true; | 187 access_token_expired_ = true; |
| 183 oauth2_token_service_->InvalidateToken(OAuth2TokenService::ScopeSet(), | 188 oauth2_token_service_->InvalidateToken(account_id_, |
| 189 OAuth2TokenService::ScopeSet(), |
| 184 access_token_); | 190 access_token_); |
| 185 StartFetching(); | 191 StartFetching(); |
| 186 return; | 192 return; |
| 187 } | 193 } |
| 188 | 194 |
| 189 if (response_code != net::HTTP_OK) { | 195 if (response_code != net::HTTP_OK) { |
| 190 // TODO(bauerb): We should return the HTTP response code somehow. | 196 // TODO(bauerb): We should return the HTTP response code somehow. |
| 191 DLOG(WARNING) << "HTTP error " << response_code; | 197 DLOG(WARNING) << "HTTP error " << response_code; |
| 192 DispatchGoogleServiceAuthError( | 198 DispatchGoogleServiceAuthError( |
| 193 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), | 199 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 const std::string& token) { | 264 const std::string& token) { |
| 259 callback_.Run(error, token); | 265 callback_.Run(error, token); |
| 260 callback_.Reset(); | 266 callback_.Reset(); |
| 261 } | 267 } |
| 262 | 268 |
| 263 } // namespace | 269 } // namespace |
| 264 | 270 |
| 265 // static | 271 // static |
| 266 scoped_ptr<ManagedUserRefreshTokenFetcher> | 272 scoped_ptr<ManagedUserRefreshTokenFetcher> |
| 267 ManagedUserRefreshTokenFetcher::Create(OAuth2TokenService* oauth2_token_service, | 273 ManagedUserRefreshTokenFetcher::Create(OAuth2TokenService* oauth2_token_service, |
| 274 const std::string& account_id, |
| 268 URLRequestContextGetter* context) { | 275 URLRequestContextGetter* context) { |
| 269 scoped_ptr<ManagedUserRefreshTokenFetcher> fetcher( | 276 scoped_ptr<ManagedUserRefreshTokenFetcher> fetcher( |
| 270 new ManagedUserRefreshTokenFetcherImpl(oauth2_token_service, context)); | 277 new ManagedUserRefreshTokenFetcherImpl(oauth2_token_service, account_id, |
| 278 context)); |
| 271 return fetcher.Pass(); | 279 return fetcher.Pass(); |
| 272 } | 280 } |
| 273 | 281 |
| 274 ManagedUserRefreshTokenFetcher::~ManagedUserRefreshTokenFetcher() {} | 282 ManagedUserRefreshTokenFetcher::~ManagedUserRefreshTokenFetcher() {} |
| OLD | NEW |