package net.hackergarten.ldapAuth

/*
 *
 */

import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

public class LdapAuthenticator {
    private final static String searchBase = "ou=accounts,dc=hackergarten,dc=net"
    private final static String ldapURI = "ldap://hackergartenserver:389/"// + searchBase;
    private final static String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory"

      DirContext ldapContext () throws Exception {
        Hashtable<String,String> env = new Hashtable <String,String>();
        return ldapContext(env);
    }

      DirContext ldapContext (Hashtable <String,String>env) throws Exception {
        env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
        env.put(Context.PROVIDER_URL, ldapURI);
        DirContext ctx = new InitialDirContext(env);
        return ctx;
    }

     String getUid (String user) throws Exception {
        DirContext ctx = ldapContext();

        String filter = "(uid=" + user + ")";
        SearchControls ctrl = new SearchControls();
        ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration answer = ctx.search(searchBase, filter, ctrl);

        String dn;
        if (answer.hasMore()) {
            SearchResult result = (SearchResult) answer.next();
            dn = result.getNameInNamespace();
        }
        else {
            dn = null;
        }
        answer.close();
        return dn;
    }

     boolean testBind (String dn, String password) throws Exception {
        Hashtable<String,String> env = new Hashtable <String,String>();
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, dn);
        env.put(Context.SECURITY_CREDENTIALS, password);

        try {
            ldapContext(env);
        }
        catch (javax.naming.AuthenticationException e) {
            return false;
        }
        return true;
    }

    def search(String uid){
        def ldap = ldapContext()

        SearchControls searchCtls = new SearchControls();
        searchCtls.setReturningAttributes("cn,givenName,mail".split(","));
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        NamingEnumeration<SearchResult> answer = ldap.search(searchBase, "(uid=" + uid + ")", searchCtls);
        Map<String, String> amap = null;
        if (answer.hasMoreElements()) {
            Attributes attrs = answer.next().getAttributes();
            if (attrs != null) {
                amap = new HashMap<String, String>();
                NamingEnumeration<? extends Attribute> ne = attrs.getAll();

                while (ne.hasMore()) {
                    Attribute attr = ne.next();
                    amap.put(attr.getID(), attr.get().toString());
                }
                ne.close();
            }
        }
        return amap;
    }
}

@@ -26,16 +26,17 @@
 ldapAuth
+;
 %0A%0A/*%0A *%0A
@@ -231,16 +231,17 @@
 ,dc=net%22
+;
 %0A    pri
@@ -307,16 +307,17 @@
 er:389/%22
+;
 // + sea
@@ -408,23 +408,30 @@
 Factory%22
+;
 %0A%0A    
- 
+private
  DirCont
@@ -448,32 +448,38 @@
 ntext () throws 
+Naming
 Exception %7B%0A    
@@ -586,25 +586,31 @@
 %0A    %7D%0A%0A    
- 
+private
  DirContext 
@@ -651,32 +651,38 @@
 ing%3Eenv) throws 
+Naming
 Exception %7B%0A    
@@ -871,24 +871,30 @@
 %0A    %7D%0A%0A    
+public
  String getU
@@ -1474,16 +1474,22 @@
  %7D%0A%0A    
+public
  boolean
@@ -1968,43 +1968,95 @@
     
-def search(String uid)%7B%0A        def
+public Map%3CString,String%3E search(String uid) throws NamingException%7B%0A        DirContext
  lda
@@ -2072,16 +2072,17 @@
 ontext()
+;
 %0A%0A      

convert groovy to java code