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            @Override
070            protected String doEncrypt(
071                            String algorithm, String plainTextPassword,
072                            String encryptedPassword)
073                    throws PwdEncryptorException {
074    
075                    if (Validator.isNull(algorithm)) {
076                            throw new IllegalArgumentException("Invalid algorithm");
077                    }
078    
079                    PasswordEncryptor passwordEncryptor = null;
080    
081                    if (algorithm.startsWith(PasswordEncryptorUtil.TYPE_BCRYPT)) {
082                            passwordEncryptor = _passwordEncryptors.get(
083                                    PasswordEncryptorUtil.TYPE_BCRYPT);
084                    }
085                    else if (algorithm.startsWith(PasswordEncryptorUtil.TYPE_PBKDF2)) {
086                            passwordEncryptor = _passwordEncryptors.get(
087                                    PasswordEncryptorUtil.TYPE_PBKDF2);
088                    }
089                    else {
090                            passwordEncryptor = _passwordEncryptors.get(algorithm);
091                    }
092    
093                    if (passwordEncryptor == null) {
094                            if (_log.isDebugEnabled()) {
095                                    _log.debug("No password encryptor found for " + algorithm);
096                            }
097    
098                            passwordEncryptor = _defaultPasswordEncryptor;
099                    }
100    
101                    if (_log.isDebugEnabled()) {
102                            _log.debug(
103                                    "Found " + ClassUtil.getClassName(passwordEncryptor) +
104                                            " to encrypt password using " + algorithm);
105                    }
106    
107                    return passwordEncryptor.encrypt(
108                            algorithm, plainTextPassword, encryptedPassword);
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(
112                    CompositePasswordEncryptor.class);
113    
114            private PasswordEncryptor _defaultPasswordEncryptor;
115            private Map<String, PasswordEncryptor> _passwordEncryptors =
116                    new HashMap<String, PasswordEncryptor>();
117    
118    }