public async Task <PwUser> CreateUserAsync(SignUpDto signUpDto) { await CheckUserRegistred(signUpDto); var passwordSalt = _encryptionService.CreateSalt(); int startBalance; try { startBalance = int.Parse(_configuration.GetSection("User:StartBalance").Value); } catch (Exception) { throw new PWException(CanNotGetStartBalanceMessage); } var user = new PwUser() { UserName = signUpDto.UserName, Salt = passwordSalt, Email = signUpDto.Email, PasswordHash = _encryptionService.EncryptPassword(signUpDto.Password, passwordSalt), Balance = startBalance }; await _userRepository.AddAsync(user); return(user); }
public ClaimsPrincipal GetUserClaimsPrincipal(PwUser user) { ClaimsPrincipal claimsPrincipal = null; var claims = new List <Claim> { new Claim(ClaimTypes.Name, user.Email) }; var claimsIdentity = new ClaimsIdentity(claims, "Cookies"); claimsPrincipal = new ClaimsPrincipal(claimsIdentity); return(claimsPrincipal); }
private void ValidateCreation(PwUser payee, PwUser recipient, int amount, string recipientName) { if (recipient == null) { throw new PWException(string.Format(UserNotExistErrorMessage, recipientName)); } if (amount > payee.Balance) { throw new PWException(TransactionSizeErrorMessage); } if (payee.Id == recipient.Id) { throw new PWException(SendSelfErrorMessage); } }
private bool IsUserValid(PwUser user, string password) { var result = string.Equals(_encryptionService.EncryptPassword(password, user.Salt), user.PasswordHash); return(result); }
public PwMutation(IHttpContextAccessor httpContextAccessor, IMembershipService membershipService, ITransactionService transactionService) { #region Session FieldAsync <SessionInfoType>( "login", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <LoginOptionsInput> > { Name = "loginOptions" }), resolve: async context => { var loginDto = context.GetArgument <LoginDto>("loginOptions"); if (!this.ValidateInput(context, loginDto)) { return(null); } PwUser user = null; ClaimsPrincipal claimsPrincipal = null; try { user = await membershipService.GetUserAsync(loginDto); claimsPrincipal = membershipService.GetUserClaimsPrincipal(user); } catch (PWException ex) { context.Errors.Add(new ExecutionError(ex.Message)); return(null); } await httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); return(user); } ); FieldAsync <SessionInfoType>( "signUp", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <SignUpOptionsInput> > { Name = "signUpOptions" }), resolve: async context => { var signUpDto = context.GetArgument <SignUpDto>("signUpOptions"); if (!this.ValidateInput(context, signUpDto)) { return(null); } PwUser user = null; ClaimsPrincipal claimsPrincipal = null; try { user = await membershipService.CreateUserAsync(signUpDto); claimsPrincipal = membershipService.GetUserClaimsPrincipal(user); } catch (PWException ex) { context.Errors.Add(new ExecutionError(ex.Message)); return(null); } await httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); return(user); } ); FieldAsync <SessionInfoType>( "logout", resolve: async context => { if (!this.Authorize(httpContextAccessor, context)) { return(null); } await httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return(null); } ); #endregion #region Transactions FieldAsync <ListGraphType <TransactionType> >( "createTransaction", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <NewTransactionInput> > { Name = "newTransaction" }), resolve: async context => { if (!this.Authorize(httpContextAccessor, context)) { return(null); } var createTransactionDto = context.GetArgument <CreateTransactionDto>("newTransaction"); if (!this.ValidateInput(context, createTransactionDto)) { return(null); } var payeeEmail = httpContextAccessor.HttpContext.User.Identity.Name; try { await transactionService.CreateTransactionAsync(payeeEmail, createTransactionDto); } catch (PWException ex) { context.Errors.Add(new ExecutionError(ex.Message)); return(null); } return(null); } ); #endregion }