1 16 package org.outerj.daisy.authentication.impl; 17 18 import org.outerj.daisy.authentication.AuthenticationScheme; 19 import org.outerj.daisy.authentication.AuthenticationException; 20 import org.outerj.daisy.authentication.UserCreator; 21 import org.outerj.daisy.repository.Credentials; 22 import org.outerj.daisy.repository.user.User; 23 import org.outerj.daisy.repository.user.UserManager; 24 import org.apache.avalon.framework.logger.Logger; 25 26 import javax.naming.Context ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.InitialDirContext ; 29 import java.util.Hashtable ; 30 import java.util.Map ; 31 import java.util.Iterator ; 32 33 public class LdapAuthenticationScheme implements AuthenticationScheme { 34 private final String name; 35 private final String description; 36 private final Map templateEnvironment; 37 private final UserCreator userCreator; 38 private final Logger logger; 39 40 public LdapAuthenticationScheme(String name, String description, Map templateEnvironment, UserCreator userCreator, Logger logger) { 41 this.name = name; 42 this.description = description; 43 this.templateEnvironment = templateEnvironment; 44 this.userCreator = userCreator; 45 this.logger = logger; 46 } 47 48 public String getName() { 49 return name; 50 } 51 52 public String getDescription() { 53 return description; 54 } 55 56 public void clearCaches() { 57 } 59 60 public boolean check(Credentials credentials) throws AuthenticationException { 61 Hashtable env = new Hashtable (); 62 Iterator templateEnvIt = templateEnvironment.entrySet().iterator(); 63 while (templateEnvIt.hasNext()) { 64 Map.Entry entry = (Map.Entry )templateEnvIt.next(); 65 String key = (String )entry.getKey(); 66 String value = (String )entry.getValue(); 67 if (key.equals(Context.SECURITY_PRINCIPAL)) { 68 value = value.replaceAll("\\$daisyLogin", credentials.getLogin()); 69 } 70 env.put(key, value); 71 } 72 73 String password = credentials.getPassword(); 75 if (password == null || password.trim().length() == 0) { 76 return false; 77 } 78 env.put(Context.SECURITY_CREDENTIALS, password); 79 80 try { 81 new InitialDirContext (env); 82 } catch (NamingException e) { 83 if (logger.isDebugEnabled()) { 84 env.put(Context.SECURITY_CREDENTIALS, "***REMOVED ON PURPOSE***"); 85 logger.debug("Failed to authenticate user with following environment: " + env, e); 86 } 87 return false; 88 } 89 return true; 90 } 91 92 public User createUser(Credentials crendentials, UserManager userManager) throws AuthenticationException { 93 if (userCreator != null) { 94 if (check(crendentials)) { 95 return userCreator.create(crendentials.getLogin(), userManager); 96 } 97 } 98 return null; 99 } 100 } 101 | Popular Tags |