Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/json/json_reader.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string16.h" | |
| 11 #include "base/stringprintf.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/signin/oauth2_token_service.h" | |
| 15 #include "google_apis/gaia/gaia_oauth_client.h" | |
| 16 #include "google_apis/gaia/gaia_urls.h" | |
| 17 #include "google_apis/gaia/google_service_auth_error.h" | |
| 18 #include "google_apis/gaia/oauth2_api_call_flow.h" | |
| 19 #include "net/base/escape.h" | |
| 20 #include "net/base/load_flags.h" | |
| 21 #include "net/base/net_errors.h" | |
| 22 #include "net/http/http_status_code.h" | |
| 23 #include "net/url_request/url_fetcher.h" | |
| 24 #include "net/url_request/url_request_status.h" | |
| 25 | |
| 26 using base::Time; | |
| 27 using gaia::GaiaOAuthClient; | |
| 28 using net::URLFetcher; | |
| 29 using net::URLFetcherDelegate; | |
| 30 using net::URLRequestContextGetter; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 const int kNumRetries = 1; | |
| 35 | |
| 36 static const char kChromeSyncManagedScope[] = | |
| 37 "https://www.googleapis.com/auth/chromesync_playpen"; | |
| 38 | |
| 39 static const char kIssueTokenBodyFormat[] = | |
| 40 "client_id=%s" | |
| 41 "&scope=&%s" | |
| 42 "&response_type=code" | |
| 43 "&profile_id=%s" | |
| 44 "&profile_name=%s" | |
| 45 "&device_name=%s"; | |
| 46 | |
| 47 static const char kAuthorizationHeaderFormat[] = | |
| 48 "Authorization: Bearer %s"; | |
| 49 | |
| 50 static const char kCodeKey[] = "code"; | |
| 51 | |
| 52 class ManagedUserRefreshTokenFetcherImpl | |
| 53 : public ManagedUserRefreshTokenFetcher, | |
| 54 public OAuth2TokenService::Consumer, | |
| 55 public URLFetcherDelegate, | |
| 56 public GaiaOAuthClient::Delegate { | |
| 57 public: | |
| 58 ManagedUserRefreshTokenFetcherImpl(OAuth2TokenService* oauth2_token_service, | |
| 59 URLRequestContextGetter* context); | |
| 60 virtual ~ManagedUserRefreshTokenFetcherImpl(); | |
| 61 | |
| 62 // ManagedUserRefreshTokenFetcher implementation: | |
| 63 virtual void Start(const std::string& managed_user_id, | |
| 64 const string16& name, | |
| 65 const std::string& device_name, | |
| 66 const TokenCallback& callback) OVERRIDE; | |
| 67 | |
| 68 protected: | |
| 69 // OAuth2TokenService::Consumer implementation: | |
| 70 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 71 const std::string& access_token, | |
| 72 const Time& expiration_time) OVERRIDE; | |
| 73 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 74 const GoogleServiceAuthError& error) OVERRIDE; | |
| 75 | |
| 76 // net::URLFetcherDelegate implementation. | |
| 77 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
| 78 | |
| 79 // GaiaOAuthClient::Delegate implementation: | |
| 80 virtual void OnGetTokensResponse(const std::string& refresh_token, | |
| 81 const std::string& access_token, | |
| 82 int expires_in_seconds) OVERRIDE; | |
| 83 virtual void OnRefreshTokenResponse(const std::string& access_token, | |
| 84 int expires_in_seconds) OVERRIDE; | |
| 85 virtual void OnOAuthError() OVERRIDE; | |
| 86 virtual void OnNetworkError(int response_code) OVERRIDE; | |
| 87 | |
| 88 private: | |
| 89 // Requests an access token, which is the first thing we need. This is where | |
| 90 // we restart when the returned access token has expired. | |
| 91 void StartFetching(); | |
| 92 | |
| 93 void DispatchNetworkError(int error_code); | |
| 94 void DispatchGoogleServiceAuthError(GoogleServiceAuthError::State foo); | |
| 95 | |
| 96 OAuth2TokenService* oauth2_token_service_; | |
| 97 URLRequestContextGetter* context_; | |
| 98 | |
| 99 std::string device_name_; | |
| 100 std::string managed_user_id_; | |
| 101 string16 name_; | |
| 102 TokenCallback callback_; | |
| 103 | |
| 104 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | |
| 105 std::string access_token_; | |
| 106 bool access_token_expired_; | |
| 107 scoped_ptr<URLFetcher> url_fetcher_; | |
| 108 scoped_ptr<GaiaOAuthClient> gaia_oauth_client_; | |
| 109 }; | |
| 110 | |
| 111 ManagedUserRefreshTokenFetcherImpl::ManagedUserRefreshTokenFetcherImpl( | |
| 112 OAuth2TokenService* oauth2_token_service, | |
| 113 URLRequestContextGetter* context) | |
| 114 : oauth2_token_service_(oauth2_token_service), | |
| 115 context_(context), | |
| 116 access_token_expired_(false) {} | |
| 117 | |
| 118 ManagedUserRefreshTokenFetcherImpl::~ManagedUserRefreshTokenFetcherImpl() {} | |
| 119 | |
| 120 void ManagedUserRefreshTokenFetcherImpl::Start( | |
| 121 const std::string& managed_user_id, | |
| 122 const string16& name, | |
| 123 const std::string& device_name, | |
| 124 const TokenCallback& callback) { | |
| 125 DCHECK(callback_.is_null()); | |
| 126 managed_user_id_ = managed_user_id; | |
| 127 name_ = name; | |
| 128 device_name_ = device_name; | |
| 129 callback_ = callback; | |
| 130 StartFetching(); | |
| 131 } | |
| 132 | |
| 133 void ManagedUserRefreshTokenFetcherImpl::StartFetching() { | |
| 134 // An empty set of scopes means the same scope as the refresh token. | |
| 135 OAuth2TokenService::ScopeSet scopes; | |
| 136 access_token_request_ = oauth2_token_service_->StartRequest(scopes, this); | |
| 137 } | |
| 138 | |
| 139 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenSuccess( | |
| 140 const OAuth2TokenService::Request* request, | |
| 141 const std::string& access_token, | |
| 142 const Time& expiration_time) { | |
| 143 DCHECK_EQ(access_token_request_.get(), request); | |
| 144 access_token_ = access_token; | |
| 145 | |
| 146 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url()); | |
| 147 // GaiaOAuthClient uses id 0, so we use 1 to distinguish the requests in | |
| 148 // unit tests. | |
| 149 const int id = 1; | |
| 150 | |
| 151 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); | |
| 152 | |
| 153 url_fetcher_->SetRequestContext(context_); | |
| 154 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 155 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 156 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
| 157 url_fetcher_->AddExtraRequestHeader( | |
| 158 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | |
| 159 | |
| 160 std::string body = base::StringPrintf( | |
| 161 kIssueTokenBodyFormat, | |
| 162 net::EscapeUrlEncodedData( | |
| 163 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true).c_str(), | |
| 164 net::EscapeUrlEncodedData(kChromeSyncManagedScope, true).c_str(), | |
| 165 net::EscapeUrlEncodedData(managed_user_id_, true).c_str(), | |
| 166 net::EscapeUrlEncodedData(UTF16ToUTF8(name_), true).c_str(), | |
| 167 net::EscapeUrlEncodedData(device_name_, true).c_str()); | |
| 168 url_fetcher_->SetUploadData("application/x-www-form-urlencoded", body); | |
| 169 | |
| 170 url_fetcher_->Start(); | |
| 171 } | |
| 172 | |
| 173 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenFailure( | |
| 174 const OAuth2TokenService::Request* request, | |
| 175 const GoogleServiceAuthError& error) { | |
| 176 DCHECK_EQ(access_token_request_.get(), request); | |
| 177 callback_.Run(error, std::string()); | |
|
Andrew T Wilson (Slow)
2013/05/29 12:05:33
DispatchGoogleServiceAuthError(error.state())?
Bernhard Bauer
2013/05/29 12:56:56
Done.
| |
| 178 callback_.Reset(); | |
| 179 } | |
| 180 | |
| 181 void ManagedUserRefreshTokenFetcherImpl::OnURLFetchComplete( | |
| 182 const URLFetcher* source) { | |
| 183 const net::URLRequestStatus& status = source->GetStatus(); | |
| 184 if (!status.is_success()) { | |
| 185 DispatchNetworkError(status.error()); | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 int response_code = source->GetResponseCode(); | |
| 190 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | |
| 191 access_token_expired_ = true; | |
| 192 oauth2_token_service_->InvalidateToken(OAuth2TokenService::ScopeSet(), | |
| 193 access_token_); | |
| 194 StartFetching(); | |
| 195 return; | |
| 196 } | |
| 197 | |
| 198 if (response_code != net::HTTP_OK) { | |
| 199 // TODO(bauerb): We should return the HTTP response code somehow. | |
| 200 LOG(WARNING) << "HTTP error " << response_code; | |
| 201 DispatchGoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED); | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 std::string response_body; | |
| 206 source->GetResponseAsString(&response_body); | |
| 207 scoped_ptr<base::Value> value(base::JSONReader::Read(response_body)); | |
| 208 DictionaryValue* dict = NULL; | |
| 209 if (!value.get() || !value->GetAsDictionary(&dict)) { | |
| 210 DispatchNetworkError(net::ERR_INVALID_RESPONSE); | |
| 211 return; | |
| 212 } | |
| 213 std::string auth_code; | |
| 214 if (!dict->GetString(kCodeKey, &auth_code)) { | |
| 215 DispatchNetworkError(net::ERR_INVALID_RESPONSE); | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 gaia::OAuthClientInfo client_info; | |
| 220 GaiaUrls* urls = GaiaUrls::GetInstance(); | |
| 221 client_info.client_id = urls->oauth2_chrome_client_id(); | |
| 222 client_info.client_secret = urls->oauth2_chrome_client_secret(); | |
| 223 gaia_oauth_client_.reset( | |
| 224 new gaia::GaiaOAuthClient(GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 225 context_)); | |
| 226 gaia_oauth_client_->GetTokensFromAuthCode(client_info, auth_code, kNumRetries, | |
| 227 this); | |
| 228 } | |
| 229 | |
| 230 void ManagedUserRefreshTokenFetcherImpl::OnGetTokensResponse( | |
| 231 const std::string& refresh_token, | |
| 232 const std::string& access_token, | |
| 233 int expires_in_seconds) { | |
| 234 // TODO(bauerb): It would be nice if we could pass the access token as well, | |
| 235 // so we don't need to fetch another one immediately. | |
| 236 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); | |
| 237 callback_.Run(error, refresh_token); | |
|
Andrew T Wilson (Slow)
2013/05/29 12:05:33
Could also use GoogleServiceAuthError::AuthErrorNo
Bernhard Bauer
2013/05/29 12:56:56
Done.
| |
| 238 callback_.Reset(); | |
| 239 } | |
| 240 | |
| 241 void ManagedUserRefreshTokenFetcherImpl::OnRefreshTokenResponse( | |
| 242 const std::string& access_token, | |
| 243 int expires_in_seconds) { | |
| 244 NOTREACHED(); | |
| 245 } | |
| 246 | |
| 247 void ManagedUserRefreshTokenFetcherImpl::OnOAuthError() { | |
| 248 DispatchGoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED); | |
| 249 } | |
| 250 | |
| 251 void ManagedUserRefreshTokenFetcherImpl::OnNetworkError(int response_code) { | |
| 252 // TODO(bauerb): We should return the HTTP response code somehow. | |
| 253 LOG(WARNING) << "HTTP error " << response_code; | |
| 254 DispatchGoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED); | |
| 255 } | |
| 256 | |
| 257 void ManagedUserRefreshTokenFetcherImpl::DispatchNetworkError(int error_code) { | |
| 258 callback_.Run(GoogleServiceAuthError::FromConnectionError(error_code), | |
| 259 std::string()); | |
| 260 callback_.Reset(); | |
| 261 } | |
| 262 | |
| 263 void ManagedUserRefreshTokenFetcherImpl::DispatchGoogleServiceAuthError( | |
| 264 GoogleServiceAuthError::State state) { | |
|
Andrew T Wilson (Slow)
2013/05/29 12:05:33
Consider taking a const GoogleServiceAuthError& he
Bernhard Bauer
2013/05/29 12:56:56
Yes, that's what I ended up doing.
| |
| 265 GoogleServiceAuthError error(state); | |
| 266 callback_.Run(error, std::string()); | |
| 267 callback_.Reset(); | |
| 268 } | |
| 269 | |
| 270 } // namespace | |
| 271 | |
| 272 // static | |
| 273 scoped_ptr<ManagedUserRefreshTokenFetcher> | |
| 274 ManagedUserRefreshTokenFetcher::Create(OAuth2TokenService* oauth2_token_service, | |
| 275 URLRequestContextGetter* context) { | |
| 276 scoped_ptr<ManagedUserRefreshTokenFetcher> fetcher( | |
| 277 new ManagedUserRefreshTokenFetcherImpl(oauth2_token_service, context)); | |
| 278 return fetcher.Pass(); | |
| 279 } | |
| 280 | |
| 281 ManagedUserRefreshTokenFetcher::~ManagedUserRefreshTokenFetcher() {} | |
| OLD | NEW |