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.PwdEncryptorException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
021    import com.liferay.portal.kernel.security.pwd.PasswordEncryptorUtil;
022    import com.liferay.portal.kernel.util.ClassUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.HashMap;
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * @author Michael C. Han
032     */
033    public class CompositePasswordEncryptor
034            extends BasePasswordEncryptor implements PasswordEncryptor {
035    
036            @Override
037            public String[] getSupportedAlgorithmTypes() {
038                    throw new UnsupportedOperationException();
039            }
040    
041            public void setDefaultPasswordEncryptor(
042                    PasswordEncryptor defaultPasswordEncryptor) {
043    
044                    _defaultPasswordEncryptor = defaultPasswordEncryptor;
045            }
046    
047            public void setPasswordEncryptors(
048                    List<PasswordEncryptor> passwordEncryptors) {
049    
050                    for (PasswordEncryptor passwordEncryptor : passwordEncryptors) {
051                            if (_log.isDebugEnabled()) {
052                                    _log.debug("Registering " + passwordEncryptor);
053                            }
054    
055                            String[] supportedAlgorithmTypes =
056                                    passwordEncryptor.getSupportedAlgorithmTypes();
057    
058                            if (_log.isDebugEnabled()) {
059                                    Class<?> clazz = passwordEncryptor.getClass();
060    
061                                    _log.debug(
062                                            "Registering " + StringUtil.merge(supportedAlgorithmTypes) +
063                                                    " for " + clazz.getName());
064                            }
065    
066                            for (String supportedAlgorithmType : supportedAlgorithmTypes) {
067                                    _passwordEncryptors.put(
068                                            supportedAlgorithmType, passwordEncryptor);
069                            }
070                    }
071            }
072    
073            @Override
074            protected String doEncrypt(
075                            String algorithm, String plainTextPassword,
076                            String encryptedPassword)
077                    throws PwdEncryptorException {
078    
079                    if (Validator.isNull(algorithm)) {
080                            throw new IllegalArgumentException("Invalid algorithm");
081                    }
082    
083                    PasswordEncryptor passwordEncryptor = null;
084    
085                    if (algorithm.startsWith(PasswordEncryptorUtil.TYPE_BCRYPT)) {
086                            passwordEncryptor = _passwordEncryptors.get(
087                                    PasswordEncryptorUtil.TYPE_BCRYPT);
088                    }
089                    else if (algorithm.startsWith(PasswordEncryptorUtil.TYPE_PBKDF2)) {
090                            passwordEncryptor = _passwordEncryptors.get(
091                                    PasswordEncryptorUtil.TYPE_PBKDF2);
092                    }
093                    else {
094                            passwordEncryptor = _passwordEncryptors.get(algorithm);
095                    }
096    
097                    if (passwordEncryptor == null) {
098                            if (_log.isDebugEnabled()) {
099                                    _log.debug("No password encryptor found for " + algorithm);
100                            }
101    
102                            passwordEncryptor = _defaultPasswordEncryptor;
103                    }
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug(
107                                    "Found " + ClassUtil.getClassName(passwordEncryptor) +
108                                            " to encrypt password using " + algorithm);
109                    }
110    
111                    return passwordEncryptor.encrypt(
112                            algorithm, plainTextPassword, encryptedPassword);
113            }
114    
115            private static final Log _log = LogFactoryUtil.getLog(
116                    CompositePasswordEncryptor.class);
117    
118            private PasswordEncryptor _defaultPasswordEncryptor;
119            private final Map<String, PasswordEncryptor> _passwordEncryptors =
120                    new HashMap<>();
121    
122    }