-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAddTrustedClientViewModel.cs
More file actions
341 lines (266 loc) · 10.4 KB
/
AddTrustedClientViewModel.cs
File metadata and controls
341 lines (266 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Avalonia.Controls;
using Avalonia.Input.Platform;
using ByteSync.Business;
using ByteSync.Business.Communications;
using ByteSync.Common.Business.EndPoints;
using ByteSync.Interfaces;
using ByteSync.Interfaces.Controls.Communications;
using ByteSync.Services.Communications;
using ByteSync.ViewModels.Misc;
using ByteSync.Views;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
namespace ByteSync.ViewModels.TrustedNetworks;
public class AddTrustedClientViewModel : FlyoutElementViewModel
{
private readonly IPublicKeysManager _publicKeysManager;
private readonly IApplicationSettingsRepository _applicationSettingsRepository;
private readonly IPublicKeysTruster _publicKeysTruster;
private readonly ILogger<AddTrustedClientViewModel> _logger;
private readonly MainWindow _mainWindow;
[ExcludeFromCodeCoverage]
public AddTrustedClientViewModel()
{
#if DEBUG
if (Design.IsDesignMode)
{
MyClientId = Environment.MachineName;
IsWaitingForOtherParty = true;
IsJoinerSide = true;
var safetyKey = "";
for (var i = 0; i < 16; i++)
{
safetyKey += "string_" + i + " ";
}
SafetyKeyParts = safetyKey.Split(" ", StringSplitOptions.RemoveEmptyEntries);
IsClipboardCheckError = true;
}
#endif
}
public AddTrustedClientViewModel(PublicKeyCheckData? publicKeyCheckData, TrustDataParameters trustDataParameters,
IPublicKeysManager publicKeysManager, IApplicationSettingsRepository applicationSettingsManager,
IPublicKeysTruster publicKeysTruster, ILogger<AddTrustedClientViewModel> logger, MainWindow mainWindow)
{
#if DEBUG
if (Design.IsDesignMode)
{
return;
}
#endif
_publicKeysManager = publicKeysManager;
_applicationSettingsRepository = applicationSettingsManager;
_publicKeysTruster = publicKeysTruster;
_logger = logger;
_mainWindow = mainWindow;
if (publicKeyCheckData == null)
{
throw new ArgumentNullException(nameof(publicKeyCheckData));
}
MyClientId = _applicationSettingsRepository.GetCurrentApplicationSettings().ClientId;
OtherClientId = publicKeyCheckData.IssuerPublicKeyInfo.ClientId;
var publicKeyFormatter = new PublicKeyFormatter();
MyPublicKey = publicKeyFormatter.Format(_publicKeysManager.GetMyPublicKeyInfo().PublicKey);
OtherClientKey = publicKeyFormatter.Format(publicKeyCheckData.IssuerPublicKeyInfo.PublicKey);
ShowSuccess = false;
ShowError = false;
TrustedPublicKey = _publicKeysManager.BuildTrustedPublicKey(publicKeyCheckData);
SafetyKey = TrustedPublicKey.SafetyKey;
SafetyKeyParts = BuildSafetyWords();
PublicKeyCheckData = publicKeyCheckData;
IsJoinerSide = trustDataParameters.IsJoinerSide;
TrustDataParameters = trustDataParameters;
var canRun = new BehaviorSubject<bool>(true);
CopyToClipboardCommand = ReactiveCommand.CreateFromTask(CopyToClipboard, canRun);
CheckClipboardCommand = ReactiveCommand.CreateFromTask(CheckClipboard, canRun);
ValidateClientCommand = ReactiveCommand.CreateFromTask(ValidateClient, canRun);
RejectClientCommand = ReactiveCommand.CreateFromTask(RejectClient, canRun);
CancelCommand = ReactiveCommand.CreateFromTask(Cancel);
this.WhenActivated(disposables =>
{
Observable.Merge(CopyToClipboardCommand.IsExecuting, CheckClipboardCommand.IsExecuting,
ValidateClientCommand.IsExecuting, RejectClientCommand.IsExecuting,
CancelCommand.IsExecuting)
.Select(executing => !executing)
.Subscribe(canRun)
.DisposeWith(disposables);
});
}
public TrustedPublicKey TrustedPublicKey { get; set; }
public PublicKeyCheckData? PublicKeyCheckData { get; }
public bool IsJoinerSide { get; set; }
public TrustDataParameters TrustDataParameters { get; set; }
public ReactiveCommand<Unit, Unit> CopyToClipboardCommand { get; }
public ReactiveCommand<Unit, Unit> CheckClipboardCommand { get; }
public ReactiveCommand<Unit, Unit> ValidateClientCommand { get; }
public ReactiveCommand<Unit, Unit> RejectClientCommand { get; }
public ReactiveCommand<Unit, Unit> CancelCommand { get; }
[Reactive]
public string SafetyKey { get; set; }
[Reactive]
public string[] SafetyKeyParts { get; set; }
[Reactive]
public string? MyClientId { get; set; }
[Reactive]
internal string MyPublicKey { get; set; }
[Reactive]
public string? OtherClientId { get; set; }
[Reactive]
internal string OtherClientKey { get; set; }
[Reactive]
public bool ShowSuccess { get; set; }
[Reactive]
public bool ShowError { get; set; }
[Reactive]
public bool IsClipboardCheckOK { get; set; }
[Reactive]
public bool IsClipboardCheckError { get; set; }
[Reactive]
public bool IsCopyToClipboardOK { get; set; }
[Reactive]
public bool IsCopyToClipboardError { get; set; }
[Reactive]
public bool IsWaitingForOtherParty { get; set; }
public override void OnDisplayed()
{
base.OnDisplayed();
#if DEBUG
if (Design.IsDesignMode)
{
return;
}
#endif
Container.CanCloseCurrentFlyout = false;
}
private async Task CopyToClipboard()
{
IsClipboardCheckOK = false;
IsClipboardCheckError = false;
try
{
var clipboard = GetClipboard();
if (clipboard != null)
{
await clipboard.SetTextAsync(SafetyKeyParts.JoinToString(" "));
IsCopyToClipboardOK = true;
IsClipboardCheckError = false;
}
else
{
_logger.LogWarning("CopyToClipboard: unable to acess clipboard");
IsCopyToClipboardOK = false;
IsClipboardCheckError = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "CopyToClipboard error");
IsCopyToClipboardOK = false;
IsClipboardCheckError = true;
}
}
private async Task CheckClipboard()
{
IsCopyToClipboardOK = false;
IsCopyToClipboardError = false;
try
{
var clipboard = GetClipboard();
if (clipboard != null)
{
var clipBoardValue = await clipboard.GetTextAsync();
var isSuccess = SafetyKeyParts.Length > 1 && SafetyKeyParts.JoinToString(" ").Equals(clipBoardValue);
IsClipboardCheckOK = isSuccess;
IsClipboardCheckError = !isSuccess;
}
else
{
_logger.LogWarning("CheckClipboard: unable to acess clipboard");
IsClipboardCheckOK = false;
IsClipboardCheckError = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "CheckClipboard error");
IsClipboardCheckOK = false;
IsClipboardCheckError = true;
}
}
protected virtual IClipboard? GetClipboard()
{
return TopLevel.GetTopLevel(_mainWindow)?.Clipboard;
}
private async Task ValidateClient()
{
try
{
await _publicKeysTruster.OnPublicKeyValidationFinished(PublicKeyCheckData!, TrustDataParameters, true);
bool isSuccess;
if (!TrustDataParameters.PeerTrustProcessData.OtherPartyHasFinished())
{
IsWaitingForOtherParty = true;
isSuccess = await TrustDataParameters.PeerTrustProcessData.WaitForPeerTrustProcessFinished();
IsWaitingForOtherParty = false;
}
else
{
isSuccess = TrustDataParameters.PeerTrustProcessData.IsPeerTrustSuccess;
}
if (isSuccess)
{
_publicKeysManager.Trust(TrustedPublicKey);
ShowSuccess = true;
await DelayAsync(TimeSpan.FromSeconds(3));
ShowSuccess = false;
}
else
{
ShowError = true;
await DelayAsync(TimeSpan.FromSeconds(3));
ShowError = false;
}
Container.CanCloseCurrentFlyout = true;
RaiseCloseFlyoutRequested();
}
catch (Exception ex)
{
_logger.LogError(ex, "ValidateClient");
}
}
private async Task RejectClient()
{
try
{
_logger.LogWarning("Current user rejected Public Key {@publicKey}", TrustedPublicKey);
var task = _publicKeysTruster.OnPublicKeyValidationFinished(PublicKeyCheckData!, TrustDataParameters, false);
// We also cancel, otherwise, we continue to wait for the other's response
var task2 = _publicKeysTruster.OnPublicKeyValidationCanceled(PublicKeyCheckData!, TrustDataParameters);
ShowError = true;
await DelayAsync(TimeSpan.FromSeconds(3));
ShowError = false;
await Task.WhenAll(task, task2);
Container.CanCloseCurrentFlyout = true;
RaiseCloseFlyoutRequested();
}
catch (Exception ex)
{
_logger.LogError(ex, "RejectClient");
}
}
private async Task Cancel()
{
_logger.LogWarning("Current user cancelled waiting for Public Key {@publicKey} cross check", TrustedPublicKey);
await _publicKeysTruster.OnPublicKeyValidationCanceled(PublicKeyCheckData!, TrustDataParameters);
}
protected virtual Task DelayAsync(TimeSpan delay) => Task.Delay(delay);
private string[] BuildSafetyWords()
{
var safetyWordsComputer = new SafetyWordsComputer(SafetyWordsValues.AVAILABLE_WORDS);
return safetyWordsComputer.Compute(TrustedPublicKey.SafetyKey);
}
}