Search in sources :

Example 16 with MatchingRule

use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.

the class DefaultObjectEncoder method constructAttributeType.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public AttributeTypeDefinition constructAttributeType(@NotNull final Field f, @NotNull final OIDAllocator a) throws LDAPPersistException {
    final LDAPField at = f.getAnnotation(LDAPField.class);
    final String attrName;
    if (at.attribute().isEmpty()) {
        attrName = f.getName();
    } else {
        attrName = at.attribute();
    }
    final String oid = a.allocateAttributeTypeOID(attrName);
    final TypeInfo typeInfo = new TypeInfo(f.getGenericType());
    if (!typeInfo.isSupported()) {
        throw new LDAPPersistException(ERR_DEFAULT_ENCODER_UNSUPPORTED_TYPE.get(String.valueOf(typeInfo.getType())));
    }
    final boolean isSingleValued = (!supportsMultipleValues(typeInfo));
    final String syntaxOID;
    if (isSingleValued) {
        syntaxOID = getSyntaxOID(typeInfo.getBaseClass());
    } else {
        syntaxOID = getSyntaxOID(typeInfo.getComponentType());
    }
    final MatchingRule mr = MatchingRule.selectMatchingRuleForSyntax(syntaxOID);
    return new AttributeTypeDefinition(oid, new String[] { attrName }, null, false, null, mr.getEqualityMatchingRuleNameOrOID(), mr.getOrderingMatchingRuleNameOrOID(), mr.getSubstringMatchingRuleNameOrOID(), syntaxOID, isSingleValued, false, false, AttributeUsage.USER_APPLICATIONS, null);
}
Also used : AttributeTypeDefinition(com.unboundid.ldap.sdk.schema.AttributeTypeDefinition) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) GeneralizedTimeMatchingRule(com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule) OctetStringMatchingRule(com.unboundid.ldap.matchingrules.OctetStringMatchingRule) BooleanMatchingRule(com.unboundid.ldap.matchingrules.BooleanMatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) NotNull(com.unboundid.util.NotNull)

Example 17 with MatchingRule

use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.

the class DefaultObjectEncoder method encodeArray.

/**
 * Encodes the contents of the provided array object.
 *
 * @param  arrayType      The component type of the array.
 * @param  arrayObject    The array object to process.
 * @param  attributeName  The name to use for the attribute to create.
 *
 * @return  The attribute containing the encoded array contents.
 *
 * @throws  LDAPPersistException  If a problem occurs while trying to create
 *                                the attribute.
 */
