<commit_before>package com.krishagni.catissueplus.core.auth.services.impl;

import java.util.Map;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.authentication.BindAuthenticator;
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;

import com.krishagni.catissueplus.core.auth.domain.AuthErrorCode;
import com.krishagni.catissueplus.core.auth.events.LoginDetail;
import com.krishagni.catissueplus.core.auth.services.AuthenticationService;
import com.krishagni.catissueplus.core.common.errors.OpenSpecimenException;

public class LdapAuthenticationServiceImpl implements AuthenticationService {
	
	private LdapAuthenticationProvider provider;
	
	public LdapAuthenticationServiceImpl() {
		
	}
	
	public LdapAuthenticationServiceImpl(Map<String, String> props) {
		provider = getProvider(props);
	}
	
	@Override
	public void authenticate(LoginDetail loginDetail) {
		try {
			UsernamePasswordAuthenticationToken authenticationToken =
				new UsernamePasswordAuthenticationToken(loginDetail.getLoginName(), loginDetail.getPassword());

			provider.authenticate(authenticationToken);
		} catch (AuthenticationException e) {
			throw OpenSpecimenException.userError(AuthErrorCode.INVALID_CREDENTIALS);
		}
	}	
	
	private LdapAuthenticationProvider getProvider(Map<String, String> props) {
		DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(props.get("url"));
		contextSource.setUserDn(props.get("userDn"));
		contextSource.setPassword(props.get("password"));
		
		contextSource.afterPropertiesSet();
		
		BindAuthenticator authenticator = new BindAuthenticator(contextSource);
		<commit_msg>Log the error details received from the LDAP server. This will help in knowing exactly why the user authentication fails:   - invalid service account password   - invalid user password   - invalid configuration.<commit_after>package com.krishagni.catissueplus.core.auth.services.impl;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.authentication.BindAuthenticator;
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;

import com.krishagni.catissueplus.core.auth.domain.AuthErrorCode;
import com.krishagni.catissueplus.core.auth.events.LoginDetail;
import com.krishagni.catissueplus.core.auth.services.AuthenticationService;
import com.krishagni.catissueplus.core.common.errors.OpenSpecimenException;

public class LdapAuthenticationServiceImpl implements AuthenticationService {

	private final static Log logger = LogFactory.getLog(LdapAuthenticationServiceImpl.class);
	
	private LdapAuthenticationProvider provider;
	
	public LdapAuthenticationServiceImpl() {
		
	}
	
	public LdapAuthenticationServiceImpl(Map<String, String> props) {
		provider = getProvider(props);
	}
	
	@Override
	public void authenticate(LoginDetail loginDetail) {
		try {
			UsernamePasswordAuthenticationToken authenticationToken =
				new UsernamePasswordAuthenticationToken(loginDetail.getLoginName(), loginDetail.getPassword());

			provider.authenticate(authenticationToken);
		} catch (AuthenticationException e) {
			logger.error("User authentication failed", e);
			throw OpenSpecimenException.userError(AuthErrorCode.INVALID_CREDENTIALS);
		}
	}	
	
	private LdapAuthenticationProvider getProvider(Map<String, String> props) {
		DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(props.get("url"));
		contextSource.setUserDn(props.get("userDn"));
		contextSource.setPassword(props.get("password"));
		
		contextSource.afterPropertiesSet();
		
		BindAuthenticator authenticator = new BindAuthenticator(contextSource);
		
{'fraction_non_alphanumeric': 0.08174073221275616, 'fraction_numerical': 0.0, 'mean_word_length': 7.258555133079848, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026198', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1124, 'n_words': 188}