public async Task Execute(string accountId, PasswordChangeModel model, IValidator validator)
        {
            try
            {
                //Validate input
                _validationStrategy.Validate(model, validator);
                if (validator.HasErrors)
                {
                    return;
                }

                //Validate old password and find account
                Account account = await _accountRepo.Find(x => x.AccountId == accountId && x.AccountStatus != AccountStatus.Inactive).SingleOrDefaultAsync();

                if (account == null ||
                    account.PasswordStatus == PasswordStatus.Empty ||
                    !_secretHashHelper.ValidateSecret(account.Salt + model.OldPassword, account.Password))
                {
                    validator.AddError("Wrong old password", "OldPassword");
                    return;
                }

                //Generate new salt
                string newSalt = _passwordSaltGenerator.Generate();

                //Generate new hashed password
                string newHashedPass = _secretHashHelper.GenerateHash(newSalt + model.NewPassword);

                //Save in db
                _accountRepo.UpdateOne(x => x.AccountId == accountId, CreateUpdateDef(newHashedPass, newSalt));

                //Raise a PasswordChanged event
                PasswordChangedEvent passwordChangedEvent = CreatePasswordChangedEvent(account);
                await Publish(passwordChangedEvent);

                return;
            }
            catch (Exception ex)
            {
                await _logger.LogErrorAsync("ChangeAccountPasswordCommand.Execute", "Exception occurred", new
                {
                    Exception = ex
                });

                throw;
            }
        }
Example #2
0
        public async Task <(bool Result, string AccountId)> Execute(AuthValidationRequest request)
        {
            try
            {
                if ((request == null) || (request.UsernameOrEmail == null) || (request.Password == null))
                {
                    throw new ArgumentNullException("Credentials were not provided");
                }

                //Retrieve account and check existence
                Account account = _accountRepo.Find(a => a.Username == request.UsernameOrEmail || a.Email == request.UsernameOrEmail)
                                  .FirstOrDefault();

                if (account == null)
                {
                    return(false, null);
                }

                //Validate password
                if (!_hashHelper.ValidateSecret(account.Salt + request.Password, account.Password))
                {
                    return(false, null);
                }

                return(true, account.AccountId);
            }
            catch (Exception ex)
            {
                //Log error
                await _logger.LogErrorAsync("ValidateCredentialsCommand.Execute", "Exception was thrown", new
                {
                    AuthValidationRequest = request,
                    Exception             = ex
                });

                throw;
            }
        }