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 "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 | |
30 namespace policy { | 19 namespace policy { |
31 | 20 |
32 UserInfoFetcher::UserInfoFetcher(Delegate* delegate, | 21 UserInfoFetcher::UserInfoFetcher(Delegate* delegate, |
33 net::URLRequestContextGetter* context) | 22 net::URLRequestContextGetter* context) |
34 : delegate_(delegate), | 23 : delegate_(delegate), gaia_client_(context) { |
35 context_(context) { | |
36 DCHECK(delegate); | 24 DCHECK(delegate); |
37 } | 25 } |
38 | 26 |
39 UserInfoFetcher::~UserInfoFetcher() { | 27 UserInfoFetcher::~UserInfoFetcher() { |
40 } | 28 } |
41 | 29 |
42 void UserInfoFetcher::Start(const std::string& access_token) { | 30 void UserInfoFetcher::Start(const std::string& access_token) { |
43 // Create a URLFetcher and start it. | 31 // Create a URLFetcher and start it. |
44 url_fetcher_.reset(net::URLFetcher::Create( | 32 gaia_client_.GetUserInfo(access_token, 0, &delegate_); |
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(). | |
52 } | 33 } |
53 | 34 |
54 void UserInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 35 UserInfoFetcher::GaiaDelegate::GaiaDelegate(UserInfoFetcher::Delegate* delegate) |
55 net::URLRequestStatus status = source->GetStatus(); | 36 : delegate_(delegate) { |
56 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone(); | 37 } |
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 } | |
72 | 38 |
73 // Successfully fetched userinfo from the server - parse it and hand it off | 39 void UserInfoFetcher::GaiaDelegate::OnGetUserInfoResponse( |
74 // to the delegate. | 40 scoped_ptr<base::DictionaryValue> user_info) { |
75 std::string unparsed_data; | 41 delegate_->OnGetUserInfoSuccess(user_info.get()); |
76 source->GetResponseAsString(&unparsed_data); | 42 } |
77 DVLOG(1) << "Received UserInfo response: " << unparsed_data; | 43 |
78 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(unparsed_data)); | 44 void UserInfoFetcher::GaiaDelegate::OnOAuthError() { |
79 base::DictionaryValue* dict; | 45 GoogleServiceAuthError error = |
80 if (parsed_value.get() && parsed_value->GetAsDictionary(&dict)) { | 46 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
81 delegate_->OnGetUserInfoSuccess(dict); | 47 delegate_->OnGetUserInfoFailure(error); |
82 } else { | 48 } |
83 NOTREACHED() << "Could not parse userinfo response from server"; | 49 |
84 delegate_->OnGetUserInfoFailure(GoogleServiceAuthError( | 50 void UserInfoFetcher::GaiaDelegate::OnNetworkError(int response_code) { |
85 GoogleServiceAuthError::CONNECTION_FAILED)); | 51 GoogleServiceAuthError error = |
86 } | 52 GoogleServiceAuthError::FromConnectionError(response_code); |
| 53 delegate_->OnGetUserInfoFailure(error); |
87 } | 54 } |
88 | 55 |
89 }; // namespace policy | 56 }; // namespace policy |
OLD | NEW |