Search in sources :

Example 21 with PwmSecurityKey

use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.

the class PasswordValue method factory.

public static StoredValueFactory factory() {
    return new StoredValueFactory() {

        public PasswordValue fromJson(final String value) {
            final String strValue = JsonUtil.deserialize(value, String.class);
            if (strValue != null && !strValue.isEmpty()) {
                try {
                    return new PasswordValue(new PasswordData(strValue));
                } catch (PwmUnrecoverableException e) {
                    throw new IllegalStateException("PasswordValue can not be json de-serialized: " + e.getMessage());
                }
            }
            return new PasswordValue();
        }

        public PasswordValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) throws PwmOperationalException, PwmUnrecoverableException {
            final Element valueElement = settingElement.getChild("value");
            final String rawValue = valueElement.getText();
            final PasswordValue newPasswordValue = new PasswordValue();
            if (rawValue == null || rawValue.isEmpty()) {
                return newPasswordValue;
            }
            final boolean plainTextSetting;
            {
                final String plainTextAttributeStr = valueElement.getAttributeValue("plaintext");
                plainTextSetting = plainTextAttributeStr != null && Boolean.parseBoolean(plainTextAttributeStr);
            }
            if (plainTextSetting) {
                newPasswordValue.value = new PasswordData(rawValue);
                newPasswordValue.requiresStoredUpdate = true;
            } else {
                try {
                    newPasswordValue.value = new PasswordData(SecureEngine.decryptStringValue(rawValue, key, PwmBlockAlgorithm.CONFIG));
                    return newPasswordValue;
                } catch (Exception e) {
                    final String errorMsg = "unable to decode encrypted password value for setting: " + e.getMessage();
                    final ErrorInformation errorInfo = new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, errorMsg);
                    throw new PwmOperationalException(errorInfo);
                }
            }
            return newPasswordValue;
        }
    };
}
Also used : PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) ErrorInformation(password.pwm.error.ErrorInformation) PasswordData(password.pwm.util.PasswordData) Element(org.jdom2.Element) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 22 with PwmSecurityKey

use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.

the class FormValue method factory.

public static StoredValueFactory factory() {
    return new StoredValueFactory() {

        public FormValue fromJson(final String input) {
            if (input == null) {
                return new FormValue(Collections.<FormConfiguration>emptyList());
            } else {
                List<FormConfiguration> srcList = JsonUtil.deserialize(input, new TypeToken<List<FormConfiguration>>() {
                });
                srcList = srcList == null ? Collections.<FormConfiguration>emptyList() : srcList;
                while (srcList.contains(null)) {
                    srcList.remove(null);
                }
                return new FormValue(Collections.unmodifiableList(srcList));
            }
        }

        public FormValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) throws PwmOperationalException {
            final boolean oldType = PwmSettingSyntax.LOCALIZED_STRING_ARRAY.toString().equals(settingElement.getAttributeValue("syntax"));
            final List valueElements = settingElement.getChildren("value");
            final List<FormConfiguration> values = new ArrayList<>();
            for (final Object loopValue : valueElements) {
                final Element loopValueElement = (Element) loopValue;
                final String value = loopValueElement.getText();
                if (value != null && value.length() > 0 && loopValueElement.getAttribute("locale") == null) {
                    if (oldType) {
                        values.add(FormConfiguration.parseOldConfigString(value));
                    } else {
                        values.add(JsonUtil.deserialize(value, FormConfiguration.class));
                    }
                }
            }
            final FormValue formValue = new FormValue(values);
            formValue.needsXmlUpdate = oldType;
            return formValue;
        }
    };
}
Also used : PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) FormConfiguration(password.pwm.config.value.data.FormConfiguration) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with PwmSecurityKey

use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.

the class LocalizedStringValue method factory.

