Search in sources :

Example 11 with JSONNumber

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

the class PasswordPolicyStateJSONTestCase method testSASLAuthentication.

/**
 * Tests the behavior for the properties related to SASL authentication.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSASLAuthentication() throws Exception {
    List<String> availableSASLMechanisms = Arrays.asList("PLAIN", "UNBOUNDID-TOTP");
    PasswordPolicyStateJSON state = createState(StaticUtils.mapOf(AVAILABLE_SASL_MECHANISMS, availableSASLMechanisms, HAS_TOTP_SHARED_SECRET, true, HAS_REGISTERED_YUBIKEY_OTP_DEVICE, false));
    assertNotNull(state.getAvailableSASLMechanisms());
    assertFalse(state.getAvailableSASLMechanisms().isEmpty());
    assertEquals(state.getAvailableSASLMechanisms(), availableSASLMechanisms);
    assertNotNull(state.getAvailableOTPDeliveryMechanisms());
    assertTrue(state.getAvailableOTPDeliveryMechanisms().isEmpty());
    assertNotNull(state.getHasTOTPSharedSecret());
    assertTrue(state.getHasTOTPSharedSecret().booleanValue());
    assertNotNull(state.getHasRegisteredYubiKeyOTPDevice());
    assertFalse(state.getHasRegisteredYubiKeyOTPDevice());
    availableSASLMechanisms = Arrays.asList("PLAIN", "UNBOUNDID-DELIVERED-OTP");
    final List<String> availableOTPDeliveryMechanisms = Arrays.asList("SMS", "E-Mail");
    state = createState(StaticUtils.mapOf(AVAILABLE_SASL_MECHANISMS, availableSASLMechanisms, AVAILABLE_OTP_DELIVERY_MECHANISMS, availableOTPDeliveryMechanisms, HAS_TOTP_SHARED_SECRET, false, HAS_REGISTERED_YUBIKEY_OTP_DEVICE, false));
    assertNotNull(state.getAvailableSASLMechanisms());
    assertFalse(state.getAvailableSASLMechanisms().isEmpty());
    assertEquals(state.getAvailableSASLMechanisms(), availableSASLMechanisms);
    assertNotNull(state.getAvailableOTPDeliveryMechanisms());
    assertFalse(state.getAvailableOTPDeliveryMechanisms().isEmpty());
    assertEquals(state.getAvailableOTPDeliveryMechanisms(), availableOTPDeliveryMechanisms);
    assertNotNull(state.getHasTOTPSharedSecret());
    assertFalse(state.getHasTOTPSharedSecret().booleanValue());
    assertNotNull(state.getHasRegisteredYubiKeyOTPDevice());
    assertFalse(state.getHasRegisteredYubiKeyOTPDevice());
    availableSASLMechanisms = Arrays.asList("PLAIN", "UNBOUNDID-YUBIKEY-OTP");
    state = createState(StaticUtils.mapOf(AVAILABLE_SASL_MECHANISMS, availableSASLMechanisms, HAS_TOTP_SHARED_SECRET, false, HAS_REGISTERED_YUBIKEY_OTP_DEVICE, true));
    assertNotNull(state.getAvailableSASLMechanisms());
    assertFalse(state.getAvailableSASLMechanisms().isEmpty());
    assertEquals(state.getAvailableSASLMechanisms(), availableSASLMechanisms);
    assertNotNull(state.getAvailableOTPDeliveryMechanisms());
    assertTrue(state.getAvailableOTPDeliveryMechanisms().isEmpty());
    assertNotNull(state.getHasTOTPSharedSecret());
    assertFalse(state.getHasTOTPSharedSecret().booleanValue());
    assertNotNull(state.getHasRegisteredYubiKeyOTPDevice());
    assertTrue(state.getHasRegisteredYubiKeyOTPDevice());
    final JSONObject o = new JSONObject(new JSONField(AVAILABLE_SASL_MECHANISMS.getFieldName(), new JSONArray(new JSONNumber(1234))), new JSONField(AVAILABLE_OTP_DELIVERY_MECHANISMS.getFieldName(), new JSONArray(new JSONNumber(5678))));
    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());
    state = PasswordPolicyStateJSON.get(entry);
    assertNotNull(state);
    assertNotNull(state.getAvailableSASLMechanisms());
    assertTrue(state.getAvailableSASLMechanisms().isEmpty());
    assertNotNull(state.getAvailableOTPDeliveryMechanisms());
    assertTrue(state.getAvailableOTPDeliveryMechanisms().isEmpty());
    assertNull(state.getHasTOTPSharedSecret());
    assertNull(state.getHasRegisteredYubiKeyOTPDevice());
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 12 with JSONNumber

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

the class RecentLoginHistoryAttempt method encodeToJSON.

/**
 * Encodes the provided information about a successful authentication attempt
 * to a JSON object.
 *
 * @param  successful              Indicates whether the attempt was
 *                                 successful.
 * @param  timestamp               The time of the authentication attempt.
 * @param  authenticationMethod    The name of the authentication method
 *                                 used for the attempt.  This must not be
 *                                 {@code null} or empty.
 * @param  clientIPAddress         The IP address of the client that made the
 *                                 authentication attempt.  This may be
 *                                 {@code null} if no client IP address is
 *                                 available.
 * @param  failureReason           A general reason that the authentication
 *                                 attempt failed.  It must be {@code null} if
 *                                 the attempt succeeded and must not be
 *                                 {@code null} if the attempt failed.  If
 *                                 provided, the value should be one of the
 *                                 {@code FAILURE_NAME_}* constants in the
 *                                 {@link AuthenticationFailureReason} class.
 * @param  additionalAttemptCount  The number of additional authentication
 *                                 attempts that occurred on the same date (in
 *                                 the UTC time zone) as the provided
 *                                 timestamp with the same values for the
 *                                 successful, authentication method, client
 *                                 IP address, and failure reason fields.  It
 *                                 may be {@code null} if this should not be
 *                                 included (e.g., if information about
 *                                 similar attempts should not be collapsed).
 *
 * @return  A JSON object containing the provided information.
 */