@NotNull()
private static Attribute encodeArray(@NotNull final Class<?> arrayType, @NotNull final Object arrayObject, @NotNull final String attributeName) throws LDAPPersistException {
    final ASN1OctetString[] values = new ASN1OctetString[Array.getLength(arrayObject)];
    final AtomicReference<MatchingRule> matchingRule = new AtomicReference<>();
    for (int i = 0; i < values.length; i++) {
        final Object o = Array.get(arrayObject, i);
        if (arrayType.equals(AtomicInteger.class) || arrayType.equals(AtomicLong.class) || arrayType.equals(BigDecimal.class) || arrayType.equals(BigInteger.class) || arrayType.equals(Double.class) || arrayType.equals(Double.TYPE) || arrayType.equals(Float.class) || arrayType.equals(Float.TYPE) || arrayType.equals(Integer.class) || arrayType.equals(Integer.TYPE) || arrayType.equals(Long.class) || arrayType.equals(Long.TYPE) || arrayType.equals(Short.class) || arrayType.equals(Short.TYPE) || arrayType.equals(String.class) || arrayType.equals(StringBuffer.class) || arrayType.equals(StringBuilder.class) || arrayType.equals(UUID.class) || arrayType.equals(DN.class) || arrayType.equals(Filter.class) || arrayType.equals(LDAPURL.class) || arrayType.equals(RDN.class)) {
            if (matchingRule.get() == null) {
                final String syntaxOID = getSyntaxOID(arrayType);
                matchingRule.set(MatchingRule.selectMatchingRuleForSyntax(syntaxOID));
            }
            values[i] = new ASN1OctetString(String.valueOf(o));
        } else if (arrayType.equals(URI.class)) {
            final URI uri = (URI) o;
            values[i] = new ASN1OctetString(uri.toASCIIString());
        } else if (arrayType.equals(URL.class)) {
            final URL url = (URL) o;
            values[i] = new ASN1OctetString(url.toExternalForm());
        } else if (o instanceof byte[]) {
            matchingRule.compareAndSet(null, OctetStringMatchingRule.getInstance());
            values[i] = new ASN1OctetString((byte[]) o);
        } else if (o instanceof char[]) {
            values[i] = new ASN1OctetString(new String((char[]) o));
        } else if (arrayType.equals(Boolean.class) || arrayType.equals(Boolean.TYPE)) {
            matchingRule.compareAndSet(null, BooleanMatchingRule.getInstance());
            final Boolean b = (Boolean) o;
            if (b) {
                values[i] = new ASN1OctetString("TRUE");
            } else {
                values[i] = new ASN1OctetString("FALSE");
            }
        } else if (arrayType.equals(Date.class)) {
            matchingRule.compareAndSet(null, GeneralizedTimeMatchingRule.getInstance());
            final Date d = (Date) o;
            values[i] = new ASN1OctetString(StaticUtils.encodeGeneralizedTime(d));
        } else if (arrayType.isEnum()) {
            final Enum<?> e = (Enum<?>) o;
            values[i] = new ASN1OctetString(e.name());
        } else if (Serializable.class.isAssignableFrom(arrayType)) {
            matchingRule.compareAndSet(null, OctetStringMatchingRule.getInstance());
            try {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                final ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(o);
                oos.close();
                values[i] = new ASN1OctetString(baos.toByteArray());
            } catch (final Exception e) {
                Debug.debugException(e);
                throw new LDAPPersistException(ERR_DEFAULT_ENCODER_CANNOT_SERIALIZE.get(attributeName, StaticUtils.getExceptionMessage(e)), e);
            }
        } else {
            throw new LDAPPersistException(ERR_DEFAULT_ENCODER_UNSUPPORTED_TYPE.get(arrayType.getName()));
        }
    }
    matchingRule.compareAndSet(null, CaseIgnoreStringMatchingRule.getInstance());
    return new Attribute(attributeName, matchingRule.get(), values);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Attribute(com.unboundid.ldap.sdk.Attribute) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ObjectOutputStream(java.io.ObjectOutputStream) URI(java.net.URI) URL(java.net.URL) LDAPURL(com.unboundid.ldap.sdk.LDAPURL) UUID(java.util.UUID) RDN(com.unboundid.ldap.sdk.RDN) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Filter(com.unboundid.ldap.sdk.Filter) BigInteger(java.math.BigInteger) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) GeneralizedTimeMatchingRule(com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule) OctetStringMatchingRule(com.unboundid.ldap.matchingrules.OctetStringMatchingRule) BooleanMatchingRule(com.unboundid.ldap.matchingrules.BooleanMatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) NotNull(com.unboundid.util.NotNull)

Example 18 with MatchingRule

use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.

the class ScrambleAttributeTransformation method scrambleAttribute.

/**
 * Creates a copy of the provided attribute with its values scrambled if
 * appropriate.
 *
 * @param  a  The attribute to scramble.
 *
 * @return  A copy of the provided attribute with its values scrambled, or
 *          the original attribute if no scrambling should be performed.
 */