public static StoredValueFactory factory() {
    return new StoredValueFactory() {

        public LocalizedStringValue fromJson(final String input) {
            if (input == null) {
                return new LocalizedStringValue(Collections.emptyMap());
            } else {
                Map<String, String> srcMap = JsonUtil.deserialize(input, new TypeToken<Map<String, String>>() {
                });
                srcMap = srcMap == null ? Collections.emptyMap() : new TreeMap<>(srcMap);
                return new LocalizedStringValue(Collections.unmodifiableMap(srcMap));
            }
        }

        public LocalizedStringValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) {
            final List elements = settingElement.getChildren("value");
            final Map<String, String> values = new TreeMap<>();
            for (final Object loopValue : elements) {
                final Element loopValueElement = (Element) loopValue;
                final String localeString = loopValueElement.getAttributeValue("locale");
                final String value = loopValueElement.getText();
                values.put(localeString == null ? "" : localeString, value);
            }
            return new LocalizedStringValue(values);
        }
    };
}
Also used : PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 24 with PwmSecurityKey

use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.

the class UserPermissionValue method factory.

public static StoredValueFactory factory() {
    return new StoredValueFactory() {

        public UserPermissionValue fromJson(final String input) {
            if (input == null) {
                return new UserPermissionValue(Collections.<UserPermission>emptyList());
            } else {
                List<UserPermission> srcList = JsonUtil.deserialize(input, new TypeToken<List<UserPermission>>() {
                });
                srcList = srcList == null ? Collections.<UserPermission>emptyList() : srcList;
                while (srcList.contains(null)) {
                    srcList.remove(null);
                }
                return new UserPermissionValue(Collections.unmodifiableList(srcList));
            }
        }

        public UserPermissionValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) throws PwmOperationalException {
            final boolean newType = "2".equals(settingElement.getAttributeValue(StoredConfigurationImpl.XML_ATTRIBUTE_SYNTAX_VERSION));
            final List valueElements = settingElement.getChildren("value");
            final List<UserPermission> values = new ArrayList<>();
            for (final Object loopValue : valueElements) {
                final Element loopValueElement = (Element) loopValue;
                final String value = loopValueElement.getText();
                if (value != null && !value.isEmpty()) {
                    if (newType) {
                        final UserPermission userPermission = JsonUtil.deserialize(value, UserPermission.class);
                        values.add(userPermission);
                    } else {
                        values.add(new UserPermission(UserPermission.Type.ldapQuery, null, value, null));
                    }
                }
            }
            final UserPermissionValue userPermissionValue = new UserPermissionValue(values);
            userPermissionValue.needsXmlUpdate = !newType;
            return userPermissionValue;
        }
    };
}
Also used : PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UserPermission(password.pwm.config.value.data.UserPermission)

Example 25 with PwmSecurityKey

use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.

the class VerificationMethodValue method factory.

public static StoredValueFactory factory() {
    return new StoredValueFactory() {

        public VerificationMethodValue fromJson(final String input) {
            if (input == null) {
                return new VerificationMethodValue();
            } else {
                final VerificationMethodSettings settings = JsonUtil.deserialize(input, VerificationMethodSettings.class);
                return new VerificationMethodValue(settings);
            }
        }

        public VerificationMethodValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) throws PwmOperationalException {
            final Element valueElement = settingElement.getChild("value");
            final String inputStr = valueElement.getText();
            final VerificationMethodSettings settings = JsonUtil.deserialize(inputStr, VerificationMethodSettings.class);
            return new VerificationMethodValue(settings);
        }
    };
}
Also used : PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) Element(org.jdom2.Element)

Aggregations

PwmSecurityKey (password.pwm.util.secure.PwmSecurityKey)26 Element (org.jdom2.Element)19 ArrayList (java.util.ArrayList)15 List (java.util.List)11 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)6 Map (java.util.Map)4 ErrorInformation (password.pwm.error.ErrorInformation)4 PasswordData (password.pwm.util.PasswordData)4 TreeMap (java.util.TreeMap)3 IOException (java.io.IOException)2 X509Certificate (java.security.cert.X509Certificate)2 LinkedHashMap (java.util.LinkedHashMap)2 PwmException (password.pwm.error.PwmException)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 PwmSessionBean (password.pwm.http.bean.PwmSessionBean)2 PwmBlockAlgorithm (password.pwm.util.secure.PwmBlockAlgorithm)2 TypeToken (com.google.gson.reflect.TypeToken)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivateKey (java.security.PrivateKey)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1