public async Task <bool> UnblockCardAsync(Guid id, string phoneNumber, string number, string cvv, string pinCode)
        {
            var card = await _cardRepository.Find(id, track : true);

            if (card == null || card.Number != _cryptoHelper.Hash(number) ||
                card.SecurityCode != _cryptoHelper.Hash(cvv) || card.PinCode != _cryptoHelper.Hash(pinCode))
            {
                return(false);
            }

            card.StatusId = 1;

            try
            {
                await _cardRepository.Update();
            }
            catch (Exception e)
            {
                return(false);
            }

            await _smsService.SendAsync(phoneNumber, $"Your card was unblocked: ************{card.Number.Substring(12)}");

            return(true);
        }
Beispiel #2
0
        public virtual void Setup()
        {
            try
            {
                _testDataDirectory   = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(TestContext.CurrentContext.TestDirectory))), "TestData");
                _sampleUser          = JsonConvert.DeserializeObject <User>(File.ReadAllText(Path.Combine(_testDataDirectory, "sampleUser.json")));
                _cyrptoHelper        = new CryptoHelper();
                _sampleUser.Password = _cyrptoHelper.Hash(_sampleUser.Password);

                _mapper = new MapperConfiguration(mc =>
                {
                    mc.AddProfile(new MappingProfile(_cyrptoHelper));
                }).CreateMapper();

                _userClaims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
                    new Claim(ClaimTypes.Sid, _sampleUser.Id.ToString()),
                    new Claim(ClaimTypes.Email, _sampleUser.Email)
                }, "mock"));
            }
            catch
            {
                // Ignored
            }
        }
Beispiel #3
0
        public IActionResult Login(LoginRequest loginRequest)
        {
            var toReturn = new Result();
            var user     = _userRepository.GetByMail(loginRequest.Email);

            if (user == null)
            {
                return(NotFound(toReturn.AddError("User not found.")));
            }

            if (user.Password != _cryptoHelper.Hash(loginRequest.Password))
            {
                return(BadRequest(toReturn.AddError("Password is not correct.")));
            }

            var handler = new JwtSecurityTokenHandler();
            var token   = handler.CreateToken(new SecurityTokenDescriptor
            {
                Audience  = _jwtOptions.Audience,
                Issuer    = _jwtOptions.Issuer,
                Expires   = _jwtOptions.Expiration,
                NotBefore = _jwtOptions.NotBefore,
                Subject   = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
                    new Claim(ClaimTypes.Email, user.Email),
                    new Claim(ClaimTypes.Sid, user.Id.ToString())
                }),
                SigningCredentials = _jwtOptions.SigningCredentials
            });

            toReturn.Data = new { UserToken = handler.WriteToken(token) };
            return(Ok(toReturn));
        }
        public async Task <IActionResult> CreateGlobal(GlobalMoneyTransferViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Accounts =
                    (List <Core.Entities.Account>) await _accountService.GetAccountsAsync(
                        Guid.Parse(GetCurrentUserId()));

                return(View(model));
            }

            model.ToAccountNumber = model.ToAccountNumber.ToUpperInvariant();

            var fromAccount = await _accountService.GetAccountAsync(model.AccountId.Value, true);

            Account toAccount = null;

            if (model.PaymentTypeId == 1)
            {
                toAccount = await _accountService.GetAccountByNumberAsync(model.ToAccountNumber, true);
            }
            else
            {
                if (!_cardHelper.CheckLuhn(model.ToAccountNumber))
                {
                    ModelState.AddModelError("ToAccountNumber", "This credit card is not valid");
                    model.Accounts =
                        (List <Account>) await _accountService.GetAccountsAsync(
                            Guid.Parse(GetCurrentUserId()));

                    return(View(model));
                }

                toAccount = await _accountService.GetAccountByCardNumberAsync(_cryptoHelper.Hash(model.ToAccountNumber), true);
            }

            if (toAccount == null)
            {
                ModelState.AddModelError("ToAccountNumber", model.PaymentTypeId == 1 ? "This account does not exist" : "This credit card does not exist");
                model.Accounts =
                    (List <Account>) await _accountService.GetAccountsAsync(
                        Guid.Parse(GetCurrentUserId()));

                return(View(model));
            }

            var currency = await _currencyService.Convert(fromAccount.Currency.Code, toAccount.Currency.Code, model.Amount);

            await _transactionService.CreateTransactionAsync(fromAccount, toAccount, currency.Rate, model.Amount,
                                                             model.Description, fromAccount.Currency.Code, toAccount.Currency.Code);

            return(RedirectToHome());
        }
