Esempio n. 1
0
        public async Task Send(ConsumeContext <TMessage> context, IPipe <ConsumeContext <TMessage> > next)
        {
            try
            {
                await _transactionProvider.BeginTransactionAsync();

                await next.Send(context);

                await _transactionProvider.CommitTransactionAsync();
            }
            catch (Exception ex)
            {
                //TODO: Logging
                Console.WriteLine(ex.ToString());
                await _transactionProvider.RollbackTransactionAsync();

                throw;
            }
        }
Esempio n. 2
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            if (_transactionalBehaviorValidator.IsCommand <TRequest>())
            {
                await _transactionProvider.BeginTransactionAsync();

                try
                {
                    var response = await next();

                    if (response is IResult result)
                    {
                        if (result.IsSuccess)
                        {
                            await _transactionProvider.CommitTransactionAsync();
                        }
                        else
                        {
                            await _transactionProvider.RollbackTransactionAsync();
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("The transaction status for the command could not be determined");
                    }

                    return(response);
                }
                catch
                {
                    await _transactionProvider.RollbackTransactionAsync();

                    //TODO: Logging
                    throw;
                }
            }

            return(await next());
        }
Esempio n. 3
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));
                    }
                }
            }
        }