Search in sources :

Example 1 with JSONObject

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

the class JSONLogFieldSyntax method valueStringIsCompletelyTokenized.

/**
 * {@inheritDoc}
 */
@Override()
public boolean valueStringIsCompletelyTokenized(@NotNull final String valueString) {
    if (super.valueStringIsCompletelyTokenized(valueString)) {
        return true;
    }
    try {
        final JSONObject jsonObject = new JSONObject(valueString);
        final Map<String, JSONValue> fields = jsonObject.getFields();
        return ((fields.size() == 1) && fields.containsKey("tokenized"));
    } catch (final Exception e) {
        Debug.debugException(e);
        return false;
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString)

Example 2 with JSONObject

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

the class JSONLogFieldSyntax method sanitize.

/**
 * Sanitizes the provided JSON value.
 *
 * @param  value  The value to be sanitized.  It must not be {@code null}.
 *
 * @return  A sanitized representation of the provided JSON value.
 */
@NotNull()
private JSONValue sanitize(@NotNull final JSONValue value) {
    if (value instanceof JSONObject) {
        final Map<String, JSONValue> originalFields = ((JSONObject) value).getFields();
        final Map<String, JSONValue> sanitizedFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            sanitizedFields.put(e.getKey(), sanitize(e.getValue()));
        }
        return new JSONObject(sanitizedFields);
    } else if (value instanceof JSONArray) {
        final List<JSONValue> originalValues = ((JSONArray) value).getValues();
        final List<JSONValue> sanitizedValues = new ArrayList<>(originalValues.size());
        for (final JSONValue v : originalValues) {
            sanitizedValues.add(sanitize(v));
        }
        return new JSONArray(sanitizedValues);
    } else if (value instanceof JSONString) {
        final String stringValue = ((JSONString) value).stringValue();
        return new JSONString(sanitize(stringValue));
    } else {
        return value;
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 3 with JSONObject

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

the class JSONLogFieldSyntax method redactValue.

/**
 * Retrieves a redacted representation of the provided JSON value.
 *
 * @param  value  The value to be redacted.
 *
 * @return  A redacted representation of the provided JSON value.
 */
@NotNull()
private JSONValue redactValue(@NotNull final JSONValue value) {
    if (value instanceof JSONObject) {
        final Map<String, JSONValue> originalFields = ((JSONObject) value).getFields();
        final Map<String, JSONValue> redactedFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            final String fieldName = e.getKey();
            if (shouldRedactOrTokenize(fieldName)) {
                redactedFields.put(fieldName, new JSONString(REDACTED_STRING));
            } else {
                redactedFields.put(fieldName, redactValue(e.getValue()));
            }
        }
        return new JSONObject(redactedFields);
    } else if (value instanceof JSONArray) {
        final List<JSONValue> originalValues = ((JSONArray) value).getValues();
        final List<JSONValue> redactedValues = new ArrayList<>(originalValues.size());
        for (final JSONValue v : originalValues) {
            redactedValues.add(redactValue(v));
        }
        return new JSONArray(redactedValues);
    } else {
        return sanitize(value);
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 4 with JSONObject

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

the class OIDRegistry method getDefault.

/**
 * Retrieves the default instance of this OID registry.
 *
 * @return  The default instance of this OID registry.
 */
@NotNull()
public static OIDRegistry getDefault() {
    OIDRegistry oidRegistry = DEFAULT_INSTANCE.get();
    if (oidRegistry == null) {
        synchronized (DEFAULT_INSTANCE) {
            oidRegistry = DEFAULT_INSTANCE.get();
            if (oidRegistry == null) {
                final Map<OID, OIDRegistryItem> items = new TreeMap<>();
                try (InputStream inputStream = OIDRegistry.class.getClassLoader().getResourceAsStream(OID_REGISTRY_JSON_RESOURCE_NAME);
                    JSONObjectReader jsonObjectReader = new JSONObjectReader(inputStream)) {
                    while (true) {
                        final JSONObject o = jsonObjectReader.readObject();
                        if (o == null) {
                            break;
                        }
                        try {
                            final OIDRegistryItem item = new OIDRegistryItem(o);
                            items.put(new OID(item.getOID()), item);
                        } catch (final Exception e) {
                            Debug.debugException(e);
                        }
                    }
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
                oidRegistry = new OIDRegistry(Collections.unmodifiableMap(items));
                DEFAULT_INSTANCE.set(oidRegistry);
            }
        }
    }
    return oidRegistry;
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) InputStream(java.io.InputStream) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) TreeMap(java.util.TreeMap)

Example 5 with JSONObject

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

the class OAUTHBEARERBindResultTestCase method testFailureResultWithCredentials.

/**
 * Tests the behavior for a failure result that includes server SASL
 * credentials that has all elements.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailureResultWithCredentials() throws Exception {
    final String[] initialReferralURLs = { "ldap://ds1.example.com:389/o=initial" };
    final Control[] initialControls = { new Control("1.2.3.4") };
    final JSONObject initialFailureDetails = new JSONObject(new JSONField("status", "invalid_token"), new JSONField("scope", "scope1 scope2 scope3"), new JSONField("openid-configuration", "https://openid.example.com/config"), new JSONField("some-other-field", "foo"));
    final ASN1OctetString initialServerSASLCredentials = new ASN1OctetString(initialFailureDetails.toSingleLineString());
    final String[] finalReferralURLs = { "ldap://ds1.example.com:389/o=final" };
    final Control[] finalControls = { new Control("1.2.3.5") };
    final BindResult initialBindResult = new BindResult(3, ResultCode.SASL_BIND_IN_PROGRESS, "initial diagnostic message", "o=initial matched DN", initialReferralURLs, initialControls, initialServerSASLCredentials);
    final BindResult finalBindResult = new BindResult(4, ResultCode.INVALID_CREDENTIALS, "final diagnostic message", "o=final matched DN", finalReferralURLs, finalControls, null);
    final OAUTHBEARERBindResult bindResult = new OAUTHBEARERBindResult(initialBindResult, finalBindResult);
    assertEquals(bindResult.getMessageID(), 4);
    assertNotNull(bindResult.getResultCode());
    assertEquals(bindResult.getResultCode(), ResultCode.INVALID_CREDENTIALS);
    assertNotNull(bindResult.getDiagnosticMessage());
    assertEquals(bindResult.getDiagnosticMessage(), "final diagnostic message");
    assertNotNull(bindResult.getMatchedDN());
    assertDNsEqual(bindResult.getMatchedDN(), "o=final matched DN");
    assertNotNull(bindResult.getReferralURLs());
    assertEquals(bindResult.getReferralURLs().length, 1);
    assertEquals(bindResult.getReferralURLs()[0], finalReferralURLs[0]);
    assertNotNull(bindResult.getResponseControls());
    assertEquals(bindResult.getResponseControls().length, 1);
    assertEquals(bindResult.getResponseControls()[0], finalControls[0]);
    assertNotNull(bindResult.getServerSASLCredentials());
    assertTrue(bindResult.getServerSASLCredentials().equalsIgnoreType(initialServerSASLCredentials));
    assertNotNull(bindResult.getInitialBindResult());
    assertEquals(bindResult.getInitialBindResult(), initialBindResult);
    assertNotNull(bindResult.getFinalBindResult());
    assertEquals(bindResult.getFinalBindResult(), finalBindResult);
    assertNotNull(bindResult.getFailureDetailsObject());
    assertEquals(bindResult.getFailureDetailsObject(), initialFailureDetails);
    assertNotNull(bindResult.getAuthorizationErrorCode());
    assertEquals(bindResult.getAuthorizationErrorCode(), "invalid_token");
    assertNotNull(bindResult.getScopes());
    assertFalse(bindResult.getScopes().isEmpty());
    assertEquals(bindResult.getScopes(), StaticUtils.setOf("scope1", "scope2", "scope3"));
    assertNotNull(bindResult.getOpenIDConfigurationURL());
    assertEquals(bindResult.getOpenIDConfigurationURL(), "https://openid.example.com/config");
    assertNotNull(bindResult.toString());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Aggregations

JSONObject (com.unboundid.util.json.JSONObject)253 Test (org.testng.annotations.Test)205 JSONString (com.unboundid.util.json.JSONString)105 JSONField (com.unboundid.util.json.JSONField)97 JSONArray (com.unboundid.util.json.JSONArray)89 File (java.io.File)67 JSONValue (com.unboundid.util.json.JSONValue)40 LinkedHashMap (java.util.LinkedHashMap)32 NotNull (com.unboundid.util.NotNull)27 ArrayList (java.util.ArrayList)26 JSONNumber (com.unboundid.util.json.JSONNumber)23 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)22 TestLogHandler (com.unboundid.util.TestLogHandler)21 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)20 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)11 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)10 Entry (com.unboundid.ldap.sdk.Entry)9 JSONException (com.unboundid.util.json.JSONException)9 Date (java.util.Date)9