ldap
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ldap [2020/07/20 10:52] – skipidar | ldap [2020/12/27 20:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 365: | Line 365: | ||
contextSource.setUserDn(env.getRequiredProperty(" | contextSource.setUserDn(env.getRequiredProperty(" | ||
contextSource.setPassword(env.getRequiredProperty(" | contextSource.setPassword(env.getRequiredProperty(" | ||
- | contextSource.setPooled(false); | + | contextSource.setPooled(false); |
Map< | Map< | ||
Line 1042: | Line 1042: | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== LDAP tools ==== | ||
+ | |||
+ | <sxh java> | ||
+ | |||
+ | @Component | ||
+ | public class Tools { | ||
+ | |||
+ | private static Logger LOG = LoggerFactory.getLogger(Tools.class); | ||
+ | |||
+ | private final LdapName baseLdapPath; | ||
+ | |||
+ | @Autowired | ||
+ | public Tools(Environment env) { | ||
+ | this.baseLdapPath = LdapUtils.newLdapName(env.getRequiredProperty(" | ||
+ | } | ||
+ | |||
+ | public static final <T> List< | ||
+ | return StreamSupport.stream(i.spliterator(), | ||
+ | } | ||
+ | |||
+ | public LdapName toAbsoluteDn(Name relativeName) { | ||
+ | // check if already absolute | ||
+ | if (relativeName.startsWith(baseLdapPath)) { | ||
+ | LOG.info(String.format(" | ||
+ | return LdapNameBuilder.newInstance(relativeName) | ||
+ | .build(); | ||
+ | } else { | ||
+ | return LdapNameBuilder.newInstance(baseLdapPath) | ||
+ | .add(relativeName) | ||
+ | .build(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static Optional< | ||
+ | try { | ||
+ | return Optional.of(LdapNameBuilder.newInstance(str).build()); | ||
+ | } catch (NullPointerException | org.springframework.ldap.InvalidNameException e) { | ||
+ | return Optional.empty(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static boolean isLdapId(String str) { | ||
+ | try { | ||
+ | LdapName name = LdapNameBuilder.newInstance(str).build(); | ||
+ | return true; | ||
+ | } catch (NullPointerException | org.springframework.ldap.InvalidNameException e) { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public LdapName toRelativeDn(Name absoluteName) { | ||
+ | Name name = absoluteName; | ||
+ | if (absoluteName.startsWith(baseLdapPath)) { | ||
+ | name = absoluteName.getSuffix(baseLdapPath.size()); | ||
+ | } | ||
+ | LOG.info(String.format(" | ||
+ | return LdapNameBuilder.newInstance(name).build(); | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * The relative DN is typically what is needed to perform lookups and searches in | ||
+ | * the LDAP tree, whereas the absolute DN is needed when authenticating and when | ||
+ | * an LDAP entry is referred to in e.g. a group. This wrapper class contains | ||
+ | * both of these representations. | ||
+ | * <p> | ||
+ | * See LdapEntryIdentification.class comment | ||
+ | * | ||
+ | * @param ldapIds | ||
+ | * @return | ||
+ | */ | ||
+ | public List< | ||
+ | final ArrayList< | ||
+ | ldapIds.forEach(ldapId -> { | ||
+ | final LdapName absLdapId = toAbsoluteDn(ldapId); | ||
+ | list.add(absLdapId); | ||
+ | }); | ||
+ | return list; | ||
+ | } | ||
+ | |||
+ | |||
+ | public boolean ldapNameContainsSegment(final Name ldapName, final Name fragment) { | ||
+ | if (ldapName == null || fragment == null || ldapName.size() < fragment.size()) { | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | // Create an empty list | ||
+ | final List< | ||
+ | ldapName.getAll().asIterator().forEachRemaining(listLdapName:: | ||
+ | |||
+ | final List< | ||
+ | fragment.getAll().asIterator().forEachRemaining(listFragment:: | ||
+ | |||
+ | // is the fragment like " | ||
+ | return (Collections.indexOfSubList(listLdapName, | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Assumes that tags are separated by empty spaces | ||
+ | * | ||
+ | * @param tags | ||
+ | * @return | ||
+ | */ | ||
+ | public List< | ||
+ | if (tags == null ) return null; | ||
+ | if (tags.isEmpty() ) return Collections.EMPTY_LIST; | ||
+ | final String[] tagsList = tags.trim().split(" | ||
+ | return Arrays.asList(tagsList); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * Takes two ldap names and replaces the suffix | ||
+ | * by cutting off the length of suffix from the ldapName and appending the suffix | ||
+ | * | ||
+ | * The suffix will be appended on the right side of String. | ||
+ | * Eg in " | ||
+ | * | ||
+ | * @param ldapName | ||
+ | * @param newSuffix - suffix to append. | ||
+ | * @return name with new suffix | ||
+ | */ | ||
+ | public LdapName replaceSuffix(final LdapName ldapName, final LdapName newSuffix) { | ||
+ | Assert.isTrue(ldapName.size() >= newSuffix.size(), | ||
+ | if (newSuffix.size() == 0) return ldapName; | ||
+ | |||
+ | try { | ||
+ | // cut off the the initial suffix, prepare merge with new suffix | ||
+ | final LdapName copyName = LdapUtils.newLdapName(ldapName); | ||
+ | for (int i = 0; i < newSuffix.size(); | ||
+ | copyName.remove(0); | ||
+ | } | ||
+ | copyName.addAll(0, | ||
+ | return copyName; | ||
+ | |||
+ | } catch (InvalidNameException e) { | ||
+ | throw new RuntimeException(e); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
ldap.1595242370.txt.gz · Last modified: (external edit)