use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class PasswordPolicyStateJSONTestCase method createState.
/**
* Creates a password policy state JSON object with the provided fields.
*
* @param fields The fields to include in the JSON object.
*
* @return The password policy state JSON object that was created.
*
* @throws Exception If an unexpected problem occurs.
*/
private PasswordPolicyStateJSON createState(final Map<PasswordPolicyStateJSONField, ?> fields) throws Exception {
final Map<String, JSONValue> jsonFields = new LinkedHashMap<>();
for (final PasswordPolicyStateJSONField field : fields.keySet()) {
final String name = field.getFieldName();
final Object value = fields.get(field);
if (value instanceof Boolean) {
final Boolean b = (Boolean) value;
jsonFields.put(name, new JSONBoolean(b));
} else if (value instanceof Integer) {
final Integer i = (Integer) value;
jsonFields.put(name, new JSONNumber(i));
} else if (value instanceof String) {
final String s = (String) value;
jsonFields.put(name, new JSONString(s));
} else if (value instanceof Date) {
final Date d = (Date) value;
jsonFields.put(name, new JSONString(StaticUtils.encodeRFC3339Time(d)));
} else if (value instanceof List) {
final List<?> l = (List<?>) value;
final List<JSONValue> arrayValues = new ArrayList<>();
for (final Object o : l) {
if (o instanceof Date) {
final Date d = (Date) o;
arrayValues.add(new JSONString(StaticUtils.encodeRFC3339Time(d)));
} else if (o instanceof String) {
final String s = (String) o;
arrayValues.add(new JSONString(s));
} else if (o instanceof PasswordPolicyStateAccountUsabilityError) {
final PasswordPolicyStateAccountUsabilityError e = (PasswordPolicyStateAccountUsabilityError) o;
if (e.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", e.getName()), new JSONField("type-id", e.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", e.getName()), new JSONField("type-id", e.getIntValue()), new JSONField("message", e.getMessage())));
}
} else if (o instanceof PasswordPolicyStateAccountUsabilityWarning) {
final PasswordPolicyStateAccountUsabilityWarning w = (PasswordPolicyStateAccountUsabilityWarning) o;
if (w.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", w.getName()), new JSONField("type-id", w.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", w.getName()), new JSONField("type-id", w.getIntValue()), new JSONField("message", w.getMessage())));
}
} else if (o instanceof PasswordPolicyStateAccountUsabilityNotice) {
final PasswordPolicyStateAccountUsabilityNotice n = (PasswordPolicyStateAccountUsabilityNotice) o;
if (n.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", n.getName()), new JSONField("type-id", n.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", n.getName()), new JSONField("type-id", n.getIntValue()), new JSONField("message", n.getMessage())));
}
} else {
fail("Unexpected list element " + o + " of type " + o.getClass().getName());
}
}
jsonFields.put(name, new JSONArray(arrayValues));
} else if (value instanceof JSONValue) {
jsonFields.put(name, (JSONValue) value);
} else {
fail("Unexpected field value " + value + " of type " + value.getClass().getName());
}
}
final JSONObject o = new JSONObject(jsonFields);
final Entry entry = new Entry("dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User");
entry.addAttribute("ds-pwp-state-json", o.toSingleLineString());
final PasswordPolicyStateJSON state = PasswordPolicyStateJSON.get(entry);
assertNotNull(state);
assertNotNull(state.getPasswordPolicyStateJSONObject());
assertFalse(state.getPasswordPolicyStateJSONObject().getFields().isEmpty());
assertEquals(state.getPasswordPolicyStateJSONObject().getFields().size(), jsonFields.size());
assertNotNull(state.toString());
assertFalse(state.toString().isEmpty());
return state;
}
use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class PasswordPolicyStateJSONTestCase method encodeRequirement.
/**
* Encodes the provided password quality requirement to a JSON object suitable
* for inclusion in the password policy state properties object.
*
* @param requirement The requirement to be encoded.
* @param appliesToAdd Indicates whether the requirement applies to
* add operations.
* @param appliesToSelfChange Indicates whether the requirement applies to
* self password changes.
* @param appliesToAdminReset Indicates whether the requirement applies to
* administrative password resets.
* @param appliesToBind Indicates whether the requirement applies to
* bind operations.
*
* @return The encoded JSON object.
*/
private static JSONObject encodeRequirement(final PasswordQualityRequirement requirement, final boolean appliesToAdd, final boolean appliesToSelfChange, final boolean appliesToAdminReset, final boolean appliesToBind) {
final Map<String, JSONValue> objectFields = new LinkedHashMap<>();
objectFields.put("description", new JSONString(requirement.getDescription()));
final String validationType = requirement.getClientSideValidationType();
if (validationType != null) {
objectFields.put("client-side-validation-type", new JSONString(validationType));
final List<JSONValue> propertyObjects = new ArrayList<>();
for (Map.Entry<String, String> e : requirement.getClientSideValidationProperties().entrySet()) {
propertyObjects.add(new JSONObject(new JSONField("name", e.getKey()), new JSONField("value", e.getValue())));
}
objectFields.put("client-side-validation-properties", new JSONArray(propertyObjects));
}
objectFields.put("applies-to-add", new JSONBoolean(appliesToAdd));
objectFields.put("applies-to-self-change", new JSONBoolean(appliesToSelfChange));
objectFields.put("applies-to-administrative-reset", new JSONBoolean(appliesToAdminReset));
objectFields.put("applies-to-bind", new JSONBoolean(appliesToBind));
return new JSONObject(objectFields);
}
use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class ScrambleAttributeTransformation method scrambleJSONValue.
/**
* Scrambles the provided JSON value.
*
* @param v The JSON value to be scrambled.
* @param scrambleAllFields Indicates whether all fields of any JSON object
* should be scrambled.
*
* @return The scrambled JSON value.
*/
@NotNull()
private JSONValue scrambleJSONValue(@NotNull final JSONValue v, final boolean scrambleAllFields) {
if (v instanceof JSONArray) {
final JSONArray a = (JSONArray) v;
final List<JSONValue> originalValues = a.getValues();
final ArrayList<JSONValue> scrambledValues = new ArrayList<>(originalValues.size());
for (final JSONValue arrayValue : originalValues) {
scrambledValues.add(scrambleJSONValue(arrayValue, true));
}
return new JSONArray(scrambledValues);
} else if (v instanceof JSONBoolean) {
return new JSONBoolean(ThreadLocalRandom.get().nextBoolean());
} else if (v instanceof JSONNumber) {
try {
return new JSONNumber(scrambleNumericValue(v.toString()));
} catch (final Exception e) {
// This should never happen.
Debug.debugException(e);
return v;
}
} else if (v instanceof JSONObject) {
final JSONObject o = (JSONObject) v;
final Map<String, JSONValue> originalFields = o.getFields();
final LinkedHashMap<String, JSONValue> scrambledFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
final JSONValue scrambledValue;
final String fieldName = e.getKey();
final JSONValue originalValue = e.getValue();
if (scrambleAllFields || jsonFields.contains(StaticUtils.toLowerCase(fieldName))) {
scrambledValue = scrambleJSONValue(originalValue, scrambleAllFields);
} else if (originalValue instanceof JSONArray) {
scrambledValue = scrambleObjectsInArray((JSONArray) originalValue);
} else if (originalValue instanceof JSONObject) {
scrambledValue = scrambleJSONValue(originalValue, false);
} else {
scrambledValue = originalValue;
}
scrambledFields.put(fieldName, scrambledValue);
}
return new JSONObject(scrambledFields);
} else if (v instanceof JSONString) {
final JSONString s = (JSONString) v;
return new JSONString(scrambleString(s.stringValue()));
} else {
// those.
return v;
}
}
use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class ScrambleAttributeTransformation method scrambleJSONObject.
/**
* Scrambles the provided JSON object value. If the provided value can be
* parsed as a valid JSON object, then the resulting value will be a JSON
* object with all field names preserved and some or all of the field values
* scrambled. If this {@code AttributeScrambler} was created with a set of
* JSON fields, then only the values of those fields will be scrambled;
* otherwise, all field values will be scrambled.
*
* @param s The time value to scramble.
*
* @return The scrambled value.
*/
@Nullable()
public String scrambleJSONObject(@Nullable final String s) {
if (s == null) {
return null;
}
// Try to parse the value as a JSON object. If this fails, then just
// scramble it as a generic string.
final JSONObject o;
try {
o = new JSONObject(s);
} catch (final Exception e) {
Debug.debugException(e);
return scrambleString(s);
}
final boolean scrambleAllFields = jsonFields.isEmpty();
final Map<String, JSONValue> originalFields = o.getFields();
final LinkedHashMap<String, JSONValue> scrambledFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
final JSONValue scrambledValue;
final String fieldName = e.getKey();
final JSONValue originalValue = e.getValue();
if (scrambleAllFields || jsonFields.contains(StaticUtils.toLowerCase(fieldName))) {
scrambledValue = scrambleJSONValue(originalValue, true);
} else if (originalValue instanceof JSONArray) {
scrambledValue = scrambleObjectsInArray((JSONArray) originalValue);
} else if (originalValue instanceof JSONObject) {
scrambledValue = scrambleJSONValue(originalValue, false);
} else {
scrambledValue = originalValue;
}
scrambledFields.put(fieldName, scrambledValue);
}
return new JSONObject(scrambledFields).toString();
}
use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class PasswordPolicyStateJSON method getAccountUsabilityWarnings.
/**
* Retrieves a list of information about any warning conditions that may soon
* affect usability of the user's account.
*
* @return A list of information about any warning conditions that may soon
* affect the usability of the user's account. The returned list may
* be empty if there are no account usability warnings or if this was
* not included in the password policy state JSON object.
*/
@NotNull()
public List<PasswordPolicyStateAccountUsabilityWarning> getAccountUsabilityWarnings() {
final List<PasswordPolicyStateAccountUsabilityWarning> warnings = new ArrayList<>();
final List<JSONValue> values = passwordPolicyStateObject.getFieldAsArray(ACCOUNT_USABILITY_WARNINGS.getFieldName());
if (values != null) {
for (final JSONValue v : values) {
if (v instanceof JSONObject) {
final JSONObject o = (JSONObject) v;
final String typeName = o.getFieldAsString(USABILITY_FIELD_TYPE_NAME);
final Integer typeID = o.getFieldAsInteger(USABILITY_FIELD_TYPE_ID);
final String message = o.getFieldAsString(USABILITY_FIELD_MESSAGE);
if ((typeName != null) && (typeID != null)) {
warnings.add(new PasswordPolicyStateAccountUsabilityWarning(typeID, typeName, message));
}
}
}
}
return Collections.unmodifiableList(warnings);
}
Aggregations