Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: google_apis/gaia/oauth2_mint_token_fetcher.cc

Issue 12755010: Remove unused code in google_apis/gaia (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Really rebase Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_mint_token_fetcher.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/json/json_reader.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "base/values.h"
14 #include "google_apis/gaia/gaia_urls.h"
15 #include "google_apis/gaia/google_service_auth_error.h"
16 #include "net/base/escape.h"
17 #include "net/base/load_flags.h"
18 #include "net/http/http_status_code.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "net/url_request/url_request_status.h"
22
23 using net::URLFetcher;
24 using net::URLFetcherDelegate;
25 using net::ResponseCookies;
26 using net::URLRequestContextGetter;
27 using net::URLRequestStatus;
28
29 namespace {
30 static const char kAuthorizationHeaderFormat[] =
31 "Authorization: Bearer %s";
32 static const char kOAuth2IssueTokenBodyFormat[] =
33 "force=true"
34 "&response_type=token"
35 "&scope=%s"
36 "&client_id=%s"
37 "&origin=%s";
38 static const char kAccessTokenKey[] = "token";
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& headers,
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
71 if (!empty_body)
72 result->SetUploadData("application/x-www-form-urlencoded", body);
73 if (!headers.empty())
74 result->SetExtraRequestHeaders(headers);
75
76 return result;
77 }
78 } // namespace
79
80 OAuth2MintTokenFetcher::OAuth2MintTokenFetcher(
81 OAuth2MintTokenConsumer* consumer,
82 URLRequestContextGetter* getter,
83 const std::string& source)
84 : consumer_(consumer),
85 getter_(getter),
86 source_(source),
87 state_(INITIAL) { }
88
89 OAuth2MintTokenFetcher::~OAuth2MintTokenFetcher() { }
90
91 void OAuth2MintTokenFetcher::CancelRequest() {
92 fetcher_.reset();
93 }
94
95 void OAuth2MintTokenFetcher::Start(const std::string& oauth_login_access_token,
96 const std::string& client_id,
97 const std::vector<std::string>& scopes,
98 const std::string& origin) {
99 oauth_login_access_token_ = oauth_login_access_token;
100 client_id_ = client_id;
101 scopes_ = scopes;
102 origin_ = origin;
103 StartMintToken();
104 }
105
106 void OAuth2MintTokenFetcher::StartMintToken() {
107 CHECK_EQ(INITIAL, state_);
108 state_ = MINT_TOKEN_STARTED;
109 fetcher_.reset(CreateFetcher(
110 getter_,
111 MakeMintTokenUrl(),
112 MakeMintTokenHeader(oauth_login_access_token_),
113 MakeMintTokenBody(client_id_, scopes_, origin_),
114 this));
115 fetcher_->Start(); // OnURLFetchComplete will be called.
116 }
117
118 void OAuth2MintTokenFetcher::EndMintToken(const net::URLFetcher* source) {
119 CHECK_EQ(MINT_TOKEN_STARTED, state_);
120 state_ = MINT_TOKEN_DONE;
121
122 URLRequestStatus status = source->GetStatus();
123 if (!status.is_success()) {
124 OnMintTokenFailure(CreateAuthError(status));
125 return;
126 }
127
128 if (source->GetResponseCode() != net::HTTP_OK) {
129 OnMintTokenFailure(GoogleServiceAuthError(
130 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
131 return;
132 }
133
134 // The request was successfully fetched and it returned OK.
135 // Parse out the access token.
136 std::string access_token;
137 ParseMintTokenResponse(source, &access_token);
138 OnMintTokenSuccess(access_token);
139 }
140
141 void OAuth2MintTokenFetcher::OnMintTokenSuccess(
142 const std::string& access_token) {
143 consumer_->OnMintTokenSuccess(access_token);
144 }
145
146 void OAuth2MintTokenFetcher::OnMintTokenFailure(
147 const GoogleServiceAuthError& error) {
148 state_ = ERROR_STATE;
149 consumer_->OnMintTokenFailure(error);
150 }
151
152 void OAuth2MintTokenFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
153 CHECK(source);
154 CHECK_EQ(MINT_TOKEN_STARTED, state_);
155 EndMintToken(source);
156 }
157
158 // static
159 GURL OAuth2MintTokenFetcher::MakeMintTokenUrl() {
160 return GURL(GaiaUrls::GetInstance()->oauth2_issue_token_url());
161 }
162
163 // static
164 std::string OAuth2MintTokenFetcher::MakeMintTokenHeader(
165 const std::string& access_token) {
166 return base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str());
167 }
168
169 // static
170 std::string OAuth2MintTokenFetcher::MakeMintTokenBody(
171 const std::string& client_id,
172 const std::vector<std::string>& scopes,
173 const std::string& origin) {
174 return base::StringPrintf(
175 kOAuth2IssueTokenBodyFormat,
176 net::EscapeUrlEncodedData(JoinString(scopes, ','), true).c_str(),
177 net::EscapeUrlEncodedData(client_id, true).c_str(),
178 net::EscapeUrlEncodedData(origin, true).c_str());
179 }
180
181 // static
182 bool OAuth2MintTokenFetcher::ParseMintTokenResponse(
183 const net::URLFetcher* source,
184 std::string* access_token) {
185 CHECK(source);
186 CHECK(access_token);
187 std::string data;
188 source->GetResponseAsString(&data);
189 scoped_ptr<base::Value> value(base::JSONReader::Read(data));
190 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY)
191 return false;
192
193 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get());
194 return dict->GetString(kAccessTokenKey, access_token);
195 }
OLDNEW
« no previous file with comments | « google_apis/gaia/oauth2_mint_token_fetcher.h ('k') | google_apis/gaia/oauth2_mint_token_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698