OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "google_apis/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 "google_apis/gaia/gaia_urls.h" | |
16 #include "google_apis/gaia/google_service_auth_error.h" | |
17 #include "net/base/escape.h" | |
18 #include "net/base/load_flags.h" | |
19 #include "net/http/http_status_code.h" | |
20 #include "net/url_request/url_fetcher.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 #include "net/url_request/url_request_status.h" | |
23 | |
24 using net::ResponseCookies; | |
25 using net::URLFetcher; | |
26 using net::URLFetcherDelegate; | |
27 using net::URLRequestContextGetter; | |
28 using net::URLRequestStatus; | |
29 | |
30 namespace { | |
31 static const char kOAuth2RevokeTokenURL[] = | |
32 "https://www.googleapis.com/oauth2/v2/RevokeToken"; | |
33 | |
34 static const char kAuthorizationHeaderFormat[] = | |
35 "Authorization: Bearer %s"; | |
36 | |
37 static const char kRevocationBodyFormat[] = | |
38 "client_id=%s&origin=%s"; | |
39 | |
40 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { | |
41 CHECK(!status.is_success()); | |
42 if (status.status() == URLRequestStatus::CANCELED) { | |
43 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | |
44 } else { | |
45 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | |
46 << status.error(); | |
47 return GoogleServiceAuthError::FromConnectionError(status.error()); | |
48 } | |
49 } | |
50 | |
51 static URLFetcher* CreateFetcher(URLRequestContextGetter* getter, | |
52 const GURL& url, | |
53 const std::string& header, | |
54 const std::string& body, | |
55 URLFetcherDelegate* delegate) { | |
56 bool empty_body = body.empty(); | |
57 URLFetcher* result = net::URLFetcher::Create( | |
58 0, url, | |
59 empty_body ? URLFetcher::GET : URLFetcher::POST, | |
60 delegate); | |
61 | |
62 result->SetRequestContext(getter); | |
63 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
64 net::LOAD_DO_NOT_SAVE_COOKIES); | |
65 // Fetchers are sometimes cancelled because a network change was detected, | |
66 // especially at startup and after sign-in on ChromeOS. Retrying once should | |
67 // be enough in those cases; let the fetcher retry up to 3 times just in case. | |
68 // http://crbug.com/163710 | |
69 result->SetAutomaticallyRetryOnNetworkChanges(3); | |
70 if (!header.empty()) | |
71 result->SetExtraRequestHeaders(header); | |
72 | |
73 if (!empty_body) | |
74 result->SetUploadData("application/x-www-form-urlencoded", body); | |
75 | |
76 return result; | |
77 } | |
78 } // namespace | |
79 | |
80 OAuth2RevocationFetcher::OAuth2RevocationFetcher( | |
81 OAuth2RevocationConsumer* consumer, | |
82 URLRequestContextGetter* getter) | |
83 : consumer_(consumer), | |
84 getter_(getter), | |
85 state_(INITIAL) { } | |
86 | |
87 OAuth2RevocationFetcher::~OAuth2RevocationFetcher() { } | |
88 | |
89 void OAuth2RevocationFetcher::CancelRequest() { | |
90 fetcher_.reset(); | |
91 } | |
92 | |
93 void OAuth2RevocationFetcher::Start(const std::string& access_token, | |
94 const std::string& client_id, | |
95 const std::string& origin) { | |
96 access_token_ = access_token; | |
97 client_id_ = client_id; | |
98 origin_ = origin; | |
99 StartRevocation(); | |
100 } | |
101 | |
102 void OAuth2RevocationFetcher::StartRevocation() { | |
103 CHECK_EQ(INITIAL, state_); | |
104 state_ = REVOCATION_STARTED; | |
105 fetcher_.reset(CreateFetcher( | |
106 getter_, | |
107 MakeRevocationUrl(), | |
108 MakeRevocationHeader(access_token_), | |
109 MakeRevocationBody(client_id_, origin_), | |
110 this)); | |
111 fetcher_->Start(); // OnURLFetchComplete will be called. | |
112 } | |
113 | |
114 void OAuth2RevocationFetcher::EndRevocation(const net::URLFetcher* source) { | |
115 CHECK_EQ(REVOCATION_STARTED, state_); | |
116 state_ = REVOCATION_DONE; | |
117 | |
118 URLRequestStatus status = source->GetStatus(); | |
119 if (!status.is_success()) { | |
120 OnRevocationFailure(CreateAuthError(status)); | |
121 return; | |
122 } | |
123 | |
124 if (source->GetResponseCode() != net::HTTP_NO_CONTENT) { | |
125 OnRevocationFailure(GoogleServiceAuthError( | |
126 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
127 return; | |
128 } | |
129 | |
130 OnRevocationSuccess(); | |
131 } | |
132 | |
133 void OAuth2RevocationFetcher::OnRevocationSuccess() { | |
134 consumer_->OnRevocationSuccess(); | |
135 } | |
136 | |
137 void OAuth2RevocationFetcher::OnRevocationFailure( | |
138 const GoogleServiceAuthError& error) { | |
139 state_ = ERROR_STATE; | |
140 consumer_->OnRevocationFailure(error); | |
141 } | |
142 | |
143 void OAuth2RevocationFetcher::OnURLFetchComplete( | |
144 const net::URLFetcher* source) { | |
145 CHECK(source); | |
146 EndRevocation(source); | |
147 } | |
148 | |
149 // static | |
150 GURL OAuth2RevocationFetcher::MakeRevocationUrl() { | |
151 return GURL(kOAuth2RevokeTokenURL); | |
152 } | |
153 | |
154 // static | |
155 std::string OAuth2RevocationFetcher::MakeRevocationHeader( | |
156 const std::string& access_token) { | |
157 return base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()); | |
158 } | |
159 | |
160 // static | |
161 std::string OAuth2RevocationFetcher::MakeRevocationBody( | |
162 const std::string& client_id, | |
163 const std::string& origin) { | |
164 std::string enc_client_id = net::EscapeUrlEncodedData(client_id, true); | |
165 std::string enc_origin = net::EscapeUrlEncodedData(origin, true); | |
166 return base::StringPrintf( | |
167 kRevocationBodyFormat, | |
168 enc_client_id.c_str(), | |
169 enc_origin.c_str()); | |
170 } | |
OLD | NEW |