1 13 14 package org.ejbca.core.ejb.ra; 15 16 import java.io.UnsupportedEncodingException ; 17 import java.security.MessageDigest ; 18 import java.security.NoSuchAlgorithmException ; 19 import java.util.Date ; 20 import java.util.HashMap ; 21 22 import javax.ejb.CreateException ; 23 import javax.ejb.EJBException ; 24 25 import org.apache.log4j.Logger; 26 import org.bouncycastle.util.encoders.Hex; 27 import org.ejbca.core.ejb.BaseEntityBean; 28 import org.ejbca.core.model.SecConst; 29 import org.ejbca.core.model.ra.ExtendedInformation; 30 import org.ejbca.core.model.ra.UserDataConstants; 31 import org.ejbca.core.model.ra.UserDataVO; 32 import org.ejbca.util.Base64PutHashMap; 33 import org.ejbca.util.CertTools; 34 import org.ejbca.util.StringTools; 35 36 118 public abstract class UserDataBean extends BaseEntityBean { 119 120 private static final Logger log = Logger.getLogger(UserDataBean.class); 121 122 123 128 public abstract String getUsername(); 129 130 134 public abstract void setUsername(String username); 135 136 140 public abstract String getSubjectDN(); 141 142 145 public abstract void setSubjectDN(String subjectDN); 146 147 151 public abstract int getCaId(); 152 153 156 public abstract void setCaId(int caid); 157 158 162 public abstract String getSubjectAltName(); 163 164 167 public abstract void setSubjectAltName(String subjectAltName); 168 169 173 public abstract String getSubjectEmail(); 174 175 178 public abstract void setSubjectEmail(String subjectEmail); 179 180 184 public abstract int getStatus(); 185 186 189 public abstract void setStatus(int status); 190 191 195 public abstract int getType(); 196 197 200 public abstract void setType(int type); 201 202 208 public abstract String getClearPassword(); 209 210 215 public abstract void setClearPassword(String clearPassword); 216 217 223 public abstract String getPasswordHash(); 224 225 230 public abstract void setPasswordHash(String passwordHash); 231 232 238 public abstract long getTimeCreated(); 239 240 244 public abstract void setTimeCreated(long createtime); 245 246 252 public abstract long getTimeModified(); 253 254 259 public abstract void setTimeModified(long createtime); 260 261 267 public abstract int getEndEntityProfileId(); 268 269 274 public abstract void setEndEntityProfileId(int endentityprofileid); 275 276 282 public abstract int getCertificateProfileId(); 283 284 289 public abstract void setCertificateProfileId(int certificateprofileid); 290 291 297 public abstract int getTokenType(); 298 299 304 public abstract void setTokenType(int tokentype); 305 306 312 public abstract int getHardTokenIssuerId(); 313 314 319 public abstract void setHardTokenIssuerId(int hardtokenissuerid); 320 321 326 public abstract String getExtendedInformationData(); 327 328 332 public abstract void setExtendedInformationData(String data); 333 334 335 339 public abstract String getKeyStorePassword(); 340 341 343 public abstract void setKeyStorePassword(String keystorepassword); 344 345 346 350 354 public void setDN(String dn) { 355 setSubjectDN(CertTools.stringToBCDNString(dn)); 356 } 357 358 362 public void setPassword(String password) throws NoSuchAlgorithmException { 363 String passwordHash = makePasswordHash(password); 364 setPasswordHash(passwordHash); 365 setClearPassword(null); 366 } 367 368 373 public void setOpenPassword(String password) throws NoSuchAlgorithmException { 374 375 String passwordHash = makePasswordHash(password); 376 setPasswordHash(passwordHash); 377 setClearPassword(password); 378 } 379 380 384 public boolean comparePassword(String password) throws NoSuchAlgorithmException { 385 log.debug(">comparePassword()"); 386 if (password == null) 387 return false; 388 389 log.debug("<comparePassword()"); 390 return (makePasswordHash(password).equals(getPasswordHash())); 392 } 393 394 395 399 400 401 404 405 private String makePasswordHash(String password) throws NoSuchAlgorithmException { 406 log.debug(">makePasswordHash()"); 407 408 if (password == null) 409 return null; 410 411 String ret = null; 412 try { 413 MessageDigest md = MessageDigest.getInstance("SHA1"); 414 byte[] pwdhash = md.digest(password.trim().getBytes()); 415 ret = new String (Hex.encode(pwdhash)); 416 } catch (NoSuchAlgorithmException nsae) { 417 log.error("SHA1 algorithm not supported.", nsae); 418 throw nsae; 419 } 420 421 log.debug("<makePasswordHash()"); 422 return ret; 423 } 424 425 426 430 public ExtendedInformation getExtendedInformation() { 431 return UserDataVO.getExtendedInformation(getExtendedInformationData()); 432 } 433 434 438 public void setExtendedInformation(ExtendedInformation extendedinformation) { 439 if(extendedinformation != null){ 440 HashMap a = new Base64PutHashMap(); 442 a.putAll((HashMap )extendedinformation.saveData()); 443 444 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 445 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 446 encoder.writeObject(a); 447 encoder.close(); 448 try { 449 setExtendedInformationData(baos.toString("UTF8")); 450 } catch (UnsupportedEncodingException e) { 451 throw new EJBException ("Problems storing extended information for user :" + getUsername(),e); 452 } 453 454 } 455 } 456 457 461 473 public UserDataPK ejbCreate(String username, String password, String dn, int caid) 474 throws CreateException , NoSuchAlgorithmException { 475 476 long time = (new Date ()).getTime(); 477 478 setUsername(StringTools.strip(username)); 479 setClearPassword(null); 480 setPasswordHash(makePasswordHash(password)); 481 setSubjectDN(CertTools.stringToBCDNString(dn)); 482 setCaId(caid); 483 setSubjectAltName(null); 484 setSubjectEmail(null); 485 setStatus(UserDataConstants.STATUS_NEW); 486 setType(SecConst.USER_INVALID); 487 setTimeCreated(time); 488 setTimeModified(time); 489 setEndEntityProfileId(0); 490 setCertificateProfileId(0); 491 setTokenType(SecConst.TOKEN_SOFT_BROWSERGEN); 492 setHardTokenIssuerId(0); 493 setExtendedInformationData(null); 494 UserDataPK pk = new UserDataPK(username); 495 log.debug("Created user " + username); 496 497 return pk; 498 } 499 500 public void ejbPostCreate(String username, String password, String dn, int caid) { 501 } 503 504 } 505 | Popular Tags |