use of org.ldaptive.LdapAttribute in project cas by apereo.
the class OptionalWarningLdapLdapAccountStateHandler method handleWarning.
@Override
protected void handleWarning(final AccountState.Warning warning, final AuthenticationResponse response, final LdapPasswordPolicyConfiguration configuration, final List<MessageDescriptor> messages) {
if (StringUtils.isBlank(this.warnAttributeName)) {
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.warnAttributeName);
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, this.displayWarningOnMatch);
if (this.displayWarningOnMatch == matches) {
super.handleWarning(warning, response, configuration, messages);
}
}
use of org.ldaptive.LdapAttribute in project cas by apereo.
the class LdaptiveResourceCRLFetcher method fetchCRLFromLdap.
/**
* Downloads a CRL from given LDAP url.
*
* @param r the resource that is the ldap url.
* @return the x 509 cRL
* @throws IOException the exception thrown if resources cant be fetched
* @throws CRLException the exception thrown if resources cant be fetched
* @throws CertificateException if connection to ldap fails, or attribute to get the revocation list is unavailable
*/
protected X509CRL fetchCRLFromLdap(final Object r) throws CertificateException, IOException, CRLException {
try {
final String ldapURL = r.toString();
LOGGER.debug("Fetching CRL from ldap [{}]", ldapURL);
final Response<SearchResult> result = performLdapSearch(ldapURL);
if (result.getResultCode() == ResultCode.SUCCESS) {
final LdapEntry entry = result.getResult().getEntry();
final LdapAttribute attribute = entry.getAttribute(this.certificateAttribute);
if (attribute.isBinary()) {
LOGGER.debug("Located entry [{}]. Retrieving first attribute [{}]", entry, attribute);
return fetchX509CRLFromAttribute(attribute);
}
LOGGER.warn("Found certificate attribute [{}] but it is not marked as a binary attribute", this.certificateAttribute);
}
LOGGER.debug("Failed to execute the search [{}]", result);
throw new CertificateException("Failed to establish a connection ldap and search.");
} catch (final LdapException e) {
LOGGER.error(e.getMessage(), e);
throw new CertificateException(e.getMessage());
}
}
use of org.ldaptive.LdapAttribute in project cas by apereo.
the class LdapUtils method getString.
/**
* 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, StandardCharsets.UTF_8);
} else {
v = attr.getStringValue();
}
if (StringUtils.isNotBlank(v)) {
return v;
}
return nullValue;
}
use of org.ldaptive.LdapAttribute in project cas by apereo.
the class LdapUtils method executeModifyOperation.
/**
* Execute modify operation boolean.
*
* @param currentDn the current dn
* @param connectionFactory the connection factory
* @param attributes the attributes
* @return true/false
*/
public static boolean executeModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final Map<String, Set<String>> attributes) {
try (Connection modifyConnection = createConnection(connectionFactory)) {
final ModifyOperation operation = new ModifyOperation(modifyConnection);
final List<AttributeModification> mods = attributes.entrySet().stream().map(entry -> new AttributeModification(AttributeModificationType.REPLACE, new LdapAttribute(entry.getKey(), entry.getValue().toArray(new String[] {})))).collect(Collectors.toList());
final ModifyRequest request = new ModifyRequest(currentDn, mods.toArray(new AttributeModification[] {}));
request.setReferralHandler(new ModifyReferralHandler());
operation.execute(request);
return true;
} catch (final LdapException e) {
LOGGER.error(e.getMessage(), e);
}
return false;
}
use of org.ldaptive.LdapAttribute in project cas by apereo.
the class OptionalWarningLdapLdapAccountStateHandlerTests method verifyNoWarningOnMatch.
@Test
public void verifyNoWarningOnMatch() {
final OptionalWarningLdapLdapAccountStateHandler h = new OptionalWarningLdapLdapAccountStateHandler();
h.setWarnAttributeName("attribute");
h.setWarningAttributeValue("value");
h.setDisplayWarningOnMatch(false);
final AuthenticationResponse response = mock(AuthenticationResponse.class);
final LdapEntry entry = mock(LdapEntry.class);
when(response.getLdapEntry()).thenReturn(entry);
when(entry.getAttribute(anyString())).thenReturn(new LdapAttribute("attribute", "value"));
final List<MessageDescriptor> messages = new ArrayList<>();
final LdapPasswordPolicyConfiguration config = new LdapPasswordPolicyConfiguration();
config.setPasswordWarningNumberOfDays(5);
h.handleWarning(new AccountState.DefaultWarning(ZonedDateTime.now(), 1), response, config, messages);
assertEquals(0, messages.size());
}
Aggregations