@Nullable()
public Attribute scrambleAttribute(@NotNull final Attribute a) {
    if ((a == null) || (a.size() == 0)) {
        return a;
    }
    final String baseName = StaticUtils.toLowerCase(a.getBaseName());
    final MatchingRule matchingRule = attributes.get(baseName);
    if (matchingRule == null) {
        return a;
    }
    if (matchingRule instanceof BooleanMatchingRule) {
        // results.  We will just  pick boolean values at random.
        if (a.size() == 1) {
            return new Attribute(a.getName(), schema, ThreadLocalRandom.get().nextBoolean() ? "TRUE" : "FALSE");
        } else {
            // regardless of how many values the provided attribute actually had.
            return new Attribute(a.getName(), schema, "TRUE", "FALSE");
        }
    } else if (matchingRule instanceof DistinguishedNameMatchingRule) {
        final String[] originalValues = a.getValues();
        final String[] scrambledValues = new String[originalValues.length];
        for (int i = 0; i < originalValues.length; i++) {
            try {
                scrambledValues[i] = scrambleDN(new DN(originalValues[i])).toString();
            } catch (final Exception e) {
                Debug.debugException(e);
                scrambledValues[i] = scrambleString(originalValues[i]);
            }
        }
        return new Attribute(a.getName(), schema, scrambledValues);
    } else if (matchingRule instanceof GeneralizedTimeMatchingRule) {
        final String[] originalValues = a.getValues();
        final String[] scrambledValues = new String[originalValues.length];
        for (int i = 0; i < originalValues.length; i++) {
            scrambledValues[i] = scrambleGeneralizedTime(originalValues[i]);
        }
        return new Attribute(a.getName(), schema, scrambledValues);
    } else if ((matchingRule instanceof IntegerMatchingRule) || (matchingRule instanceof NumericStringMatchingRule) || (matchingRule instanceof TelephoneNumberMatchingRule)) {
        final String[] originalValues = a.getValues();
        final String[] scrambledValues = new String[originalValues.length];
        for (int i = 0; i < originalValues.length; i++) {
            scrambledValues[i] = scrambleNumericValue(originalValues[i]);
        }
        return new Attribute(a.getName(), schema, scrambledValues);
    } else if (matchingRule instanceof OctetStringMatchingRule) {
        // If the target attribute is userPassword, then treat it like an encoded
        // password.
        final byte[][] originalValues = a.getValueByteArrays();
        final byte[][] scrambledValues = new byte[originalValues.length][];
        for (int i = 0; i < originalValues.length; i++) {
            if (baseName.equals("userpassword") || baseName.equals("2.5.4.35")) {
                scrambledValues[i] = StaticUtils.getBytes(scrambleEncodedPassword(StaticUtils.toUTF8String(originalValues[i])));
            } else {
                scrambledValues[i] = scrambleBinaryValue(originalValues[i]);
            }
        }
        return new Attribute(a.getName(), schema, scrambledValues);
    } else {
        final String[] originalValues = a.getValues();
        final String[] scrambledValues = new String[originalValues.length];
        for (int i = 0; i < originalValues.length; i++) {
            if (baseName.equals("userpassword") || baseName.equals("2.5.4.35") || baseName.equals("authpassword") || baseName.equals("1.3.6.1.4.1.4203.1.3.4")) {
                scrambledValues[i] = scrambleEncodedPassword(originalValues[i]);
            } else if (originalValues[i].startsWith("{") && originalValues[i].endsWith("}")) {
                scrambledValues[i] = scrambleJSONObject(originalValues[i]);
            } else {
                scrambledValues[i] = scrambleString(originalValues[i]);
            }
        }
        return new Attribute(a.getName(), schema, scrambledValues);
    }
}
Also used : GeneralizedTimeMatchingRule(com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule) Attribute(com.unboundid.ldap.sdk.Attribute) NumericStringMatchingRule(com.unboundid.ldap.matchingrules.NumericStringMatchingRule) IntegerMatchingRule(com.unboundid.ldap.matchingrules.IntegerMatchingRule) DN(com.unboundid.ldap.sdk.DN) RDN(com.unboundid.ldap.sdk.RDN) JSONString(com.unboundid.util.json.JSONString) BooleanMatchingRule(com.unboundid.ldap.matchingrules.BooleanMatchingRule) TelephoneNumberMatchingRule(com.unboundid.ldap.matchingrules.TelephoneNumberMatchingRule) DistinguishedNameMatchingRule(com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule) OctetStringMatchingRule(com.unboundid.ldap.matchingrules.OctetStringMatchingRule) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) NumericStringMatchingRule(com.unboundid.ldap.matchingrules.NumericStringMatchingRule) IntegerMatchingRule(com.unboundid.ldap.matchingrules.IntegerMatchingRule) BooleanMatchingRule(com.unboundid.ldap.matchingrules.BooleanMatchingRule) DistinguishedNameMatchingRule(com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule) TelephoneNumberMatchingRule(com.unboundid.ldap.matchingrules.TelephoneNumberMatchingRule) GeneralizedTimeMatchingRule(com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) OctetStringMatchingRule(com.unboundid.ldap.matchingrules.OctetStringMatchingRule) Nullable(com.unboundid.util.Nullable)

