OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "components/proximity_auth/remote_device_loader.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "components/proximity_auth/cryptauth/secure_message_delegate.h" | |
9 #include "components/proximity_auth/logging/logging.h" | |
10 | |
11 namespace proximity_auth { | |
12 | |
13 RemoteDeviceLoader::RemoteDeviceLoader( | |
14 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys, | |
15 const std::string& user_id, | |
16 const std::string& user_private_key, | |
17 scoped_ptr<SecureMessageDelegate> secure_message_delegate) | |
18 : remaining_unlock_keys_(unlock_keys), | |
19 user_id_(user_id), | |
20 user_private_key_(user_private_key), | |
21 secure_message_delegate_(secure_message_delegate.Pass()), | |
22 weak_ptr_factory_(this) {} | |
23 | |
24 RemoteDeviceLoader::~RemoteDeviceLoader() {} | |
25 | |
26 void RemoteDeviceLoader::Load(const RemoteDeviceCallback& callback) { | |
27 DCHECK(callback_.is_null()); | |
28 callback_ = callback; | |
29 PA_LOG(INFO) << "Loading " << remaining_unlock_keys_.size() | |
30 << " remote devices"; | |
31 | |
32 if (!remaining_unlock_keys_.size()) { | |
sacomoto
2015/09/24 12:41:59
nit: remaining_unlock_keys_.empty()
Tim Song
2015/09/26 02:08:14
Done.
| |
33 callback_.Run(remote_devices_); | |
34 return; | |
35 } | |
36 | |
37 std::vector<cryptauth::ExternalDeviceInfo> all_unlock_keys = | |
38 remaining_unlock_keys_; | |
39 | |
40 for (const auto& unlock_key : all_unlock_keys) { | |
41 secure_message_delegate_->DeriveKey( | |
42 user_private_key_, unlock_key.public_key(), | |
43 base::Bind(&RemoteDeviceLoader::OnPSKDerived, | |
44 weak_ptr_factory_.GetWeakPtr(), unlock_key)); | |
45 } | |
46 } | |
47 | |
48 void RemoteDeviceLoader::OnPSKDerived( | |
49 const cryptauth::ExternalDeviceInfo& unlock_key, | |
50 const std::string& psk) { | |
51 std::string public_key = unlock_key.public_key(); | |
52 auto iterator = std::find_if( | |
53 remaining_unlock_keys_.begin(), remaining_unlock_keys_.end(), | |
54 [&public_key](const cryptauth::ExternalDeviceInfo& unlock_key) { | |
55 return unlock_key.public_key() == public_key; | |
56 }); | |
57 | |
58 DCHECK(iterator != remaining_unlock_keys_.end()); | |
59 remaining_unlock_keys_.erase(iterator); | |
60 PA_LOG(INFO) << "Derived psk for " << unlock_key.friendly_device_name() | |
sacomoto
2015/09/24 12:41:59
nit: s/psk/PSK/.
Tim Song
2015/09/26 02:08:14
Done.
| |
61 << ", " << remaining_unlock_keys_.size() << " keys remaining."; | |
62 | |
63 remote_devices_.push_back(RemoteDevice( | |
64 user_id_, unlock_key.friendly_device_name(), unlock_key.public_key(), | |
65 unlock_key.bluetooth_address(), psk)); | |
66 | |
67 if (!remaining_unlock_keys_.size()) | |
68 callback_.Run(remote_devices_); | |
69 } | |
70 | |
71 } // namespace proximity_auth | |
OLD | NEW |