class LogsearchExternalServerAuthenticationProvider extends LogsearchAbstractAuthenticationProvider {

  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!authPropsConfig.isAuthExternalEnabled()) {
      LOG.debug("external server 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.");
    }
    password = StringEscapeUtils.unescapeHtml(password);
    username = StringEscapeUtils.unescapeHtml(username);
    
    try {
      String finalLoginUrl = authPropsConfig.getExternalAuthLoginUrl().replace("$USERNAME", username);
      String responseObj = (String) externalServerClient.sendGETRequest(finalLoginUrl, String.class, username, password);
      if (!isAllowedRole(responseObj)) {
        LOG.error(username + " doesn't have permission");
        throw new BadCredentialsException("Invalid User");
      }
    } catch (Exception e) {
      LOG.error("Login failed for username :" + username + " Error :" + e.getLocalizedMessage());
      throw new BadCredentialsException("Bad credentials");
    }
    authentication = new UsernamePasswordAuthenticationToken(username, password, getAuthorities());
    return authentication;
  }

}