class LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider {

  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!authPropsConfig.isAuthFileEnabled()) {
      logger.debug("File auth is disabled.");
      return authentication;
    }
    
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    if (StringUtils.isBlank(username)) {
      throw new BadCredentialsException("Username can't be null or empty.");
    }
    if (StringUtils.isBlank(password)) {
      throw new BadCredentialsException("Password can't be null or empty.");
    }
    // html unescape
    password = StringEscapeUtils.unescapeHtml(password);
    username = StringEscapeUtils.unescapeHtml(username);

    UserDetails user = userDetailsService.loadUserByUsername(username);
    if (user == null) {
      logger.error("Username not found.");
      throw new BadCredentialsException("User not found.");
    }
    if (StringUtils.isEmpty(user.getPassword())) {
      logger.error("Password can't be null or empty.");
      throw new BadCredentialsException("Password can't be null or empty.");
    }
    //String encPassword = passwordEncoder.encode(password);
    if (!passwordEncoder.matches(password, user.getPassword())) {
      logger.error("Wrong password for user=" + username);
      throw new BadCredentialsException("Wrong password.");
    }
    
    Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
    authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities);
    return authentication;
  }

}