001    package org.esupportail.cas.server.handlers.example;
002    
003    import org.dom4j.Element;
004    import org.esupportail.cas.server.util.BasicHandler;
005    
006    /**
007     * This class implements a very simple handler accepting one
008     * particular login/password.
009     *
010     * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
011     */
012    public final class ExampleHandler extends BasicHandler {
013            
014            /** the only netId that the handler will accept */
015            private String login;
016    
017            /** the associated password */
018            private String password;
019    
020            /**
021             * Analyse the XML configuration to set netId and password attributes (constructor).
022             * 
023             * @param handlerElement the XML element that declares the handler in the configuration file
024             * @param configDebug debugging mode of the global configuration (set by default to the handler)
025             * 
026             * @throws Exception when the handler not configured correctly
027             */
028            public ExampleHandler(
029                            final Element handlerElement, 
030                            final Boolean configDebug) throws Exception {
031                    super(handlerElement, configDebug);
032                    traceBegin();
033    
034                    // check that a config element is present
035                    checkConfigElement(true);
036    
037                    // get the configuration parameters
038                    login = getConfigSubElementContent("login", true/*needed*/);
039                    trace("login = " + login);
040                    password = getConfigSubElementContent("password", true/*needed*/);
041                    trace("password = " + password);
042    
043                    traceEnd();
044            }
045            
046            /**
047             * Try to authenticate a user (compare with the local credentials).
048             *
049             * @param userLogin the user's login
050             * @param userPassword the user's password
051             *
052             * @return BasicHandler.SUCCEDED on success, 
053             * BasicHandler.FAILED_CONTINUE or BasicHandler.FAILED_STOP otherwise.
054             */     
055            public int authenticate(
056                            final String userLogin,
057                            final String userPassword) {
058                    traceBegin();
059                    
060                    trace("Checking user's login...");
061                    if (userLogin.equals(login) && userPassword.equals(password)) {
062                            trace("Users's login matches, checking user's password...");
063                            if (userPassword.equals(password)) {
064                                    trace("User's password matches.");
065                                    traceEnd("SUCCEEDED");
066                                    return SUCCEEDED;
067                            } else {
068                                    trace("User's password does not match (no more handler should be tried).");
069                                    traceEnd("FAILED_STOP");
070                                    return FAILED_STOP;
071                            }
072                    } else {
073                            trace("User's login does not match (another handler can be tried).");
074                            traceEnd("FAILED_CONTINUE");
075                            return FAILED_CONTINUE;
076                    }
077            }
078    }