Changeset 1332

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

closes #57

Location:
trunk/src/org/rapla/plugin/jndi
Files:
7 added
2 modified
1 moved

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 
  • trunk/src/org/rapla/plugin/jndi/JNDIPlugin.java

    r1091 r1332  
    1717import org.rapla.framework.PluginDescriptor; 
    1818import org.rapla.plugin.RaplaExtensionPoints; 
     19import org.rapla.plugin.jndi.internal.JNDIOption; 
     20import org.rapla.plugin.jndi.internal.JNDITest; 
     21import org.rapla.plugin.jndi.internal.RaplaJNDITestOnLocalhost; 
     22import org.rapla.plugin.jndi.internal.RaplaJNDITestOnServer; 
     23import org.rapla.plugin.jndi.internal.RaplaJNDITestRemoteServiceFactory; 
     24import org.rapla.server.RaplaRemoteServiceFactory; 
     25import org.rapla.server.ServerService; 
    1926import org.rapla.storage.AuthenticationStore; 
    2027 
    2128public class JNDIPlugin implements PluginDescriptor { 
    2229    public static final String PLUGIN_CLASS = JNDIPlugin.class.getName(); 
    23     static final String PLUGIN_NAME = "Ldap or other JNDI Authentication"; 
     30    public static final String PLUGIN_NAME = "Ldap or other JNDI Authentication"; 
    2431 
    25      
     32    public static final String JNDI_ON_SERVER = JNDIPlugin.class.getPackage().getName() + ".JNDIOnServer"; 
     33    public static final String JNDI_ON_LOCALHOST = JNDIPlugin.class.getPackage().getName() + ".JNDIOnLocalhost"; 
     34    
    2635    public String toString() { 
    2736        return PLUGIN_NAME; 
    2837    } 
    2938     
    30     public void provideServices(Container container, Configuration config) { 
     39    public void provideServices(Container container, Configuration config)  
     40    { 
     41        if ( container.getContext().has( ServerService.ROLE) ){ 
     42            container.addContainerProvidedComponent( RaplaRemoteServiceFactory.ROLE, RaplaJNDITestRemoteServiceFactory.class.getName()); 
     43            // only add mail service on localhost 
     44            container.addContainerProvidedComponent( JNDITest.ROLE, RaplaJNDITestOnLocalhost.class.getName(), JNDI_ON_LOCALHOST , config); 
     45        } else { 
     46            // the following order is important for resolving, 
     47            // first add the service on the server 
     48            // then on localhost 
     49    //        container.addContainerProvidedComponent( JNDITest.ROLE, RaplaJNDITestOnLocalhost.class.getName(), JNDI_ON_LOCALHOST , config); 
     50            container.addContainerProvidedComponent( JNDITest.ROLE, RaplaJNDITestOnServer.class.getName(), JNDI_ON_SERVER , config); 
     51        } 
    3152        container.addContainerProvidedComponent( RaplaExtensionPoints.PLUGIN_OPTION_PANEL_EXTENSION,JNDIOption.class.getName(),JNDIPlugin.class.getName(), config); 
    32          
     53            
    3354        if ( !config.getAttributeAsBoolean("enabled", false) ) 
    3455                return; 
  • trunk/src/org/rapla/plugin/jndi/internal/JNDIOption.java

    r1141 r1332  
    1111 | Definition as published by the Open Source Initiative (OSI).             | 
    1212 *--------------------------------------------------------------------------*/ 
    13 package org.rapla.plugin.jndi; 
     13package org.rapla.plugin.jndi.internal; 
    1414 
    1515import java.awt.BorderLayout; 
     16import java.awt.event.ActionEvent; 
     17import java.awt.event.ActionListener; 
    1618import java.util.Locale; 
    1719 
     20import javax.swing.JButton; 
    1821import javax.swing.JComponent; 
    1922import javax.swing.JLabel; 
     
    2831import org.rapla.gui.DefaultPluginOption; 
    2932import org.rapla.gui.OptionPanel; 
    30  
    31 public class JNDIOption extends DefaultPluginOption implements OptionPanel { 
     33import org.rapla.gui.toolkit.DialogUI; 
     34import org.rapla.plugin.jndi.JNDIPlugin; 
     35 
     36public class JNDIOption extends DefaultPluginOption implements OptionPanel,JNDIConf { 
    3237        TableLayout tableLayout; 
    3338        JPanel content; 
     
    4449        JTextField userBase; 
    4550         
     51         
    4652 
    4753    public JNDIOption(RaplaContext sm) throws RaplaException { 
     
    7177        tableLayout.insertRow( 0, TableLayout.PREFERRED); 
    7278        content.add( new JLabel("WARNING! Only enable plugin, if you have a LDAP Server running!"), "0,0,2,0"); 
    73         addRow("connectionName", connectionName); 
    74         addRow("connectionPassword", connectionPassword ); 
    75         addRow("connectionURL", connectionURL ); 
    76         addRow("contextFactory", contextFactory); 
    77         addRow( "digest", digest); 
    78         addRow("userPassword", userPassword ); 
    79         addRow("userMail", userMail ); 
    80         addRow("userCn", userCn ); 
    81         addRow("userSearch", userSearch ); 
    82         addRow("userBase", userBase ); 
     79        addRow(CONNECTION_NAME, connectionName); 
     80        addRow(CONNECTION_PASSWORD, connectionPassword ); 
     81        addRow(CONNECTION_URL, connectionURL ); 
     82        addRow(CONTEXT_FACTORY, contextFactory); 
     83        addRow(DIGEST, digest); 
     84        addRow(USER_PASSWORD, userPassword ); 
     85        addRow(USER_MAIL, userMail ); 
     86        addRow(USER_CN, userCn ); 
     87        addRow(USER_SEARCH, userSearch ); 
     88        addRow(USER_BASE, userBase ); 
     89        JButton testButton = new JButton("Test access"); 
     90        addRow("TestAccess", testButton ); 
     91        testButton.addActionListener( new ActionListener()  
     92        { 
     93 
     94            public void actionPerformed(ActionEvent e) { 
     95                try 
     96                { 
     97                    JNDITest test = (JNDITest)getContext().lookup(JNDITest.ROLE); 
     98                    DefaultConfiguration conf = new DefaultConfiguration("test"); 
     99                    StringBuffer buf = new StringBuffer(); 
     100                    addChildren(conf); 
     101                    String[] attributes = conf.getAttributeNames(); 
     102                    for (int i=0;i<attributes.length;i++) 
     103                    { 
     104                        String attribute = attributes[i]; 
     105                        String value = conf.getAttribute( attribute, null); 
     106                        buf.append( attribute); 
     107                        buf.append("="); 
     108                        buf.append( value ); 
     109                        buf.append("RAPLANEXT"); 
     110                    } 
     111                    String list = buf.toString(); 
     112                    String username = "admin"; 
     113                    String password =""; 
     114                    { 
     115                        PasswordEnterUI testUser = new PasswordEnterUI(getContext()); 
     116                        DialogUI dialog =DialogUI.create( getContext(), getComponent(), true,testUser.getComponent(),new String[] {"test","abort"}); 
     117                        dialog.setTitle("Please enter valid user!"); 
     118                        dialog.start(); 
     119                        username = testUser.getUsername(); 
     120                        password = new String(testUser.getNewPassword()); 
     121                        int index=dialog.getSelectedIndex(); 
     122                        if ( index > 0) 
     123                        { 
     124                            return; 
     125                        } 
     126                    } 
     127                    test.test(list,username,password); 
     128                    { 
     129                        DialogUI dialog =DialogUI.create( getContext(), getComponent(), true, "JNDI","JNDI Authentification successfull"); 
     130                        dialog.start(); 
     131                    } 
     132                } catch (Exception ex) 
     133                { 
     134                    showException(ex, getComponent()); 
     135                } 
     136                 
     137            } 
     138             
     139        }); 
    83140        panel.add( content, BorderLayout.CENTER); 
    84141        return panel; 
     
    94151         
    95152    protected void addChildren( DefaultConfiguration newConfig) { 
    96         setAttribute(newConfig,"digest", digest); 
    97         setAttribute(newConfig,"connectionName", connectionName); 
    98         setAttribute(newConfig,"connectionPassword", connectionPassword ); 
    99         setAttribute(newConfig,"connectionURL", connectionURL ); 
    100         setAttribute(newConfig,"contextFactory", contextFactory); 
    101         setAttribute(newConfig,"userPassword", userPassword ); 
    102         setAttribute(newConfig,"userMail", userMail ); 
    103         setAttribute(newConfig,"userCn", userCn ); 
    104         setAttribute(newConfig,"userSearch", userSearch ); 
    105         setAttribute(newConfig,"userBase", userBase ); 
     153        setAttribute(newConfig,CONNECTION_NAME, connectionName); 
     154        setAttribute(newConfig,CONNECTION_PASSWORD, connectionPassword ); 
     155        setAttribute(newConfig,CONNECTION_URL, connectionURL ); 
     156        setAttribute(newConfig,CONTEXT_FACTORY, contextFactory); 
     157        setAttribute(newConfig,DIGEST, digest); 
     158        setAttribute(newConfig,USER_BASE, userBase ); 
     159        setAttribute(newConfig,USER_CN, userCn ); 
     160        setAttribute(newConfig,USER_MAIL, userMail ); 
     161        setAttribute(newConfig,USER_PASSWORD, userPassword ); 
     162        setAttribute(newConfig,USER_SEARCH, userSearch ); 
    106163    } 
    107164