public Object mapFromAttributes(Attributes attributes) throws NamingException { Attribute roleAttr = attributes.get(groupRoleAttributeName); NamingEnumeration<?> ne = roleAttr.getAll(); // assert ne.hasMore(); Object group = ne.next(); String role = group.toString(); return new SimpleGrantedAuthority(rolePrefix + role.toUpperCase()); } };
/** * convert search result into URI * * @param result * search result to convert to URI */ protected URI toURI(SearchResult result) throws Exception { Attributes attributes = result.getAttributes(); String address = (String) attributes.get("iphostnumber").get(); String port = (String) attributes.get("ipserviceport").get(); String protocol = (String) attributes.get("ipserviceprotocol").get(); URI connectorURI = new URI("static:(" + protocol + "://" + address + ":" + port + ")"); LOG.debug("retrieved URI from SearchResult [{}]", connectorURI); return connectorURI; }
private List<String> addAttributeValues(String attrId, Attributes attrs, List<String> values) throws NamingException { if (attrId == null || attrs == null) { return values; } if (values == null) { values = new ArrayList<String>(); } Attribute attr = attrs.get(attrId); if (attr == null) { return values; } NamingEnumeration<?> e = attr.getAll(); while (e.hasMore()) { String value = (String)e.next(); values.add(value); } return values; }
@Override public Enumeration<String> engineAliases() { DirContext context = obtainDirContext(); if (context == null) { log.trace("Unable to obtain DirContext"); return null; } try { NamingEnumeration<SearchResult> results = context.search(searchPath, filterIterate, null, createSearchControl(new String[]{aliasAttribute})); // TODO pagination List<String> aliases = new LinkedList<>(); while (results.hasMore()) { Attribute attribute = results.next().getAttributes().get(aliasAttribute); if (attribute != null) aliases.add((String) attribute.get()); } return Collections.enumeration(aliases); } catch (NamingException e) { throw log.ldapKeyStoreFailedToIterateAliases(e); } finally { returnDirContext(context); } }
Attribute groupDNAttr = result.getAttributes().get(memberOfAttr); if (groupDNAttr == null) { throw new NamingException("The user object does not have '" + while (groupEnumeration.hasMore()) { String groupDN = groupEnumeration.next().toString(); groups.add(getRelativeDistinguishedName(groupDN));
/** * Looks up the DNS name provided in the JNDI context. */ public static Set<String> getCNamesFromTxtRecord(String discoveryDnsName) throws NamingException { Attributes attrs = getDirContext().getAttributes(discoveryDnsName, new String[]{TXT_RECORD_TYPE}); Attribute attr = attrs.get(TXT_RECORD_TYPE); String txtRecord = null; if (attr != null) { txtRecord = attr.get().toString(); /** * compatible splited txt record of "host1 host2 host3" but not "host1" "host2" "host3". * some dns service provider support txt value only format "host1 host2 host3" */ if (txtRecord.startsWith("\"") && txtRecord.endsWith("\"")) { txtRecord = txtRecord.substring(1, txtRecord.length() - 1); } } Set<String> cnamesSet = new TreeSet<String>(); if (txtRecord == null || txtRecord.trim().isEmpty()) { return cnamesSet; } String[] cnames = txtRecord.split(" "); Collections.addAll(cnamesSet, cnames); return cnamesSet; } }
return principal; Attributes atrs = ((SearchResult)answer.next()).getAttributes(); Attribute usernameAttribute = atrs.get(usernameField); username = (String) usernameAttribute.get();
Attribute memberAttribute = result.getAttributes().get(permissionGroupMemberAttribute); NamingEnumeration<?> memberAttributeEnum = memberAttribute.getAll(); Attribute memberEntryObjectClassAttribute = memberAttributes.get("objectClass"); NamingEnumeration<?> memberEntryObjectClassAttributeEnum = memberEntryObjectClassAttribute.getAll(); Attribute name = memberAttributes.get(groupNameAttribute); if (name == null) { LOG.error("Policy not applied! Group {} does not have name attribute {} under entry {}", new Object[]{ memberDn, groupNameAttribute, result.getNameInNamespace() }); principalName = (String) name.get(); Attribute name = memberAttributes.get(userNameAttribute); if (name == null) { LOG.error("Policy not applied! User {} does not have name attribute {} under entry {}", new Object[]{ memberDn, userNameAttribute, result.getNameInNamespace() }); principalName = (String) name.get();
private void setRoles(final DirContext authContext, final AuthenticationRequest request, final String username) throws NamingException { final List roles = new ArrayList(); final String filter = "(objectclass=organizationalRole)"; final SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); controls.setReturningAttributes(new String[] { "cn" }); final NamingEnumeration answer = authContext.search("uid=" + username + ", " + ldapDn, filter, controls); while (answer.hasMore()) { final SearchResult result = (SearchResult) answer.nextElement(); final String roleName = (String) result.getAttributes().get("cn").get(0); roles.add(roleName); LOG.debug("Adding role: " + roleName); } request.setRoles((String[]) roles.toArray(new String[roles.size()])); }
private BasicBulkDataDescriptor loadBulkDataDescriptor(SearchResult sr) throws NamingException { Attributes attrs = sr.getAttributes(); BasicBulkDataDescriptor descriptor = new BasicBulkDataDescriptor( LdapUtils.stringValue(attrs.get("dcmBulkDataDescriptorID"), null)); descriptor.excludeDefaults(LdapUtils.booleanValue(attrs.get("dcmBulkDataExcludeDefaults"), false)); descriptor.setAttributeSelectorsFromStrings(LdapUtils.stringArray(attrs.get("dcmAttributeSelector"))); descriptor.setLengthsThresholdsFromStrings(LdapUtils.stringArray(attrs.get("dcmBulkDataVRLengthThreshold"))); return descriptor ; }
private void forEachAttributeValue(SearchResult entry, String attrId, Consumer<String> action) { NamingEnumeration<?> attributesEnum = null; try { Attribute attribute = entry.getAttributes().get(attrId); if (attribute == null) return; attributesEnum = attribute.getAll(); Collections.list(attributesEnum).stream().map(Object::toString).forEach(action); } catch (NamingException e) { throw ElytronMessages.log.ldapRealmFailedObtainAttributes(entry.getNameInNamespace(), e); } finally { if (attributesEnum != null) { try { attributesEnum.close(); } catch (NamingException e) { log.trace("Unable to close attributesEnum", e); } } } }
protected Set<String> addAttributeValues(String attrId, Attributes attrs, Set<String> values) throws NamingException { if (attrId == null || attrs == null) { return values; } if (values == null) { values = new HashSet<String>(); } Attribute attr = attrs.get(attrId); if (attr == null) { return values; } NamingEnumeration<?> e = attr.getAll(); while (e.hasMore()) { String value = (String)e.next(); values.add(value); } return values; }
NamingEnumeration<SearchResult> renum = context.search(toDC(domainName), "(& (userPrincipalName=" + principalName + ")(objectClass=user))", controls); if (!renum.hasMore()) { System.out.println("Cannot locate user information for " + username); System.exit(1); SearchResult result = renum.next(); Attribute memberOf = result.getAttributes().get("memberOf"); if (memberOf != null) {// null if this user belongs to no group at all for (int i = 0; i < memberOf.size(); i++) { Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[] { "CN" }); Attribute att = atts.get("CN"); groups.add(new GrantedAuthorityImpl(att.get().toString()));
private String getAttribute(SearchResult searchResult, String attributeId) { if (searchResult == null) { return null; } Attribute entry = searchResult.getAttributes().get(attributeId); if (entry == null) { log.warn("The attribute with ID '{}' has not been found.", attributeId); return null; } try { return entry.get().toString(); } catch (NamingException ex) { log.error("Failed to get attribute value", ex); return null; } }
String description; try { name = ((String)((a.get(manager.getGroupNameField())).get())); description = ((String)((a.get(manager.getGroupDescriptionField())).get())); Attribute memberField = a.get(manager.getGroupMemberField()); if (memberField != null) { NamingEnumeration ne = memberField.getAll(); while (ne.hasMore()) { String username = (String) ne.next(); Attributes attrs = searchResult.getAttributes(); if (isAD) { Attribute userdnAttr = attrs.get("distinguishedName"); if (username.equals((String)userdnAttr.get())) { username = (String)attrs.get(manager.getUsernameField()).get(); break; username = (String)attrs.get(manager.getUsernameField()).get(); break;
@Override public boolean verifyCertificate(X509Certificate certificate, Attributes attributes) throws NamingException { Attribute attribute = attributes.get(ldapAttribute); if (attribute == null) return false; final int size = attribute.size(); for (int i = 0; i < size; i++) { Object attrSerialNumber = attribute.get(i); if (attrSerialNumber != null){ BigInteger value = new BigInteger((String) attrSerialNumber); if (certificate.getSerialNumber().equals(value)) { return true; } } } return false; } }
count++; if (skip > 0 && count <= skip) { answer.next(); continue; answer.next(); break; String result = (String)answer.next().getAttributes().get(attribute).get(); String result = (String)answer.next().getAttributes().get(attribute).get();
@Override public List<LdapUser> getUsersInGroup(String groupName, LdapContext context, Long domainId) throws NamingException { String attributeName = _ldapConfiguration.getGroupUniqueMemberAttribute(domainId); final SearchControls controls = new SearchControls(); controls.setSearchScope(_ldapConfiguration.getScope()); controls.setReturningAttributes(new String[] {attributeName}); NamingEnumeration<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(domainId), generateGroupSearchFilter(groupName, domainId), controls); final List<LdapUser> users = new ArrayList<LdapUser>(); //Expecting only one result which has all the users if (result.hasMoreElements()) { Attribute attribute = result.nextElement().getAttributes().get(attributeName); NamingEnumeration<?> values = attribute.getAll(); while (values.hasMoreElements()) { String userdn = String.valueOf(values.nextElement()); try{ users.add(getUserForDn(userdn, context, domainId)); } catch (NamingException e){ s_logger.info("Userdn: " + userdn + " Not Found:: Exception message: " + e.getMessage()); } } } Collections.sort(users); return users; }
private void setRoles(final DirContext authContext, final AuthenticationRequest request, final String username) throws NamingException { final List<String> roles = new ArrayList<String>(); final SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); controls.setReturningAttributes(new String[] { "cn" }); final String name = "uid=" + username + ", " + ldapDn; final NamingEnumeration<SearchResult> answer = authContext.search(name, LdapAuthenticationConstants.FILTER, controls); while (answer.hasMore()) { final SearchResult result = (SearchResult) answer.nextElement(); final String roleName = (String) result.getAttributes().get("cn").get(0); roles.add(roleName); LOG.debug("Adding role: " + roleName); } request.setRoles(roles); }