1 19 20 package com.sslexplorer.properties.impl.userattributes; 21 22 import java.io.IOException ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.jdom.JDOMException; 27 28 import com.sslexplorer.boot.AbstractPropertyKey; 29 import com.sslexplorer.boot.ContextHolder; 30 import com.sslexplorer.boot.PropertyClass; 31 import com.sslexplorer.boot.PropertyDefinition; 32 import com.sslexplorer.core.CoreServlet; 33 import com.sslexplorer.properties.ProfilesFactory; 34 import com.sslexplorer.properties.attributes.AbstractXMLDefinedAttributesPropertyClass; 35 import com.sslexplorer.properties.attributes.AttributeDefinition; 36 import com.sslexplorer.properties.attributes.DefaultAttributeDefinition; 37 import com.sslexplorer.security.PublicKeyStore; 38 39 44 public class UserAttributes extends AbstractXMLDefinedAttributesPropertyClass { 45 46 final static Log log = LogFactory.getLog(UserAttributes.class); 47 48 51 public final static String NAME = "userAttributes"; 52 53 59 public UserAttributes() throws IOException , JDOMException { 60 super(NAME, false, "properties", true); 61 } 62 63 68 public String retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException { 69 AttributeDefinition def = (AttributeDefinition)getDefinition(key.getName()); 70 UserAttributeKey userAttrKey = (UserAttributeKey) key; 71 try { 72 String val = ProfilesFactory.getInstance().retrieveAttributeValue(userAttrKey); 73 if (val != null) { 74 if (def.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 75 81 if (PublicKeyStore.getInstance().hasLoadedKey(userAttrKey.getUser().getPrincipalName())) { 82 try { 83 val = PublicKeyStore.getInstance().decryptText(val, userAttrKey.getUser().getPrincipalName()); 84 } catch (Throwable t) { 85 log.warn("Failed to decrypt confidential user attribute, probably corrupt. Returning default value", t); 86 return null; 87 } 88 } 89 } 90 } 91 92 if (def.getType() == PropertyDefinition.TYPE_PASSWORD && val != null) { 94 try { 95 val = ContextHolder.getContext().deobfuscatePassword(val); 96 } catch (Throwable t) { 97 log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.", t); 98 } 99 } 100 101 return val == null ? def.getDefaultValue() : val; 103 } catch (Exception e) { 104 log.error("Failed to retrieve property.", e); 105 } 106 return null; 107 } 108 109 public String storePropertyImpl(AbstractPropertyKey key, String value) throws IllegalArgumentException { 110 AttributeDefinition def = (AttributeDefinition)getDefinition(key.getName()); 111 UserAttributeKey userAttrKey = (UserAttributeKey) key; 112 113 116 String oldValue = retrieveProperty(key); 117 118 119 120 if (def.getDefaultValue().equals(value)) { 121 value = null; 122 } 123 124 125 if (def.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE || oldValue == null || !oldValue.equals(value) ) { 126 127 129 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) { 130 try { 131 value = ContextHolder.getContext().obfuscatePassword(value); 132 } catch (Throwable t) { 133 if(log.isDebugEnabled()) 134 log.warn("Password property " + def.getName() + " could not be encoded.", t); 135 } 136 } 137 138 if (value != null) { 139 if (def.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 140 146 if (PublicKeyStore.getInstance().hasLoadedKey(userAttrKey.getUser().getPrincipalName())) { 147 try { 148 value = PublicKeyStore.getInstance().encryptText(value, userAttrKey.getUser().getPrincipalName()); 149 } catch (Throwable t) { 150 throw new IllegalArgumentException ("Failed to decrypt confidential user attributre, probably corrup.", t); 151 } 152 } 153 } 154 } 155 156 try { 158 ProfilesFactory.getInstance().storeAttributeValue(userAttrKey, value); 159 } catch (Exception e) { 160 log.error("Failed to update user attributes.", e); 161 } 162 } 163 return oldValue; 164 } 165 166 public AttributeDefinition createAttributeDefinition(int type, String name, 167 String typeMeta, int category, String categoryLabel, 168 String defaultValue, int visibility, int sortOrder, 169 String messageResourcesKey, boolean hidden, String label, 170 String description, boolean system, boolean replaceable, 171 String validationString) { 172 AttributeDefinition def = new DefaultAttributeDefinition(type, name, typeMeta, category, 173 categoryLabel, defaultValue, visibility, sortOrder, 174 messageResourcesKey, hidden, label, description, system, 175 replaceable, validationString); 176 def.init(this); 177 return def; 178 } 179 } 180 | Popular Tags |