/*
 * $Id: LdapProxy.java,v 1.4 2004/11/23 13:32:41 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: LdapProxy.java,v $
 * Revision 1.4  2004/11/23 13:32:41  jonyp
 * + isAlive-Method
 *
 * Revision 1.3  2004/11/09 16:08:01  marbobo
 * - Unnötige Anweisungen entfernt
 *
 * Revision 1.2  2004/10/15 15:15:50  jonyp
 * changed massively layout of this object...
 *
 * Revision 1.1  2004/10/07 14:41:26  jonyp
 * first strike
 *
 * Revision 1.4  2004/10/06 10:47:45  marbobo
 * + packagename angepasst
 *
 * Revision 1.3  2004/10/05 14:59:16  jonyp
 * minor changes
 *
 * Revision 1.2  2004/10/05 13:56:12  jonyp
 * userInGroup proxy method
 * 
 */ 
package vkc.ldap;

import java.util.Hashtable;
import javax.naming.directory.*;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

/* sample data
 ctxFatory = "com.sun.jndi.ldap.LdapCtxFactory";
 providerUrl = "ldap://ldap.baylor.edu:389";
 principalDN = "ou=people, o=Baylor University, c=US";
 passwort = "";
 searchbase = "ou=people, ou=Faculty and Staff, ou=Advising & Teacher Certification - Education,ou=Other";
 filter = "ou=people";
*/

/**
 * @author       Johannes Plaßmann
 * @version      1.0
 */

public class LdapProxy {
    private static String ctxFactory = "com.sun.jndi.ldap.LdapCtxFactory";    

    public final static LdapContext getDirContext( String providerURL, String principalDN, String password, String authProtocol ) {
        // Hashtable for environmental information
        Hashtable env = new Hashtable();
    
        // Specify which class to use for our JNDI Provider
        env.put( Context.INITIAL_CONTEXT_FACTORY, ctxFactory );

        // Specify the host and port to use for directory service
        env.put( Context.PROVIDER_URL, providerURL );
        
        // Security Information
        env.put( Context.SECURITY_AUTHENTICATION, authProtocol );
        env.put( Context.SECURITY_PRINCIPAL, principalDN );
        env.put( Context.SECURITY_CREDENTIALS, password );

        // Get a reference to a directory context
        // Create the initial context
        // if the context is created successfully, "user<->password" combination is ok...
        // otherwise an exception is thrown
        try {
            LdapContext ldapCtx = new InitialLdapContext( env, null );
            
            return ldapCtx;
        } catch ( Exception e ) {
            return null;
        }
    }
    
    public static boolean authenticateUser( String providerURL, String principalDN, String password, String authProtocol ) {
        if ( getDirContext( providerURL, principalDN, password, authProtocol ) != null ) {
            return true;
        }
        
        return false; 
    }
    
    public static boolean isAlive (String providerURL, String managerDN, String password, String authProtocol) {
        // preliminiary: return true
        return true;
    }
    
    public static NamingEnumeration search( LdapContext ctx, String searchBase, String searchFilter, int searchScope ) throws NamingException {
        if ( ctx == null ) {
            return null;
        }
        
        // Specify the scope of the search
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope( searchScope );

        // Perform the actual search
        // We give it a searchbase, a filter and the constraints
        // containing the scope of the search
        return ctx.search( searchBase, searchFilter, constraints );
    }
}