Beispiel #1
0
        public void Should_Fail_On_Password_Too_Short()
        {
            var validator   = new LoginRequestValidator();
            var mockRequest = new LoginRequest()
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            var result = validator.Validate(mockRequest);

            result.IsValid.Should().Be(false);
            result.Errors.FirstOrDefault(e => e.ErrorMessage == errorMessage).Should().NotBeNull();
        }
Beispiel #2
0
        public void Should_Fail_Email_Too_Long()
        {
            var validator   = new LoginRequestValidator();
            var mockRequest = new LoginRequest()
            {
                Email    = new string('&', 300),
                Password = "******"
            };

            var result = validator.Validate(mockRequest);

            result.IsValid.Should().Be(false);
            result.Errors.FirstOrDefault(e => e.ErrorMessage == errorMessage).Should().NotBeNull();
        }
Beispiel #3
0
        public void Should_Pass_On_Correct_Request()
        {
            var validator   = new LoginRequestValidator();
            var mockRequest = new LoginRequest()
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            var result = validator.Validate(mockRequest);

            result.IsValid.Should().Be(true);
            result.Errors.Count.Should().Be(0);
        }
Beispiel #4
0
        public async Task <IActionResult> Login([FromBody] LoginRequest request)
        {
            var validationResult = _loginValidator.Validate(request);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors.First().ErrorMessage));
            }
            var result = await _bus.Send(new LoginCommand(request.UserName, request.Password));

            return(result.Match <IActionResult>(
                       some: x => Ok(new TokenResponse {
                Token = x
            }),
                       none: () => BadRequest()));
        }
        public async Task <IActionResult> Login([FromBody] LoginRequest request)
        {
            var validationResult = _loginRequestValidator.Validate(request);

            if (!validationResult.IsValid)
            {
                return(BadRequest());
            }
            try
            {
                var token = await _authService.Login(request.Identifier, request.Password);

                return(Ok(new TokenResponse {
                    Token = token
                }));
            }
            catch (Exception e)
            {
                return(BadRequest(new ErrorPayload(1, e.Message)));
            }
        }
        public LoginResponse Login(LoginRequest loginRequest)
        {
            LoginResponse         loginResponse = new LoginResponse();
            LoginRequestValidator validator     = new LoginRequestValidator();
            ValidationResult      result        = validator.Validate(loginRequest);

            if (result.IsValid == false)
            {
                loginResponse.setValidator(result);
                return(loginResponse);
            }

            try
            {
                User _user = _userRepository.Get(t => ((t.Email != null && t.Email == loginRequest.Email) || (t.Phone != null && t.Phone == loginRequest.Phone))).FirstOrDefault();

                if (_user == null)
                {
                    loginResponse.SetStatus(Constants.ResponseCode.INVALID_USERNAME_OR_PASSWORD);
                    return(loginResponse);
                }

                loginResponse.Username = _user.Username;


                if (_user.IsLocked && _user.LockedTime > DateTime.Now)
                {
                    loginResponse.SetStatus(Constants.ResponseCode.BANNED);
                    return(loginResponse);
                }

                if (_user.IsLocked)
                {
                    _user.IsLocked = false; _user.WrongCount = 0;
                }

                if (_user.Password != Cryptor.sha512encrypt(loginRequest.Password))
                {
                    _user.WrongCount++;
                    if (_user.WrongCount > 5)
                    {
                        _user.UserLocked(1);
                    }

                    if (_unitOfWork.SaveChanges() > 0)
                    {
                        if (_user.IsLocked)
                        {
                            loginResponse.SetStatus(Constants.ResponseCode.BANNED);
                            return(loginResponse);
                        }
                        else
                        {
                            loginResponse.SetStatus(Constants.ResponseCode.INVALID_USERNAME_OR_PASSWORD);
                            return(loginResponse);
                        }
                    }
                    else
                    {
                        loginResponse.SetStatus(Constants.ResponseCode.SYSTEM_ERROR);
                        return(loginResponse);
                    }
                }

                Token token = CreateToken();
                _user.WrongCount = 0;
                _user.Tokens.Add(token);

                if (_unitOfWork.SaveChanges() > 0)
                {
                    _cacheManager.AddUserToCache(token.TokenText, _user);

                    loginResponse.IsVerify   = _user.IsVerified;
                    loginResponse.Username   = _user.FullName();
                    loginResponse.Token      = token.TokenText;
                    loginResponse.ExpireDate = token.EndDate;
                    loginResponse.Email      = _user.Email;
                    loginResponse.UserRole   = _user.UserRoles.Select(t => t.Role.RoleName).ToList();
                    loginResponse.SetStatus(Constants.ResponseCode.SUCCESS);
                    return(loginResponse);
                }
            }
            catch (Exception ex)
            {
                loginResponse.SetStatus(Constants.ResponseCode.SYSTEM_ERROR);
                return(loginResponse);
            }

            return(loginResponse);
        }