public async Task <TokenViewModel> AuthenticateAsync(AuthenticateDTO authDTO) { var authValidator = new AuthenticateValidator(_authSettings); await authValidator.ValidateAndThrowAsync(authDTO); return(_tokenService.GenerateTokenAuthentication()); }
public async Task <AuthenticatedUserDto> Handle(Authenticate request, CancellationToken cancellationToken) { AuthenticateValidator validator = new AuthenticateValidator(); ValidationResult validationResult = await validator.ValidateAsync(request, cancellationToken); if (!validationResult.IsValid) { throw new EntityValidationException(nameof(User), request, validationResult.Errors); } User user = await _userRepository.Get(Builders <User> .Filter.Eq(x => x.Username, request.Username), cancellationToken); if (user == null) { throw new InvalidCredentialException("Invalid username or password"); } bool hasCorrectPassword = await _securityService.VerifyPassword(request.Password, user.Password); if (!hasCorrectPassword) { throw new InvalidCredentialException("Invalid username or password"); } try { WebToken webToken = await _mediator.Send(new GenerateWebToken() { User = user }, cancellationToken); return(new AuthenticatedUserDto { User = _mapper.Map <UserDto>(user), Token = webToken, }); } catch (EntityNotFoundException ex) { throw new InvalidCredentialException("", ex); } }
public void SetUp() { _authenticateResource = new Authenticate {ConfirmPassword = "******", Password = "******", Email = "username"}; _sut = new AuthenticateValidator(); }