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

Side by Side Diff: chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc

Issue 2320813002: arc: Enable silent PlayStore Sign-In. (Closed)
Patch Set: nits Created 4 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
OLDNEW
(Empty)
1 // Copyright 2016 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/browser/chromeos/arc/arc_auth_code_fetcher.h"
6
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/json/json_writer.h"
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
14 #include "components/signin/core/account_id/account_id.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "components/signin/core/browser/signin_manager_base.h"
17 #include "components/user_manager/known_user.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/common/url_constants.h"
20 #include "google_apis/gaia/gaia_auth_fetcher.h"
21 #include "google_apis/gaia/gaia_constants.h"
22 #include "net/base/load_flags.h"
23 #include "net/http/http_status_code.h"
24 #include "net/url_request/url_fetcher.h"
25
26 namespace arc {
27
28 namespace {
29
30 constexpr int kGetAuthCodeNetworkRetry = 3;
31
32 constexpr char kConsumerName[] = "ArcAuthContext";
33 constexpr char kToken[] = "token";
34 constexpr char kDeviceId[] = "device_id";
35 constexpr char kDeviceType[] = "device_type";
36 constexpr char kDeviceTypeArc[] = "arc_plus_plus";
37 constexpr char kLoginScopedToken[] = "login_scoped_token";
38 constexpr char kGetAuthCodeHeaders[] =
39 "Content-Type: application/json; charset=utf-8";
40 constexpr char kContentTypeJSON[] = "application/json";
41
42 } // namespace
43
44 ArcAuthCodeFetcher::ArcAuthCodeFetcher(
45 ArcAuthCodeFetcherDelegate* delegate,
46 net::URLRequestContextGetter* request_context_getter,
47 Profile* profile,
48 const std::string& auth_endpoint)
49 : OAuth2TokenService::Consumer(kConsumerName),
50 delegate_(delegate),
51 request_context_getter_(request_context_getter),
52 profile_(profile),
53 auth_endpoint_(auth_endpoint) {
54 // Get token service and account ID to fetch auth tokens.
55 ProfileOAuth2TokenService* const token_service =
56 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
57 const SigninManagerBase* const signin_manager =
58 SigninManagerFactory::GetForProfile(profile_);
59 CHECK(token_service && signin_manager);
60 const std::string& account_id = signin_manager->GetAuthenticatedAccountId();
61 DCHECK(!account_id.empty());
62 DCHECK(token_service->RefreshTokenIsAvailable(account_id));
63
64 OAuth2TokenService::ScopeSet scopes;
65 scopes.insert(GaiaConstants::kOAuth1LoginScope);
66 login_token_request_.reset(
67 token_service->StartRequest(account_id, scopes, this).release());
68 }
69
70 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() {}
71
72 void ArcAuthCodeFetcher::OnGetTokenSuccess(
73 const OAuth2TokenService::Request* request,
74 const std::string& access_token,
75 const base::Time& expiration_time) {
76 ResetFetchers();
77
78 const std::string device_id = user_manager::known_user::GetDeviceId(
79 multi_user_util::GetAccountIdFromProfile(profile_));
80 DCHECK(!device_id.empty());
81
82 base::DictionaryValue request_data;
83 request_data.SetString(kLoginScopedToken, access_token);
84 request_data.SetString(kDeviceType, kDeviceTypeArc);
85 request_data.SetString(kDeviceId, device_id);
86 std::string request_string;
87 base::JSONWriter::Write(request_data, &request_string);
88
89 DCHECK(!auth_endpoint_.empty());
90 auth_code_fetcher_ = net::URLFetcher::Create(0, GURL(auth_endpoint_),
91 net::URLFetcher::POST, this);
92 auth_code_fetcher_->SetRequestContext(request_context_getter_);
93 auth_code_fetcher_->SetUploadData(kContentTypeJSON, request_string);
94 auth_code_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE |
95 net::LOAD_BYPASS_CACHE);
96 auth_code_fetcher_->SetAutomaticallyRetryOnNetworkChanges(
97 kGetAuthCodeNetworkRetry);
98 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders);
99 auth_code_fetcher_->Start();
100 }
101
102 void ArcAuthCodeFetcher::OnGetTokenFailure(
103 const OAuth2TokenService::Request* request,
104 const GoogleServiceAuthError& error) {
105 VLOG(2) << "Failed to get LST " << error.ToString() << ".";
106 ResetFetchers();
107
108 delegate_->OnAuthCodeFailed();
109 }
110
111 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
112 const int response_code = source->GetResponseCode();
113 std::string json_string;
114 source->GetResponseAsString(&json_string);
115
116 ResetFetchers();
117
118 if (response_code != net::HTTP_OK) {
119 VLOG(2) << "Server returned wrong response code: " << response_code << ".";
120 delegate_->OnAuthCodeFailed();
121 return;
122 }
123
124 JSONStringValueDeserializer deserializer(json_string);
125 std::string error_msg;
126 std::unique_ptr<base::Value> auth_code_info =
127 deserializer.Deserialize(nullptr, &error_msg);
128 if (!auth_code_info) {
129 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg
130 << ".";
131 delegate_->OnAuthCodeFailed();
132 return;
133 }
134
135 std::unique_ptr<base::DictionaryValue> auth_code_dictionary =
136 base::DictionaryValue::From(std::move(auth_code_info));
137 if (!auth_code_dictionary) {
138 NOTREACHED();
139 delegate_->OnAuthCodeFailed();
140 return;
141 }
142
143 std::string auth_code;
144 if (!auth_code_dictionary->GetString(kToken, &auth_code) ||
145 auth_code.empty()) {
146 VLOG(2) << "Response does not contain auth code.";
147 delegate_->OnAuthCodeFailed();
148 return;
149 }
150
151 delegate_->OnAuthCodeSuccess(auth_code);
152 }
153
154 void ArcAuthCodeFetcher::ResetFetchers() {
155 login_token_request_.reset();
156 auth_code_fetcher_.reset();
157 }
158
159 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_auth_code_fetcher.h ('k') | chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698