- Timestamp:
- 08/19/08 00:55:11 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/rapla/plugin/jndi/JNDIAuthenticationStore.java
r853 r1332 65 65 import java.util.ArrayList; 66 66 import java.util.Hashtable; 67 import java.util.Map; 68 import java.util.TreeMap; 67 69 68 70 import javax.naming.AuthenticationException; … … 75 77 import javax.naming.directory.Attribute; 76 78 import javax.naming.directory.Attributes; 79 import javax.naming.directory.BasicAttribute; 80 import javax.naming.directory.BasicAttributes; 77 81 import javax.naming.directory.DirContext; 78 82 import javax.naming.directory.InitialDirContext; … … 82 86 import org.apache.avalon.framework.activity.Startable; 83 87 import org.apache.avalon.framework.configuration.Configuration; 84 import org.apache.avalon.framework.configuration.ConfigurationException;85 88 import org.apache.avalon.framework.logger.AbstractLogEnabled; 86 89 import org.apache.avalon.framework.logger.ConsoleLogger; … … 89 92 import org.rapla.entities.Category; 90 93 import org.rapla.framework.RaplaException; 94 import org.rapla.plugin.jndi.internal.JNDIConf; 91 95 import org.rapla.storage.AuthenticationStore; 96 import org.rapla.storage.RaplaSecurityException; 92 97 93 98 /** … … 125 130 */ 126 131 127 public class JNDIAuthenticationStore extends AbstractLogEnabled implements AuthenticationStore,Startable {132 public class JNDIAuthenticationStore extends AbstractLogEnabled implements AuthenticationStore,Startable,JNDIConf { 128 133 // ----------------------------------------------------- Instance Variables 129 134 135 130 136 /** 131 137 * Digest algorithm used in storing passwords in a non-plaintext format. … … 199 205 */ 200 206 protected String userSearch = null; 207 201 208 202 209 … … 213 220 protected int connectionAttempt = 0; 214 221 215 public JNDIAuthenticationStore(Configuration config,Logger logger) throws ConfigurationException{222 public JNDIAuthenticationStore(Configuration config,Logger logger) throws RaplaException { 216 223 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; 227 291 } 228 292 … … 262 326 JNDIUser intUser = authenticateUser( username, password ); 263 327 if ( intUser == null ) 264 throw new Rapla Exception("Can't authenticate user " + username);328 throw new RaplaSecurityException("Can't authenticate user " + username); 265 329 String oldUsername = user.getUsername(); 266 330 if ( oldUsername == null || !oldUsername.equals( username ) ) { … … 278 342 modified = true; 279 343 } 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 280 351 return modified; 281 352 } … … 545 616 log(" base: " + userBase + " filter: " + filter); 546 617 } 547 618 //filter = ""; 619 Attributes attributes = new BasicAttributes(true); 620 attributes.put(new BasicAttribute("uid","admin")); 548 621 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 553 630 if (results == null || !results.hasMore()) { 554 631 if (getLogger().isDebugEnabled()) { … … 861 938 // Ensure that we have a directory context available 862 939 context = new InitialDirContext(getDirectoryContextEnvironment()); 863 940 /* 864 941 } catch (NamingException e) { 865 942 … … 871 948 // Try connecting to the alternate url. 872 949 context = new InitialDirContext(getDirectoryContextEnvironment()); 873 950 */ 874 951 } finally { 875 952 … … 903 980 if (connectionURL != null && connectionAttempt == 0) 904 981 env.put(Context.PROVIDER_URL, connectionURL); 905 906 982 return env; 907 908 983 } 909 984 … … 943 1018 JNDIAuthenticationStore aut = new JNDIAuthenticationStore(); 944 1019 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" ); 948 1023 //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})" ); 951 1026 try { 952 1027 aut.start(); 953 if ( aut.authenticate ( "admin", " test" ) ) {1028 if ( aut.authenticate ( "admin", "admin" ) ) { 954 1029 System.out.println( "Authentication succeeded." ); 955 1030 } else { … … 961 1036 } 962 1037 1038 963 1039 /** 964 1040 * A private class representing a User
![(please configure the [header_logo] section in trac.ini)](/chrome/site/rapla.jpg)