Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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/common/net/gaia/oauth2_revocation_fetcher.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/stringprintf.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/common/net/gaia/gaia_urls.h" | |
| 16 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
| 17 #include "chrome/common/net/http_return.h" | |
| 18 #include "net/base/escape.h" | |
| 19 #include "net/base/load_flags.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 #include "net/url_request/url_request_status.h" | |
| 22 | |
| 23 using content::URLFetcher; | |
| 24 using content::URLFetcherDelegate; | |
| 25 using net::ResponseCookies; | |
| 26 using net::URLRequestContextGetter; | |
| 27 using net::URLRequestStatus; | |
| 28 | |
| 29 namespace { | |
| 30 static const char kOAuth2RevokeTokenURL[] = | |
| 31 "https://www.googleapis.com/oauth2/v2/RevokeToken"; | |
| 32 | |
| 33 static const char kAuthorizationHeaderFormat[] = | |
| 34 "Authorization: Bearer %s"; | |
| 35 | |
| 36 static const char kRevocationBodyFormat[] = | |
| 37 "client_id=%s&origin=%s"; | |
| 38 | |
| 39 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { | |
| 40 CHECK(!status.is_success()); | |
| 41 if (status.status() == URLRequestStatus::CANCELED) { | |
| 42 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 43 } else { | |
| 44 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | |
| 45 << status.error(); | |
| 46 return GoogleServiceAuthError::FromConnectionError(status.error()); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 static URLFetcher* CreateFetcher(URLRequestContextGetter* getter, | |
| 51 const GURL& url, | |
| 52 const std::string& header, | |
| 53 const std::string& body, | |
| 54 URLFetcherDelegate* delegate) { | |
| 55 bool empty_body = body.empty(); | |
| 56 URLFetcher* result = URLFetcher::Create( | |
| 57 0, url, | |
| 58 empty_body ? URLFetcher::GET : URLFetcher::POST, | |
| 59 delegate); | |
| 60 | |
| 61 result->SetRequestContext(getter); | |
| 62 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 63 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 64 if (!header.empty()) | |
| 65 result->SetExtraRequestHeaders(header); | |
| 66 | |
| 67 if (!empty_body) | |
| 68 result->SetUploadData("application/x-www-form-urlencoded", body); | |
| 69 | |
| 70 return result; | |
| 71 } | |
| 72 } // namespace | |
| 73 | |
| 74 OAuth2RevocationFetcher::OAuth2RevocationFetcher( | |
| 75 OAuth2RevocationConsumer* consumer, | |
| 76 URLRequestContextGetter* getter) | |
| 77 : consumer_(consumer), | |
| 78 getter_(getter), | |
| 79 state_(INITIAL) { } | |
| 80 | |
| 81 OAuth2RevocationFetcher::~OAuth2RevocationFetcher() { } | |
| 82 | |
| 83 void OAuth2RevocationFetcher::CancelRequest() { | |
| 84 fetcher_.reset(); | |
| 85 } | |
| 86 | |
| 87 void OAuth2RevocationFetcher::Start(const std::string& access_token, | |
| 88 const std::string& client_id, | |
| 89 const std::string& origin) { | |
| 90 access_token_ = access_token; | |
| 91 client_id_ = client_id; | |
| 92 origin_ = origin; | |
| 93 StartRevocation(); | |
| 94 } | |
| 95 | |
| 96 void OAuth2RevocationFetcher::StartRevocation() { | |
| 97 CHECK_EQ(INITIAL, state_); | |
| 98 state_ = REVOCATION_STARTED; | |
| 99 fetcher_.reset(CreateFetcher( | |
| 100 getter_, | |
| 101 MakeRevocationUrl(), | |
| 102 MakeRevocationHeader(access_token_), | |
| 103 MakeRevocationBody(client_id_, origin_), | |
| 104 this)); | |
| 105 fetcher_->Start(); // OnURLFetchComplete will be called. | |
| 106 } | |
| 107 | |
| 108 void OAuth2RevocationFetcher::EndRevocation(const URLFetcher* source) { | |
| 109 CHECK_EQ(REVOCATION_STARTED, state_); | |
| 110 state_ = REVOCATION_DONE; | |
| 111 | |
| 112 URLRequestStatus status = source->GetStatus(); | |
| 113 if (!status.is_success()) { | |
| 114 OnRevocationFailure(CreateAuthError(status)); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 if (source->GetResponseCode() != RC_REQUEST_OK_EMPTY_BODY) { | |
|
zel
2011/12/06 01:18:19
what do you get here on regular 200 code?
Munjal (Google)
2011/12/06 01:36:14
We don't get a regular 200 code from this API. It
| |
| 119 OnRevocationFailure(GoogleServiceAuthError( | |
| 120 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 OnRevocationSuccess(); | |
| 125 } | |
| 126 | |
| 127 void OAuth2RevocationFetcher::OnRevocationSuccess() { | |
| 128 consumer_->OnRevocationSuccess(); | |
| 129 } | |
| 130 | |
| 131 void OAuth2RevocationFetcher::OnRevocationFailure( | |
| 132 GoogleServiceAuthError error) { | |
| 133 state_ = ERROR_STATE; | |
| 134 consumer_->OnRevocationFailure(error); | |
| 135 } | |
| 136 | |
| 137 void OAuth2RevocationFetcher::OnURLFetchComplete(const URLFetcher* source) { | |
| 138 CHECK(source); | |
| 139 EndRevocation(source); | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 GURL OAuth2RevocationFetcher::MakeRevocationUrl() { | |
| 144 return GURL(kOAuth2RevokeTokenURL); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 std::string OAuth2RevocationFetcher::MakeRevocationHeader( | |
| 149 const std::string& access_token) { | |
| 150 return StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()); | |
| 151 } | |
| 152 | |
| 153 // static | |
| 154 std::string OAuth2RevocationFetcher::MakeRevocationBody( | |
| 155 const std::string& client_id, | |
| 156 const std::string& origin) { | |
| 157 std::string enc_client_id = net::EscapeUrlEncodedData(client_id, true); | |
| 158 std::string enc_origin = net::EscapeUrlEncodedData(origin, true); | |
| 159 return StringPrintf( | |
| 160 kRevocationBodyFormat, | |
| 161 enc_client_id.c_str(), | |
| 162 enc_origin.c_str()); | |
| 163 } | |
| OLD | NEW |