View Javadoc
1   /*
2    * Copyright 2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.spring.security.ldaptive.authentication;
18  
19  import static org.springframework.util.ObjectUtils.isEmpty;
20  
21  import java.io.Serial;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.stream.Collectors;
29  import java.util.stream.Stream;
30  import lombok.AllArgsConstructor;
31  import lombok.Data;
32  import org.bremersee.spring.security.core.mapping.CaseTransformation;
33  import org.ldaptive.SearchScope;
34  
35  /**
36   * The ldaptive authentication properties.
37   *
38   * @author Christian Bremer
39   */
40  @Data
41  public class LdaptiveAuthenticationProperties implements Serializable {
42  
43    @Serial
44    private static final long serialVersionUID = 1L;
45  
46    /**
47     * The user base dn (like 'ou=people,dc=example,dc=org'). This value is always required.
48     */
49    protected String userBaseDn;
50  
51    /**
52     * A list with refused usernames.
53     */
54    protected List<String> refusedUsernames;
55  
56    /**
57     * The object class of the user (like 'inetOrgPerson'). The selected template contains a default.
58     */
59    protected String userObjectClass;
60  
61    /**
62     * The username attribute of the user (like 'uid' or 'sAMAccountName'). The selected template
63     * contains a default.
64     */
65    protected String usernameAttribute;
66  
67    /**
68     * The password attribute of the user (like 'userPassword'). If it is empty, a simple user bind
69     * will be done with the credentials of the user for authentication. If it is present, the
70     * connection to the ldap server must be done by a 'global' user and a password encoder that fits
71     * your requirements must be present. The default password encoder only supports SHA, that is
72     * insecure.
73     */
74    protected String passwordAttribute;
75  
76    /**
77     * The password last set attribute (like 'pwdLastSet') can be used to activate the remember-me
78     * functionality.
79     */
80    protected String passwordLastSetAttribute;
81  
82    /**
83     * The filter to find the user. If it is empty, it will be generated from {@code userObjectClass}
84     * and {@code usernameAttribute} like this {@code (&(objectClass=inetOrgPerson)(uid={0}))}.
85     */
86    protected String userFindOneFilter;
87  
88    /**
89     * The scope to find a user. Default is 'one level'.
90     */
91    protected SearchScope userFindOneSearchScope;
92  
93    /**
94     * The first name attribute of the user. Default is 'givenName'.
95     */
96    protected String firstNameAttribute;
97  
98    /**
99     * The last name attribute of the user. Default is 'sn'.
100    */
101   protected String lastNameAttribute;
102 
103   /**
104    * The email attribute of the user. Default is 'mail';
105    */
106   protected String emailAttribute;
107 
108   /**
109    * The account control evaluator.
110    */
111   protected AccountControlEvaluatorProperty accountControlEvaluator;
112 
113   /**
114    * The group fetch strategy.
115    */
116   protected GroupFetchStrategy groupFetchStrategy;
117 
118   /**
119    * The member attribute.
120    */
121   protected String memberAttribute;
122 
123   /**
124    * The group base dn (like 'ou=groups,dc=example,dc=org'). It's only required, if
125    * {@code groupFetchStrategy} is set to {@code GROUP_CONTAINS_USERS}.
126    */
127   protected String groupBaseDn;
128 
129   /**
130    * The group search scope. It's only required, if {@code groupFetchStrategy} is set to
131    * {@code GROUP_CONTAINS_USERS},
132    */
133   protected SearchScope groupSearchScope;
134 
135   /**
136    * The group object class. It's only required, if {@code groupFetchStrategy} is set to
137    * {@code GROUP_CONTAINS_USERS}
138    */
139   protected String groupObjectClass;
140 
141   /**
142    * The group id attribute. It's only required, if {@code groupFetchStrategy} is set to
143    * {@code GROUP_CONTAINS_USERS}
144    */
145   protected String groupIdAttribute;
146 
147   /**
148    * The group member attribute. It's only required, if {@code groupFetchStrategy} is set to
149    * {@code GROUP_CONTAINS_USERS}
150    */
151   protected String groupMemberAttribute;
152 
153   /**
154    * The group member format. It's only required, if {@code groupFetchStrategy} is set to
155    * {@code GROUP_CONTAINS_USERS}
156    */
157   protected String groupMemberFormat;
158 
159   /**
160    * The role mappings.
161    */
162   protected List<RoleMapping> roleMapping;
163 
164   /**
165    * The default roles.
166    */
167   protected List<String> defaultRoles;
168 
169   /**
170    * The role prefix (like 'ROLE_').
171    */
172   protected String rolePrefix;
173 
174   /**
175    * The role case transformation.
176    */
177   protected CaseTransformation roleCaseTransformation;
178 
179   /**
180    * The string replacements for roles.
181    */
182   protected List<StringReplacement> roleStringReplacements;
183 
184   /**
185    * Instantiates new ldaptive authentication properties.
186    */
187   public LdaptiveAuthenticationProperties() {
188     super();
189   }
190 
191   /**
192    * To role mappings map.
193    *
194    * @return the map
195    */
196   public Map<String, String> toRoleMappings() {
197     return Stream.ofNullable(getRoleMapping())
198         .flatMap(Collection::stream)
199         .collect(Collectors.toMap(
200             RoleMapping::getSource,
201             RoleMapping::getTarget,
202             (first, second) -> first,
203             LinkedHashMap::new));
204   }
205 
206   /**
207    * To role string replacements map.
208    *
209    * @return the map
210    */
211   public Map<String, String> toRoleStringReplacements() {
212     return Stream.ofNullable(getRoleStringReplacements())
213         .flatMap(Collection::stream)
214         .collect(Collectors.toMap(
215             StringReplacement::getRegex,
216             StringReplacement::getReplacement,
217             (first, second) -> first,
218             LinkedHashMap::new));
219   }
220 
221   /**
222    * The ldaptive authentication properties with defaults.
223    */
224   public static class WithDefaults extends LdaptiveAuthenticationProperties {
225 
226     @Serial
227     private static final long serialVersionUID = 1L;
228 
229     /**
230      * Instantiates a new ldaptive authentication properties with defaults.
231      */
232     public WithDefaults() {
233       refusedUsernames = new ArrayList<>();
234 
235       userObjectClass = "inetOrgPerson";
236       usernameAttribute = "uid";
237 
238       userFindOneSearchScope = SearchScope.ONELEVEL;
239       firstNameAttribute = "givenName";
240       lastNameAttribute = "sn";
241       emailAttribute = "mail";
242       memberAttribute = "memberOf";
243       accountControlEvaluator = AccountControlEvaluatorProperty.NONE;
244 
245       groupFetchStrategy = GroupFetchStrategy.USER_CONTAINS_GROUPS;
246       groupObjectClass = "groupOfUniqueNames";
247       groupMemberAttribute = "uniqueMember";
248       groupSearchScope = SearchScope.ONELEVEL;
249       roleMapping = new ArrayList<>();
250       defaultRoles = new ArrayList<>();
251       roleStringReplacements = new ArrayList<>();
252       roleCaseTransformation = CaseTransformation.NONE;
253     }
254 
255     /**
256      * Get user find one filter.
257      *
258      * @return the user find one filter
259      */
260     @Override
261     public String getUserFindOneFilter() {
262       if (isEmpty(userFindOneFilter)
263           && !isEmpty(getUserObjectClass())
264           && !isEmpty(getUsernameAttribute())) {
265         return String
266             .format("(&(objectClass=%s)(%s={0}))", getUserObjectClass(), getUsernameAttribute());
267       }
268       return userFindOneFilter;
269     }
270 
271   }
272 
273   /**
274    * The group fetch strategy.
275    */
276   public enum GroupFetchStrategy {
277 
278     /**
279      * Groups will not be fetched.
280      */
281     NONE,
282 
283     /**
284      * User contains groups group-fetch strategy.
285      */
286     USER_CONTAINS_GROUPS,
287 
288     /**
289      * Group contains users group-fetch strategy.
290      */
291     GROUP_CONTAINS_USERS
292   }
293 
294   /**
295    * The string replacement.
296    */
297   @Data
298   @AllArgsConstructor
299   public static class StringReplacement implements Serializable {
300 
301     @Serial
302     private static final long serialVersionUID = 1L;
303 
304     /**
305      * The regular expression to which the string is to be matched. '{@code [- ]}' for example would
306      * replace every '-' and every space.
307      */
308     private String regex;
309 
310     /**
311      * The string to be substituted for each match.
312      */
313     private String replacement;
314 
315     /**
316      * Instantiates a new string replacement.
317      */
318     public StringReplacement() {
319       super();
320     }
321   }
322 
323   /**
324    * The role mapping.
325    */
326   @Data
327   @AllArgsConstructor
328   public static class RoleMapping implements Serializable {
329 
330     @Serial
331     private static final long serialVersionUID = 1L;
332 
333     /**
334      * The value from the ldap (like 'developers').
335      */
336     private String source;
337 
338     /**
339      * The value in the spring security context (like 'ROLE_DEVELOPER').
340      */
341     private String target;
342 
343     /**
344      * Instantiates a new role mapping.
345      */
346     public RoleMapping() {
347       super();
348     }
349   }
350 
351 }