コード例 #1
0
        public IActionResult Register([FromBody] AuthenticationInputDto authenticationDto)
        {
            var credentials   = new Credentials(authenticationDto.Email, authenticationDto.Password);
            var user          = _usersRepository.Insert(credentials);
            var userOutputDto = _mapper.Map <UserRegisteredOutputDto>(user);

            return(StatusCode((int)HttpStatusCode.Created, userOutputDto));
        }
コード例 #2
0
        public ActionResult <UserOutputDto> Authenticate([FromBody] AuthenticationInputDto authenticationDto)
        {
            var credentials = new Credentials(authenticationDto.Email, authenticationDto.Password);
            var user        = _usersRepository.Authenticate(credentials);

            if (user == null)
            {
                return(Unauthorized(new { message = "Invalid credentials." }));
            }

            var token = _securityManager.GenerateToken(user);

            return(Ok(token));
        }
コード例 #3
0
        public virtual AuthenticationOutputDto Authenticate(AuthenticationInputDto authenticationInputDto)
        {
            if (authenticationInputDto == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, Constants.Messages.MissingInputDto, new ArgumentNullException(nameof(authenticationInputDto)));
            }

            try
            {
                AuthenticationInputDtoValidator.ValidateAndThrow(authenticationInputDto);
            }
            catch (ValidationException exception)
            {
                throw new RestException(HttpStatusCode.BadRequest, exception.Message, exception);
            }

            bool exists = UserDao.ExistsByUserNameAndPassword(authenticationInputDto.UserName, authenticationInputDto.Password);

            return new AuthenticationOutputDto { Success = exists };
        }
コード例 #4
0
        /// <summary>
        /// Authenticates the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public async Task <AuthenticationOutputDto> Authenticate(AuthenticationInputDto input)
        {
            var result = new AuthenticationOutputDto();

            using (var unitOfWork = UnitOfWorkFactory.Create())
            {
                var user = await unitOfWork.Users.Authenticate(input.Username, input.Password);

                if (user != null)
                {
                    result = new AuthenticationOutputDto()
                    {
                        UserId = user.Id
                    };
                    await GenerateCookieAsync(user.Id);
                }
                else
                {
                    throw new NotFoundException(new ErrorResponse("user not found"));
                }
            }

            return(result);
        }
コード例 #5
0
            public async Task PayloadPreenchido_UsuarioSenhaValidos_UsuarioEncontrado_UsuarioHabilitado_RetornaOk(AuthenticationInputDto authenticationInputDto, JwtAuthenticationResult jwtAuthenticationResult)
            {
                jwtAuthenticationServiceMock.Setup(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(jwtAuthenticationResult);

                HttpResponseMessage response = await httpClient.PostAsJsonAsync($"{endpointUri}/Authenticate", authenticationInputDto);

                bool wasDeserializationSuccessful = response.Content.TryGetContentValue(out JwtAuthenticationResultDto jwtAuthenticationResultDtoReturned);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.True(wasDeserializationSuccessful);
                Assert.NotNull(jwtAuthenticationResultDtoReturned);
                jwtAuthenticationServiceMock.Verify(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            }
コード例 #6
0
            public async Task PayloadPreenchido_UsuarioSenhaValidos_UsuarioEncontrado_UsuarioDesabilitado_RetornaUnauthorized(AuthenticationInputDto authenticationInputDto, User user)
            {
                jwtAuthenticationServiceMock.Setup(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>())).ThrowsAsync(new DisabledUserException(user));

                HttpResponseMessage response = await httpClient.PostAsJsonAsync($"{endpointUri}/Authenticate", authenticationInputDto);

                Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
                jwtAuthenticationServiceMock.Verify(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            }
コード例 #7
0
            public async Task PayloadPreenchido_UsuarioSenhaValidos_UsuarioNaoEncontrado_RetornaBadRequest(AuthenticationInputDto authenticationInputDto)
            {
                jwtAuthenticationServiceMock.Setup(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>())).ThrowsAsync(new ResourceNotFoundException(typeof(User), string.Empty));

                HttpResponseMessage response = await httpClient.PostAsJsonAsync($"{endpointUri}/Authenticate", authenticationInputDto);

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
                jwtAuthenticationServiceMock.Verify(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            }
コード例 #8
0
            public async Task PayloadPreenchido_UsuarioOuSenhaInvalidos_RetornaBadRequest(AuthenticationInputDto authenticationInputDto)
            {
                jwtAuthenticationServiceMock.Setup(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>())).ThrowsAsync(new InvalidParameterException(string.Empty));

                HttpResponseMessage response = await httpClient.PostAsJsonAsync($"{endpointUri}/Authenticate", authenticationInputDto);

                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                jwtAuthenticationServiceMock.Verify(mock => mock.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            }
コード例 #9
0
        public async Task <IActionResult> Authenticate([FromBody] AuthenticationInputDto input)
        {
            await _userBusiness.Authenticate(input);

            return(Ok());
        }