Search in sources :

Example 6 with JSONValue

use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.

the class PasswordPolicyStateJSON method getGraceLoginUseTimes.

/**
 * Retrieves a list of the times that the user has used a grace login to
 * authenticate.
 *
 * @return  A list of the times that the user has used a grace login to
 *          authenticate, or an empty list if the user has not used any grace
 *          logins, or if this was not included in the password policy state
 *          JSON object (e.g., if grace logins are not configured in the
 *          password policy that governs the user).
 */
@NotNull()
public List<Date> getGraceLoginUseTimes() {
    final List<Date> graceLoginTimes = new ArrayList<>();
    final List<JSONValue> values = passwordPolicyStateObject.getFieldAsArray(GRACE_LOGIN_USE_TIMES.getFieldName());
    if (values != null) {
        for (final JSONValue v : values) {
            try {
                final String valueString = ((JSONString) v).stringValue();
                graceLoginTimes.add(StaticUtils.decodeRFC3339Time(valueString));
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
    }
    return Collections.unmodifiableList(graceLoginTimes);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) ArrayList(java.util.ArrayList) JSONString(com.unboundid.util.json.JSONString) Date(java.util.Date) JSONString(com.unboundid.util.json.JSONString) LDAPException(com.unboundid.ldap.sdk.LDAPException) NotNull(com.unboundid.util.NotNull)

Example 7 with JSONValue

use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.

the class JSONLogsTestCase method createPopulatedMessageObject.

/**
 * Creates a JSON object with an encoded representation of a log message that
 * has values populated for most of the optional common fields.
 *
 * @param  messageType    The message type for the log message.  This must not
 *                        be {@code null}.
 * @param  operationType  The operation type for the log message.  This may be
 *                        {@code null} if it is not an operation log message.
 * @param  fields         The set of additional fields to include in the log
 *                        message.  This must not be {@code null} but may be
 *                        empty.
 *
 * @return  The log message that was created.
 */
@NotNull()
protected static JSONObject createPopulatedMessageObject(@NotNull final AccessLogMessageType messageType, @Nullable final AccessLogOperationType operationType, @NotNull final JSONField... fields) {
    final Map<String, JSONValue> fieldMap = createMinimalFieldMap(messageType, operationType);
    fieldMap.put(PRODUCT_NAME.getFieldName(), new JSONString(DEFAULT_PRODUCT_NAME));
    fieldMap.put(INSTANCE_NAME.getFieldName(), new JSONString(DEFAULT_INSTANCE_NAME));
    fieldMap.put(STARTUP_ID.getFieldName(), new JSONString(DEFAULT_STARTUP_ID));
    fieldMap.put(THREAD_ID.getFieldName(), new JSONNumber(DEFAULT_THREAD_ID));
    fieldMap.put(CONNECTION_ID.getFieldName(), new JSONNumber(DEFAULT_CONNECTION_ID));
    if (operationType != null) {
        fieldMap.put(OPERATION_ID.getFieldName(), new JSONNumber(DEFAULT_OPERATION_ID));
        fieldMap.put(MESSAGE_ID.getFieldName(), new JSONNumber(DEFAULT_MESSAGE_ID));
        fieldMap.put(TRIGGERED_BY_CONNECTION_ID.getFieldName(), new JSONNumber(DEFAULT_TRIGGERED_BY_CONNECTION_ID));
        fieldMap.put(TRIGGERED_BY_OPERATION_ID.getFieldName(), new JSONNumber(DEFAULT_TRIGGERED_BY_OPERATION_ID));
        fieldMap.put(ORIGIN.getFieldName(), new JSONString(DEFAULT_ORIGIN));
        fieldMap.put(REQUESTER_IP_ADDRESS.getFieldName(), new JSONString(DEFAULT_REQUESTER_IP));
        fieldMap.put(REQUESTER_DN.getFieldName(), new JSONString(DEFAULT_REQUESTER_DN));
        fieldMap.put(REQUEST_CONTROL_OIDS.getFieldName(), createArray(DEFAULT_REQUEST_CONTROL_OIDS));
        fieldMap.put(USING_ADMIN_SESSION_WORKER_THREAD.getFieldName(), new JSONBoolean(DEFAULT_USING_ADMIN_SESSION_WORKER_THREAD));
        fieldMap.put(ADMINISTRATIVE_OPERATION.getFieldName(), new JSONString(DEFAULT_ADMIN_OP_MESSAGE));
        fieldMap.put(INTERMEDIATE_CLIENT_REQUEST_CONTROL.getFieldName(), DEFAULT_INTERMEDIATE_CLIENT_REQUEST.getControlObject());
        fieldMap.put(OPERATION_PURPOSE.getFieldName(), DEFAULT_OPERATION_PURPOSE_REQUEST.getControlObject());
        if ((messageType == AccessLogMessageType.FORWARD) || (messageType == AccessLogMessageType.FORWARD_FAILED) || (messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(TARGET_HOST.getFieldName(), new JSONString(DEFAULT_FORWARD_TARGET_HOST));
            fieldMap.put(TARGET_PORT.getFieldName(), new JSONNumber(DEFAULT_FORWARD_TARGET_PORT));
            fieldMap.put(TARGET_PROTOCOL.getFieldName(), new JSONString(DEFAULT_FORWARD_TARGET_PROTOCOL));
        }
        if ((messageType == AccessLogMessageType.FORWARD_FAILED) || (messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(RESULT_CODE_VALUE.getFieldName(), new JSONNumber(DEFAULT_RESULT_CODE.intValue()));
            fieldMap.put(RESULT_CODE_NAME.getFieldName(), new JSONString(DEFAULT_RESULT_CODE.getName()));
            fieldMap.put(DIAGNOSTIC_MESSAGE.getFieldName(), new JSONString(DEFAULT_DIAGNOSTIC_MESSAGE));
        }
        if ((messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE) || (messageType == AccessLogMessageType.ENTRY) || (messageType == AccessLogMessageType.REFERENCE) || (messageType == AccessLogMessageType.INTERMEDIATE_RESPONSE)) {
            fieldMap.put(RESPONSE_CONTROL_OIDS.getFieldName(), createArray(DEFAULT_RESPONSE_CONTROL_OIDS));
        }
        if ((messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(ADDITIONAL_INFO.getFieldName(), new JSONString(DEFAULT_ADDITIONAL_INFO_MESSAGE));
            fieldMap.put(MATCHED_DN.getFieldName(), new JSONString(DEFAULT_MATCHED_DN));
            fieldMap.put(REFERRAL_URLS.getFieldName(), createArray(DEFAULT_REFERRAL_URLS));
            fieldMap.put(SERVERS_ACCESSED.getFieldName(), createArray(DEFAULT_SERVERS_ACCESSED));
            fieldMap.put(UNCACHED_DATA_ACCESSED.getFieldName(), new JSONBoolean(DEFAULT_UNCACHED_DATA_ACCESSED));
            fieldMap.put(WORK_QUEUE_WAIT_TIME_MILLIS.getFieldName(), new JSONNumber(DEFAULT_WORK_QUEUE_WAIT_TIME_MILLIS));
            fieldMap.put(PROCESSING_TIME_MILLIS.getFieldName(), new JSONNumber(DEFAULT_PROCESSING_TIME_MILLIS));
            fieldMap.put(INTERMEDIATE_RESPONSES_RETURNED.getFieldName(), new JSONNumber(DEFAULT_INTERMEDIATE_RESPONSES_RETURNED));
            fieldMap.put(USED_PRIVILEGES.getFieldName(), createArray(DEFAULT_USED_PRIVILEGES));
            fieldMap.put(PRE_AUTHORIZATION_USED_PRIVILEGES.getFieldName(), createArray(DEFAULT_PRE_AUTHZ_USED_PRIVILEGES));
            fieldMap.put(MISSING_PRIVILEGES.getFieldName(), createArray(DEFAULT_MISSING_PRIVILEGES));
            if (operationType != AccessLogOperationType.ABANDON) {
                fieldMap.put(INTERMEDIATE_CLIENT_RESPONSE_CONTROL.getFieldName(), DEFAULT_INTERMEDIATE_CLIENT_RESPONSE.getControlObject());
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.COMPARE) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN) || (operationType == AccessLogOperationType.SEARCH)) {
                fieldMap.put(AUTHORIZATION_DN.getFieldName(), new JSONString(DEFAULT_AUTHZ_DN));
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN)) {
                fieldMap.put(REPLICATION_CHANGE_ID.getFieldName(), new JSONString(DEFAULT_REPLICATION_CHANGE_ID));
                fieldMap.put(ASSURED_REPLICATION_REQUIREMENTS.getFieldName(), createAssuredReplicationRequirements());
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN) || (operationType == AccessLogOperationType.SEARCH)) {
                fieldMap.put(INDEXES_WITH_KEYS_ACCESSED_NEAR_ENTRY_LIMIT.getFieldName(), createArray(DEFAULT_INDEXES_NEAR_ENTRY_LIMIT));
                fieldMap.put(INDEXES_WITH_KEYS_ACCESSED_EXCEEDING_ENTRY_LIMIT.getFieldName(), createArray(DEFAULT_INDEXES_EXCEEDING_ENTRY_LIMIT));
            }
        }
        if (messageType == AccessLogMessageType.ASSURANCE_COMPLETE) {
            fieldMap.put(LOCAL_ASSURANCE_SATISFIED.getFieldName(), new JSONBoolean(DEFAULT_LOCAL_ASSURANCE_SATISFIED));
            fieldMap.put(REMOTE_ASSURANCE_SATISFIED.getFieldName(), new JSONBoolean(DEFAULT_REMOTE_ASSURANCE_SATISFIED));
            fieldMap.put(SERVER_ASSURANCE_RESULTS.getFieldName(), new JSONArray(DEFAULT_ASSURED_REPLICATION_SERVER_RESULTS.get(0).getServerResultObject(), DEFAULT_ASSURED_REPLICATION_SERVER_RESULTS.get(1).getServerResultObject()));
        }
    }
    for (final JSONField field : fields) {
        fieldMap.put(field.getName(), field.getValue());
    }
    return new JSONObject(fieldMap);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Example 8 with JSONValue

use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testIncludeFields.

/**
 * Tests the behavior for the syntax when it is configured to only consider
 * a specified set of fields as sensitive, and therefore only those fields
 * will be redacted or tokenized when operating on components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testIncludeFields() throws Exception {
    final Set<String> includeFields = StaticUtils.setOf("a", "b", "c");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertEquals(syntax.getIncludedSensitiveFields(), includeFields);
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertTrue(syntax.getExcludedSensitiveFields().isEmpty());
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", 5), new JSONField("c", true), new JSONField("d", "e"), new JSONField("f", new JSONObject(new JSONField("a", "g"), new JSONField("h", "i"))), new JSONField("j", new JSONArray(new JSONString("k"), new JSONObject(new JSONField("a", "l"), new JSONField("m", "n")))));
    assertNotNull(syntax.valueToSanitizedString(o));
    assertEquals(syntax.valueToSanitizedString(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"g\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"l\", \"m\":\"n\" } ] }");
    assertNotNull(syntax.redactComponents(o));
    assertEquals(syntax.redactComponents(o), "{ \"a\":\"{REDACTED}\", " + "\"b\":\"{REDACTED}\", " + "\"c\":\"{REDACTED}\", " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"{REDACTED}\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"{REDACTED}\", \"m\":\"n\" } ] }");
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    assertNotNull(syntax.tokenizeComponents(o, pepper));
    final JSONObject tokenizedObject = new JSONObject(syntax.tokenizeComponents(o, pepper));
    assertEquals(tokenizedObject.getFields().size(), 6);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertTrue(tokenizedObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("b"));
    assertTrue(tokenizedObject.getFieldAsString("b").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("c"));
    assertTrue(tokenizedObject.getFieldAsString("c").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertEquals(tokenizedObject.getFieldAsString("d"), "e");
    final JSONObject tokenizedFObject = tokenizedObject.getFieldAsObject("f");
    assertNotNull(tokenizedFObject);
    assertEquals(tokenizedFObject.getFields().size(), 2);
    assertNotNull(tokenizedFObject.getFieldAsString("a"));
    assertTrue(tokenizedFObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedFObject.getFieldAsString("h"));
    assertEquals(tokenizedFObject.getFieldAsString("h"), "i");
    final List<JSONValue> arrayElements = tokenizedObject.getFieldAsArray("j");
    assertNotNull(arrayElements);
    assertEquals(arrayElements.size(), 2);
    assertEquals(arrayElements.get(0), new JSONString("k"));
    final JSONObject arrayObject = (JSONObject) arrayElements.get(1);
    assertEquals(arrayObject.getFields().size(), 2);
    assertTrue(arrayObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertEquals(arrayObject.getFieldAsString("m"), "n");
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 9 with JSONValue

use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.

the class JSONObjectFilter method getFilters.

/**
 * Retrieves the value of the specified field from the provided JSON object as
 * a list of JSON object filters.  The specified field must be a top-level
 * field in the JSON object and it must have a value that is an array of
 * JSON objects that represent valid JSON object filters.
 *
 * @param  o          The JSON object to examine.  It must not be
 *                    {@code null}.
 * @param  fieldName  The name of a top-level field in the JSON object that is
 *                    expected to have a value that is an array of JSON
 *                    objects that represent valid JSON object filters.  It
 *                    must not be {@code null}.
 *
 * @return  The list of JSON object filters retrieved from the JSON object.
 *
 * @throws  JSONException  If the object doesn't have the specified field, or
 *                         if the value of that field is not an array of
 *                         JSON objects that represent valid JSON object
 *                         filters.
 */
@NotNull()
protected List<JSONObjectFilter> getFilters(@NotNull final JSONObject o, @NotNull final String fieldName) throws JSONException {
    final JSONValue value = o.getField(fieldName);
    if (value == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(o), getFilterType(), fieldName));
    }
    if (!(value instanceof JSONArray)) {
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_ARRAY.get(String.valueOf(o), getFilterType(), fieldName));
    }
    final List<JSONValue> values = ((JSONArray) value).getValues();
    final ArrayList<JSONObjectFilter> filterList = new ArrayList<>(values.size());
    for (final JSONValue arrayValue : values) {
        if (!(arrayValue instanceof JSONObject)) {
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_OBJECT.get(String.valueOf(o), getFilterType(), fieldName));
        }
        final JSONObject filterObject = (JSONObject) arrayValue;
        try {
            filterList.add(decode(filterObject));
        } catch (final JSONException e) {
            Debug.debugException(e);
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_FILTER.get(String.valueOf(o), getFilterType(), String.valueOf(filterObject), fieldName, e.getMessage()), e);
        }
    }
    return filterList;
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(com.unboundid.util.json.JSONException) NotNull(com.unboundid.util.NotNull)

