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);
}
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);
}
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);
}
}
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);
}
}
Aggregations