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