Show
Ignore:
Timestamp:
08/19/08 00:55:11 (4 years ago)
Author:
kohlhaas
Message:

closes #57

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/org/rapla/plugin/jndi/JNDIAuthenticationStore.java

    r853 r1332  
    6565import java.util.ArrayList; 
    6666import java.util.Hashtable; 
     67import java.util.Map; 
     68import java.util.TreeMap; 
    6769 
    6870import javax.naming.AuthenticationException; 
     
    7577import javax.naming.directory.Attribute; 
    7678import javax.naming.directory.Attributes; 
     79import javax.naming.directory.BasicAttribute; 
     80import javax.naming.directory.BasicAttributes; 
    7781import javax.naming.directory.DirContext; 
    7882import javax.naming.directory.InitialDirContext; 
     
    8286import org.apache.avalon.framework.activity.Startable; 
    8387import org.apache.avalon.framework.configuration.Configuration; 
    84 import org.apache.avalon.framework.configuration.ConfigurationException; 
    8588import org.apache.avalon.framework.logger.AbstractLogEnabled; 
    8689import org.apache.avalon.framework.logger.ConsoleLogger; 
     
    8992import org.rapla.entities.Category; 
    9093import org.rapla.framework.RaplaException; 
     94import org.rapla.plugin.jndi.internal.JNDIConf; 
    9195import org.rapla.storage.AuthenticationStore; 
     96import org.rapla.storage.RaplaSecurityException; 
    9297 
    9398/** 
     
    125130 */ 
    126131 
    127 public class JNDIAuthenticationStore extends AbstractLogEnabled implements AuthenticationStore,Startable { 
     132public class JNDIAuthenticationStore extends AbstractLogEnabled implements AuthenticationStore,Startable,JNDIConf { 
    128133    // ----------------------------------------------------- Instance Variables 
    129134 
     135    
    130136    /** 
    131137     * Digest algorithm used in storing passwords in a non-plaintext format. 
     
    199205     */ 
    200206    protected String userSearch = null; 
     207     
    201208 
    202209 
     
    213220    protected int connectionAttempt = 0; 
    214221 
    215     public JNDIAuthenticationStore(Configuration config,Logger logger) throws ConfigurationException{ 
     222    public JNDIAuthenticationStore(Configuration config,Logger logger) throws RaplaException { 
    216223        enableLogging( logger); 
    217         setDigest( config.getAttribute( "digest", null ) ); 
    218         setConnectionName( config.getAttribute( "connectionName" ) ); 
    219         setConnectionPassword( config.getAttribute( "connectionPassword", null) ); 
    220         setConnectionURL( config.getAttribute( "connectionURL" ) ); 
    221         setContextFactory( config.getAttribute( "contextFactory", contextFactory ) ); 
    222         setUserPassword( config.getAttribute( "userPassword", null ) ); 
    223         setUserMail( config.getAttribute( "userMail", null ) ); 
    224         setUserCn( config.getAttribute( "userCn", null ) ); 
    225         setUserSearch( config.getAttribute( "userSearch") ); 
    226         setUserBase( config.getAttribute( "userBase") ); 
     224        Map map = generateMap(config); 
     225        initWithMap(map); 
     226        /* 
     227        setDigest( config.getAttribute( DIGEST, null ) ); 
     228        setConnectionName( config.getAttribute( CONNECTION_NAME ) ); 
     229        setConnectionPassword( config.getAttribute( CONNECTION_PASSWORD, null) ); 
     230        setConnectionURL( config.getAttribute( CONNECTION_URL ) ); 
     231        setContextFactory( config.getAttribute( CONTEXT_FACTORY, contextFactory ) ); 
     232        setUserPassword( config.getAttribute( USER_PASSWORD, null ) ); 
     233        setUserMail( config.getAttribute( USER_MAIL, null ) ); 
     234        setUserCn( config.getAttribute( USER_CN, null ) ); 
     235        setUserSearch( config.getAttribute( USER_SEARCH) ); 
     236        setUserBase( config.getAttribute( USER_BASE) ); 
     237    */ 
     238    } 
     239 
     240    static public Map generateMap(Configuration config) { 
     241        String[] attributes = config.getAttributeNames(); 
     242        Map map = new TreeMap(); 
     243        for (int i=0;i<attributes.length;i++) 
     244        { 
     245            map.put( attributes[i], config.getAttribute(attributes[i], null)); 
     246        } 
     247        return map; 
     248    } 
     249     
     250    public static JNDIAuthenticationStore createJNDIAuthenticationStore( 
     251            Map config, Logger logger) throws RaplaException { 
     252        return new JNDIAuthenticationStore(config, logger); 
     253    } 
     254 
     255    private JNDIAuthenticationStore(Map config, Logger logger) throws RaplaException 
     256    { 
     257        enableLogging(logger); 
     258        initWithMap(config); 
     259         
     260    } 
     261 
     262    private void initWithMap(Map config) throws RaplaException { 
     263        setDigest( getAttribute( config,DIGEST, null ) ); 
     264        setConnectionName( getAttribute(config, CONNECTION_NAME ) ); 
     265        setConnectionPassword( getAttribute( config,CONNECTION_PASSWORD, null) ); 
     266        setConnectionURL( getAttribute( config,CONNECTION_URL ) ); 
     267        setContextFactory( getAttribute( config,CONTEXT_FACTORY, contextFactory ) ); 
     268        setUserPassword( getAttribute( config,USER_PASSWORD, null ) ); 
     269        setUserMail( getAttribute( config,USER_MAIL, null ) ); 
     270        setUserCn( getAttribute( config,USER_CN, null ) ); 
     271        setUserSearch( getAttribute( config,USER_SEARCH) ); 
     272        setUserBase( getAttribute( config,USER_BASE) ); 
     273    } 
     274 
     275    private String getAttribute(Map config, String key, String defaultValue) { 
     276        Object object = config.get(key); 
     277        if (object == null) 
     278        { 
     279            return defaultValue; 
     280        } 
     281        return (String)object; 
     282    } 
     283 
     284    private String getAttribute(Map config, String key) throws RaplaException{ 
     285        String result = getAttribute(config, key, null); 
     286        if ( result == null) 
     287        { 
     288            throw new RaplaException("Can't find provided configuration entry for key " + key); 
     289        } 
     290        return result; 
    227291    } 
    228292 
     
    262326        JNDIUser intUser = authenticateUser( username, password ); 
    263327        if ( intUser == null ) 
    264             throw new RaplaException("Can't authenticate user " + username); 
     328            throw new RaplaSecurityException("Can't authenticate user " + username); 
    265329        String oldUsername = user.getUsername(); 
    266330        if ( oldUsername == null || !oldUsername.equals( username ) ) { 
     
    278342            modified = true; 
    279343        }  
     344        /* Uncomment this if you want to add predefined groups for the new users 
     345        Category canReadEvents = userGroupCategory.getCategory( Permission.GROUP_CAN_READ_EVENTS_FROM_OTHERS); 
     346        user.addGroup( canReadEvents); 
     347        Category canModifiyPreferences = userGroupCategory.getCategory( Permission.GROUP_MODIFY_PREFERENCES_KEY); 
     348        user.addGroup( canModifiyPreferences); 
     349        */ 
     350         
    280351        return modified; 
    281352    } 
     
    545616            log("  base: " + userBase + "  filter: " + filter); 
    546617        } 
    547  
     618        //filter = ""; 
     619        Attributes attributes = new BasicAttributes(true); 
     620        attributes.put(new BasicAttribute("uid","admin")); 
    548621        NamingEnumeration results = 
    549             context.search(userBase, filter, constraints); 
    550  
    551  
    552         // Fail if no entries found 
     622            //context.search(userBase,attributes);// 
     623            context.search(userBase,  filter,constraints); 
     624/* 
     625        while ( results.hasMore()) 
     626        { 
     627            System.out.println( results.next()); 
     628        } 
     629  */      // Fail if no entries found 
    553630        if (results == null || !results.hasMore()) { 
    554631            if (getLogger().isDebugEnabled()) { 
     
    861938            // Ensure that we have a directory context available 
    862939            context = new InitialDirContext(getDirectoryContextEnvironment()); 
    863  
     940/* 
    864941        } catch (NamingException e) { 
    865942 
     
    871948            // Try connecting to the alternate url. 
    872949            context = new InitialDirContext(getDirectoryContextEnvironment()); 
    873  
     950*/ 
    874951        } finally { 
    875952 
     
    903980        if (connectionURL != null && connectionAttempt == 0) 
    904981            env.put(Context.PROVIDER_URL, connectionURL); 
    905  
    906982        return env; 
    907  
    908983    } 
    909984 
     
    9431018        JNDIAuthenticationStore aut = new JNDIAuthenticationStore(); 
    9441019        aut.enableLogging(new ConsoleLogger()); 
    945         aut.setConnectionName( "cn=Manager,dc=einfachanders" ); 
    946         aut.setConnectionPassword( "rapla2003" ); 
    947         aut.setConnectionURL( "ldap://localhost:389" ); 
     1020        aut.setConnectionName( "uid=admin,ou=system" ); 
     1021        aut.setConnectionPassword( "secret" ); 
     1022        aut.setConnectionURL( "ldap://localhost:10389" ); 
    9481023        //aut.setUserPassword ( "userPassword" ); 
    949         aut.setUserBase ( "ou=people,dc=einfachanders" ); 
    950         aut.setUserSearch (" (uid={0})" ); 
     1024        aut.setUserBase ( "dc=example,dc=com" ); 
     1025        aut.setUserSearch ("(uid={0})" ); 
    9511026        try { 
    9521027            aut.start(); 
    953             if ( aut.authenticate ( "admin", "test" ) ) { 
     1028            if ( aut.authenticate ( "admin", "admin" ) ) { 
    9541029                System.out.println( "Authentication succeeded." ); 
    9551030            } else { 
     
    9611036    } 
    9621037 
     1038    
    9631039    /** 
    9641040     * A private class representing a User