Search in sources :

Example 51 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class MoveSubtree method getAuthenticatedUserDN.

/**
 * Retrieves the DN of the user authenticated on the provided connection.  It
 * will first try to look at the last successful bind request processed on the
 * connection, and will fall back to using the "Who Am I?" extended request.
 *
 * @param  connection        The connection for which to make the
 *                           determination.
 * @param  isSource          Indicates whether the connection is to the source
 *                           or target server.
 * @param  opPurposeControl  An optional operation purpose request control
 *                           that may be included in the request.
 *
 * @return  The DN of the user authenticated on the provided connection, or
 *          {@code null} if the connection is not authenticated.
 *
 * @throws  LDAPException  If a problem is encountered while making the
 *                         determination.
 */
@Nullable()
private static String getAuthenticatedUserDN(@NotNull final LDAPConnection connection, final boolean isSource, @Nullable final OperationPurposeRequestControl opPurposeControl) throws LDAPException {
    final BindRequest bindRequest = InternalSDKHelper.getLastBindRequest(connection);
    if ((bindRequest != null) && (bindRequest instanceof SimpleBindRequest)) {
        final SimpleBindRequest r = (SimpleBindRequest) bindRequest;
        return r.getBindDN();
    }
    final Control[] controls;
    if (opPurposeControl == null) {
        controls = StaticUtils.NO_CONTROLS;
    } else {
        controls = new Control[] { opPurposeControl };
    }
    final String connectionName = isSource ? INFO_MOVE_SUBTREE_CONNECTION_NAME_SOURCE.get() : INFO_MOVE_SUBTREE_CONNECTION_NAME_TARGET.get();
    final WhoAmIExtendedResult whoAmIResult;
    try {
        whoAmIResult = (WhoAmIExtendedResult) connection.processExtendedOperation(new WhoAmIExtendedRequest(controls));
    } catch (final LDAPException le) {
        Debug.debugException(le);
        throw new LDAPException(le.getResultCode(), ERR_MOVE_SUBTREE_ERROR_INVOKING_WHO_AM_I.get(connectionName, StaticUtils.getExceptionMessage(le)), le);
    }
    if (whoAmIResult.getResultCode() != ResultCode.SUCCESS) {
        throw new LDAPException(whoAmIResult.getResultCode(), ERR_MOVE_SUBTREE_ERROR_INVOKING_WHO_AM_I.get(connectionName, whoAmIResult.getDiagnosticMessage()));
    }
    final String authzID = whoAmIResult.getAuthorizationID();
    if ((authzID != null) && authzID.startsWith("dn:")) {
        return authzID.substring(3);
    } else {
        throw new LDAPException(ResultCode.UNWILLING_TO_PERFORM, ERR_MOVE_SUBTREE_CANNOT_IDENTIFY_CONNECTED_USER.get(connectionName));
    }
}
Also used : WhoAmIExtendedResult(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult) SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) Control(com.unboundid.ldap.sdk.Control) RealAttributesOnlyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RealAttributesOnlyRequestControl) OperationPurposeRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OperationPurposeRequestControl) SuppressReferentialIntegrityUpdatesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SuppressReferentialIntegrityUpdatesRequestControl) SoftDeletedEntryAccessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeletedEntryAccessRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) ReturnConflictEntriesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReturnConflictEntriesRequestControl) DraftLDUPSubentriesRequestControl(com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl) LDAPException(com.unboundid.ldap.sdk.LDAPException) WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) BindRequest(com.unboundid.ldap.sdk.BindRequest) SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Nullable(com.unboundid.util.Nullable)

Example 52 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ModifiablePasswordPolicyStateJSON method get.

/**
 * Attempts to retrieve and decode the modifiable password policy state
 * information from the provided user entry.
 *
 * @param  userEntry  The entry for the user for whom to obtain the modifiable
 *                    password policy state information.  It must not be
 *                    {@code null}.
 *
 * @return  The modifiable password policy state information from the provided
 *          user entry, or {@code null} if no modifiable password policy state
 *          information is available for the user.
 *
 * @throws  LDAPException  If a problem is encountered while trying to decode
 *                         the modifiable password policy state JSON object.
 */
