public async Task HandleAsync(RequestPasswordResetTokenCommand command, CancellationToken cancellationToken = default) { var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email); if (!getAccountResult.Success) { throw new ValidationException(getAccountResult.Errors); } var accountIsConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsConfirmed(getAccountResult.Value.Confirmed); if (!accountIsConfirmedVerificationResult.Success) { throw new ValidationException(accountIsConfirmedVerificationResult.Errors); } var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash); if (!passwordIsSetVerificationResult.Success) { throw new ValidationException(passwordIsSetVerificationResult.Errors); } var correlationId = Guid.NewGuid(); var token = getAccountResult.Value.GenerateToken(TokenTypeEnumeration.PasswordReset, correlationId); await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken); await _accountRepository.UpdateAsync(getAccountResult.Value); await _passwordResetTokenRequestService.PublishPasswordResetRequestedIntegrationEventAsync(getAccountResult.Value.Email, token.Value, correlationId); }
public async Task HandleAsync(ChangePasswordCommand command, CancellationToken cancellationToken = default) { var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId); if (!getAccountResult.Success) { throw new ResourceNotFoundException(getAccountResult.Errors); } var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash); if (!passwordIsSetVerificationResult.Success) { throw new ValidationException(passwordIsSetVerificationResult.Errors); } var passwordIsCorrectVerificationResult = _accountVerificationService.VerifyPassword(getAccountResult.Value.PasswordHash, command.OldPassword); if (!passwordIsCorrectVerificationResult.Success) { throw new ValidationException(passwordIsCorrectVerificationResult.Errors); } getAccountResult.Value.ChangePassword(_passwordService.HashPassword(command.NewPassword), Guid.NewGuid()); await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken); await _accountRepository.UpdateAsync(getAccountResult.Value); }
public void VerifyPasswordIsSet_Should_Return_VerificationResult_With_Success_True() { var account = Account.Builder() .SetId(Guid.NewGuid()) .SetEmail("*****@*****.**") .SetConfirmed(true) .SetPasswordHash("PasswordHash") .SetSecurityStamp(Guid.NewGuid()) .SetCreated(DateTimeOffset.UtcNow) .SetRoles(new List <Guid> { Guid.NewGuid() }) .Build(); var expectedResult = VerificationResult.Ok(); var result = _accountVerificationService.VerifyPasswordIsSet(account.PasswordHash); result.Should().BeEquivalentTo(expectedResult); }
public async Task HandleAsync(ResetPasswordCommand command, CancellationToken cancellationToken = default) { var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email); if (!getAccountResult.Success) { throw new ValidationException(getAccountResult.Errors); } var accountIsConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsConfirmed(getAccountResult.Value.Confirmed); if (!accountIsConfirmedVerificationResult.Success) { throw new ValidationException(accountIsConfirmedVerificationResult.Errors); } var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash); if (!passwordIsSetVerificationResult.Success) { throw new ValidationException(passwordIsSetVerificationResult.Errors); } var passwordResetToken = getAccountResult.Value.Tokens.SingleOrDefault(x => Equals(x.Type, TokenTypeEnumeration.PasswordReset)); var confirmationCodeVerificationResult = _accountVerificationService.VerifyConfirmationCode(passwordResetToken, command.Code); if (!confirmationCodeVerificationResult.Success) { throw new ValidationException(confirmationCodeVerificationResult.Errors); } getAccountResult.Value.ChangePassword(_passwordService.HashPassword(command.Password), Guid.NewGuid()); await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken); await _accountRepository.UpdateAsync(getAccountResult.Value); }