001    package org.esupportail.cas.server.handlers.ldap;
002    
003    import org.dom4j.Element;
004    import org.esupportail.cas.server.util.RedundantHandler;
005    
006    /**
007     * This class implements an LDAP server class, which can
008     * authenticate users by directly binding to an LDAP directory
009     * (fastbind method). It is used by FastBindLdapHandler.
010     *
011     * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
012     */
013    public final class FastBindLdapServer extends LdapServer {
014            
015            /**
016             * Constructor.
017             *
018             * @param handlerDebug debugging mode of the handler
019             * @param handler      the handler the server will be used by
020             * @param serverElement the XML element that declares the server 
021             * @throws Exception Exception
022             */
023            public FastBindLdapServer(
024                            final Boolean handlerDebug,
025                            final RedundantHandler handler,
026                            final Element serverElement) throws Exception {
027                    super(handlerDebug, handler, serverElement);
028                    traceBegin();
029                    traceEnd();
030            }
031            
032            /**
033             * Try to authenticate a user (by binding to the LDAP directory).
034             *
035             * @param username the user's name
036             * @param password the user's password
037             *
038             * @return Server.AUTHENTICATE_SUCCESS, Server.AUTHENTICATE_NOAUTH
039             * or Server.AUTHENTICATE_FAILURE.
040             */     
041            public int authenticate(final String username,
042                            final String password) {
043                    traceBegin();
044                    
045                    FastBindLdapHandler handler = (FastBindLdapHandler) getHandler();
046                    
047                    connectAndClose(replaceTokens(handler.getFilter(), username), password);
048    
049                    switch (getConnectError()) {
050                            case CONNECT_SUCCESS:
051                                    trace("Connection succeeded.");
052                                    traceEnd("AUTHENTICATE_SUCCESS");
053                                    return AUTHENTICATE_SUCCESS;
054                            case CONNECT_NOAUTH:
055                                    trace("Connection refused.");
056                                    traceEnd("AUTHENTICATE_NOAUTH");
057                                    return AUTHENTICATE_NOAUTH;
058                            default:
059                                    trace("Connection failure.");
060                            traceEnd("AUTHENTICATE_FAILURE");
061                            return AUTHENTICATE_FAILURE;
062                    }
063            }
064            
065    }
066