1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.pwd;
16  
17  import com.liferay.portal.UserPasswordException;
18  import com.liferay.portal.kernel.exception.PortalException;
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  }