1 13 14 package org.ejbca.core.ejb.ca.auth; 15 16 import java.util.Date ; 17 18 import javax.ejb.CreateException ; 19 import javax.ejb.EJBException ; 20 import javax.ejb.ObjectNotFoundException ; 21 22 import org.ejbca.core.ejb.BaseSessionBean; 23 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocal; 24 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocalHome; 25 import org.ejbca.core.ejb.log.ILogSessionLocal; 26 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 27 import org.ejbca.core.ejb.ra.UserDataLocal; 28 import org.ejbca.core.ejb.ra.UserDataLocalHome; 29 import org.ejbca.core.ejb.ra.UserDataPK; 30 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocal; 31 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocalHome; 32 import org.ejbca.core.model.InternalResources; 33 import org.ejbca.core.model.ca.AuthLoginException; 34 import org.ejbca.core.model.ca.AuthStatusException; 35 import org.ejbca.core.model.log.Admin; 36 import org.ejbca.core.model.log.LogEntry; 37 import org.ejbca.core.model.ra.UserDataConstants; 38 import org.ejbca.core.model.ra.UserDataVO; 39 40 41 42 43 44 111 public class LocalAuthenticationSessionBean extends BaseSessionBean { 112 113 private UserDataLocalHome userHome = null; 114 115 116 private ILogSessionLocal logsession; 117 118 119 private IKeyRecoverySessionLocal keyrecoverysession = null; 120 121 122 private static final InternalResources intres = InternalResources.getInstance(); 123 124 125 private boolean usekeyrecovery = true; 126 127 128 134 public void ejbCreate() throws CreateException { 135 debug(">ejbCreate()"); 136 137 userHome = (UserDataLocalHome)getLocator().getLocalHome(UserDataLocalHome.COMP_NAME); 139 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 140 logsession = logsessionhome.create(); 141 142 debug("<ejbCreate()"); 143 } 144 145 152 private IKeyRecoverySessionLocal getKeyRecoverySession(Admin admin){ 153 if(keyrecoverysession == null){ 154 try{ 155 IRaAdminSessionLocalHome raadminhome = (IRaAdminSessionLocalHome) getLocator().getLocalHome(IRaAdminSessionLocalHome.COMP_NAME); 156 IRaAdminSessionLocal raadmin = raadminhome.create(); 157 usekeyrecovery = (raadmin.loadGlobalConfiguration(admin)).getEnableKeyRecovery(); 158 if(usekeyrecovery){ 159 IKeyRecoverySessionLocalHome keyrecoveryhome = (IKeyRecoverySessionLocalHome) getLocator().getLocalHome(IKeyRecoverySessionLocalHome.COMP_NAME); 160 keyrecoverysession = keyrecoveryhome.create(); 161 } 162 }catch(Exception e){ 163 error("Error in getKeyRecoverySession: ", e); 164 throw new EJBException (e); 165 } 166 } 167 168 return keyrecoverysession; 169 } 170 171 184 public UserDataVO authenticateUser(Admin admin, String username, String password) 185 throws ObjectNotFoundException , AuthStatusException, AuthLoginException { 186 debug(">authenticateUser(" + username + ", hiddenpwd)"); 187 188 try { 189 UserDataPK pk = new UserDataPK(username); 191 UserDataLocal data = userHome.findByPrimaryKey(pk); 192 int status = data.getStatus(); 193 if ( (status == UserDataConstants.STATUS_NEW) || (status == UserDataConstants.STATUS_FAILED) || (status == UserDataConstants.STATUS_INPROCESS) || (status == UserDataConstants.STATUS_KEYRECOVERY)) { 194 debug("Trying to authenticate user: username="+data.getUsername()+", dn="+data.getSubjectDN()+", email="+data.getSubjectEmail()+", status="+data.getStatus()+", type="+data.getType()); 195 if (data.comparePassword(password) == false) 196 { 197 String msg = intres.getLocalizedMessage("authentication.invalidpwd", username); 198 logsession.log(admin, data.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_ERROR_USERAUTHENTICATION,msg); 199 throw new AuthLoginException(msg); 200 } 201 202 String msg = intres.getLocalizedMessage("authentication.authok", username); 203 logsession.log(admin, data.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_INFO_USERAUTHENTICATION,msg); 204 UserDataVO ret = new UserDataVO(data.getUsername(), data.getSubjectDN(), data.getCaId(), data.getSubjectAltName(), data.getSubjectEmail(), 205 data.getStatus(), data.getType(), data.getEndEntityProfileId(), data.getCertificateProfileId(), 206 new Date (data.getTimeCreated()), new Date (data.getTimeModified()), data.getTokenType(), data.getHardTokenIssuerId(), data.getExtendedInformation()); 207 ret.setPassword(data.getClearPassword()); 208 debug("<authenticateUser("+username+", hiddenpwd)"); 209 return ret; 210 } 211 String msg = intres.getLocalizedMessage("authentication.wrongstatus", new Integer (status), username); 212 logsession.log(admin, data.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_ERROR_USERAUTHENTICATION,msg); 213 throw new AuthStatusException("User "+username+" has status '"+status+"', NEW, FAILED or INPROCESS required."); 214 } catch (ObjectNotFoundException oe) { 215 String msg = intres.getLocalizedMessage("authentication.usernotfound", username); 216 logsession.log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_ERROR_USERAUTHENTICATION,msg); 217 throw oe; 218 } catch (AuthStatusException se) { 219 throw se; 220 } catch (AuthLoginException le) { 221 throw le; 222 } catch (Exception e) { 223 String msg = intres.getLocalizedMessage("error.unknown"); 224 error(msg, e); 225 throw new EJBException (e); 226 } 227 } 229 240 public void finishUser(Admin admin, String username, String password) 241 throws ObjectNotFoundException { 242 debug(">finishUser(" + username + ", hiddenpwd)"); 243 244 try { 245 UserDataPK pk = new UserDataPK(username); 247 UserDataLocal data = userHome.findByPrimaryKey(pk); 248 data.setStatus(UserDataConstants.STATUS_GENERATED); 249 data.setTimeModified((new Date ()).getTime()); 250 if(this.getKeyRecoverySession(admin) != null){ 252 getKeyRecoverySession(admin).unmarkUser(admin,username); 253 } 254 String msg = intres.getLocalizedMessage("authentication.statuschanged", username); 255 logsession.log(admin, data.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_INFO_CHANGEDENDENTITY,msg); 256 debug("<finishUser("+username+", hiddenpwd)"); 257 } catch (ObjectNotFoundException oe) { 258 String msg = intres.getLocalizedMessage("authentication.usernotfound", username); 259 logsession.log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_ERROR_USERAUTHENTICATION,msg); 260 throw oe; 261 } catch (Exception e) { 262 String msg = intres.getLocalizedMessage("error.unknown"); 263 error(msg, e); 264 throw new EJBException (e.toString()); 265 } 266 } } 268 | Popular Tags |