@NotNull()
private static JSONObject encodeToJSON(final boolean successful, final long timestamp, @NotNull final String authenticationMethod, @Nullable final String clientIPAddress, @Nullable final String failureReason, @Nullable final Long additionalAttemptCount) {
    final Map<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
    fields.put(JSON_FIELD_SUCCESSFUL, new JSONBoolean(successful));
    fields.put(JSON_FIELD_TIMESTAMP, new JSONString(StaticUtils.encodeRFC3339Time(timestamp)));
    fields.put(JSON_FIELD_AUTHENTICATION_METHOD, new JSONString(authenticationMethod));
    if (clientIPAddress != null) {
        fields.put(JSON_FIELD_CLIENT_IP_ADDRESS, new JSONString(clientIPAddress));
    }
    if (failureReason != null) {
        fields.put(JSON_FIELD_FAILURE_REASON, new JSONString(failureReason));
    }
    if (additionalAttemptCount != null) {
        fields.put(JSON_FIELD_ADDITIONAL_ATTEMPT_COUNT, new JSONNumber(additionalAttemptCount));
    }
    return new JSONObject(fields);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 13 with JSONNumber

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

the class LDAPCompareJSONOutputHandler method formatResult.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
String formatResult(@NotNull final CompareRequest request, @NotNull final LDAPResult result) {
    final Map<String, JSONValue> fields = new LinkedHashMap<>();
    fields.put(FIELD_NAME_DN, new JSONString(request.getDN()));
    fields.put(FIELD_NAME_ATTRIBUTE, new JSONString(request.getAttributeName()));
    fields.put(FIELD_NAME_ASSERTION_VALUE, new JSONString(request.getAssertionValue()));
    fields.put(FIELD_NAME_RESULT_CODE_VALUE, new JSONNumber(result.getResultCode().intValue()));
    fields.put(FIELD_NAME_RESULT_CODE_NAME, new JSONString(result.getResultCode().getName()));
    if (result.getDiagnosticMessage() != null) {
        fields.put(FIELD_NAME_DIAGNOSTIC_MESSAGE, new JSONString(result.getDiagnosticMessage()));
    }
    if (result.getMatchedDN() != null) {
        fields.put(FIELD_NAME_MATCHED_DN, new JSONString(result.getMatchedDN()));
    }
    if (result.getReferralURLs().length > 0) {
        final JSONValue[] referralURLValues = new JSONValue[result.getReferralURLs().length];
        for (int i = 0; i < referralURLValues.length; i++) {
            referralURLValues[i] = new JSONString(result.getReferralURLs()[i]);
        }
        fields.put(FIELD_NAME_REFERRAL_URLS, new JSONArray(referralURLValues));
    }
    return new JSONObject(fields).toSingleLineString();
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 14 with JSONNumber

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

the class JSONLogMessageTestCase method testValueToStrings.

/**
 * Tests the behavior for the {@code valueToStrings} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testValueToStrings() throws Exception {
    // Simple values.
    assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.TRUE), Collections.singletonList("true"));
    assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.FALSE), Collections.singletonList("false"));
    assertEquals(JSONLogMessage.valueToStrings(JSONNull.NULL), Collections.singletonList("null"));
    assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1234L)), Collections.singletonList("1234"));
    assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1.5d)), Collections.singletonList("1.5"));
    assertEquals(JSONLogMessage.valueToStrings(new JSONString("foo")), Collections.singletonList("foo"));
    // A JSON object value.
    final JSONObject o = new JSONObject(new JSONField("foo", "a"), new JSONField("bar", "b"));
    assertEquals(JSONLogMessage.valueToStrings(o), Collections.singletonList(o.toSingleLineString()));
    // An array of strings.
    final JSONArray stringArray = new JSONArray(new JSONString("one"), new JSONString("two"), new JSONString("three"));
    assertEquals(JSONLogMessage.valueToStrings(stringArray), Arrays.asList("one", "two", "three"));
    // An array that mixes values of different types.
    final JSONArray mixedTypeArray = new JSONArray(JSONBoolean.TRUE, new JSONNumber(0L), new JSONString("foo"), JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT);
    assertEquals(JSONLogMessage.valueToStrings(mixedTypeArray), Arrays.asList("true", "0", "foo", JSONArray.EMPTY_ARRAY.toSingleLineString(), JSONObject.EMPTY_OBJECT.toSingleLineString()));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 15 with JSONNumber

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

the class JSONLogMessageTestCase method testGetFirstValue.

/**
 * Tests the methods for retrieving the first value for a given field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetFirstValue() throws Exception {
    // Test with a field whose value is a single string.
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(CONNECT_FROM_ADDRESS, "1.2.3.4"));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getFirstValue(CONNECT_FROM_ADDRESS), new JSONString("1.2.3.4"));
    // Test with a field whose value is a single Boolean.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, true));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getFirstValue(UNCACHED_DATA_ACCESSED), JSONBoolean.TRUE);
    // Test with a field whose value is a non-empty array of strings.
    o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getFirstValue(REQUEST_CONTROL_OIDS), new JSONString("1.2.3.4"));
    // Test with a field whose value is a non-empty array of non-strings.
    final LogField testField = new LogField("testField", IntegerLogFieldSyntax.getInstance());
    o = createMinimalMessageObject(CONNECT, null, new JSONField("testField", new JSONArray(new JSONNumber(1L), new JSONNumber(2L), new JSONNumber(3L), new JSONNumber(4L))));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getFirstValue(testField), new JSONNumber(1L));
    // Test with a field whose value is an empty array.
    o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS));
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getFirstValue(REQUEST_CONTROL_OIDS));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONNumber (com.unboundid.util.json.JSONNumber)30 JSONString (com.unboundid.util.json.JSONString)28 JSONObject (com.unboundid.util.json.JSONObject)23 Test (org.testng.annotations.Test)23 JSONArray (com.unboundid.util.json.JSONArray)22 JSONField (com.unboundid.util.json.JSONField)20 JSONValue (com.unboundid.util.json.JSONValue)5 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)4 NotNull (com.unboundid.util.NotNull)4 JSONBoolean (com.unboundid.util.json.JSONBoolean)4 LinkedHashMap (java.util.LinkedHashMap)4 Entry (com.unboundid.ldap.sdk.Entry)3 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2 StringLogFieldSyntax (com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.StringLogFieldSyntax)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 PasswordPolicyStateAccountUsabilityError (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityError)1 PasswordPolicyStateAccountUsabilityNotice (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityNotice)1 PasswordPolicyStateAccountUsabilityWarning (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityWarning)1