OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 5 #ifndef REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
6 #define REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 6 #define REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 class ListValue; | 21 class ListValue; |
22 class Value; | |
22 } // namespace base | 23 } // namespace base |
23 | 24 |
24 namespace remoting { | 25 namespace remoting { |
25 namespace protocol { | 26 namespace protocol { |
26 | 27 |
27 // PairingRegistry holds information about paired clients to support | 28 // PairingRegistry holds information about paired clients to support |
28 // PIN-less authentication. For each paired client, the registry holds | 29 // PIN-less authentication. For each paired client, the registry holds |
29 // the following information: | 30 // the following information: |
30 // * The name of the client. This is supplied by the client and is not | 31 // * The name of the client. This is supplied by the client and is not |
31 // guaranteed to be unique. | 32 // guaranteed to be unique. |
32 // * The unique id of the client. This is generated on-demand by this | 33 // * The unique id of the client. This is generated on-demand by this |
33 // class and sent in plain-text by the client during authentication. | 34 // class and sent in plain-text by the client during authentication. |
34 // * The shared secret for the client. This is generated on-demand by this | 35 // * The shared secret for the client. This is generated on-demand by this |
35 // class and used in the SPAKE2 exchange to mutually verify identity. | 36 // class and used in the SPAKE2 exchange to mutually verify identity. |
36 class PairingRegistry : public base::RefCountedThreadSafe<PairingRegistry>, | 37 class PairingRegistry : public base::RefCountedThreadSafe<PairingRegistry>, |
37 public base::NonThreadSafe { | 38 public base::NonThreadSafe { |
38 public: | 39 public: |
39 struct Pairing { | 40 struct Pairing { |
40 Pairing(); | 41 Pairing(); |
41 Pairing(const base::Time& created_time, | 42 Pairing(const base::Time& created_time, |
42 const std::string& client_name, | 43 const std::string& client_name, |
43 const std::string& client_id, | 44 const std::string& client_id, |
44 const std::string& shared_secret); | 45 const std::string& shared_secret); |
45 ~Pairing(); | 46 ~Pairing(); |
46 | 47 |
47 static Pairing Create(const std::string& client_name); | 48 static Pairing Create(const std::string& client_name); |
49 static Pairing CreateFromJson(const base::Value& pairing_json); | |
50 | |
51 scoped_ptr<base::Value> EncodeJson() const; | |
Jamie
2013/07/30 21:35:07
These methods don't deal with JSON. It might make
alexeypa (please no reviews)
2013/07/31 21:31:24
Done.
| |
48 | 52 |
49 bool operator==(const Pairing& other) const; | 53 bool operator==(const Pairing& other) const; |
50 | 54 |
51 bool is_valid() const; | 55 bool is_valid() const; |
52 | 56 |
53 base::Time created_time() const { return created_time_; } | 57 base::Time created_time() const { return created_time_; } |
54 std::string client_id() const { return client_id_; } | 58 std::string client_id() const { return client_id_; } |
55 std::string client_name() const { return client_name_; } | 59 std::string client_name() const { return client_name_; } |
56 std::string shared_secret() const { return shared_secret_; } | 60 std::string shared_secret() const { return shared_secret_; } |
57 | 61 |
58 private: | 62 private: |
59 base::Time created_time_; | 63 base::Time created_time_; |
60 std::string client_name_; | 64 std::string client_name_; |
61 std::string client_id_; | 65 std::string client_id_; |
62 std::string shared_secret_; | 66 std::string shared_secret_; |
63 }; | 67 }; |
64 | 68 |
65 // Mapping from client id to pairing information. | 69 // Mapping from client id to pairing information. |
66 typedef std::map<std::string, Pairing> PairedClients; | 70 typedef std::map<std::string, Pairing> PairedClients; |
67 | 71 |
68 // Delegate callbacks. | 72 // Delegate callbacks. |
69 typedef base::Callback<void(const std::string& pairings_json)> LoadCallback; | 73 typedef base::Callback<void(bool success)> DoneCallback; |
70 typedef base::Callback<void(bool success)> SaveCallback; | |
71 typedef base::Callback<void(Pairing pairing)> GetPairingCallback; | |
72 typedef base::Callback<void(scoped_ptr<base::ListValue> pairings)> | 74 typedef base::Callback<void(scoped_ptr<base::ListValue> pairings)> |
73 GetAllPairingsCallback; | 75 GetAllPairingsCallback; |
76 typedef base::Callback<void(Pairing pairing)> GetPairingCallback; | |
74 | 77 |
75 static const char kCreatedTimeKey[]; | 78 static const char kCreatedTimeKey[]; |
76 static const char kClientIdKey[]; | 79 static const char kClientIdKey[]; |
77 static const char kClientNameKey[]; | 80 static const char kClientNameKey[]; |
78 static const char kSharedSecretKey[]; | 81 static const char kSharedSecretKey[]; |
79 | 82 |
80 // Interface representing the persistent storage back-end. | 83 // Interface representing the persistent storage back-end. |
81 class Delegate { | 84 class Delegate { |
82 public: | 85 public: |
83 virtual ~Delegate() {} | 86 virtual ~Delegate() {} |
84 | 87 |
85 // Save JSON-encoded pairing information to persistent storage. If | 88 // Retrieves all JSON-encoded pairings from persistent storage. Must not |
86 // a non-NULL callback is provided, invoke it on completion to | 89 // block. |
Jamie
2013/07/30 21:35:07
Since you've removed the SanitizePairings method,
alexeypa (please no reviews)
2013/07/31 21:31:24
I resurrected SanitizePairings().
| |
87 // indicate success or failure. Must not block. | 90 virtual void LoadAll(const GetAllPairingsCallback& callback) = 0; |
88 virtual void Save(const std::string& pairings_json, | |
89 const SaveCallback& callback) = 0; | |
90 | 91 |
91 // Retrieve the JSON-encoded pairing information from persistent | 92 // Deletes all pairings in persistent storage. Must not block. |
92 // storage. Must not block. | 93 virtual void DeleteAll(const DoneCallback& callback) = 0; |
93 virtual void Load(const LoadCallback& callback) = 0; | 94 |
95 // Retrieves the pairing identified by |client_id|. Must not block. | |
96 virtual void Load(const std::string& client_id, | |
97 const GetPairingCallback& callback) = 0; | |
98 | |
99 // Saves |pairing| to persistent storage. Must not block. | |
100 virtual void Save(const Pairing& pairing, | |
101 const DoneCallback& callback) = 0; | |
102 | |
103 // Deletes the pairing identified by |client_id|. Must not block. | |
104 virtual void Delete(const std::string& client_id, | |
105 const DoneCallback& callback) = 0; | |
94 }; | 106 }; |
95 | 107 |
96 explicit PairingRegistry(scoped_ptr<Delegate> delegate); | 108 explicit PairingRegistry(scoped_ptr<Delegate> delegate); |
97 | 109 |
98 // Creates a pairing for a new client and saves it to disk. | 110 // Creates a pairing for a new client and saves it to disk. |
99 // | 111 // |
100 // TODO(jamiewalch): Plumb the Save callback into the RequestPairing flow | 112 // TODO(jamiewalch): Plumb the Save callback into the RequestPairing flow |
101 // so that the client isn't sent the pairing information until it has been | 113 // so that the client isn't sent the pairing information until it has been |
102 // saved. | 114 // saved. |
103 Pairing CreatePairing(const std::string& client_name); | 115 Pairing CreatePairing(const std::string& client_name); |
104 | 116 |
105 // Gets the pairing for the specified client id. See the corresponding | 117 // Gets the pairing for the specified client id. See the corresponding |
106 // Delegate method for details. If none is found, the callback is invoked | 118 // Delegate method for details. If none is found, the callback is invoked |
107 // with an invalid Pairing. | 119 // with an invalid Pairing. |
108 void GetPairing(const std::string& client_id, | 120 void GetPairing(const std::string& client_id, |
109 const GetPairingCallback& callback); | 121 const GetPairingCallback& callback); |
110 | 122 |
111 // Gets all pairings with the shared secrets removed as a base::ListValue. | 123 // Gets all pairings with the shared secrets removed as a base::ListValue. |
112 void GetAllPairings(const GetAllPairingsCallback& callback); | 124 void GetAllPairings(const GetAllPairingsCallback& callback); |
113 | 125 |
114 // Delete a pairing, identified by its client ID. |callback| is called with | 126 // Delete a pairing, identified by its client ID. |callback| is called with |
115 // the result of saving the new config, which occurs even if the client ID | 127 // the result of saving the new config, which occurs even if the client ID |
116 // did not match any pairing. | 128 // did not match any pairing. |
117 void DeletePairing(const std::string& client_id, | 129 void DeletePairing(const std::string& client_id, |
118 const SaveCallback& callback); | 130 const DoneCallback& callback); |
119 | 131 |
120 // Clear all pairings from the registry. | 132 // Clear all pairings from the registry. |
121 void ClearAllPairings(const SaveCallback& callback); | 133 void ClearAllPairings(const DoneCallback& callback); |
122 | 134 |
123 private: | 135 private: |
124 FRIEND_TEST_ALL_PREFIXES(PairingRegistryTest, AddPairing); | 136 FRIEND_TEST_ALL_PREFIXES(PairingRegistryTest, AddPairing); |
125 FRIEND_TEST_ALL_PREFIXES(PairingRegistryTest, GetAllPairingsJSON); | 137 FRIEND_TEST_ALL_PREFIXES(PairingRegistryTest, GetAllPairingsJSON); |
126 friend class NegotiatingAuthenticatorTest; | 138 friend class NegotiatingAuthenticatorTest; |
127 friend class base::RefCountedThreadSafe<PairingRegistry>; | 139 friend class base::RefCountedThreadSafe<PairingRegistry>; |
128 | 140 |
129 virtual ~PairingRegistry(); | 141 virtual ~PairingRegistry(); |
130 | 142 |
131 // Helper method for unit tests. | 143 // Helper method for unit tests. |
132 void AddPairing(const Pairing& pairing); | 144 void AddPairing(const Pairing& pairing); |
133 | 145 |
134 // Worker functions for each of the public methods, passed as a callback to | |
135 // the delegate. | |
136 void MergePairingAndSave(const Pairing& pairing, | |
137 const SaveCallback& callback, | |
138 const std::string& pairings_json); | |
139 void DoGetPairing(const std::string& client_id, | |
140 const GetPairingCallback& callback, | |
141 const std::string& pairings_json); | |
142 void SanitizePairings(const GetAllPairingsCallback& callback, | |
143 const std::string& pairings_json); | |
144 void DoDeletePairing(const std::string& client_id, | |
145 const SaveCallback& callback, | |
146 const std::string& pairings_json); | |
147 | |
148 // "Trampoline" callbacks that schedule the next pending request and then | 146 // "Trampoline" callbacks that schedule the next pending request and then |
149 // invoke the original caller-supplied callback. | 147 // invoke the original caller-supplied callback. |
150 void InvokeLoadCallbackAndScheduleNext( | 148 void InvokeDoneCallbackAndScheduleNext( |
151 const LoadCallback& callback, const std::string& pairings_json); | 149 const DoneCallback& callback, bool success); |
152 void InvokeSaveCallbackAndScheduleNext( | |
153 const SaveCallback& callback, bool success); | |
154 void InvokeGetPairingCallbackAndScheduleNext( | 150 void InvokeGetPairingCallbackAndScheduleNext( |
155 const GetPairingCallback& callback, Pairing pairing); | 151 const GetPairingCallback& callback, Pairing pairing); |
156 void InvokeGetAllPairingsCallbackAndScheduleNext( | 152 void InvokeGetAllPairingsCallbackAndScheduleNext( |
157 const GetAllPairingsCallback& callback, | 153 const GetAllPairingsCallback& callback, |
158 scoped_ptr<base::ListValue> pairings); | 154 scoped_ptr<base::ListValue> pairings); |
159 | 155 |
160 // Queue management methods. | 156 // Queue management methods. |
161 void ServiceOrQueueRequest(const base::Closure& request); | 157 void ServiceOrQueueRequest(const base::Closure& request); |
162 void ServiceNextRequest(); | 158 void ServiceNextRequest(); |
163 | 159 |
164 // Translate between the structured and serialized forms of the pairing data. | 160 // Translate between the structured and serialized forms of the pairing data. |
165 static PairedClients DecodeJson(const std::string& pairings_json); | 161 static Pairing DecodeJson(const std::string& pairing_json); |
166 static std::string EncodeJson(const PairedClients& clients); | 162 static std::string EncodeJson(const PairedClients& clients); |
167 static scoped_ptr<base::ListValue> ConvertToListValue( | 163 static scoped_ptr<base::ListValue> ConvertToListValue( |
168 const PairedClients& clients, | 164 const PairedClients& clients, |
169 bool include_shared_secrets); | 165 bool include_shared_secrets); |
170 | 166 |
171 scoped_ptr<Delegate> delegate_; | 167 scoped_ptr<Delegate> delegate_; |
172 | 168 |
173 std::queue<base::Closure> pending_requests_; | 169 std::queue<base::Closure> pending_requests_; |
174 | 170 |
175 DISALLOW_COPY_AND_ASSIGN(PairingRegistry); | 171 DISALLOW_COPY_AND_ASSIGN(PairingRegistry); |
176 }; | 172 }; |
177 | 173 |
178 } // namespace protocol | 174 } // namespace protocol |
179 } // namespace remoting | 175 } // namespace remoting |
180 | 176 |
181 #endif // REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 177 #endif // REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
OLD | NEW |