Project

General

Profile

LdapAuth.java

Jonathan Barber, 12/06/2012 19:44

Download (3.09 KB)

 
1
/*
2
 * First create the keystore (to allow SSL protection) by importing the LDAP
3
 * certificate (cert.pem) with:
4
 *   keytool -import -keystore keystore -storepass changeit -noprompt -file cert.pem
5
 *
6
 * You can get the certificate with OpenSSL:
7
 *   openssl s_client -connect ldap.server.com:636 </dev/null 2>/dev/null | sed -n '/-----BEGIN/,/-----END/ { p }' > cert.pem
8
 *  
9
 * Then compile this class with:
10
 *   javac LdapAuth.java
11
 *
12
 * Finally execute it with:
13
 *   java -Djavax.net.ssl.trustStore=keystore -Djavax.net.ssl.keyStorePassword=changeit LdapAuth <username> <password>
14
 */
15
package pt.up.fe;
16

    
17
import java.util.*;
18
import javax.naming.*;
19
import java.util.regex.*;
20
import javax.naming.directory.*;
21

    
22
public class LdapAuth {
23
        private final static String ldapURI = "ldaps://ldap.fe.up.pt/dc=fe,dc=up,dc=pt";
24
        private final static String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
25

    
26
        private static DirContext ldapContext () throws Exception {
27
                Hashtable<String,String> env = new Hashtable <String,String>();
28
                return ldapContext(env);
29
        }
30

    
31
        private static DirContext ldapContext (Hashtable <String,String>env) throws Exception {
32
                env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
33
                env.put(Context.PROVIDER_URL, ldapURI);
34
                DirContext ctx = new InitialDirContext(env);
35
                return ctx;
36
        }
37

    
38
        private static String getUid (String user) throws Exception {
39
                DirContext ctx = ldapContext();
40

    
41
                String filter = "(uid=" + user + ")";
42
                SearchControls ctrl = new SearchControls();
43
                ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
44
                NamingEnumeration answer = ctx.search("", filter, ctrl);
45

    
46
                String dn;
47
                if (answer.hasMore()) {
48
                        SearchResult result = (SearchResult) answer.next();
49
                        dn = result.getNameInNamespace();
50
                }
51
                else {
52
                        dn = null;
53
                }
54
                answer.close();
55
                return dn;
56
        }
57

    
58
        private static boolean testBind (String dn, String password) throws Exception {
59
                Hashtable<String,String> env = new Hashtable <String,String>();
60
                env.put(Context.SECURITY_AUTHENTICATION, "simple");
61
                env.put(Context.SECURITY_PRINCIPAL, dn);
62
                env.put(Context.SECURITY_CREDENTIALS, password);
63

    
64
                try {
65
                        ldapContext(env);
66
                }
67
                catch (javax.naming.AuthenticationException e) {
68
                        return false;
69
                }
70
                return true;
71
        }
72

    
73
        public static boolean authenticate (String user, String password) throws Exception {
74
                String dn = getUid( user );
75
                if (dn != null) {
76
                        System.out.println( "user '" + user + "' not found" );
77
                        if ( testBind( dn, password ) ) {
78
                                return true;
79
                        }
80
                }
81
                return false;
82
        }
83

    
84
        public static void main(String args[]) throws Exception {
85
                if (args.length != 2) {
86
                        System.out.println( "missing requried username and password" );
87
                        System.exit(1);
88
                }
89

    
90
                String user = args[0];
91
                String password = args[1];
92
                String dn = getUid( user );
93

    
94
                if (dn != null) {
95
                        /* Found user - test password */
96
                        if ( testBind( dn, password ) ) {
97
                                System.out.println( "user '" + user + "' authentication succeeded" );
98
                                System.exit(0);
99
                        }
100
                        else {
101
                                System.out.println( "user '" + user + "' authentication failed" );
102
                                System.exit(1);
103
                        }
104
                }
105
                else {
106
                        System.out.println( "user '" + user + "' not found" );
107
                        System.exit(1);
108
                }
109
        }
110
}