Exemple #1
0
        public async Task <TResult> Decrypt <T, TResult>(T value)
        {
            var tResultProperties = typeof(TResult).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var resultInstance    = _mapperProvider.Map <T, TResult>(value);

            foreach (var property in GetEncryptableProperties(typeof(T)))
            {
                var encryptCustomAttribute = property.GetCustomAttribute <EncryptAttribute>();

                if (encryptCustomAttribute.EncryptionMethod == EncryptionMethod.Hashing)
                {
                    continue;
                }

                var cryptographicCredentials = _cryptographicCredentialsSwitch
                                               .Case(encryptCustomAttribute.EncryptionSaltKey);

                var resultProperty = tResultProperties
                                     .FirstOrDefault(prop => property.Name == prop.Name);

                var val = (IEnumerable <byte>)property.GetValue(value);

                if (val == null)
                {
                    continue;
                }

                resultProperty.SetValue(resultInstance, await _cryptographyProvider.Decrypt(cryptographicCredentials, val));
            }

            return(resultInstance);
        }
        public async Task <ProjectModel> Create(string projectRaw)
        {
            ProjectModel project;

            try
            {
                project = JsonConvert.DeserializeObject <ProjectModel>(projectRaw);
            }
            catch
            {
                throw new BadRequestException("Bad request");
            }

            _projectValidator.ValidatePostProject(project);

            var projectDTO = _mapper.Map <ProjectModel, Project>(project);

            projectDTO.createdDate      = DateTime.Now;
            projectDTO.lastModifiedDate = DateTime.Now;

            _apiContext.Project.Add(projectDTO);
            await _apiContext.SaveChangesAsync();

            var createdProject = await Get(projectDTO.id);

            return(createdProject);
        }
        public async Task <RetrieveBudgetPlannersResponse> Handle(RetrieveBudgetPlannersRequest request, CancellationToken cancellationToken)
        {
            var budgetPlanners = await _budgetPlannerService.GetBudgetPlanners(request.AccountId, request.LastUpdated, cancellationToken);

            var budgets = _mapperProvider.Map <Domains.Data.Budget, Domains.Dto.Budget>(budgetPlanners);

            return(Response.Success <RetrieveBudgetPlannersResponse>(budgets));
        }
Exemple #4
0
        public async Task <CreateBudgetPlannerResponse> Handle(CreateBudgetPlannerRequest request, CancellationToken cancellationToken)
        {
            var budgetPlanner = _mapperProvider.Map <CreateBudgetPlannerRequest, Budget>(request);

            budgetPlanner = await _budgetPlannerService.Save(budgetPlanner, cancellationToken);

            return(Response.Success <CreateBudgetPlannerResponse>(budgetPlanner));
        }
        public async Task <SaveCustomerResponse> Handle(SaveCustomerRequest request, CancellationToken cancellationToken)
        {
            var customer          = _mapperProvider.Map <SaveCustomerRequest, CustomerDto>(request);
            var encryptedCustomer = await _encryptionProvider.Encrypt <CustomerDto, Customer>(customer);

            encryptedCustomer = await _customerService.SaveCustomer(encryptedCustomer, cancellationToken);

            customer = await _encryptionProvider.Decrypt <Customer, CustomerDto>(encryptedCustomer);

            return(Response.Success <SaveCustomerResponse>(customer));
        }
Exemple #6
0
            public override async Task <Unit> Handle(SaveCustomerAction aAction, CancellationToken aCancellationToken)
            {
                var request  = _mapperProvider.Map <CustomerState, SaveCustomerRequest>(CustomerState);
                var response = await _mediator.Send(request);

                if (Response.IsSuccessful(response))
                {
                    UpdateFields(CustomerState, response.Result);
                }

                return(Unit.Value);
            }
        public async Task <LoginResponse> Handle(LoginRequest request, CancellationToken cancellationToken)
        {
            try
            {
                request.Password = Convert.ToBase64String(
                    request.Password.GetBytes(Encoding.UTF8).ToArray());

                var account          = _mapperProvider.Map <LoginRequest, Account>(request);
                var encryptedAccount = await _encryptionProvider.Encrypt <Account, Domains.Data.Account>(account);

                var foundAccount = await _accountService.GetAccount(encryptedAccount.EmailAddress, cancellationToken);

                if (foundAccount == null)
                {
                    throw new NullReferenceException();
                }

                if (!foundAccount.Password.SequenceEqual(encryptedAccount.Password))
                {
                    throw new UnauthorizedAccessException();
                }

                account = await _encryptionProvider.Decrypt <Domains.Data.Account, Account>(foundAccount);

                return(Response.Success <LoginResponse>(account));
            }
            catch (Exception ex)
            {
                var exceptionType = ex.GetType();

                if (exceptionType == typeof(NullReferenceException) ||
                    exceptionType == typeof(UnauthorizedAccessException))
                {
                    return new LoginResponse {
                               IsSuccessful = false, Errors = new [] {
                                   new ValidationFailure(nameof(Account.EmailAddress), "EmailAddress or password invalid")
                               }
                    }
                }
                ;

                throw;
            }
        }
        public async Task <CreateTransactionResponse> Handle(CreateTransactionRequest request, CancellationToken cancellationToken)
        {
            var transaction = _mapperProvider.Map <CreateTransactionRequest, Transaction>(request);

            var budgetPlanner = await _budgetPlannerService.GetBudgetPlanner(transaction.BudgetId, cancellationToken);

            budgetPlanner.LastUpdated = SqlDateTime.MinValue.Value;

            var previousBalance = await _transactionProvider.GetBalance(transaction.BudgetId, cancellationToken, true);

            if (transaction.Id != default)
            {
                transaction = await _transactionService.SaveTransaction(transaction, cancellationToken);

                return(Response.Success <CreateTransactionResponse>(transaction));
            }

            transaction = await _transactionService.SaveTransaction(transaction, cancellationToken, false);

            var transactionLedger = new TransactionLedger
            {
                Transaction = transaction,
                Amount      = transaction.Type == Domains.Enumerations.TransactionType.Expense
                    ? -transaction.Amount
                    : transaction.Amount,
                PreviousBalance = previousBalance,
                NewBalance      = transaction.Type == Domains.Enumerations.TransactionType.Expense
                    ? previousBalance - transaction.Amount
                    : previousBalance + transaction.Amount
            };

            await _transactionLedgerService
            .SaveTransactionLedger(transactionLedger, true, cancellationToken);

            return(Response.Success <CreateTransactionResponse>(transaction));
        }