@Nullable()
public static ModifiablePasswordPolicyStateJSON get(@NotNull final Entry userEntry) throws LDAPException {
    final String valueString = userEntry.getAttributeValue(MODIFIABLE_PASSWORD_POLICY_STATE_JSON_ATTRIBUTE);
    if (valueString == null) {
        return null;
    }
    final JSONObject jsonObject;
    try {
        jsonObject = new JSONObject(valueString);
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MODIFIABLE_PW_POLICY_STATE_JSON_GET_CANNOT_DECODE.get(MODIFIABLE_PASSWORD_POLICY_STATE_JSON_ATTRIBUTE, userEntry.getDN()), e);
    }
    return new ModifiablePasswordPolicyStateJSON(jsonObject);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONString(com.unboundid.util.json.JSONString) LDAPException(com.unboundid.ldap.sdk.LDAPException) Nullable(com.unboundid.util.Nullable)

Example 53 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ReplaceWithCounterTransformation method transformEntry.

/**
 * {@inheritDoc}
 */
@Override()
@Nullable()
public Entry transformEntry(@NotNull final Entry e) {
    if (e == null) {
        return null;
    }
    // See if the DN contains the target attribute in the RDN.  If so, then
    // replace its value.
    String dn = e.getDN();
    String newValue = null;
    if (replaceInRDN) {
        try {
            final DN parsedDN = new DN(dn);
            final RDN rdn = parsedDN.getRDN();
            for (final String name : names) {
                if (rdn.hasAttribute(name)) {
                    newValue = beforeText + counter.getAndAdd(incrementAmount) + afterText;
                    break;
                }
            }
            if (newValue != null) {
                if (rdn.isMultiValued()) {
                    final String[] attrNames = rdn.getAttributeNames();
                    final byte[][] originalValues = rdn.getByteArrayAttributeValues();
                    final byte[][] newValues = new byte[originalValues.length][];
                    for (int i = 0; i < attrNames.length; i++) {
                        if (names.contains(StaticUtils.toLowerCase(attrNames[i]))) {
                            newValues[i] = StaticUtils.getBytes(newValue);
                        } else {
                            newValues[i] = originalValues[i];
                        }
                    }
                    dn = new DN(new RDN(attrNames, newValues, schema), parsedDN.getParent()).toString();
                } else {
                    dn = new DN(new RDN(rdn.getAttributeNames()[0], newValue, schema), parsedDN.getParent()).toString();
                }
            }
        } catch (final Exception ex) {
            Debug.debugException(ex);
        }
    }
    // entry.
    if (newValue == null) {
        boolean hasAttribute = false;
        for (final String name : names) {
            if (e.hasAttribute(name)) {
                hasAttribute = true;
                break;
            }
        }
        if (!hasAttribute) {
            return e;
        }
    }
    // If we haven't computed the new value for this entry, then do so now.
    if (newValue == null) {
        newValue = beforeText + counter.getAndAdd(incrementAmount) + afterText;
    }
    // Iterate through the attributes in the entry and make the appropriate
    // updates.
    final Collection<Attribute> originalAttributes = e.getAttributes();
    final ArrayList<Attribute> updatedAttributes = new ArrayList<>(originalAttributes.size());
    for (final Attribute a : originalAttributes) {
        if (names.contains(StaticUtils.toLowerCase(a.getBaseName()))) {
            updatedAttributes.add(new Attribute(a.getName(), schema, newValue));
        } else {
            updatedAttributes.add(a);
        }
    }
    // Return the updated entry.
    return new Entry(dn, schema, updatedAttributes);
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) RDN(com.unboundid.ldap.sdk.RDN) DN(com.unboundid.ldap.sdk.DN) Entry(com.unboundid.ldap.sdk.Entry) RDN(com.unboundid.ldap.sdk.RDN) Nullable(com.unboundid.util.Nullable)

Example 54 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ScrambleAttributeTransformation method scrambleNumericValue.

/**
 * Scrambles the provided value, which is expected to be largely numeric.
 * Only digits will be scrambled, with all other characters left intact.
 * The first digit will be required to be nonzero unless it is also the last
 * character of the string.
 *
 * @param  s  The value to scramble.
 *
 * @return  The scrambled value.
 */
