001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.pwd;
016    
017    import com.liferay.portal.exception.UserPasswordException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.security.pwd.BasicToolkit;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.PwdGenerator;
025    import com.liferay.portal.model.PasswordPolicy;
026    import com.liferay.portal.util.PropsUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class RegExpToolkit extends BasicToolkit {
032    
033            public RegExpToolkit() {
034                    _pattern = PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_PATTERN);
035                    _charset = PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_CHARSET);
036                    _length = GetterUtil.getInteger(
037                            PropsUtil.get(PropsKeys.PASSWORDS_REGEXPTOOLKIT_LENGTH));
038            }
039    
040            @Override
041            public String generate(PasswordPolicy passwordPolicy) {
042                    return PwdGenerator.getPassword(_charset, _length);
043            }
044    
045            @Override
046            public void validate(
047                            long userId, String password1, String password2,
048                            PasswordPolicy passwordPolicy)
049                    throws PortalException {
050    
051                    boolean value = password1.matches(_pattern);
052    
053                    if (!value) {
054                            if (_log.isWarnEnabled()) {
055                                    _log.warn("User " + userId + " attempted an invalid password");
056                            }
057    
058                            throw new UserPasswordException.MustComplyWithRegex(
059                                    userId, _pattern);
060                    }
061            }
062    
063            private static final Log _log = LogFactoryUtil.getLog(RegExpToolkit.class);
064    
065            private final String _charset;
066            private final int _length;
067            private final String _pattern;
068    
069    }