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

Side by Side Diff: components/policy/core/common/cloud/user_info_fetcher.cc

Issue 265563002: Revert of Use new people.get api instead of oauth2/v1/userinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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
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 "components/policy/core/common/cloud/user_info_fetcher.h" 5 #include "components/policy/core/common/cloud/user_info_fetcher.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/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "google_apis/gaia/gaia_urls.h" 11 #include "google_apis/gaia/gaia_urls.h"
12 #include "google_apis/gaia/google_service_auth_error.h" 12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "net/base/load_flags.h" 13 #include "net/base/load_flags.h"
14 #include "net/http/http_status_code.h" 14 #include "net/http/http_status_code.h"
15 #include "net/url_request/url_fetcher.h" 15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_status.h" 16 #include "net/url_request/url_request_status.h"
17 #include "url/gurl.h" 17 #include "url/gurl.h"
18 18
19 namespace {
20
21 static const char kAuthorizationHeaderFormat[] =
22 "Authorization: Bearer %s";
23
24 static std::string MakeAuthorizationHeader(const std::string& auth_token) {
25 return base::StringPrintf(kAuthorizationHeaderFormat, auth_token.c_str());
26 }
27
28 } // namespace
29
19 namespace policy { 30 namespace policy {
20 31
21 UserInfoFetcher::UserInfoFetcher(Delegate* delegate, 32 UserInfoFetcher::UserInfoFetcher(Delegate* delegate,
22 net::URLRequestContextGetter* context) 33 net::URLRequestContextGetter* context)
23 : delegate_(delegate), gaia_client_(context) { 34 : delegate_(delegate),
35 context_(context) {
24 DCHECK(delegate); 36 DCHECK(delegate);
25 } 37 }
26 38
27 UserInfoFetcher::~UserInfoFetcher() { 39 UserInfoFetcher::~UserInfoFetcher() {
28 } 40 }
29 41
30 void UserInfoFetcher::Start(const std::string& access_token) { 42 void UserInfoFetcher::Start(const std::string& access_token) {
31 // Create a URLFetcher and start it. 43 // Create a URLFetcher and start it.
32 gaia_client_.GetUserInfo(access_token, 0, &delegate_); 44 url_fetcher_.reset(net::URLFetcher::Create(
45 0, GaiaUrls::GetInstance()->oauth_user_info_url(),
46 net::URLFetcher::GET, this));
47 url_fetcher_->SetRequestContext(context_);
48 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
49 net::LOAD_DO_NOT_SAVE_COOKIES);
50 url_fetcher_->AddExtraRequestHeader(MakeAuthorizationHeader(access_token));
51 url_fetcher_->Start(); // Results in a call to OnURLFetchComplete().
33 } 52 }
34 53
35 UserInfoFetcher::GaiaDelegate::GaiaDelegate(UserInfoFetcher::Delegate* delegate) 54 void UserInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
36 : delegate_(delegate) { 55 net::URLRequestStatus status = source->GetStatus();
37 } 56 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone();
57 if (!status.is_success()) {
58 if (status.status() == net::URLRequestStatus::CANCELED)
59 error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
60 else
61 error = GoogleServiceAuthError::FromConnectionError(status.error());
62 } else if (source->GetResponseCode() != net::HTTP_OK) {
63 DLOG(WARNING) << "UserInfo request failed with HTTP code: "
64 << source->GetResponseCode();
65 error = GoogleServiceAuthError(
66 GoogleServiceAuthError::CONNECTION_FAILED);
67 }
68 if (error.state() != GoogleServiceAuthError::NONE) {
69 delegate_->OnGetUserInfoFailure(error);
70 return;
71 }
38 72
39 void UserInfoFetcher::GaiaDelegate::OnGetUserInfoResponse( 73 // Successfully fetched userinfo from the server - parse it and hand it off
40 scoped_ptr<base::DictionaryValue> user_info) { 74 // to the delegate.
41 delegate_->OnGetUserInfoSuccess(user_info.get()); 75 std::string unparsed_data;
42 } 76 source->GetResponseAsString(&unparsed_data);
43 77 DVLOG(1) << "Received UserInfo response: " << unparsed_data;
44 void UserInfoFetcher::GaiaDelegate::OnOAuthError() { 78 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(unparsed_data));
45 GoogleServiceAuthError error = 79 base::DictionaryValue* dict;
46 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 80 if (parsed_value.get() && parsed_value->GetAsDictionary(&dict)) {
47 delegate_->OnGetUserInfoFailure(error); 81 delegate_->OnGetUserInfoSuccess(dict);
48 } 82 } else {
49 83 NOTREACHED() << "Could not parse userinfo response from server";
50 void UserInfoFetcher::GaiaDelegate::OnNetworkError(int response_code) { 84 delegate_->OnGetUserInfoFailure(GoogleServiceAuthError(
51 GoogleServiceAuthError error = 85 GoogleServiceAuthError::CONNECTION_FAILED));
52 GoogleServiceAuthError::FromConnectionError(response_code); 86 }
53 delegate_->OnGetUserInfoFailure(error);
54 } 87 }
55 88
56 }; // namespace policy 89 }; // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698