1 64 65 69 package com.jcorporate.expresso.ext.ldap; 70 71 import com.jcorporate.expresso.core.db.DBConnection; 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.misc.ConfigManager; 74 import com.jcorporate.expresso.core.misc.ConfigurationException; 75 import com.jcorporate.expresso.core.misc.EMailSender; 76 import com.jcorporate.expresso.core.misc.StringUtil; 77 import com.jcorporate.expresso.core.misc.Base64; 78 import com.jcorporate.expresso.core.security.UserInfo; 79 import com.jcorporate.expresso.core.security.CryptoManager; 80 import com.jcorporate.expresso.services.dbobj.DefaultUserInfo; 81 import com.jcorporate.expresso.services.dbobj.Setup; 82 import org.apache.log4j.Logger; 83 84 import javax.naming.AuthenticationException ; 85 import javax.naming.Context ; 86 import javax.naming.NamingEnumeration ; 87 import javax.naming.NamingException ; 88 import javax.naming.directory.Attribute ; 89 import javax.naming.directory.Attributes ; 90 import javax.naming.directory.DirContext ; 91 import javax.naming.directory.InitialDirContext ; 92 import javax.naming.directory.SearchControls ; 93 import javax.naming.directory.SearchResult ; 94 import java.util.Date ; 95 import java.util.Enumeration ; 96 import java.util.Hashtable ; 97 import java.util.Vector ; 98 99 100 115 public class UserLDAP 116 implements UserInfo { 117 private String dbName = "default"; 118 119 120 private Hashtable env = null; 121 private DirContext ctx = null; 122 private String thisClass = UserLDAP.class.getName() + "."; 123 private String userName = null; 124 private String email = null; 125 private String lastName = null; 126 private String firstName = null; 127 private String descrip = null; 128 private String ldapRoot = null; 129 private String name = null; 130 131 private static Logger log = Logger.getLogger(UserLDAP.class); 132 133 136 public UserLDAP() { 137 super(); 138 } 139 140 143 public void add() 144 throws DBException { 145 String myName = thisClass + "add()"; 146 throw new DBException(myName + ":Add not yet implemented"); 147 } 148 149 150 153 public void delete() 154 throws DBException { 155 String myName = thisClass + "delete()"; 156 throw new java.lang.UnsupportedOperationException (myName + ":Delete not yet implemented"); 157 } 158 159 160 165 public boolean find() 166 throws DBException { 167 try { 168 retrieve(); 169 } catch (DBException de) { 170 return false; 171 } 172 173 return true; 174 } 175 176 177 public String getAccountStatus() 178 throws DBException { 179 return "A"; 180 } 181 182 public Vector getAllUsers() 183 throws DBException { 184 return null; 185 } 186 187 public String getCreateDate() 188 throws DBException { 189 return null; 190 } 191 192 197 public DirContext getContext() { 198 return ctx; 199 } 200 201 public String getEmail() 202 throws DBException { 203 return email; 204 } 205 206 222 public String getEmailAuthCode() 223 throws DBException { 224 Date createDate = new Date (); 225 long dateLong = createDate.getTime(); 226 long emailAuthCode = Math.round(dateLong * 1.71); 227 228 return Long.toString(emailAuthCode); 229 } 230 231 232 public String getEmailValCode() 233 throws DBException { 234 return getField("EmailValCode"); 235 } 236 237 243 public String getField(String fieldName) 244 throws DBException { 245 String myName = thisClass + "getField(String)"; 246 247 if (fieldName.equals("UserName")) { 248 return userName; 249 } else if (fieldName.equals("EMail")) { 250 return email; 251 } else if (fieldName.equals("Descrip")) { 252 return descrip; 253 } else if (fieldName.equals("FirstName")) { 254 return firstName; 255 } else if (fieldName.equals("LastName")) { 256 return lastName; 257 } else if (fieldName.equals("AccountStatus")) { 258 return "A"; 259 } else if (fieldName.equals("CreateDate")) { 260 return null; 261 } 262 263 throw new DBException(myName + ":No such field as '" + fieldName + 264 "'"); 265 } 266 267 268 272 public Enumeration getFieldList() 273 throws DBException { 274 Vector v = new Vector (); 275 v.addElement("UserName"); 276 v.addElement("EMail"); 277 v.addElement("Descrip"); 278 v.addElement("FirstName"); 279 v.addElement("LastName"); 280 v.addElement("AccountStatus"); 281 v.addElement("CreateDate"); 282 283 return v.elements(); 284 } 285 286 287 292 public java.util.Vector getGroups() 293 throws DBException { 294 SearchControls constraints = new SearchControls (); 295 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 296 297 Vector v = new Vector (); 298 299 try { 300 setDBName("default"); 301 302 NamingEnumeration results = ctx.search("o=" + ldapRoot, 303 "(uniquemember=" + name + 304 ", o=" + ldapRoot + ")", 305 constraints); 306 307 if (results == null) { 308 return v; 309 } 310 while (results.hasMore()) { 311 SearchResult sr = (SearchResult ) results.next(); 312 Attributes a = sr.getAttributes(); 313 Attribute attr = a.get("cn"); 314 315 if (attr != null) { 316 NamingEnumeration vals = attr.getAll(); 317 318 if (vals.hasMoreElements()) { 319 v.addElement(vals.nextElement()); 320 } 321 } 322 } 323 } catch (NamingException ne) { 324 throw new DBException(ne); 325 } 326 327 return v; 328 } 329 330 331 public String getLoginName() 332 throws DBException { 333 return userName; 334 } 335 336 public String getPassword() 337 throws DBException { 338 return null; 339 } 340 341 public boolean getRegComplete() 342 throws DBException { 343 344 return true; 346 } 347 348 public String getRegistrationDomain() 349 throws DBException { 350 return "default"; 351 } 352 353 public String getRoot() { 354 return ldapRoot; 355 } 356 357 public int getUid() 358 throws DBException { 359 return 0; 360 } 361 362 public String getUpdateDate() 363 throws DBException { 364 return null; 365 } 366 367 public String getUserName() 368 throws DBException { 369 return firstName + " " + lastName; 370 } 371 372 379 public void notify(String subject, String message) 380 throws DBException { 381 String myName = (thisClass + "notify(String, String)"); 382 log.info("Notifying user " + getField("UserName") + " of " + subject); 383 384 String sendToUser = getField("EMail"); 385 386 try { 387 EMailSender ems = new EMailSender(); 388 ems.setDBName("default"); 389 ems.send(sendToUser, subject, message); 390 } catch (Exception e) { 391 throw new DBException(myName + 392 ":Uncaught exception sending e-mail", e); 393 } 394 } 395 396 397 405 private boolean okNumber(double x) { 406 int oneNumber = new Double (x).intValue(); 407 408 if ((oneNumber >= 65) && (oneNumber <= 90)) { 409 return true; 410 } 411 if ((oneNumber >= 48) && (oneNumber <= 57)) { 412 return true; 413 } 414 if ((oneNumber >= 97) && (oneNumber <= 122)) { 415 return true; 416 } 417 418 return false; 419 } 420 421 427 public boolean passwordEquals(String tryPassword) 428 throws DBException { 429 String myName = thisClass + "getField(String)"; 430 Hashtable env2 = new Hashtable (); 431 env2.put(Context.INITIAL_CONTEXT_FACTORY, 432 "com.sun.jndi.ldap.LdapCtxFactory"); 433 434 String ldapServer = ""; 435 436 try { 437 ldapServer = StringUtil.notNull(ConfigManager.getContext(DBConnection.DEFAULT_DB_CONTEXT_NAME) 438 .getLdap().getServer()); 439 } catch (Exception e) { 440 throw new DBException(myName + 441 ":Must specify ldapServer property in properties file"); 442 } 443 444 if (ldapServer.equals("")) { 445 throw new DBException(myName + 446 ":Must specify ldapServer property in properties file"); 447 } 448 449 env2.put(Context.PROVIDER_URL, ldapServer); 450 env2.put(Context.SECURITY_AUTHENTICATION, "simple"); 451 452 if (userName.equals("")) { 453 throw new DBException(myName + 454 ":Must set UserName field before checking password"); 455 } 456 457 env2.put(Context.SECURITY_PRINCIPAL, name + ", o=" + ldapRoot); 458 459 if (StringUtil.notNull(tryPassword).equals("")) { 460 throw new DBException(myName + ":Must specify non-blank password"); 461 } 462 463 env2.put(Context.SECURITY_CREDENTIALS, tryPassword); 464 465 try { 466 new InitialDirContext (env2); 467 468 return true; 469 } catch (AuthenticationException e) { 470 log.error(e); 471 472 return false; 473 } catch (NamingException ne) { 474 throw new DBException(ne); 475 } 476 } 477 478 479 484 public String randomPassword() { 485 int passwordLength = 6; 486 StringBuffer newPassword = new StringBuffer (""); 487 double oneNumber = 0; 488 char oneChar; 489 int iterations = 0; 490 491 String propValue = ""; 495 try { 496 propValue = StringUtil.notNull(ConfigManager.getContext(DBConnection.DEFAULT_DB_CONTEXT_NAME) 497 .getMinPasswordSize()); 498 } catch (Exception e) { 499 passwordLength = 6; 500 } 501 502 503 if (!propValue.equals("")) { 504 try { 505 passwordLength = Integer.parseInt(propValue, 10); 506 } catch (NumberFormatException ex) { 507 508 passwordLength = 6; 510 } 511 } 512 513 while ((newPassword.length() < passwordLength) && (iterations < 200)) { 518 iterations++; 519 oneNumber = Math.random() * 100; 520 521 if (okNumber(oneNumber)) { 522 oneChar = (char) new Double (oneNumber).intValue(); 523 newPassword.append(oneChar); 524 } 525 } 526 527 return newPassword.toString(); 528 } 529 530 533 public void retrieve() 534 throws DBException { 535 String myName = thisClass + "retrieve()"; 536 SearchControls constraints = new SearchControls (); 537 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 538 539 if (StringUtil.notNull(getField("UserName")).equals("")) { 540 throw new DBException(myName + 541 ":UserName must be specified before retrieve"); 542 } 543 try { 544 setDBName("default"); 545 546 NamingEnumeration results = ctx.search("o=" + ldapRoot, 547 "(uid=" + userName + ")", 548 constraints); 549 550 if (results == null) { 551 throw new DBException(myName + 552 ":No results for search of LDAP server"); 553 } 554 if (results.hasMore()) { 555 SearchResult sr = (SearchResult ) results.next(); 556 name = sr.getName(); 557 558 Attributes a = sr.getAttributes(); 559 Attribute attr = a.get("mail"); 560 561 if (attr != null) { 562 NamingEnumeration vals = attr.getAll(); 563 564 if (vals.hasMoreElements()) { 565 email = (String ) vals.nextElement(); 566 } 567 } 568 569 attr = a.get("sn"); 570 571 if (attr != null) { 572 NamingEnumeration vals = attr.getAll(); 573 574 if (vals.hasMoreElements()) { 575 lastName = (String ) vals.nextElement(); 576 } 577 } 578 579 attr = a.get("fn"); 580 581 if (attr != null) { 582 NamingEnumeration vals = attr.getAll(); 583 584 if (vals.hasMoreElements()) { 585 firstName = (String ) vals.nextElement(); 586 } 587 } 588 589 attr = a.get("cn"); 590 591 if (attr != null) { 592 NamingEnumeration vals = attr.getAll(); 593 594 if (vals.hasMoreElements()) { 595 descrip = (String ) vals.nextElement(); 596 } 597 } 598 } else { 599 throw new DBException(myName + ":No such user '" + userName + 600 "' found in LDAP server directory"); 601 } 602 } catch (NamingException ne) { 603 throw new DBException(myName, ne); 604 } 605 } 606 607 608 613 public void sendAuthEmail() 614 throws DBException { 615 try { 616 String dbContext = "default"; 617 String authURL = Setup.getValue(dbContext, "EmailValidateURL"); 618 String emailAuthCode = getEmailAuthCode(); 619 this.setEmailValCode(emailAuthCode); 620 this.update(); 621 authURL = authURL + "?UserName=" + getField("UserName") + 622 "&EmailAuthCode=" + emailAuthCode; 623 624 String subject = "New Account Validation - Please Respond"; 625 StringBuffer sb = new StringBuffer (); 626 627 if (!"".equals(getField("Descrip"))) { 628 sb.append("Dear " + getField("Descrip") + ","); 629 } 630 631 sb.append("\n"); 632 sb.append("\n"); 633 sb.append("Thank you for registering"); 634 635 String companyName = Setup.getValue(dbContext, "CompanyName"); 636 String homePageURL = Setup.getValue(dbContext, "HomePageURL"); 637 638 if (companyName != null && !"".equals(companyName)) { 639 sb.append(" with " + companyName); 640 } 641 if (homePageURL != null && !"".equals(homePageURL)) { 642 sb.append(" at " + homePageURL); 643 } 644 645 sb.append("!"); 646 sb.append("\n"); 647 sb.append("\n"); 648 sb.append("Your account has been successfully created. The final " + 649 "step in the"); 650 sb.append("\n"); 651 sb.append("registration process is to simply follow the link " + 652 "below to let us"); 653 sb.append("\n"); 654 sb.append("know that you received this message. You must follow " + 655 "the link below"); 656 sb.append("\n"); 657 sb.append("before your account will be activated."); 658 sb.append("\n"); 659 sb.append("\n"); 660 sb.append("NOTE: If you did not register, you may safely"); 661 sb.append("\n"); 662 sb.append("ignore this message."); 663 sb.append("\n"); 664 sb.append("\n"); 665 sb.append("In many email clients, you may simply click on the " + 666 "link below to"); 667 sb.append("\n"); 668 sb.append("complete the registration process. If your email " + 669 "client does not"); 670 sb.append("\n"); 671 sb.append("support this, cut-and-paste the link below into your " + 672 "web browser's"); 673 sb.append("\n"); 674 sb.append("\"Location\" window:"); 675 sb.append("\n"); 676 sb.append("\n"); 677 sb.append(authURL); 678 sb.append("\n"); 679 sb.append("\n"); 680 681 if (companyName != null && !"".equals(companyName)) { 682 sb.append("Thank you from all of us at " + companyName + "."); 683 } 684 685 sb.append("\n"); 686 687 if (companyName != null && !"".equals(homePageURL)) { 688 sb.append(homePageURL); 689 } 690 691 sb.append("\n"); 692 693 String message = sb.toString(); 694 notify(subject, message); 695 } catch (Exception e) { 696 throw new DBException("Error in sending account verification message to " + 697 getField("UserName") + " at " + getField("EMail") + ": " + 698 e.toString()); 699 } 700 } 701 702 703 714 public void sendFollowUpEmail() 715 throws DBException { 716 try { 717 String subject = "New Registration Complete - Welcome!"; 718 String dbContext = "default"; 719 720 String password = this.randomPassword(); 723 this.setPassword(password); 724 this.update(); 725 726 StringBuffer sb = new StringBuffer (); 727 728 if (!"".equals(getField("Descrip"))) { 729 sb.append("Dear " + getField("Descrip") + ","); 730 } 731 732 sb.append("\n"); 733 sb.append("\n"); 734 sb.append("Thank you for registering"); 735 736 String companyName = Setup.getValue(dbContext, "CompanyName"); 737 String homePageURL = Setup.getValue(dbContext, "HomePageURL"); 738 739 if (companyName != null && !"".equals(companyName)) { 740 sb.append(" with " + companyName); 741 } 742 if (homePageURL != null && !"".equals(homePageURL)) { 743 sb.append(" at " + homePageURL); 744 } 745 746 sb.append("!"); 747 sb.append("\n"); 748 sb.append("\n"); 749 sb.append("Your account is now active. Below is the information " + 750 "you will need to log in."); 751 sb.append("\n"); 752 sb.append("Please keep this information in a safe place.We hope " + 753 "you enjoy the site and"); 754 sb.append("\n"); 755 sb.append("look forward to your participation."); 756 sb.append("\n"); 757 sb.append("\n"); 758 sb.append("User Name: " + getField("UserName")); 759 sb.append("\n"); 760 sb.append("Password: " + password); 761 sb.append("\n"); 762 sb.append("\n"); 763 764 if (companyName != null && !"".equals(companyName)) { 765 sb.append("Thank you from all of us at " + companyName + "."); 766 } 767 768 sb.append("\n"); 769 770 if (companyName != null && !"".equals(homePageURL)) { 771 sb.append(homePageURL); 772 } 773 774 sb.append("\n"); 775 776 String message = sb.toString(); 777 notify(subject, message); 778 } catch (Exception e) { 779 throw new DBException("Error in sending account verification follow up message to " + 780 getField("UserName") + " at " + getField("EMail") + ": " + 781 e.toString()); 782 } 783 } 784 785 786 public void setAccountStatus(String name) 787 throws DBException { 788 } 789 790 public void setCreateDate(String name) 791 throws DBException { 792 } 793 794 799 public void setDBName(String newDBName) 800 throws DBException { 801 String myName = thisClass + "setDBName(String)"; 802 dbName = newDBName; 803 env = new Hashtable (); 804 env.put(Context.INITIAL_CONTEXT_FACTORY, 805 "com.sun.jndi.ldap.LdapCtxFactory"); 806 807 try { 808 String ldapServer = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getServer()); 809 810 if (ldapServer.equals("")) { 811 throw new DBException(myName + 812 ":Must specify ldapServer property in properties file"); 813 } 814 815 env.put(Context.PROVIDER_URL, ldapServer); 816 env.put(Context.SECURITY_AUTHENTICATION, "simple"); 817 818 String ldapUser = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getUser()); 819 820 if (ldapUser.equals("")) { 821 throw new DBException(myName + 822 ":Must specify ldapUser property in properties file"); 823 } 824 825 env.put(Context.SECURITY_PRINCIPAL, "cn=" + ldapUser); 826 827 String ldapPassword = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getPassword()); 828 829 if (ldapPassword.equals("")) { 830 throw new DBException(myName + 831 ":Must specify ldapPassword property in properties file"); 832 } 833 834 env.put(Context.SECURITY_CREDENTIALS, ldapPassword); 835 ldapRoot = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getRoot()); 836 837 if (ldapRoot.equals("")) { 838 throw new DBException(myName + 839 ":Must specify ldapRoot property in properties file"); 840 } 841 try { 842 ctx = new InitialDirContext (env); 843 } catch (NamingException ne) { 844 throw new DBException(ne); 845 } 846 } catch (ConfigurationException ce) { 847 throw new DBException(ce); 848 } 849 } 850 851 852 public void setEmail(String name) 853 throws DBException { 854 } 855 856 public void setEmailAuthCode(String name) 857 throws DBException { 858 } 859 860 public void setEmailValCode(String code) 861 throws DBException { 862 } 863 864 public void setLoginName(String name) 865 throws DBException { 866 userName = name; 867 } 868 869 public void setPassword(String password) 870 throws DBException { 871 } 872 873 public void setRegComplete(boolean status) 874 throws DBException { 875 876 } 878 879 public void setRegistrationDomain(String domain) { 880 } 881 882 public void setUid(int uid) 883 throws DBException { 884 } 885 886 887 public void setUpdateDate(String name) 888 throws DBException { 889 } 890 891 public void setUserName(String name) 892 throws DBException { 893 } 894 895 898 public void update() 899 throws DBException { 900 String myName = thisClass + "update()"; 901 throw new DBException(myName + ":Update not yet implemented"); 902 } 903 904 905 916 public Vector getValues() 917 throws DBException { 918 throw new DBException("This object does not have valid values defined."); 919 } 920 921 927 public String getDBName() { 928 return this.dbName; 929 } 930 931 934 public String getDataContext() { 935 return this.dbName; 936 } 937 938 944 public String getPrimaryGroup() 945 throws DBException { 946 return DefaultUserInfo.getPrimaryGroup(this); 947 } 948 949 954 public String hashEncodePassword(String password) throws DBException { 955 if (password == null) { 956 throw new DBException("Password Must not be NULL"); 957 } 958 if (password.length() == 0) { 959 return password; 960 } 961 try { 962 return Base64.encode(CryptoManager.getInstance().getStringHash().produceHash(password.getBytes())); 963 } catch (Exception ex) { 964 throw new DBException("Error hashing Password:" + 965 " You may not have installed the" + 966 " Cryptography Extensions Properly:", ex); 967 } 968 } 969 970 } | Popular Tags |