1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.spring.security.ldaptive.authentication;
18
19 import java.util.Collection;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24 import org.bremersee.ldaptive.LdaptiveEntryMapper;
25 import org.ldaptive.dn.Dn;
26 import org.ldaptive.dn.NameValue;
27 import org.ldaptive.dn.RDn;
28
29
30
31
32
33
34
35 @FunctionalInterface
36 public interface UsernameToBindDnConverter {
37
38
39
40
41
42
43
44
45 String convert(String username);
46
47
48
49
50 class ByUserRdnAttribute implements UsernameToBindDnConverter {
51
52 private final LdaptiveAuthenticationProperties properties;
53
54
55
56
57
58
59 public ByUserRdnAttribute(LdaptiveAuthenticationProperties properties) {
60 this.properties = Objects
61 .requireNonNull(properties, "Ldaptive authentication properties are required.");
62 }
63
64 @Override
65 public String convert(String username) {
66 return Optional.ofNullable(properties.getUserBaseDn())
67 .map(baseDn -> LdaptiveEntryMapper
68 .createDn(properties.getUserRdnAttribute(), username, baseDn))
69 .orElseThrow(() -> new IllegalStateException(String
70 .format("Converting username %s to bind dn is not possible.", username)));
71 }
72 }
73
74
75
76
77
78
79
80 class ByDomainEmail implements UsernameToBindDnConverter {
81
82 private final LdaptiveAuthenticationProperties properties;
83
84
85
86
87
88
89 public ByDomainEmail(LdaptiveAuthenticationProperties properties) {
90 this.properties = Objects
91 .requireNonNull(properties, "Ldaptive authentication properties are required.");
92 }
93
94 @Override
95 public String convert(String username) {
96 return Optional.of(extractDomainName(properties.getUserBaseDn()))
97 .filter(domain -> !domain.isEmpty())
98 .map(domain -> username + "@" + domain)
99 .orElseThrow(() -> new IllegalStateException(String
100 .format("Converting username %s to bind dn is not possible.", username)));
101 }
102
103 private static String extractDomainName(String baseDn) {
104 return Stream.ofNullable(baseDn)
105 .map(Dn::new)
106 .map(Dn::getRDns)
107 .flatMap(Collection::stream)
108 .map(RDn::getNameValue)
109 .filter(nameValue -> nameValue.hasName("dc"))
110 .map(NameValue::getStringValue)
111 .collect(Collectors.joining("."));
112 }
113
114 }
115
116 }