Пример #1
0
        public async Task <CreateFirstMessangerSessionResponse> MakeFirstSessionAsync(CreateMessangerSessionRequest model,
                                                                                      string userId,
                                                                                      string sessionId)
        {
            var savedSessions = await sessionProvider.GetModelBySearchPredicate(x => x.SessionId == sessionId &&
                                                                                x.UserId == userId);

            if (savedSessions != null)
            {
                throw new ApiError(new ServerException("This session is alredy exist"));
            }

            var serverKeys = rsaCypher.GenerateKeys();
            var user       = await userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);

            var serverSession = new Session()
            {
                ClientPublicKey  = model.PublicKey,
                ServerPrivateKey = serverKeys.privateKey,
                ServerPublicKey  = serverKeys.publicKey,
                SessionId        = sessionId,
                UserId           = userId
            };

            var savedStrongKey = await strongKeyProvider.GetModelBySearchPredicate(x => x.UserId == userId);

            if (savedStrongKey != null)
            {
                var savedCypher = await cypherProvider.GetModelBySearchPredicate(x => x.Id == savedStrongKey.CypherId);

                await sessionProvider.CreateOrUpdateAsync(serverSession);

                return(new CreateFirstMessangerSessionResponse()
                {
                    ServerPublicKey = serverKeys.publicKey,
                    CryptedAes = rsaCypher.Crypt(model.PublicKey, savedCypher.Secret.ToUrlSafeBase64())
                });
            }
            else
            {
                await using (var transaction = await transactionProvider.BeginTransactionAsync())
                {
                    try
                    {
                        await sessionProvider.CreateOrUpdateAsync(serverSession);

                        var savedRole = await roleManager.Roles.FirstOrDefaultAsync(x => x.Name == "ProtocoledUsers");

                        await userManager.AddToRoleAsync(user, savedRole.Name);

                        byte[] strongKey = CryptoRandomizer.GenerateSecurityKey(16);

                        var cypher = await cypherProvider.CreateOrUpdateAsync(new Cypher()
                        {
                            Secret = strongKey
                        });

                        var key = await strongKeyProvider.CreateOrUpdateAsync(new StrongKey()
                        {
                            CypherId = cypher.Id,
                            UserId   = user.Id
                        });

                        string strongKeyToCrypt = strongKey.ToUrlSafeBase64();
                        string cryptedAesKey    = rsaCypher.Crypt(model.PublicKey, strongKeyToCrypt);

                        await transaction.CommitAsync();

                        return(new CreateFirstMessangerSessionResponse()
                        {
                            ServerPublicKey = serverKeys.publicKey,
                            CryptedAes = cryptedAesKey
                        });
                    }
                    catch (Exception ex)
                    {
                        await transaction.RollbackAsync();

                        throw new ApiError(new ServerException(ex.Message));
                    }
                }
            }
        }