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.enc;
017
018import org.kuali.common.util.Assert;
019import org.kuali.common.util.nullify.NullUtils;
020
021import com.google.common.base.Optional;
022
023/**
024 * @deprecated Use EncContext instead
025 */
026@Deprecated
027public final class EncryptionContext {
028
029        public static final EncryptionContext DEFAULT = new EncryptionContext.Builder().build();
030
031        private final boolean enabled;
032        private final boolean passwordRequired;
033        private final boolean removePasswordSystemProperty;
034        private final Optional<String> password;
035        private final Optional<String> passwordKey;
036        private final EncStrength strength;
037
038        public static class Builder {
039
040                private boolean passwordRequired = false;
041                private boolean removePasswordSystemProperty = true;
042                private Optional<String> password = Optional.absent();
043                private Optional<String> passwordKey = Optional.absent();
044                private EncStrength strength = EncStrength.BASIC;
045
046                // For convenience only. enabled == password.isPresent()
047                private boolean enabled = false;
048
049                public Builder removePasswordSystemProperty(boolean removePasswordSystemProperty) {
050                        this.removePasswordSystemProperty = removePasswordSystemProperty;
051                        return this;
052                }
053
054                public Builder passwordRequired(boolean passwordRequired) {
055                        this.passwordRequired = passwordRequired;
056                        return this;
057                }
058
059                public Builder password(String password) {
060                        this.password = NullUtils.toAbsent(password);
061                        return this;
062                }
063
064                public Builder passwordKey(String passwordKey) {
065                        this.passwordKey = NullUtils.toAbsent(passwordKey);
066                        return this;
067                }
068
069                public Builder strength(EncStrength strength) {
070                        this.strength = strength;
071                        return this;
072                }
073
074                public EncryptionContext build() {
075                        Assert.noNulls(password, passwordKey, strength, enabled);
076                        this.enabled = password.isPresent();
077                        if (passwordRequired) {
078                                Assert.isTrue(password.isPresent(), "Encryption password is required");
079                        }
080                        if (password.isPresent()) {
081                                Assert.noBlanks(password.get());
082                        }
083                        return new EncryptionContext(this);
084                }
085
086        }
087
088        private EncryptionContext(Builder builder) {
089                this.enabled = builder.enabled;
090                this.passwordKey = builder.passwordKey;
091                this.passwordRequired = builder.passwordRequired;
092                this.strength = builder.strength;
093                this.removePasswordSystemProperty = builder.removePasswordSystemProperty;
094                this.password = builder.password;
095        }
096
097        public boolean isEnabled() {
098                return enabled;
099        }
100
101        public Optional<String> getPassword() {
102                return password;
103        }
104
105        public EncStrength getStrength() {
106                return strength;
107        }
108
109        public boolean isPasswordRequired() {
110                return passwordRequired;
111        }
112
113        public Optional<String> getPasswordKey() {
114                return passwordKey;
115        }
116
117        public boolean isRemovePasswordSystemProperty() {
118                return removePasswordSystemProperty;
119        }
120
121}