/** {@inheritDoc} */
 @Override
 protected void processAttributes(final SearchRequest request, final LdapEntry entry)
     throws LdapException {
   if (entry.getAttribute(dnAttributeName) == null) {
     entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn()));
   } else if (addIfExists) {
     entry.getAttribute(dnAttributeName).addStringValue(entry.getDn());
   }
 }
 @Test
 public void verifyAuthenticateSuccess() throws Exception {
   for (final LdapEntry entry : this.getEntries()) {
     final String username = getUsername(entry);
     final String psw = entry.getAttribute("userPassword").getStringValue();
     final HandlerResult result =
         this.handler.authenticate(new UsernamePasswordCredential(username, psw));
     assertNotNull(result.getPrincipal());
     assertEquals(username, result.getPrincipal().getId());
     assertEquals(
         entry.getAttribute("displayName").getStringValue(),
         result.getPrincipal().getAttributes().get("displayName"));
     assertEquals(
         entry.getAttribute("mail").getStringValue(),
         result.getPrincipal().getAttributes().get("mail"));
   }
 }
  /**
   * Recursively gets the attribute(s) {@link #mergeAttributes} for the supplied dn and adds the
   * values to the supplied attributes.
   *
   * @param conn to perform search operation on
   * @param dn to get attribute(s) for
   * @param entry to merge with
   * @param searchedDns list of DNs that have been searched for
   * @throws LdapException if a search error occurs
   */
  private void recursiveSearch(
      final Connection conn, final String dn, final LdapEntry entry, final List<String> searchedDns)
      throws LdapException {
    if (!searchedDns.contains(dn)) {

      LdapEntry newEntry = null;
      try {
        final SearchOperation search = new SearchOperation(conn);
        final SearchRequest sr = SearchRequest.newObjectScopeSearchRequest(dn, retAttrs);
        final SearchResult result = search.execute(sr).getResult();
        newEntry = result.getEntry(dn);
      } catch (LdapException e) {
        logger.warn("Error retrieving attribute(s): {}", Arrays.toString(retAttrs), e);
      }
      searchedDns.add(dn);

      if (newEntry != null) {
        // recursively search new attributes
        readSearchAttribute(conn, newEntry, searchedDns);

        // merge new attribute values
        for (String s : mergeAttributes) {
          final LdapAttribute newAttr = newEntry.getAttribute(s);
          if (newAttr != null) {
            final LdapAttribute oldAttr = entry.getAttribute(s);
            if (oldAttr == null) {
              entry.addAttribute(newAttr);
            } else {
              if (newAttr.isBinary()) {
                for (byte[] value : newAttr.getBinaryValues()) {
                  oldAttr.addBinaryValue(value);
                }
              } else {
                for (String value : newAttr.getStringValues()) {
                  oldAttr.addStringValue(value);
                }
              }
            }
          }
        }
      }
    }
  }
 /**
  * Reads the values of {@link #searchAttribute} from the supplied attributes and calls {@link
  * #recursiveSearch} for each.
  *
  * @param conn to perform search operation on
  * @param entry to read
  * @param searchedDns list of DNs whose attributes have been read
  * @throws LdapException if a search error occurs
  */
 private void readSearchAttribute(
     final Connection conn, final LdapEntry entry, final List<String> searchedDns)
     throws LdapException {
   if (entry != null) {
     final LdapAttribute attr = entry.getAttribute(searchAttribute);
     if (attr != null && !attr.isBinary()) {
       for (String s : attr.getStringValues()) {
         recursiveSearch(conn, s, entry, searchedDns);
       }
     }
   }
 }
Exemple #5
0
  /**
   * Reads a String value from the LdapEntry.
   *
   * @param entry the ldap entry
   * @param attribute the attribute name
   * @param nullValue the value which should be returning in case of a null value
   * @return the string
   */
  public static String getString(
      final LdapEntry entry, final String attribute, final String nullValue) {
    final LdapAttribute attr = entry.getAttribute(attribute);
    if (attr == null) {
      return nullValue;
    }

    String v = null;
    if (attr.isBinary()) {
      final byte[] b = attr.getBinaryValue();
      v = new String(b, Charset.forName("UTF-8"));
    } else {
      v = attr.getStringValue();
    }

    if (StringUtils.isNotBlank(v)) {
      return v;
    }
    return nullValue;
  }
  /** {@inheritDoc} */
  @Override
  protected void processAttributes(
      final Connection conn, final SearchRequest request, final LdapEntry entry)
      throws LdapException {
    final Map<LdapAttribute, Matcher> matchingAttrs = new HashMap<LdapAttribute, Matcher>();
    for (LdapAttribute la : entry.getAttributes()) {
      // Match attribute ID against the pattern
      final Matcher matcher = RANGE_PATTERN.matcher(la.getName());

      // If the attribute ID matches the pattern
      if (matcher.find()) {
        matchingAttrs.put(la, matcher);
      }
    }

    for (Map.Entry<LdapAttribute, Matcher> mEntry : matchingAttrs.entrySet()) {
      final LdapAttribute la = mEntry.getKey();
      final Matcher matcher = mEntry.getValue();
      final String msg = String.format("attribute '%s' entry '%s'", la.getName(), entry.getDn());

      // Determine the attribute name without the range syntax
      final String attrTypeName = matcher.group(1);
      logger.debug("Found Range option {}", msg);
      if (attrTypeName == null || attrTypeName.isEmpty()) {
        logger.error("Unable to determine the attribute type name for {}", msg);
        throw new IllegalArgumentException(
            "Unable to determine the attribute type name for " + msg);
      }

      // Create or update the attribute whose ID has the range syntax removed
      LdapAttribute newAttr = entry.getAttribute(attrTypeName);
      if (newAttr == null) {
        newAttr = new LdapAttribute(la.getSortBehavior(), la.isBinary());
        newAttr.setName(attrTypeName);
        entry.addAttribute(newAttr);
      }

      // Copy values
      if (la.isBinary()) {
        newAttr.addBinaryValues(la.getBinaryValues());
      } else {
        newAttr.addStringValues(la.getStringValues());
      }

      // Remove original attribute with range syntax from returned attributes
      entry.removeAttribute(la);

      // If the attribute ID ends with * we're done, otherwise increment
      if (!la.getName().endsWith(END_OF_RANGE)) {

        // Determine next attribute ID
        // CheckStyle:MagicNumber OFF
        final int start = Integer.parseInt(matcher.group(2));
        final int end = Integer.parseInt(matcher.group(3));
        // CheckStyle:MagicNumber ON
        final int diff = end - start;
        final String nextAttrID =
            String.format(RANGE_FORMAT, attrTypeName, end + 1, end + diff + 1);

        // Search for next increment of values
        logger.debug("Searching for '{}' to increment {}", nextAttrID, msg);

        final SearchOperation search = new SearchOperation(conn);
        final SearchRequest sr =
            SearchRequest.newObjectScopeSearchRequest(entry.getDn(), new String[] {nextAttrID});
        final SearchResult result = search.execute(sr).getResult();

        // Add all attributes to the search result
        entry.addAttributes(result.getEntry().getAttributes());

        // Iterate
        processAttributes(conn, request, entry);
      }
    }
  }