001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.pwd;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.UserPasswordException;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.model.PasswordPolicy;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.service.PasswordTrackerLocalServiceUtil;
030: import com.liferay.portal.service.UserLocalServiceUtil;
031: import com.liferay.portal.util.PropsUtil;
032: import com.liferay.portlet.words.util.WordsUtil;
033: import com.liferay.util.PwdGenerator;
034:
035: import java.util.Date;
036:
037: /**
038: * <a href="PasswordPolicyToolkit.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Scott Lee
041: *
042: */
043: public class PasswordPolicyToolkit extends BasicToolkit {
044:
045: public String generate() {
046: String generator = GetterUtil
047: .getString(PropsUtil
048: .get(PropsUtil.PASSWORDS_PASSWORDPOLICYTOOLKIT_GENERATOR));
049:
050: if (generator.equals("static")) {
051: String password = GetterUtil
052: .getString(PropsUtil
053: .get(PropsUtil.PASSWORDS_PASSWORDPOLICYTOOLKIT_STATIC));
054:
055: return password;
056: } else {
057: return PwdGenerator.getPassword();
058: }
059: }
060:
061: public void validate(long userId, String password1,
062: String password2, PasswordPolicy passwordPolicy)
063: throws PortalException, SystemException {
064:
065: if (passwordPolicy.getCheckSyntax()) {
066: if (!passwordPolicy.getAllowDictionaryWords()
067: && WordsUtil.isDictionaryWord(password1)) {
068:
069: throw new UserPasswordException(
070: UserPasswordException.PASSWORD_CONTAINS_TRIVIAL_WORDS);
071: }
072:
073: if (password1.length() < passwordPolicy.getMinLength()) {
074: throw new UserPasswordException(
075: UserPasswordException.PASSWORD_LENGTH);
076: }
077: }
078:
079: if (!passwordPolicy.getChangeable()) {
080: throw new UserPasswordException(
081: UserPasswordException.PASSWORD_NOT_CHANGEABLE);
082: }
083:
084: if (userId != 0) {
085: if (passwordPolicy.getChangeable()) {
086: User user = UserLocalServiceUtil.getUserById(userId);
087:
088: Date passwordModfiedDate = user
089: .getPasswordModifiedDate();
090:
091: if (passwordModfiedDate != null) {
092:
093: // LEP-2961
094:
095: Date now = new Date();
096:
097: long passwordModificationElapsedTime = now
098: .getTime()
099: - passwordModfiedDate.getTime();
100:
101: long userCreationElapsedTime = now.getTime()
102: - user.getCreateDate().getTime();
103:
104: long minAge = passwordPolicy.getMinAge() * 1000;
105:
106: if ((passwordModificationElapsedTime < minAge)
107: && (userCreationElapsedTime > minAge)) {
108:
109: throw new UserPasswordException(
110: UserPasswordException.PASSWORD_TOO_YOUNG);
111: }
112: }
113: }
114:
115: if (PasswordTrackerLocalServiceUtil
116: .isSameAsCurrentPassword(userId, password1)) {
117:
118: throw new UserPasswordException(
119: UserPasswordException.PASSWORD_SAME_AS_CURRENT);
120: } else if (!PasswordTrackerLocalServiceUtil
121: .isValidPassword(userId, password1)) {
122:
123: throw new UserPasswordException(
124: UserPasswordException.PASSWORD_ALREADY_USED);
125: }
126: }
127: }
128:
129: }
|