1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_get_keys_operation.h"
10 #include "base/logging.h"
11 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
12 #include "google_apis/gaia/gaia_auth_util.h"
16 EasyUnlockGetKeysOperation::EasyUnlockGetKeysOperation(
17 const UserContext
& user_context
,
18 const GetKeysCallback
& callback
)
19 : user_context_(user_context
),
22 weak_ptr_factory_(this) {
25 EasyUnlockGetKeysOperation::~EasyUnlockGetKeysOperation() {
28 void EasyUnlockGetKeysOperation::Start() {
29 // TODO(xiyuan): Use ListKeyEx.
34 void EasyUnlockGetKeysOperation::GetKeyData() {
35 std::string canonicalized
=
36 gaia::CanonicalizeEmail(user_context_
.GetUserID());
37 cryptohome::Identification
id(canonicalized
);
38 cryptohome::HomedirMethods::GetInstance()->GetKeyDataEx(
40 EasyUnlockKeyManager::GetKeyLabel(key_index_
),
41 base::Bind(&EasyUnlockGetKeysOperation::OnGetKeyData
,
42 weak_ptr_factory_
.GetWeakPtr()));
46 void EasyUnlockGetKeysOperation::OnGetKeyData(
48 cryptohome::MountError return_code
,
49 const std::vector
<cryptohome::KeyDefinition
>& key_definitions
) {
50 if (!success
|| key_definitions
.empty()) {
51 // MOUNT_ERROR_KEY_FAILURE is considered as success. Other error codes are
52 // treated as failures.
53 if (return_code
== cryptohome::MOUNT_ERROR_NONE
||
54 return_code
== cryptohome::MOUNT_ERROR_KEY_FAILURE
) {
55 callback_
.Run(true, devices_
);
59 LOG(ERROR
) << "Easy unlock failed to get key data, code=" << return_code
;
60 callback_
.Run(false, EasyUnlockDeviceKeyDataList());
64 DCHECK_EQ(1u, key_definitions
.size());
66 const std::vector
<cryptohome::KeyDefinition::ProviderData
>& provider_data
=
67 key_definitions
.front().provider_data
;
69 EasyUnlockDeviceKeyData device
;
70 for (size_t i
= 0; i
< provider_data
.size(); ++i
) {
71 const cryptohome::KeyDefinition::ProviderData
& entry
= provider_data
[i
];
72 if (entry
.name
== kEasyUnlockKeyMetaNameBluetoothAddress
) {
74 device
.bluetooth_address
= *entry
.bytes
;
77 } else if (entry
.name
== kEasyUnlockKeyMetaNamePubKey
) {
79 device
.public_key
= *entry
.bytes
;
82 } else if (entry
.name
== kEasyUnlockKeyMetaNamePsk
) {
84 device
.psk
= *entry
.bytes
;
87 } else if (entry
.name
== kEasyUnlockKeyMetaNameChallenge
) {
89 device
.challenge
= *entry
.bytes
;
92 } else if (entry
.name
== kEasyUnlockKeyMetaNameWrappedSecret
) {
94 device
.wrapped_secret
= *entry
.bytes
;
98 LOG(WARNING
) << "Unknown Easy unlock key data entry, name="
102 devices_
.push_back(device
);
108 } // namespace chromeos