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/gaia_oauth_client.h" | 5 #include "google_apis/gaia/gaia_oauth_client.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 const std::string& refresh_token, | 49 const std::string& refresh_token, |
50 const std::vector<std::string>& scopes, | 50 const std::vector<std::string>& scopes, |
51 int max_retries, | 51 int max_retries, |
52 GaiaOAuthClient::Delegate* delegate); | 52 GaiaOAuthClient::Delegate* delegate); |
53 void GetUserEmail(const std::string& oauth_access_token, | 53 void GetUserEmail(const std::string& oauth_access_token, |
54 int max_retries, | 54 int max_retries, |
55 Delegate* delegate); | 55 Delegate* delegate); |
56 void GetUserId(const std::string& oauth_access_token, | 56 void GetUserId(const std::string& oauth_access_token, |
57 int max_retries, | 57 int max_retries, |
58 Delegate* delegate); | 58 Delegate* delegate); |
59 void GetUserInfo(const std::string& oauth_access_token, | |
60 int max_retries, | |
61 Delegate* delegate); | |
62 void GetTokenInfo(const std::string& oauth_access_token, | 59 void GetTokenInfo(const std::string& oauth_access_token, |
63 int max_retries, | 60 int max_retries, |
64 Delegate* delegate); | 61 Delegate* delegate); |
65 | 62 |
66 // net::URLFetcherDelegate implementation. | 63 // net::URLFetcherDelegate implementation. |
67 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 64 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
68 | 65 |
69 private: | 66 private: |
70 friend class base::RefCountedThreadSafe<Core>; | 67 friend class base::RefCountedThreadSafe<Core>; |
71 | 68 |
72 enum RequestType { | 69 enum RequestType { |
73 NO_PENDING_REQUEST, | 70 NO_PENDING_REQUEST, |
74 TOKENS_FROM_AUTH_CODE, | 71 TOKENS_FROM_AUTH_CODE, |
75 REFRESH_TOKEN, | 72 REFRESH_TOKEN, |
76 TOKEN_INFO, | 73 TOKEN_INFO, |
77 USER_EMAIL, | 74 USER_EMAIL, |
78 USER_ID, | 75 USER_ID, |
79 USER_INFO, | |
80 }; | 76 }; |
81 | 77 |
82 virtual ~Core() {} | 78 virtual ~Core() {} |
83 | 79 |
84 void PeopleGet(const std::string& oauth_access_token, | 80 void GetUserInfo(const std::string& oauth_access_token, |
85 int max_retries, | 81 int max_retries, |
86 Delegate* delegate); | 82 Delegate* delegate); |
87 void MakeGaiaRequest(const GURL& url, | 83 void MakeGaiaRequest(const GURL& url, |
88 const std::string& post_body, | 84 const std::string& post_body, |
89 int max_retries, | 85 int max_retries, |
90 GaiaOAuthClient::Delegate* delegate); | 86 GaiaOAuthClient::Delegate* delegate); |
91 void HandleResponse(const net::URLFetcher* source, | 87 void HandleResponse(const net::URLFetcher* source, |
92 bool* should_retry_request); | 88 bool* should_retry_request); |
93 | 89 |
94 int num_retries_; | 90 int num_retries_; |
95 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 91 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
96 GaiaOAuthClient::Delegate* delegate_; | 92 GaiaOAuthClient::Delegate* delegate_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), | 138 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), |
143 post_body, max_retries, delegate); | 139 post_body, max_retries, delegate); |
144 } | 140 } |
145 | 141 |
146 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, | 142 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, |
147 int max_retries, | 143 int max_retries, |
148 Delegate* delegate) { | 144 Delegate* delegate) { |
149 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | 145 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); |
150 DCHECK(!request_.get()); | 146 DCHECK(!request_.get()); |
151 request_type_ = USER_EMAIL; | 147 request_type_ = USER_EMAIL; |
152 PeopleGet(oauth_access_token, max_retries, delegate); | 148 GetUserInfo(oauth_access_token, max_retries, delegate); |
153 } | 149 } |
154 | 150 |
155 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, | 151 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, |
156 int max_retries, | 152 int max_retries, |
157 Delegate* delegate) { | 153 Delegate* delegate) { |
158 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | 154 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); |
159 DCHECK(!request_.get()); | 155 DCHECK(!request_.get()); |
160 request_type_ = USER_ID; | 156 request_type_ = USER_ID; |
161 PeopleGet(oauth_access_token, max_retries, delegate); | 157 GetUserInfo(oauth_access_token, max_retries, delegate); |
162 } | 158 } |
163 | 159 |
164 void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, | 160 void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, |
165 int max_retries, | 161 int max_retries, |
166 Delegate* delegate) { | 162 Delegate* delegate) { |
167 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
168 DCHECK(!request_.get()); | |
169 request_type_ = USER_INFO; | |
170 PeopleGet(oauth_access_token, max_retries, delegate); | |
171 } | |
172 | |
173 void GaiaOAuthClient::Core::PeopleGet(const std::string& oauth_access_token, | |
174 int max_retries, | |
175 Delegate* delegate) { | |
176 delegate_ = delegate; | 163 delegate_ = delegate; |
177 num_retries_ = 0; | 164 num_retries_ = 0; |
178 request_.reset(net::URLFetcher::Create( | 165 request_.reset(net::URLFetcher::Create( |
179 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->people_get_url()), | 166 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), |
180 net::URLFetcher::GET, this)); | 167 net::URLFetcher::GET, this)); |
181 request_->SetRequestContext(request_context_getter_.get()); | 168 request_->SetRequestContext(request_context_getter_.get()); |
182 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); | 169 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); |
183 request_->SetMaxRetriesOn5xx(max_retries); | 170 request_->SetMaxRetriesOn5xx(max_retries); |
184 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 171 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
185 net::LOAD_DO_NOT_SAVE_COOKIES); | 172 net::LOAD_DO_NOT_SAVE_COOKIES); |
186 | 173 |
187 // Fetchers are sometimes cancelled because a network change was detected, | 174 // Fetchers are sometimes cancelled because a network change was detected, |
188 // especially at startup and after sign-in on ChromeOS. Retrying once should | 175 // especially at startup and after sign-in on ChromeOS. Retrying once should |
189 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 176 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 *should_retry_request = true; | 275 *should_retry_request = true; |
289 } | 276 } |
290 return; | 277 return; |
291 } | 278 } |
292 | 279 |
293 RequestType type = request_type_; | 280 RequestType type = request_type_; |
294 request_type_ = NO_PENDING_REQUEST; | 281 request_type_ = NO_PENDING_REQUEST; |
295 | 282 |
296 switch (type) { | 283 switch (type) { |
297 case USER_EMAIL: { | 284 case USER_EMAIL: { |
298 // Use first email of type "account" as the user's email. | 285 std::string email; |
299 const base::ListValue* emails_list; | 286 response_dict->GetString("email", &email); |
300 bool email_found = false; | 287 delegate_->OnGetUserEmailResponse(email); |
301 if (response_dict->GetList("emails", &emails_list)) { | |
302 for (size_t i = 0; i < emails_list->GetSize(); ++i) { | |
303 const base::DictionaryValue* email_dict; | |
304 if (emails_list->GetDictionary(i, &email_dict)) { | |
305 std::string email; | |
306 std::string type; | |
307 if (email_dict->GetString("type", &type) && | |
308 type == "account" && | |
309 email_dict->GetString("value", &email)) { | |
310 delegate_->OnGetUserEmailResponse(email); | |
311 email_found = true; | |
312 break; | |
313 } | |
314 } | |
315 } | |
316 } | |
317 if (!email_found) | |
318 delegate_->OnNetworkError(net::URLFetcher::RESPONSE_CODE_INVALID); | |
319 break; | 288 break; |
320 } | 289 } |
321 | 290 |
322 case USER_ID: { | 291 case USER_ID: { |
323 std::string id; | 292 std::string id; |
324 response_dict->GetString("id", &id); | 293 response_dict->GetString("id", &id); |
325 delegate_->OnGetUserIdResponse(id); | 294 delegate_->OnGetUserIdResponse(id); |
326 break; | 295 break; |
327 } | 296 } |
328 | 297 |
329 case USER_INFO: { | |
330 delegate_->OnGetUserInfoResponse(response_dict.Pass()); | |
331 break; | |
332 } | |
333 | |
334 case TOKEN_INFO: { | 298 case TOKEN_INFO: { |
335 delegate_->OnGetTokenInfoResponse(response_dict.Pass()); | 299 delegate_->OnGetTokenInfoResponse(response_dict.Pass()); |
336 break; | 300 break; |
337 } | 301 } |
338 | 302 |
339 case TOKENS_FROM_AUTH_CODE: | 303 case TOKENS_FROM_AUTH_CODE: |
340 case REFRESH_TOKEN: { | 304 case REFRESH_TOKEN: { |
341 std::string access_token; | 305 std::string access_token; |
342 std::string refresh_token; | 306 std::string refresh_token; |
343 int expires_in_seconds = 0; | 307 int expires_in_seconds = 0; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 Delegate* delegate) { | 365 Delegate* delegate) { |
402 return core_->GetUserEmail(access_token, max_retries, delegate); | 366 return core_->GetUserEmail(access_token, max_retries, delegate); |
403 } | 367 } |
404 | 368 |
405 void GaiaOAuthClient::GetUserId(const std::string& access_token, | 369 void GaiaOAuthClient::GetUserId(const std::string& access_token, |
406 int max_retries, | 370 int max_retries, |
407 Delegate* delegate) { | 371 Delegate* delegate) { |
408 return core_->GetUserId(access_token, max_retries, delegate); | 372 return core_->GetUserId(access_token, max_retries, delegate); |
409 } | 373 } |
410 | 374 |
411 void GaiaOAuthClient::GetUserInfo(const std::string& access_token, | |
412 int max_retries, | |
413 Delegate* delegate) { | |
414 return core_->GetUserInfo(access_token, max_retries, delegate); | |
415 } | |
416 | |
417 void GaiaOAuthClient::GetTokenInfo(const std::string& access_token, | 375 void GaiaOAuthClient::GetTokenInfo(const std::string& access_token, |
418 int max_retries, | 376 int max_retries, |
419 Delegate* delegate) { | 377 Delegate* delegate) { |
420 return core_->GetTokenInfo(access_token, max_retries, delegate); | 378 return core_->GetTokenInfo(access_token, max_retries, delegate); |
421 } | 379 } |
422 | 380 |
423 } // namespace gaia | 381 } // namespace gaia |
OLD | NEW |