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

Side by Side Diff: chrome/common/net/gaia/oauth2_mint_token_fetcher.cc

Issue 10928017: Moving google_apis and GaiaClient to src/google_apis. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to head Created 8 years, 3 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 "chrome/common/net/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 "chrome/common/net/gaia/gaia_urls.h"
15 #include "chrome/common/net/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
66 if (!empty_body)
67 result->SetUploadData("application/x-www-form-urlencoded", body);
68 if (!headers.empty())
69 result->SetExtraRequestHeaders(headers);
70
71 return result;
72 }
73 } // namespace
74
75 OAuth2MintTokenFetcher::OAuth2MintTokenFetcher(
76 OAuth2MintTokenConsumer* consumer,
77 URLRequestContextGetter* getter,
78 const std::string& source)
79 : consumer_(consumer),
80 getter_(getter),
81 source_(source),
82 state_(INITIAL) { }
83
84 OAuth2MintTokenFetcher::~OAuth2MintTokenFetcher() { }
85
86 void OAuth2MintTokenFetcher::CancelRequest() {
87 fetcher_.reset();
88 }
89
90 void OAuth2MintTokenFetcher::Start(const std::string& oauth_login_access_token,
91 const std::string& client_id,
92 const std::vector<std::string>& scopes,
93 const std::string& origin) {
94 oauth_login_access_token_ = oauth_login_access_token;
95 client_id_ = client_id;
96 scopes_ = scopes;
97 origin_ = origin;
98 StartMintToken();
99 }
100
101 void OAuth2MintTokenFetcher::StartMintToken() {
102 CHECK_EQ(INITIAL, state_);
103 state_ = MINT_TOKEN_STARTED;
104 fetcher_.reset(CreateFetcher(
105 getter_,
106 MakeMintTokenUrl(),
107 MakeMintTokenHeader(oauth_login_access_token_),
108 MakeMintTokenBody(client_id_, scopes_, origin_),
109 this));
110 fetcher_->Start(); // OnURLFetchComplete will be called.
111 }
112
113 void OAuth2MintTokenFetcher::EndMintToken(const net::URLFetcher* source) {
114 CHECK_EQ(MINT_TOKEN_STARTED, state_);
115 state_ = MINT_TOKEN_DONE;
116
117 URLRequestStatus status = source->GetStatus();
118 if (!status.is_success()) {
119 OnMintTokenFailure(CreateAuthError(status));
120 return;
121 }
122
123 if (source->GetResponseCode() != net::HTTP_OK) {
124 OnMintTokenFailure(GoogleServiceAuthError(
125 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
126 return;
127 }
128
129 // The request was successfully fetched and it returned OK.
130 // Parse out the access token.
131 std::string access_token;
132 ParseMintTokenResponse(source, &access_token);
133 OnMintTokenSuccess(access_token);
134 }
135
136 void OAuth2MintTokenFetcher::OnMintTokenSuccess(
137 const std::string& access_token) {
138 consumer_->OnMintTokenSuccess(access_token);
139 }
140
141 void OAuth2MintTokenFetcher::OnMintTokenFailure(
142 const GoogleServiceAuthError& error) {
143 state_ = ERROR_STATE;
144 consumer_->OnMintTokenFailure(error);
145 }
146
147 void OAuth2MintTokenFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
148 CHECK(source);
149 CHECK_EQ(MINT_TOKEN_STARTED, state_);
150 EndMintToken(source);
151 }
152
153 // static
154 GURL OAuth2MintTokenFetcher::MakeMintTokenUrl() {
155 return GURL(GaiaUrls::GetInstance()->oauth2_issue_token_url());
156 }
157
158 // static
159 std::string OAuth2MintTokenFetcher::MakeMintTokenHeader(
160 const std::string& access_token) {
161 return StringPrintf(kAuthorizationHeaderFormat, access_token.c_str());
162 }
163
164 // static
165 std::string OAuth2MintTokenFetcher::MakeMintTokenBody(
166 const std::string& client_id,
167 const std::vector<std::string>& scopes,
168 const std::string& origin) {
169 return StringPrintf(
170 kOAuth2IssueTokenBodyFormat,
171 net::EscapeUrlEncodedData(JoinString(scopes, ','), true).c_str(),
172 net::EscapeUrlEncodedData(client_id, true).c_str(),
173 net::EscapeUrlEncodedData(origin, true).c_str());
174 }
175
176 // static
177 bool OAuth2MintTokenFetcher::ParseMintTokenResponse(
178 const net::URLFetcher* source,
179 std::string* access_token) {
180 CHECK(source);
181 CHECK(access_token);
182 std::string data;
183 source->GetResponseAsString(&data);
184 scoped_ptr<base::Value> value(base::JSONReader::Read(data));
185 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY)
186 return false;
187
188 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get());
189 return dict->GetString(kAccessTokenKey, access_token);
190 }
OLDNEW
« no previous file with comments | « chrome/common/net/gaia/oauth2_mint_token_fetcher.h ('k') | chrome/common/net/gaia/oauth2_mint_token_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698