Example 19 with MatchingRule

use of com.unboundid.ldap.matchingrules.MatchingRule in project ldapsdk by pingidentity.

the class AttributeBasedLogFieldSyntaxHelper method tokenizeValue.

/**
 * Obtains a token for the specified attribute value.
 *
 * @param  syntax          The associated log field syntax instance.  It must
 *                         not be {@code null}.
 * @param  schema          The schema to use in processing.  It may be
 *                         {@code null} if no schema is available.
 * @param  attributeName   The name of the attribute containing the provided
 *                         value.  It must not be {@code null}.
 * @param  attributeValue  The attribute value to tokenize.  It must not be
 *                         {@code null}.
 * @param  pepper          A pepper used to provide brute-force protection for
 *                         the resulting token.  The pepper value should be
 *                         kept secret so that it is not available to
 *                         unauthorized users who might be able to view log
 *                         information, although the same pepper value should
 *                         be consistently provided when tokenizing values so
 *                         that the same value will consistently yield the
 *                         same token.  It must not be {@code null} and should
 *                         not be empty.
 *
 * @return  The token for the provided attribute value.
 */
@NotNull()
static String tokenizeValue(@NotNull final LogFieldSyntax<?> syntax, @Nullable final Schema schema, @NotNull final String attributeName, @NotNull final byte[] attributeValue, @NotNull final byte[] pepper) {
    // Normalize the attribute value.
    ASN1OctetString normalizedValue;
    final ASN1OctetString nonNormalizedValue = new ASN1OctetString(attributeValue);
    try {
        final MatchingRule matchingRule = MatchingRule.selectEqualityMatchingRule(attributeName, schema);
        normalizedValue = matchingRule.normalize(nonNormalizedValue);
    } catch (final Exception e) {
        Debug.debugException(e);
        normalizedValue = nonNormalizedValue;
    }
    // Tokenize the normalized value.
    final ByteStringBuffer tokenizeBuffer = syntax.getTemporaryBuffer();
    try {
        syntax.tokenize(normalizedValue.getValue(), pepper, tokenizeBuffer);
        return tokenizeBuffer.toString();
    } finally {
        syntax.releaseTemporaryBuffer(tokenizeBuffer);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MatchingRule(com.unboundid.ldap.matchingrules.MatchingRule) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) NotNull(com.unboundid.util.NotNull)

Aggregations

MatchingRule (com.unboundid.ldap.matchingrules.MatchingRule)19 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)18 NotNull (com.unboundid.util.NotNull)12 CaseIgnoreStringMatchingRule (com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule)11 Attribute (com.unboundid.ldap.sdk.Attribute)8 LDAPException (com.unboundid.ldap.sdk.LDAPException)8 GeneralizedTimeMatchingRule (com.unboundid.ldap.matchingrules.GeneralizedTimeMatchingRule)7 RDN (com.unboundid.ldap.sdk.RDN)7 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 BooleanMatchingRule (com.unboundid.ldap.matchingrules.BooleanMatchingRule)4 OctetStringMatchingRule (com.unboundid.ldap.matchingrules.OctetStringMatchingRule)4 DN (com.unboundid.ldap.sdk.DN)4 DistinguishedNameMatchingRule (com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule)3 IntegerMatchingRule (com.unboundid.ldap.matchingrules.IntegerMatchingRule)3 ASN1Exception (com.unboundid.asn1.ASN1Exception)2 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)2 ChangeLogEntry (com.unboundid.ldap.sdk.ChangeLogEntry)2 Control (com.unboundid.ldap.sdk.Control)2 Entry (com.unboundid.ldap.sdk.Entry)2