use of org.springframework.ldap.filter.EqualsFilter in project ddf by codice.
the class LdapClaimsHandler method retrieveClaims.
@Override
public ClaimsCollection retrieveClaims(ClaimsParameters parameters) {
Principal principal = parameters.getPrincipal();
String user = attributeMapLoader.getUser(principal);
if (user == null) {
LOGGER.info("Could not determine user name, possible authentication error. Returning no claims.");
return new ClaimsCollectionImpl();
}
ClaimsCollection claimsColl = new ClaimsCollectionImpl();
Connection connection = null;
try {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", this.getObjectClass())).and(new EqualsFilter(this.getUserNameAttribute(), user));
List<String> searchAttributeList = new ArrayList<String>();
for (Map.Entry<String, String> claimEntry : getClaimsLdapAttributeMapping().entrySet()) {
searchAttributeList.add(claimEntry.getValue());
}
String[] searchAttributes = null;
searchAttributes = searchAttributeList.toArray(new String[searchAttributeList.size()]);
connection = connectionFactory.getConnection();
if (connection != null) {
BindRequest request = selectBindMethod();
BindResult bindResult = connection.bind(request);
if (bindResult.isSuccess()) {
String baseDN = attributeMapLoader.getBaseDN(principal, getUserBaseDN(), overrideCertDn);
LOGGER.trace("Executing ldap search with base dn of {} and filter of {}", baseDN, filter);
ConnectionEntryReader entryReader = connection.search(baseDN, SearchScope.WHOLE_SUBTREE, filter.toString(), searchAttributes);
SearchResultEntry entry;
while (entryReader.hasNext()) {
if (entryReader.isEntry()) {
entry = entryReader.readEntry();
for (Map.Entry<String, String> claimEntry : getClaimsLdapAttributeMapping().entrySet()) {
String claimType = claimEntry.getKey();
String ldapAttribute = claimEntry.getValue();
Attribute attr = entry.getAttribute(ldapAttribute);
if (attr == null) {
LOGGER.trace("Claim '{}' is null", claimType);
} else {
Claim claim = new ClaimImpl(claimType);
for (ByteString value : attr) {
String itemValue = value.toString();
if (this.isX500FilterEnabled()) {
try {
X500Principal x500p = new X500Principal(itemValue);
itemValue = x500p.getName();
int index = itemValue.indexOf('=');
itemValue = itemValue.substring(index + 1, itemValue.indexOf(',', index));
} catch (Exception ex) {
// Ignore, not X500 compliant thus use the whole
// string as the value
LOGGER.debug("Not X500 compliant", ex);
}
}
claim.addValue(itemValue);
}
claimsColl.add(claim);
}
}
} else {
// Got a continuation reference
LOGGER.debug("Referral ignored while searching for user {}", user);
entryReader.readReference();
}
}
} else {
LOGGER.info("LDAP Connection failed.");
}
}
} catch (LdapException e) {
LOGGER.info("Cannot connect to server, therefore unable to set user attributes. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information");
LOGGER.debug("Cannot connect to server, therefore unable to set user attributes.", e);
} catch (SearchResultReferenceIOException e) {
LOGGER.info("Unable to set user attributes. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information");
LOGGER.debug("Unable to set user attributes.", e);
} finally {
if (connection != null) {
connection.close();
}
}
return claimsColl;
}
use of org.springframework.ldap.filter.EqualsFilter in project ddf by codice.
the class RoleClaimsHandler method retrieveClaims.
@Override
public ClaimsCollection retrieveClaims(ClaimsParameters parameters) {
String[] attributes = { groupNameAttribute, memberNameAttribute };
ClaimsCollection claimsColl = new ClaimsCollectionImpl();
Connection connection = null;
try {
Principal principal = parameters.getPrincipal();
String user = attributeMapLoader.getUser(principal);
if (user == null) {
LOGGER.info("Could not determine user name, possible authentication error. Returning no claims.");
return new ClaimsCollectionImpl();
}
connection = connectionFactory.getConnection();
if (connection != null) {
BindRequest request = BindMethodChooser.selectBindMethod(bindMethod, bindUserDN, bindUserCredentials, kerberosRealm, kdcAddress);
BindResult bindResult = connection.bind(request);
String membershipValue = user;
String baseDN = attributeMapLoader.getBaseDN(principal, userBaseDn, overrideCertDn);
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter(this.getLoginUserAttribute(), user));
ConnectionEntryReader entryReader = connection.search(baseDN, SearchScope.WHOLE_SUBTREE, filter.toString(), membershipUserAttribute);
String userDN = String.format("%s=%s,%s", loginUserAttribute, user, baseDN);
String specificUserBaseDN = baseDN;
while (entryReader.hasNext()) {
if (entryReader.isEntry()) {
SearchResultEntry entry = entryReader.readEntry();
userDN = entry.getName().toString();
specificUserBaseDN = userDN.substring(userDN.indexOf(',') + 1);
if (!membershipUserAttribute.equals(loginUserAttribute)) {
Attribute attr = entry.getAttribute(membershipUserAttribute);
if (attr != null) {
for (ByteString value : attr) {
membershipValue = value.toString();
}
}
}
} else {
// Got a continuation reference
LOGGER.debug("Referral ignored while searching for user {}", user);
entryReader.readReference();
}
}
filter = new AndFilter();
filter.and(new EqualsFilter("objectClass", getObjectClass())).and(new OrFilter().or(new EqualsFilter(getMemberNameAttribute(), getMembershipUserAttribute() + "=" + membershipValue + "," + specificUserBaseDN)).or(new EqualsFilter(getMemberNameAttribute(), userDN)));
if (bindResult.isSuccess()) {
LOGGER.trace("Executing ldap search with base dn of {} and filter of {}", groupBaseDn, filter);
entryReader = connection.search(groupBaseDn, SearchScope.WHOLE_SUBTREE, filter.toString(), attributes);
SearchResultEntry entry;
while (entryReader.hasNext()) {
if (entryReader.isEntry()) {
entry = entryReader.readEntry();
Attribute attr = entry.getAttribute(groupNameAttribute);
if (attr == null) {
LOGGER.trace("Claim '{}' is null", roleClaimType);
} else {
Claim claim = new ClaimImpl(roleClaimType);
for (ByteString value : attr) {
String itemValue = value.toString();
claim.addValue(itemValue);
}
claimsColl.add(claim);
}
} else {
// Got a continuation reference
LOGGER.debug("Referral ignored while searching for user {}", user);
entryReader.readReference();
}
}
} else {
LOGGER.info("LDAP Connection failed.");
}
}
} catch (LdapException e) {
LOGGER.info("Cannot connect to server, therefore unable to set role claims. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information.");
LOGGER.debug("Cannot connect to server, therefore unable to set role claims.", e);
} catch (SearchResultReferenceIOException e) {
LOGGER.info("Unable to set role claims. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information.");
LOGGER.debug("Unable to set role claims.", e);
} finally {
if (connection != null) {
connection.close();
}
}
return claimsColl;
}
use of org.springframework.ldap.filter.EqualsFilter in project coprhd-controller by CoprHD.
the class LdapFilterUtil method getPersonFilterWithValues.
/**
* Generate encoded filter to search for persons
*
* @param rawFilter
* @param username
* @return encoded filter
*/
public static String getPersonFilterWithValues(final String rawFilter, final String username) {
String filter = rawFilter;
String[] usernameParts = username.split("@");
filter = filter.replace("%u", username);
filter = filter.replace("%U", usernameParts[0]);
if (usernameParts.length > 1) {
filter = filter.replaceAll("%d", usernameParts[1]);
}
// AND it
if (!filter.startsWith("(")) {
filter = "(" + filter + ")";
}
Filter hardCodedFilter = new HardcodedFilter(filter);
// Why is this not needed in the auth handler
Filter personFilter = new EqualsFilter("objectClass", "person");
AndFilter andFilter = new AndFilter();
andFilter.and(hardCodedFilter);
andFilter.and(personFilter);
return andFilter.encode();
}
use of org.springframework.ldap.filter.EqualsFilter in project coprhd-controller by CoprHD.
the class LdapFilterUtil method getAttributeFilterWithValues.
/**
* Generates an encoded filter for attribute query
*
* @param attributeName to find
* @return encoded filter
*/
public static String getAttributeFilterWithValues(final String attributeName) {
Filter hardCodedFilter = new HardcodedFilter("(lDAPDisplayName=" + attributeName + ")");
Filter attributeFilter = new EqualsFilter("objectCategory", "attributeSchema");
AndFilter andFilter = new AndFilter();
andFilter.and(hardCodedFilter);
andFilter.and(attributeFilter);
return andFilter.encode();
}
use of org.springframework.ldap.filter.EqualsFilter in project modesti by jlsalmon.
the class LdapUserService method memberOf.
private OrFilter memberOf(List<String> groups) throws InvalidNameException {
OrFilter or = new OrFilter();
for (String group : groups) {
LdapName ln = new LdapName(ldapGroupFilter);
ln.add(new Rdn("cn", group));
// The magic number will trigger a recursive search of nested groups. It's slow, but it works.
// See https://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx
EqualsFilter filter = new EqualsFilter("memberOf:1.2.840.113556.1.4.1941:", ln.toString());
or.or(filter);
}
return or;
}
Aggregations