1 43 package net.jforum.sso; 44 45 import java.util.Hashtable ; 46 import java.util.Map ; 47 48 import javax.naming.AuthenticationException ; 49 import javax.naming.Context ; 50 import javax.naming.directory.Attribute ; 51 import javax.naming.directory.DirContext ; 52 import javax.naming.directory.InitialDirContext ; 53 54 import net.jforum.dao.UserDAO; 55 import net.jforum.entities.User; 56 import net.jforum.util.preferences.ConfigKeys; 57 import net.jforum.util.preferences.SystemGlobals; 58 59 65 public class LDAPAuthenticator implements LoginAuthenticator 66 { 67 private Hashtable prepareEnvironment() 68 { 69 Hashtable h = new Hashtable (); 70 71 h.put(Context.INITIAL_CONTEXT_FACTORY, SystemGlobals.getValue(ConfigKeys.LDAP_FACTORY)); 72 h.put(Context.PROVIDER_URL, SystemGlobals.getValue(ConfigKeys.LDAP_SERVER_URL)); 73 74 String protocol = SystemGlobals.getValue(ConfigKeys.LDAP_SECURITY_PROTOCOL); 75 76 if (protocol != null && !"".equals(protocol.trim())) { 77 h.put(Context.SECURITY_PROTOCOL, protocol); 78 } 79 80 String authentication = SystemGlobals.getValue(ConfigKeys.LDAP_AUTHENTICATION); 81 82 if (authentication != null && !"".equals(authentication.trim())) { 83 h.put(Context.SECURITY_AUTHENTICATION, authentication); 84 } 85 86 return h; 87 } 88 89 92 public User validateLogin(String username, String password, Map extraParams) throws Exception 93 { 94 Hashtable environment = this.prepareEnvironment(); 95 96 String principal = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX) 97 + username 98 + "," 99 + SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX); 100 101 environment.put(Context.SECURITY_PRINCIPAL, principal); 102 environment.put(Context.SECURITY_CREDENTIALS, password); 103 104 DirContext dir = null; 105 106 try { 107 dir = new InitialDirContext (environment); 108 109 String lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_PREFIX); 110 String lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_SUFFIX); 111 112 if (lookupPrefix == null || lookupPrefix.length() == 0) { 113 lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX); 114 } 115 116 if (lookupSuffix == null || lookupSuffix .length() == 0) { 117 lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX); 118 } 119 120 String lookupPrincipal = lookupPrefix + username + "," + lookupSuffix ; 121 122 Attribute att = dir.getAttributes(lookupPrincipal ).get(SystemGlobals.getValue(ConfigKeys.LDAP_FIELD_EMAIL)); 123 124 SSOUtils utils = new SSOUtils(); 125 126 if (!utils.userExists(username)) { 127 String email = att != null ? (String )att.get() : "noemail"; 128 utils.register("ldap", email); 129 } 130 131 return utils.getUser(); 132 } 133 catch (AuthenticationException e) { 134 return null; 135 } 136 finally { 137 if (dir != null) { 138 dir.close(); 139 } 140 } 141 } 142 143 146 public void setUserModel(UserDAO dao) 147 { 148 } 149 } 150 | Popular Tags |