1 11 12 package org.jivesoftware.messenger.ldap; 13 14 import org.jivesoftware.messenger.auth.AuthProvider; 15 import org.jivesoftware.messenger.auth.UnauthorizedException; 16 import org.jivesoftware.util.Cache; 17 import org.jivesoftware.util.JiveConstants; 18 import org.jivesoftware.util.JiveGlobals; 19 import org.jivesoftware.util.StringUtils; 20 21 40 public class LdapAuthProvider implements AuthProvider { 41 42 private LdapManager manager; 43 private Cache authCache = null; 44 45 public LdapAuthProvider() { 46 manager = LdapManager.getInstance(); 47 if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.authCache.enabled")).booleanValue()) { 48 int maxSize = JiveGlobals.getXMLProperty("ldap.authCache.size", 512*1024); 49 long maxLifetime = (long)JiveGlobals.getXMLProperty("ldap.authCache.maxLifetime", 50 (int)JiveConstants.HOUR * 2); 51 authCache = new Cache("LDAP Auth Cache", maxSize, maxLifetime); 52 } 53 } 54 55 public boolean isPlainSupported() { 56 return true; 57 } 58 59 public boolean isDigestSupported() { 60 return false; 61 } 62 63 public void authenticate(String username, String password) throws UnauthorizedException { 64 if (username == null || password == null || "".equals(password.trim())) { 65 throw new UnauthorizedException(); 66 } 67 68 if (authCache != null && authCache.containsKey(username)) { 70 String hash = (String )authCache.get(username); 71 if (StringUtils.hash(password).equals(hash)) { 72 return; 73 } 74 } 75 76 String userDN = null; 77 try { 78 userDN = manager.findUserDN(username); 89 90 if (!manager.checkAuthentication(userDN, password)) { 92 throw new UnauthorizedException("Username and password don't match"); 93 } 94 } 95 catch (Exception e) { 96 throw new UnauthorizedException(e); 97 } 98 99 if (authCache != null) { 101 authCache.put(username, StringUtils.hash(password)); 102 } 103 } 104 105 public void authenticate(String username, String token, String digest) throws UnsupportedOperationException { 106 throw new UnsupportedOperationException ("Digest authentication not currently supported."); 107 } 108 109 public void updatePassword(String username, String password) throws UnsupportedOperationException { 110 throw new UnsupportedOperationException ("Cannot update password in LDAP"); 111 } 112 } | Popular Tags |