@Nullable()
public String scrambleNumericValue(@Nullable final String s) {
    if (s == null) {
        return null;
    }
    // Scramble all digits in the value, leaving all non-digits intact.
    int firstDigitPos = -1;
    boolean multipleDigits = false;
    final char[] chars = s.toCharArray();
    final Random random = getRandom(s);
    final StringBuilder scrambledValue = new StringBuilder(s.length());
    for (int i = 0; i < chars.length; i++) {
        final char c = chars[i];
        if ((c >= '0') && (c <= '9')) {
            scrambledValue.append(random.nextInt(10));
            if (firstDigitPos < 0) {
                firstDigitPos = i;
            } else {
                multipleDigits = true;
            }
        } else {
            scrambledValue.append(c);
        }
    }
    // string.
    if (firstDigitPos < 0) {
        return scrambleString(s);
    }
    // nonzero.
    if (multipleDigits && (scrambledValue.charAt(firstDigitPos) == '0')) {
        scrambledValue.setCharAt(firstDigitPos, (char) (random.nextInt(9) + (int) '1'));
    }
    return scrambledValue.toString();
}
Also used : Random(java.util.Random) ThreadLocalRandom(com.unboundid.util.ThreadLocalRandom) Nullable(com.unboundid.util.Nullable)

Example 55 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ScrambleAttributeTransformation method scrambleString.

/**
 * Scrambles the provided string.  The scrambling will be performed as
 * follows:
 * <UL>
 *   <LI>
 *     Each lowercase ASCII letter will be replaced with a randomly-selected
 *     lowercase ASCII letter.
 *   </LI>
 *   <LI>
 *     Each uppercase ASCII letter will be replaced with a randomly-selected
 *     uppercase ASCII letter.
 *   </LI>
 *   <LI>
 *     Each ASCII digit will be replaced with a randomly-selected ASCII digit.
 *   </LI>
 *   <LI>
 *     All other characters will remain unchanged.
 *   <LI>
 * </UL>
 *
 * @param  s  The value to scramble.
 *
 * @return  The scrambled value.
 */
@Nullable()
public String scrambleString(@Nullable final String s) {
    if (s == null) {
        return null;
    }
    final Random random = getRandom(s);
    final StringBuilder scrambledString = new StringBuilder(s.length());
    for (final char c : s.toCharArray()) {
        if ((c >= 'a') && (c <= 'z')) {
            scrambledString.append(randomCharacter(LOWERCASE_ASCII_LETTERS, random));
        } else if ((c >= 'A') && (c <= 'Z')) {
            scrambledString.append(randomCharacter(UPPERCASE_ASCII_LETTERS, random));
        } else if ((c >= '0') && (c <= '9')) {
            scrambledString.append(randomCharacter(ASCII_DIGITS, random));
        } else {
            scrambledString.append(c);
        }
    }
    return scrambledString.toString();
}
Also used : Random(java.util.Random) ThreadLocalRandom(com.unboundid.util.ThreadLocalRandom) Nullable(com.unboundid.util.Nullable)

Aggregations

Nullable (com.unboundid.util.Nullable)149 ArrayList (java.util.ArrayList)47 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)42 Entry (com.unboundid.ldap.sdk.Entry)30 LDAPException (com.unboundid.ldap.sdk.LDAPException)30 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)21 Attribute (com.unboundid.ldap.sdk.Attribute)21 ASN1Element (com.unboundid.asn1.ASN1Element)20 Filter (com.unboundid.ldap.sdk.Filter)20 SearchResult (com.unboundid.ldap.sdk.SearchResult)18 IOException (java.io.IOException)16 ReadOnlyEntry (com.unboundid.ldap.sdk.ReadOnlyEntry)14 File (java.io.File)14 DN (com.unboundid.ldap.sdk.DN)12 ArgumentException (com.unboundid.util.args.ArgumentException)10 RDN (com.unboundid.ldap.sdk.RDN)9 LDIFException (com.unboundid.ldif.LDIFException)8 ChangeLogEntry (com.unboundid.ldap.sdk.ChangeLogEntry)7 Modification (com.unboundid.ldap.sdk.Modification)7 LDIFModifyChangeRecord (com.unboundid.ldif.LDIFModifyChangeRecord)7