/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.cp.xemelios.auth.ldap;

import java.util.Hashtable;
import java.util.Properties;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import fr.gouv.finances.cp.xemelios.auth.AbstractAuthenticationManager;
import fr.gouv.finances.cp.xemelios.auth.AuthChecker;
import fr.gouv.finances.cp.xemelios.auth.AuthProvider;
import fr.gouv.finances.cp.xemelios.auth.AuthenticationConfigurationException;
import fr.gouv.finances.cp.xemelios.auth.XemeliosUser;
import fr.gouv.finances.cp.xemelios.data.DataAccessException;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;

public class LdapManager extends AbstractAuthenticationManager implements AuthChecker {

	private String baseUrl=null, userDN=null;
	private String readAccessDN=null, writeAccessDN=null;
	private AuthProvider provider = null;

	
	public boolean authenticate(XemeliosUser user) throws DataAccessException, AuthenticationConfigurationException {
		LdapUser lu = null;
		if(user instanceof LdapUser) lu = (LdapUser)user;
		else throw new AuthenticationConfigurationException("can only authenticate LdapUser");
		
		String login = user.getId().toUpperCase();
		
		String bindDN = userDN.replaceAll("%LOGIN%", login);
		String readDN = readAccessDN==null?null:readAccessDN.replaceAll("%LOGIN%", login);	
		String writeDN = writeAccessDN==null?null:writeAccessDN.replaceAll("%LOGIN%", login);	
		
		Hashtable<String, String> ht = new Hashtable<String, String>();
		ht.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		ht.put(DirContext.SECURITY_AUTHENTICATION, "simple");
		ht.put(DirContext.SECURITY_PRINCIPAL, bindDN);
		ht.put(DirContext.SECURITY_CREDENTIALS, lu.getPassword());	
		ht.put(DirContext.PROVIDER_URL, baseUrl);
		ht.put("com.sun.jndi.ldap.connect.pool", "false");
		ht.put("com.sun.jndi.ldap.connect.timeout", "60000");
		
		int auth = 0;
		try {
			DirContext context = new InitialDirContext(ht);
			
			//droit de lecture
			try {
				Attributes transaction = context.getAttributes(readDN);
				if(transaction.size() > 0)
					auth += XemeliosUser.ROLE_CONNECT + XemeliosUser.ROLE_BROWSE + XemeliosUser.ROLE_SEARCH + XemeliosUser.ROLE_EXPORT;			
			} catch(NamingException e) {
				e.printStackTrace();				
			}

			//droit d'ecriture
			try {
				Attributes transaction = context.getAttributes(writeDN);
				if(transaction.size() > 0)
					auth += XemeliosUser.ROLE_CLEAN + XemeliosUser.ROLE_IMPORT;
			} catch(NamingException e) {
				e.printStackTrace();				
			}

			context.close();
		} catch(NamingException e) {
			System.out.println("Bad password ? : " + e.getMessage());
			e.printStackTrace();
		}

		lu.setAuth(auth);		
		return (auth != 0);
	}

	public LdapManager(MainWindow mw) {
		super();
		setMainWindow(mw);

	}

	public AuthChecker getChecker() {
		return this;
	}

	public AuthProvider getProvider() {
		if(provider==null) provider = new LdapProvider(getMainWindow()); 
		return provider;
	}

	public String getAuthenticationManagerName() {
		return "Authentification LDAP (CHRU Strasbourg)";
	}

	@Override
	public void setProperties(Properties props) {
		super.setProperties(props);
		baseUrl = (String)getProperty("auth.ldap.baseUrl");
		userDN = (String)getProperty("auth.ldap.userDN"); 
		readAccessDN = (String)getProperty("auth.ldap.readAccessDN");
		writeAccessDN = (String)getProperty("auth.ldap.writeAccessDN");
	}
}
