1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.security.pwd;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.UserPasswordException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.model.PasswordPolicy;
24  import com.liferay.portal.util.PropsUtil;
25  import com.liferay.util.PwdGenerator;
26  
27  /**
28   * <a href="RegExpToolkit.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class RegExpToolkit extends BasicToolkit {
33  
34      public RegExpToolkit() {
35          _pattern = PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_PATTERN);
36          _charset = PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_CHARSET);
37          _length = GetterUtil.getInteger(
38              PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_LENGTH));
39      }
40  
41      public String generate() {
42          return PwdGenerator.getPassword(_charset, _length);
43      }
44  
45      public void validate(
46              long userId, String password1, String password2,
47              PasswordPolicy passwordPolicy)
48          throws PortalException {
49  
50          boolean value = password1.matches(_pattern);
51  
52          if (!value) {
53              if (_log.isWarnEnabled()) {
54                  _log.warn("User " + userId + " attempted an invalid password");
55              }
56  
57              throw new UserPasswordException(
58                  UserPasswordException.PASSWORD_INVALID);
59          }
60      }
61  
62      private static Log _log = LogFactoryUtil.getLog(RegExpToolkit.class);
63  
64      private String _pattern;
65      private String _charset;
66      private int _length;
67  
68  }