1 16 17 package org.apache.jetspeed.services.security.ldap; 18 19 import javax.naming.directory.BasicAttributes ; 20 import javax.servlet.ServletConfig ; 21 import org.apache.jetspeed.om.security.JetspeedUser; 22 import org.apache.jetspeed.om.security.UserNamePrincipal; 23 import org.apache.jetspeed.services.JetspeedSecurity; 24 import org.apache.jetspeed.services.JetspeedUserManagement; 25 import org.apache.jetspeed.services.rundata.JetspeedRunData; 26 import org.apache.jetspeed.services.rundata.JetspeedRunDataService; 27 import org.apache.jetspeed.services.security.FailedLoginException; 28 import org.apache.jetspeed.services.security.JetspeedSecurityCache; 29 import org.apache.jetspeed.services.security.JetspeedSecurityException; 30 import org.apache.jetspeed.services.security.JetspeedSecurityService; 31 import org.apache.jetspeed.services.security.LoginException; 32 import org.apache.jetspeed.services.security.PortalAuthentication; 33 import org.apache.jetspeed.services.security.UnknownUserException; 34 import org.apache.turbine.services.InitializationException; 35 import org.apache.turbine.services.TurbineBaseService; 36 import org.apache.turbine.services.TurbineServices; 37 import org.apache.turbine.services.resources.ResourceService; 38 import org.apache.turbine.services.rundata.RunDataService; 39 40 48 public class LDAPAuthentication extends TurbineBaseService 49 implements PortalAuthentication 50 { 51 private final static String CONFIG_ANONYMOUS_USER = "user.anonymous"; 53 private final static String CACHING_ENABLE = "caching.enable"; 54 55 private JetspeedRunDataService runDataService = null; 57 private String anonymousUser = "anon"; 58 private boolean cachingEnable = true; 59 60 73 public JetspeedUser login(String username, String password) 74 throws LoginException 75 { 76 77 if (username.equals(this.anonymousUser)) 78 { 79 throw new LoginException("Anonymous user cannot login"); 80 } 81 82 JetspeedUser user = null; 83 BasicAttributes attr= new BasicAttributes (); 84 String [] attrs = {"ou", "userPassword", "uid", "mail"}; 85 String dN = null; 86 87 username = JetspeedSecurity.convertUserName(username); 88 password = JetspeedSecurity.convertPassword(password); 89 90 try 91 { 92 user = JetspeedUserManagement.getUser(new UserNamePrincipal(username)); 93 } 94 catch (UnknownUserException e) 95 { 96 throw new FailedLoginException(e.toString()); 97 } 98 catch (JetspeedSecurityException e) 99 { 100 throw new LoginException(e.toString()); 101 } 102 103 if(!LDAPUserManagement.passwordsMatch(user, password)) 104 { 105 throw new FailedLoginException("Credential authentication failure"); 106 } 107 108 user.setHasLoggedIn(new Boolean (true)); 110 111 user.setTemp( "sessionPassword", password ); 114 115 try 117 { 118 user.updateLastLogin(); 119 putUserIntoContext(user); 120 121 if (cachingEnable) 122 { 123 JetspeedSecurityCache.load(username); 124 } 125 } 126 catch (Exception e) 127 { 128 putUserIntoContext(JetspeedSecurity.getAnonymousUser()); 129 throw new LoginException("Failed to update last login ", e); 130 } 131 132 return user; 133 134 } 135 141 public JetspeedUser getAnonymousUser() 142 throws LoginException 143 { 144 JetspeedUser user = null; 145 146 try 147 { 148 user = JetspeedUserManagement.getUser(new UserNamePrincipal(anonymousUser)); 149 user.setHasLoggedIn(new Boolean (false)); 150 putUserIntoContext(user); 151 152 if (cachingEnable) 153 { 154 JetspeedSecurityCache.load(user.getUserName()); 155 } 156 157 } 158 catch (JetspeedSecurityException e) 159 { 160 throw new LoginException("Failed to get anonymous user: " + e); 161 } 162 return user; 163 } 164 165 174 public void logout() 175 throws LoginException 176 { 177 try 178 { 179 getAnonymousUser(); 184 } 185 catch (Exception e) 186 { 187 throw new LoginException("Exception logging user out ", e ); 188 } 189 } 190 191 198 public synchronized void init(ServletConfig conf) 199 throws InitializationException 200 { 201 if (getInit()) return; 202 super.init(conf); 203 204 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) 206 .getResources(JetspeedSecurityService.SERVICE_NAME); 207 208 anonymousUser = serviceConf.getString(CONFIG_ANONYMOUS_USER, anonymousUser); 209 cachingEnable = serviceConf.getBoolean(CACHING_ENABLE, cachingEnable); 210 211 this.runDataService = 212 (JetspeedRunDataService)TurbineServices.getInstance() 213 .getService(RunDataService.SERVICE_NAME); 214 215 216 setInit(true); 217 } 218 219 221 protected JetspeedRunData getRunData() 222 { 223 JetspeedRunData rundata = null; 224 if (this.runDataService != null) 225 { 226 rundata = this.runDataService.getCurrentRunData(); 227 } 228 return rundata; 229 } 230 231 protected JetspeedUser getUserFromContext() 232 { 233 JetspeedRunData rundata = getRunData(); 234 JetspeedUser user = null; 235 if (rundata != null) 236 { 237 user = (JetspeedUser)rundata.getUser(); 238 } 239 return user; 240 } 241 242 protected JetspeedRunData putUserIntoContext(JetspeedUser user) 243 { 244 JetspeedRunData rundata = getRunData(); 245 if (rundata != null) 246 { 247 rundata.setUser(user); 248 rundata.save(); 249 } 250 return rundata; 251 } 252 253 } 254 | Popular Tags |