Beispiel #5
0
        public async Task CreateAccountAsync(Account account, string name, string phoneNumber, string cardName)
        {
            var accounts = await _accountRepository.ListAccountsNumbers();

            var number = _accountHelper.GenerateIban();

            while (accounts.Contains(number))
            {
                number = _accountHelper.GenerateIban();
            }

            account.Number = number;

            var cards = await _cardRepository.ListCardsNumber();

            var cardNumber = _cardHelper.Generate16DigitNumber();

            while (cards.Contains(_cryptoHelper.Hash(cardNumber)))
            {
                cardNumber = _cardHelper.Generate16DigitNumber();
            }

            var securityCode = _cardHelper.Generate3DigitSecurityCode();
            var pinCode      = _cardHelper.GeneratePinCode();

            var card = new Card
            {
                Id             = new Guid(),
                Number         = _cryptoHelper.Hash(cardNumber),
                Last4Digits    = cardNumber.Substring(12),
                SecurityCode   = _cryptoHelper.Hash(securityCode),
                PinCode        = _cryptoHelper.Hash(pinCode),
                ExpireTime     = DateTime.Now.AddYears(3),
                Name           = string.IsNullOrEmpty(cardName) ? "Card Name" : cardName,
                CardHolderName = name,
                StatusId       = 1,
                AccountId      = account.Id
            };

            account.Cards.Add(card);

            await _smsService.SendAsync(phoneNumber,
                                        $"Do not share your data with anyone\nCard Number: {cardNumber}\nName: {card.CardHolderName}\nExpiration date: {card.ExpireTime}\nCVV: {securityCode}\nPin-Code: {pinCode}");

            await _accountRepository.Add(account);
        }
Beispiel #6
0
        public async Task <IActionResult> WithdrawMoneyByCard([FromBody] string data)
        {
            var model = JsonConvert.DeserializeObject <WithdrawMoneyModel>(data);

            if (!TryValidateModel(model))
            {
                return(BadRequest());
            }

            var account =
                await _accountService.GetAccountByCardNumberAsync(model.CardNumber);

            if (account == null || account.Cards.Find(x => x.Number == model.CardNumber).PinCode != _cryptoHelper.Hash(model.PinCode))
            {
                return(BadRequest());
            }

            var serviceModel = new WithdrawMoneyServiceModel
            {
                Account = account,
                Amount  = model.Amount,
            };

            var isSuccessful = await _transactionService.WithdrawMoneyByCardAsync(serviceModel);

            if (!isSuccessful)
            {
                return(NoContent());
            }

            return(Ok());
        }
Beispiel #7
0
        public MappingProfile(ICryptoHelper cryptoHelper)
        {
            // viewmodel -> entity
            CreateMap <NewUserRequest, User>().ForMember(dest => dest.Password, src => src.MapFrom(x => cryptoHelper.Hash(x.Password)));

            CreateMap <ProjectTaskCreateViewModel, ProjectTask>().ReverseMap();
            CreateMap <ProjectTaskViewModel, ProjectTask>().ReverseMap();

            CreateMap <ProjectCreateViewModel, Project>().ReverseMap();
            CreateMap <ProjectViewModel, Project>().ReverseMap();
            CreateMap <ProjectAndTasksViewModel, Project>().ReverseMap();

            // entity -> viewmodel
            CreateMap <User, UserViewModel>();
        }