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