Example 10 with JSONValue

use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.

the class LessThanJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
    fields.put(FIELD_FILTER_TYPE, new JSONString(FILTER_TYPE));
    if (field.size() == 1) {
        fields.put(FIELD_FIELD_PATH, new JSONString(field.get(0)));
    } else {
        final ArrayList<JSONValue> fieldNameValues = new ArrayList<>(field.size());
        for (final String s : field) {
            fieldNameValues.add(new JSONString(s));
        }
        fields.put(FIELD_FIELD_PATH, new JSONArray(fieldNameValues));
    }
    fields.put(FIELD_VALUE, value);
    if (allowEquals) {
        fields.put(FIELD_ALLOW_EQUALS, JSONBoolean.TRUE);
    }
    if (matchAllElements) {
        fields.put(FIELD_MATCH_ALL_ELEMENTS, JSONBoolean.TRUE);
    }
    if (caseSensitive) {
        fields.put(FIELD_CASE_SENSITIVE, JSONBoolean.TRUE);
    }
    return new JSONObject(fields);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONValue (com.unboundid.util.json.JSONValue)50 JSONString (com.unboundid.util.json.JSONString)45 JSONObject (com.unboundid.util.json.JSONObject)40 NotNull (com.unboundid.util.NotNull)33 LinkedHashMap (java.util.LinkedHashMap)31 JSONArray (com.unboundid.util.json.JSONArray)27 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)9 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)8 JSONBoolean (com.unboundid.util.json.JSONBoolean)6 Map (java.util.Map)6 JSONNumber (com.unboundid.util.json.JSONNumber)5 File (java.io.File)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONException (com.unboundid.util.json.JSONException)4 JSONField (com.unboundid.util.json.JSONField)4 Date (java.util.Date)4 List (java.util.List)4 Entry (com.unboundid.ldap.sdk.Entry)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2