001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.property.processor;
017
018import java.util.Properties;
019
020import org.jasypt.util.text.TextEncryptor;
021import org.kuali.common.util.PropertyUtils;
022import org.kuali.common.util.enc.EncStrength;
023import org.kuali.common.util.enc.EncUtils;
024
025/**
026 * @deprecated Use ContextDecryptingProcessor instead
027 */
028@Deprecated
029public final class DecryptingProcessor implements PropertyProcessor {
030
031        public static final String DEFAULT_DECRYPT_KEY = "properties.decrypt";
032        public static final String DEFAULT_PASSWORD_KEY = "properties.enc.password";
033        public static final String DEFAULT_STRENGTH_KEY = "properties.enc.strength";
034
035        public DecryptingProcessor() {
036                this(DEFAULT_DECRYPT_KEY, DEFAULT_PASSWORD_KEY, DEFAULT_STRENGTH_KEY);
037        }
038
039        public DecryptingProcessor(String passwordKey) {
040                this(DEFAULT_DECRYPT_KEY, passwordKey, DEFAULT_STRENGTH_KEY);
041        }
042
043        public DecryptingProcessor(String decryptKey, String passwordKey, String strengthKey) {
044                this.decryptKey = decryptKey;
045                this.passwordKey = passwordKey;
046                this.strengthKey = strengthKey;
047        }
048
049        private final String decryptKey;
050        private final String passwordKey;
051        private final String strengthKey;
052
053        @Override
054        public void process(Properties properties) {
055                boolean decrypt = PropertyUtils.getBoolean(decryptKey, properties, false);
056                if (decrypt) {
057                        TextEncryptor encryptor = getTextEncryptor(properties);
058                        PropertyUtils.decrypt(properties, encryptor);
059                }
060        }
061
062        protected TextEncryptor getTextEncryptor(Properties properties) {
063                // If they asked to decrypt, a password is required
064                String password = PropertyUtils.getRequiredResolvedProperty(properties, passwordKey);
065
066                // Strength is optional (defaults to BASIC)
067                String strengthString = PropertyUtils.getRequiredResolvedProperty(properties, strengthKey, EncStrength.DEFAULT_VALUE.name());
068                EncStrength strength = EncStrength.valueOf(strengthString.toUpperCase());
069
070                // Setup a TextEncryptor with the appropriate password and strength
071                return EncUtils.getTextEncryptor(password, strength);
072        }
073
074        public String getDecryptKey() {
075                return decryptKey;
076        }
077
078        public String getPasswordKey() {
079                return passwordKey;
080        }
081
082        public String getStrengthKey() {
083                return strengthKey;
084        }
085
086}