-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCloudSessionPasswordExchangeKeyGivenService.cs
More file actions
96 lines (85 loc) · 4.41 KB
/
CloudSessionPasswordExchangeKeyGivenService.cs
File metadata and controls
96 lines (85 loc) · 4.41 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
using ByteSync.Business.Communications;
using ByteSync.Business.Sessions.Connecting;
using ByteSync.Common.Business.Sessions.Cloud.Connections;
using ByteSync.Interfaces.Controls.Applications;
using ByteSync.Interfaces.Controls.Communications;
using ByteSync.Interfaces.Controls.Communications.Http;
using ByteSync.Interfaces.Repositories;
using ByteSync.Interfaces.Services.Sessions.Connecting;
using ByteSync.Interfaces.Services.Sessions.Connecting.Joining;
namespace ByteSync.Services.Sessions.Connecting;
public class CloudSessionPasswordExchangeKeyGivenService : ICloudSessionPasswordExchangeKeyGivenService
{
private readonly ICloudSessionConnectionRepository _cloudSessionConnectionRepository;
private readonly IEnvironmentService _environmentService;
private readonly IPublicKeysManager _publicKeysManager;
private readonly ICloudSessionApiClient _cloudSessionApiClient;
private readonly ICloudSessionConnectionService _cloudSessionConnectionService;
private readonly ILogger<CloudSessionPasswordExchangeKeyGivenService> _logger;
private const string UNKNOWN_RECEIVED_SESSION_ID = "unknown received sessionId {sessionId}";
private const string PUBLIC_KEY_IS_NOT_TRUSTED = "Public key is not trusted";
public CloudSessionPasswordExchangeKeyGivenService(
ICloudSessionConnectionRepository cloudSessionConnectionRepository,
IEnvironmentService environmentService,
IPublicKeysManager publicKeysManager,
ICloudSessionApiClient cloudSessionApiClient,
ICloudSessionConnectionService cloudSessionConnectionService,
ILogger<CloudSessionPasswordExchangeKeyGivenService> logger)
{
_cloudSessionConnectionRepository = cloudSessionConnectionRepository;
_environmentService = environmentService;
_publicKeysManager = publicKeysManager;
_cloudSessionApiClient = cloudSessionApiClient;
_cloudSessionConnectionService = cloudSessionConnectionService;
_logger = logger;
}
public async Task Process(GiveCloudSessionPasswordExchangeKeyParameters request)
{
try
{
if (!await _cloudSessionConnectionRepository.CheckConnectingCloudSession(request.SessionId))
{
_logger.LogError(UNKNOWN_RECEIVED_SESSION_ID, request.SessionId);
return;
}
if (!_environmentService.ClientInstanceId.Equals(request.JoinerInstanceId))
{
_logger.LogWarning("Unexpected password provide request received with JoinerId {joinerId}", request.JoinerInstanceId);
return;
}
await _cloudSessionConnectionRepository.SetPasswordExchangeKeyReceived(request.SessionId);
var isTrusted = _publicKeysManager.IsTrusted(request.PublicKeyInfo);
if (isTrusted)
{
var password = await _cloudSessionConnectionRepository.GetTempSessionPassword(request.SessionId);
ExchangePassword exchangePassword = new(request.SessionId, _environmentService.ClientInstanceId, password!);
var encryptedPassword = _publicKeysManager.EncryptString(request.PublicKeyInfo, exchangePassword.Data);
AskJoinCloudSessionParameters outParameters = new (request, encryptedPassword);
_logger.LogInformation("...Providing encrypted password to the validator");
var joinSessionResult = await _cloudSessionApiClient.AskJoinCloudSession(outParameters,
_cloudSessionConnectionRepository.CancellationToken);
if (!joinSessionResult.IsOK)
{
var joinSessionError = new JoinSessionError
{
Status = joinSessionResult.Status
};
await _cloudSessionConnectionService.OnJoinSessionError(joinSessionError);
}
else
{
await _cloudSessionConnectionRepository.WaitOrThrowAsync(request.SessionId,
data => data.WaitForJoinSessionEvent, data => data.WaitTimeSpan, "Join session failed");
}
}
else
{
throw new Exception(PUBLIC_KEY_IS_NOT_TRUSTED);
}
}
catch (Exception ex)
{
await _cloudSessionConnectionService.HandleJoinSessionError(ex);
}
}
}