OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 5 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "google_apis/gaia/gaia_urls.h" | 20 #include "google_apis/gaia/gaia_urls.h" |
20 #include "google_apis/gaia/google_service_auth_error.h" | 21 #include "google_apis/gaia/google_service_auth_error.h" |
21 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
22 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
24 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
25 | 26 |
26 using net::URLFetcher; | 27 using net::URLFetcher; |
(...skipping 11 matching lines...) Expand all Loading... |
38 "force=%s" | 39 "force=%s" |
39 "&response_type=%s" | 40 "&response_type=%s" |
40 "&scope=%s" | 41 "&scope=%s" |
41 "&client_id=%s" | 42 "&client_id=%s" |
42 "&origin=%s"; | 43 "&origin=%s"; |
43 static const char kIssueAdviceKey[] = "issueAdvice"; | 44 static const char kIssueAdviceKey[] = "issueAdvice"; |
44 static const char kIssueAdviceValueAuto[] = "auto"; | 45 static const char kIssueAdviceValueAuto[] = "auto"; |
45 static const char kIssueAdviceValueConsent[] = "consent"; | 46 static const char kIssueAdviceValueConsent[] = "consent"; |
46 static const char kAccessTokenKey[] = "token"; | 47 static const char kAccessTokenKey[] = "token"; |
47 static const char kConsentKey[] = "consent"; | 48 static const char kConsentKey[] = "consent"; |
| 49 static const char kExpiresInKey[] = "expiresIn"; |
48 static const char kScopesKey[] = "scopes"; | 50 static const char kScopesKey[] = "scopes"; |
49 static const char kDescriptionKey[] = "description"; | 51 static const char kDescriptionKey[] = "description"; |
50 static const char kDetailKey[] = "detail"; | 52 static const char kDetailKey[] = "detail"; |
51 static const char kDetailSeparators[] = "\n"; | 53 static const char kDetailSeparators[] = "\n"; |
52 | 54 |
53 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { | 55 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { |
54 if (status.status() == URLRequestStatus::CANCELED) { | 56 if (status.status() == URLRequestStatus::CANCELED) { |
55 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 57 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
56 } else { | 58 } else { |
57 // TODO(munjal): Improve error handling. Currently we return connection | 59 // TODO(munjal): Improve error handling. Currently we return connection |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 : OAuth2ApiCallFlow(context, | 97 : OAuth2ApiCallFlow(context, |
96 parameters.login_refresh_token, | 98 parameters.login_refresh_token, |
97 std::string(), | 99 std::string(), |
98 std::vector<std::string>()), | 100 std::vector<std::string>()), |
99 delegate_(delegate), | 101 delegate_(delegate), |
100 parameters_(parameters), | 102 parameters_(parameters), |
101 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 103 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
102 | 104 |
103 OAuth2MintTokenFlow::~OAuth2MintTokenFlow() { } | 105 OAuth2MintTokenFlow::~OAuth2MintTokenFlow() { } |
104 | 106 |
105 void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token) { | 107 void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token, |
| 108 int time_to_live) { |
106 if (delegate_) | 109 if (delegate_) |
107 delegate_->OnMintTokenSuccess(access_token); | 110 delegate_->OnMintTokenSuccess(access_token, time_to_live); |
108 | 111 |
109 // |this| may already be deleted. | 112 // |this| may already be deleted. |
110 } | 113 } |
111 | 114 |
112 void OAuth2MintTokenFlow::ReportIssueAdviceSuccess( | 115 void OAuth2MintTokenFlow::ReportIssueAdviceSuccess( |
113 const IssueAdviceInfo& issue_advice) { | 116 const IssueAdviceInfo& issue_advice) { |
114 if (delegate_) | 117 if (delegate_) |
115 delegate_->OnIssueAdviceSuccess(issue_advice); | 118 delegate_->OnIssueAdviceSuccess(issue_advice); |
116 | 119 |
117 // |this| may already be deleted. | 120 // |this| may already be deleted. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 return; | 170 return; |
168 } | 171 } |
169 if (issue_advice == kIssueAdviceValueConsent) { | 172 if (issue_advice == kIssueAdviceValueConsent) { |
170 IssueAdviceInfo issue_advice; | 173 IssueAdviceInfo issue_advice; |
171 if (ParseIssueAdviceResponse(dict, &issue_advice)) | 174 if (ParseIssueAdviceResponse(dict, &issue_advice)) |
172 ReportIssueAdviceSuccess(issue_advice); | 175 ReportIssueAdviceSuccess(issue_advice); |
173 else | 176 else |
174 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | 177 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
175 } else { | 178 } else { |
176 std::string access_token; | 179 std::string access_token; |
177 if (ParseMintTokenResponse(dict, &access_token)) | 180 int time_to_live; |
178 ReportSuccess(access_token); | 181 if (ParseMintTokenResponse(dict, &access_token, &time_to_live)) |
| 182 ReportSuccess(access_token, time_to_live); |
179 else | 183 else |
180 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | 184 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
181 } | 185 } |
182 | 186 |
183 // |this| may be deleted! | 187 // |this| may be deleted! |
184 } | 188 } |
185 | 189 |
186 void OAuth2MintTokenFlow::ProcessApiCallFailure( | 190 void OAuth2MintTokenFlow::ProcessApiCallFailure( |
187 const net::URLFetcher* source) { | 191 const net::URLFetcher* source) { |
188 ReportFailure(CreateAuthError(source->GetStatus())); | 192 ReportFailure(CreateAuthError(source->GetStatus())); |
189 } | 193 } |
190 void OAuth2MintTokenFlow::ProcessNewAccessToken( | 194 void OAuth2MintTokenFlow::ProcessNewAccessToken( |
191 const std::string& access_token) { | 195 const std::string& access_token) { |
192 // We don't currently store new access tokens. We generate one every time. | 196 // We don't currently store new access tokens. We generate one every time. |
193 // So we have nothing to do here. | 197 // So we have nothing to do here. |
194 return; | 198 return; |
195 } | 199 } |
196 void OAuth2MintTokenFlow::ProcessMintAccessTokenFailure( | 200 void OAuth2MintTokenFlow::ProcessMintAccessTokenFailure( |
197 const GoogleServiceAuthError& error) { | 201 const GoogleServiceAuthError& error) { |
198 ReportFailure(error); | 202 ReportFailure(error); |
199 } | 203 } |
200 | 204 |
201 // static | 205 // static |
202 bool OAuth2MintTokenFlow::ParseMintTokenResponse( | 206 bool OAuth2MintTokenFlow::ParseMintTokenResponse( |
203 const base::DictionaryValue* dict, std::string* access_token) { | 207 const base::DictionaryValue* dict, std::string* access_token, |
| 208 int* time_to_live) { |
204 CHECK(dict); | 209 CHECK(dict); |
205 CHECK(access_token); | 210 CHECK(access_token); |
206 return dict->GetString(kAccessTokenKey, access_token); | 211 CHECK(time_to_live); |
| 212 std::string ttl_string; |
| 213 return dict->GetString(kExpiresInKey, &ttl_string) && |
| 214 base::StringToInt(ttl_string, time_to_live) && |
| 215 dict->GetString(kAccessTokenKey, access_token); |
207 } | 216 } |
208 | 217 |
209 // static | 218 // static |
210 bool OAuth2MintTokenFlow::ParseIssueAdviceResponse( | 219 bool OAuth2MintTokenFlow::ParseIssueAdviceResponse( |
211 const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice) { | 220 const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice) { |
212 CHECK(dict); | 221 CHECK(dict); |
213 CHECK(issue_advice); | 222 CHECK(issue_advice); |
214 | 223 |
215 const base::DictionaryValue* consent_dict = NULL; | 224 const base::DictionaryValue* consent_dict = NULL; |
216 if (!dict->GetDictionary(kConsentKey, &consent_dict)) | 225 if (!dict->GetDictionary(kConsentKey, &consent_dict)) |
(...skipping 21 matching lines...) Expand all Loading... |
238 for (size_t i = 0; i < entry.details.size(); i++) | 247 for (size_t i = 0; i < entry.details.size(); i++) |
239 TrimWhitespace(entry.details[i], TRIM_ALL, &entry.details[i]); | 248 TrimWhitespace(entry.details[i], TRIM_ALL, &entry.details[i]); |
240 issue_advice->push_back(entry); | 249 issue_advice->push_back(entry); |
241 } | 250 } |
242 | 251 |
243 if (!success) | 252 if (!success) |
244 issue_advice->clear(); | 253 issue_advice->clear(); |
245 | 254 |
246 return success; | 255 return success; |
247 } | 256 } |
OLD | NEW |