| OLD | NEW |
| (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/auth/arc_robot_auth.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 10 #include "components/policy/core/common/cloud/device_management_service.h" | |
| 11 #include "google_apis/gaia/gaia_constants.h" | |
| 12 #include "google_apis/gaia/gaia_urls.h" | |
| 13 #include "net/url_request/url_request_context_getter.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // OAuth2 Client id of Android. | |
| 18 constexpr char kAndoidClientId[] = | |
| 19 "1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.googleusercontent.com"; | |
| 20 | |
| 21 policy::DeviceManagementService* GetDeviceManagementService() { | |
| 22 policy::BrowserPolicyConnectorChromeOS* const connector = | |
| 23 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | |
| 24 return connector->device_management_service(); | |
| 25 } | |
| 26 | |
| 27 const policy::CloudPolicyClient* GetCloudPolicyClient() { | |
| 28 const policy::BrowserPolicyConnectorChromeOS* connector = | |
| 29 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | |
| 30 const policy::DeviceCloudPolicyManagerChromeOS* policy_manager = | |
| 31 connector->GetDeviceCloudPolicyManager(); | |
| 32 return policy_manager->core()->client(); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 namespace arc { | |
| 38 | |
| 39 ArcRobotAuth::ArcRobotAuth() : weak_ptr_factory_(this) {} | |
| 40 | |
| 41 ArcRobotAuth::~ArcRobotAuth() = default; | |
| 42 | |
| 43 void ArcRobotAuth::FetchRobotAuthCode(const RobotAuthCodeCallback& callback) { | |
| 44 DCHECK(!fetch_request_job_); | |
| 45 const policy::CloudPolicyClient* client = GetCloudPolicyClient(); | |
| 46 | |
| 47 policy::DeviceManagementService* service = GetDeviceManagementService(); | |
| 48 fetch_request_job_.reset(service->CreateJob( | |
| 49 policy::DeviceManagementRequestJob::TYPE_API_AUTH_CODE_FETCH, | |
| 50 g_browser_process->system_request_context())); | |
| 51 | |
| 52 fetch_request_job_->SetDMToken(client->dm_token()); | |
| 53 fetch_request_job_->SetClientID(client->client_id()); | |
| 54 | |
| 55 enterprise_management::DeviceServiceApiAccessRequest* request = | |
| 56 fetch_request_job_->GetRequest()->mutable_service_api_access_request(); | |
| 57 request->set_oauth2_client_id(kAndoidClientId); | |
| 58 request->add_auth_scope(GaiaConstants::kAnyApiOAuth2Scope); | |
| 59 | |
| 60 fetch_request_job_->Start( | |
| 61 base::Bind(&ArcRobotAuth::OnFetchRobotAuthCodeCompleted, | |
| 62 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 63 } | |
| 64 | |
| 65 void ArcRobotAuth::OnFetchRobotAuthCodeCompleted( | |
| 66 RobotAuthCodeCallback callback, | |
| 67 policy::DeviceManagementStatus status, | |
| 68 int net_error, | |
| 69 const enterprise_management::DeviceManagementResponse& response) { | |
| 70 fetch_request_job_.reset(); | |
| 71 | |
| 72 if (status == policy::DM_STATUS_SUCCESS && | |
| 73 (!response.has_service_api_access_response())) { | |
| 74 LOG(WARNING) << "Invalid service api access response."; | |
| 75 status = policy::DM_STATUS_RESPONSE_DECODING_ERROR; | |
| 76 } | |
| 77 | |
| 78 if (status != policy::DM_STATUS_SUCCESS) { | |
| 79 LOG(ERROR) << "Fetching of robot auth code failed. DM Status: " << status; | |
| 80 callback.Run(std::string()); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 callback.Run(response.service_api_access_response().auth_code()); | |
| 85 } | |
| 86 | |
| 87 } // namespace arc | |
| OLD | NEW |