/** * Verify spnegy attribute value. * This impl simply makes sure the attribute exists and has a value. * @param attribute the ldap attribute * @return true if available. false otherwise. */ protected boolean verifySpnegyAttributeValue(final LdapAttribute attribute) { return attribute != null && StringUtils.isNotBlank(attribute.getStringValue()); } }
protected Map<String, Object> getAttributesFromEntry(final LdapEntry entry) { final Map<String, Object> attributes = new HashMap<>(); for (final LdapAttribute attribute : entry.getAttributes()) { final String name = attribute.getName(); if (attribute.size() > 1) { attributes.put(name, attribute.getStringValues()); } else { attributes.put(name, attribute.getStringValue()); } } return attributes; }
private List<String> getUserRoles(LdapEntry ldapEntry) { Set<String> roles = new HashSet(); if (roleMapper != null && roleMapper.getRoles() != null) { roleMapper.getRoles().forEach((role, users) -> { Arrays.asList(users).forEach(u -> { // user/group have the following syntax userAttribute=userValue String[] attributes = u.split("="); String userAttribute = attributes[0]; String userValue = attributes[1]; // group if (MEMBEROF_ATTRIBUTE.equals(userAttribute) && ldapEntry.getAttribute(MEMBEROF_ATTRIBUTE) != null) { if (ldapEntry.getAttribute(MEMBEROF_ATTRIBUTE).getStringValues().contains(userValue)) { roles.add(role); } // user } else { if (ldapEntry.getAttribute(userAttribute) != null && ldapEntry.getAttribute(userAttribute).getStringValue().equals(userValue)) { roles.add(role); } } }); }); } return new ArrayList<>(roles); } }
private static String attributeStringValue(final LdapEntry entry, final String ldapAttribute) { final LdapAttribute attribute = entry.getAttribute(ldapAttribute); if (attribute == null) { return ""; } return attribute.getStringValue(); }
/** Tests create with one value. */ @Test(groups = {"bean"}) public void createOne() { final LdapAttribute la = new LdapAttribute("givenName", "William"); AssertJUnit.assertEquals("William", la.getStringValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals("William", la.getStringValues().iterator().next()); AssertJUnit.assertTrue(Arrays.equals("William".getBytes(), la.getBinaryValue())); AssertJUnit.assertEquals(1, la.size()); AssertJUnit.assertEquals(la, new LdapAttribute("givenName", "William")); try { la.addStringValue((String) null); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } try { la.addBinaryValue("Bill".getBytes()); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** * Returns a single value for the first attribute name found in the supplied entry. * * @param entry containing the attributes * @param names to search for in the entry * * @return single attribute value */ private static String getAttributeValue(final LdapEntry entry, final String... names) { String value = null; for (String name : names) { final LdapAttribute la = entry.getAttribute(name); if (la != null) { value = la.getStringValue(); break; } } return value; }
/** Tests various binary input. */ @Test(groups = {"bean"}) public void binaryValue() { final LdapAttribute la = new LdapAttribute("jpegPhoto", "image".getBytes()); AssertJUnit.assertTrue(Arrays.equals("image".getBytes(), la.getBinaryValue())); AssertJUnit.assertEquals(1, la.getBinaryValues().size()); AssertJUnit.assertEquals("aW1hZ2U=", la.getStringValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals(la, new LdapAttribute("jpegPhoto", "image".getBytes())); try { la.addBinaryValue((byte[][]) null); AssertJUnit.fail("Should have thrown NullPointerException"); } catch (Exception e) { AssertJUnit.assertEquals(NullPointerException.class, e.getClass()); } try { la.addStringValue("Bill"); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** * Returns a single value for the first attribute name found in the supplied entry. * * @param entry containing the attributes * @param names to search for in the entry * * @return single attribute value */ private static String getAttributeValue(final LdapEntry entry, final String... names) { String value = null; for (String name : names) { final LdapAttribute la = entry.getAttribute(name); if (la != null) { value = la.getStringValue(); break; } } return value; }
/** Tests various string input. */ @Test(groups = {"bean"}) public void stringValue() { final LdapAttribute la = new LdapAttribute("cn", "William Wallace"); AssertJUnit.assertEquals("William Wallace", la.getStringValue()); AssertJUnit.assertEquals("William Wallace".getBytes(StandardCharsets.UTF_8), la.getBinaryValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals(1, la.getBinaryValues().size()); AssertJUnit.assertEquals(la, new LdapAttribute("cn", "William Wallace")); try { la.addStringValue((String[]) null); AssertJUnit.fail("Should have thrown NullPointerException"); } catch (Exception e) { AssertJUnit.assertEquals(NullPointerException.class, e.getClass()); } try { la.addBinaryValue("Bill".getBytes()); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** * Returns a single value for the first attribute name found in the supplied entry. * * @param entry containing the attributes * @param names to search for in the entry * * @return single attribute value */ private static String getAttributeValue(final LdapEntry entry, final String... names) { String value = null; for (String name : names) { final LdapAttribute la = entry.getAttribute(name); if (la != null) { value = la.getStringValue(); break; } } return value; }
private Map<String, Object> addClaim(Map<String, Object> claims, LdapEntry ldapEntry, String claimKey, String attributeKey) { if (ldapEntry.getAttribute(attributeKey) != null) { claims.put(claimKey, ldapEntry.getAttribute(attributeKey).getStringValue()); } return claims; }
@Override protected void handleWarning( final AccountState.Warning warning, final AuthenticationResponse response, final LdapPasswordPolicyConfiguration configuration, final List<MessageDescriptor> messages) { if (StringUtils.isBlank(this.warningAttributeName)) { logger.debug("No warning attribute name is defined"); return; } if (StringUtils.isBlank(this.warningAttributeValue)) { logger.debug("No warning attribute value to match is defined"); return; } final LdapAttribute attribute = response.getLdapEntry().getAttribute(this.warningAttributeName); boolean matches = false; if (attribute != null) { logger.debug("Found warning attribute {} with value {}", attribute.getName(), attribute.getStringValue()); matches = this.warningAttributeValue.equals(attribute.getStringValue()); } logger.debug("matches={}, displayWarningOnMatch={}", matches, displayWarningOnMatch); if (displayWarningOnMatch == matches) { super.handleWarning(warning, response, configuration, messages); } } }
/** * 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; } final String v; 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 @Nullable public StorageRecord read(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key) throws IOException { SearchResult result = null; try { result = search(context, key).getResult(); } catch (LdapException e) { if (e.getResultCode() != ResultCode.NO_SUCH_OBJECT) { log.error("LDAP search operation failed", e); throw new IOException(e); } } StorageRecord record = null; if (result != null && result.size() > 0) { final LdapEntry entry = result.getEntry(); if (entry != null) { final LdapAttribute attr = entry.getAttribute(key); if (attr != null) { record = new StorageRecord(attr.getStringValue(), null); } } } return record; }
/** * Creates a new schema. The subschema subentry is searched for on the root DSE, followed by searching for the * subschema entry itself. * * @param factory to obtain an LDAP connection from * * @return schema created from the connection factory * * @throws LdapException if the search fails */ public static Schema createSchema(final ConnectionFactory factory) throws LdapException { final LdapEntry rootDSE = getLdapEntry(factory, "", "(objectClass=*)", new String[] {SUBSCHEMA_SUBENTRY_ATTR_NAME}); final String entryDn = rootDSE.getAttribute(SUBSCHEMA_SUBENTRY_ATTR_NAME).getStringValue(); return createSchema(getLdapEntry(factory, entryDn, "(objectClass=subSchema)", ReturnAttributes.ALL.value())); }
/** * Creates a new schema. The subschema subentry is searched for on the root DSE, followed by searching for the * subschema entry itself. * * @param factory to obtain an LDAP connection from * * @return schema created from the connection factory * * @throws LdapException if the search fails */ public static Schema createSchema(final ConnectionFactory factory) throws LdapException { final LdapEntry rootDSE = getLdapEntry(factory, "", "(objectClass=*)", new String[] {SUBSCHEMA_SUBENTRY_ATTR_NAME}); final String entryDn = rootDSE.getAttribute(SUBSCHEMA_SUBENTRY_ATTR_NAME).getStringValue(); return createSchema(getLdapEntry(factory, entryDn, "(objectClass=subSchema)", ReturnAttributes.ALL.value())); }
/** * Creates a new schema. The subschema subentry is searched for on the root DSE, followed by searching for the * subschema entry itself. * * @param factory to obtain an LDAP connection from * * @return schema created from the connection factory * * @throws LdapException if the search fails */ public static Schema createSchema(final ConnectionFactory factory) throws LdapException { final LdapEntry rootDSE = getLdapEntry(factory, "", "(objectClass=*)", new String[] {SUBSCHEMA_SUBENTRY_ATTR_NAME}); final String entryDn = rootDSE.getAttribute(SUBSCHEMA_SUBENTRY_ATTR_NAME).getStringValue(); return createSchema(getLdapEntry(factory, entryDn, "(objectClass=subSchema)", ReturnAttributes.ALL.value())); }
@Override public void handle(final AuthenticationResponse response) { if (response.getMessage() != null) { final EDirectoryAccountState.Error edError = EDirectoryAccountState.Error.parse(response.getMessage()); if (edError != null) { response.setAccountState(new EDirectoryAccountState(edError)); } } else if (response.getResult()) { final LdapEntry entry = response.getLdapEntry(); final LdapAttribute expTime = entry.getAttribute("passwordExpirationTime"); final LdapAttribute loginRemaining = entry.getAttribute("loginGraceRemaining"); final int loginRemainingValue = loginRemaining != null ? Integer.parseInt(loginRemaining.getStringValue()) : 0; if (expTime != null) { final ZonedDateTime exp = expTime.getValue(new GeneralizedTimeValueTranscoder()); if (warningPeriod != null) { final ZonedDateTime warn = exp.minus(warningPeriod); if (ZonedDateTime.now().isAfter(warn)) { response.setAccountState(new EDirectoryAccountState(exp, loginRemainingValue)); } } else { response.setAccountState(new EDirectoryAccountState(exp, loginRemainingValue)); } } else if (loginRemaining != null) { response.setAccountState(new EDirectoryAccountState(null, loginRemainingValue)); } } }
/** Tests default sort behavior. */ @Test(groups = {"bean"}) public void defaultSortBehavior() { final LdapAttribute la = new LdapAttribute("givenName"); AssertJUnit.assertEquals(SortBehavior.getDefaultSortBehavior(), la.getSortBehavior()); AssertJUnit.assertEquals(0, la.size()); AssertJUnit.assertNull(la.getStringValue()); AssertJUnit.assertNull(la.getBinaryValue()); AssertJUnit.assertEquals("givenName", la.getName()); la.setName("sn"); AssertJUnit.assertEquals("sn", la.getName()); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
@Override public Response<Boolean> compare(final CompareRequest request) throws LdapException { Response<Boolean> response = null; try { final CompareRequestImpl cri = new CompareRequestImpl(); if (request.getControls() != null) { cri.addAllControls(config.getControlProcessor().processRequestControls(request.getControls())); } cri.setName(new Dn(request.getDn())); cri.setAttributeId(request.getAttribute().getName()); if (request.getAttribute().isBinary()) { cri.setAssertionValue(request.getAttribute().getBinaryValue()); } else { cri.setAssertionValue(request.getAttribute().getStringValue()); } final CompareResponse cr = connection.compare(cri); throwOperationException(request, cr); response = createResponse(request, cr.isTrue(), cr); } catch (LdapOperationException e) { processLdapOperationException(e); } catch (org.apache.directory.api.ldap.model.exception.LdapException e) { processLdapException(e); } return response; }