use of org.ldaptive.LdapEntry in project cas by apereo.
the class LdapServiceRegistry method insert.
private RegisteredService insert(final RegisteredService rs) {
try {
final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
LdapUtils.executeAddOperation(this.connectionFactory, entry);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return rs;
}
use of org.ldaptive.LdapEntry in project cas by apereo.
the class LdapServiceRegistry method update.
/**
* Update the ldap entry with the given registered service.
*
* @param rs the rs
* @return the registered service
*/
private RegisteredService update(final RegisteredService rs) {
String currentDn = null;
try {
final Response<SearchResult> response = searchForServiceById(rs.getId());
if (LdapUtils.containsResultEntry(response)) {
currentDn = response.getResult().getEntry().getDn();
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
if (StringUtils.isNotBlank(currentDn)) {
LOGGER.debug("Updating registered service at [{}]", currentDn);
final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
LdapUtils.executeModifyOperation(currentDn, this.connectionFactory, entry);
} else {
LOGGER.debug("Failed to locate DN for registered service by id [{}]. Attempting to save the service anew", rs.getId());
insert(rs);
}
return rs;
}
use of org.ldaptive.LdapEntry in project cas by apereo.
the class LdapTestUtils method createLdapEntries.
/**
* Creates the given LDAP entries.
*
* @param connection Open LDAP connection used to connect to directory.
* @param entries Collection of LDAP entries.
*/
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) {
try {
for (final LdapEntry entry : entries) {
final Collection<Attribute> attrs = new ArrayList<>(entry.getAttributeNames().length);
attrs.addAll(entry.getAttributes().stream().map(a -> new Attribute(a.getName(), a.getStringValues())).collect(Collectors.toList()));
final AddRequest ad = new AddRequest(entry.getDn(), attrs);
connection.add(ad);
}
} catch (final Exception e) {
LOGGER.error(e.getLocalizedMessage());
}
}
use of org.ldaptive.LdapEntry in project cas by apereo.
the class SurrogateLdapAuthenticationService method getEligibleAccountsForSurrogateToProxy.
@Override
public Collection<String> getEligibleAccountsForSurrogateToProxy(final String username) {
final Collection<String> eligible = new LinkedHashSet<>();
try {
final SearchFilter filter = LdapUtils.newLdaptiveSearchFilter(ldapProperties.getSearchFilter(), CollectionUtils.wrap(username));
LOGGER.debug("Using search filter: [{}]", filter);
final Response<SearchResult> response = LdapUtils.executeSearchOperation(this.connectionFactory, ldapProperties.getBaseDn(), filter);
LOGGER.debug("LDAP response: [{}]", response);
if (!LdapUtils.containsResultEntry(response)) {
return eligible;
}
final LdapEntry ldapEntry = response.getResult().getEntry();
final LdapAttribute attribute = ldapEntry.getAttribute(ldapProperties.getMemberAttributeName());
if (attribute == null || attribute.getStringValues().isEmpty()) {
return eligible;
}
final Pattern pattern = RegexUtils.createPattern(ldapProperties.getMemberAttributeValueRegex());
eligible.addAll(attribute.getStringValues().stream().map(pattern::matcher).filter(Matcher::matches).map(p -> p.group(1)).collect(Collectors.toList()));
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return eligible;
}
use of org.ldaptive.LdapEntry in project cas by apereo.
the class LdapConsentRepository method findConsentDecision.
@Override
public ConsentDecision findConsentDecision(final Service service, final RegisteredService registeredService, final Authentication authentication) {
final String principal = authentication.getPrincipal().getId();
final LdapEntry entry = readConsentEntry(principal);
if (entry != null) {
final LdapAttribute consentDecisions = entry.getAttribute(this.ldap.getConsentAttributeName());
if (consentDecisions != null) {
final Collection<String> values = consentDecisions.getStringValues();
LOGGER.debug("Locating consent decision(s) for [{}] and service [{}]", principal, service.getId());
return values.stream().map(LdapConsentRepository::mapFromJson).filter(d -> d.getService().equals(service.getId())).findFirst().orElse(null);
}
}
return null;
}
Aggregations