View Javadoc
1   /*
2    * Copyright 2021 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.security.core.userdetails;
18  
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  import lombok.AccessLevel;
24  import lombok.Getter;
25  import lombok.ToString;
26  import org.bremersee.data.ldaptive.LdaptiveEntryMapper;
27  import org.ldaptive.SearchScope;
28  import org.springframework.security.core.userdetails.UserDetails;
29  import org.springframework.util.StringUtils;
30  
31  /**
32   * The abstract user details service.
33   *
34   * @author Christian Bremer
35   */
36  @ToString
37  public abstract class AbstractUserDetailsService {
38  
39    @Getter(value = AccessLevel.PROTECTED)
40    private final String userBaseDn;
41  
42    @Getter(value = AccessLevel.PROTECTED)
43    private final String userFindOneFilter;
44  
45    @Getter(value = AccessLevel.PROTECTED)
46    private final SearchScope userFindOneSearchScope;
47  
48    @Getter(value = AccessLevel.PROTECTED)
49    private final String userAccountControlAttributeName;
50  
51    @Getter(value = AccessLevel.PROTECTED)
52    private final List<String> authorities;
53  
54    @Getter(value = AccessLevel.PROTECTED)
55    private final String authorityAttributeName;
56  
57    @Getter(value = AccessLevel.PROTECTED)
58    private final boolean authorityDn;
59  
60    @Getter(value = AccessLevel.PROTECTED)
61    private final Map<String, String> authorityMap;
62  
63    @Getter(value = AccessLevel.PROTECTED)
64    private final String authorityPrefix;
65  
66    /**
67     * Instantiates a new abstract user details service.
68     *
69     * @param userBaseDn the user base dn
70     * @param userFindOneFilter the user find one filter
71     * @param userFindOneSearchScope the user find one search scope
72     * @param userAccountControlAttributeName the user account control attribute name
73     * @param authorities the authorities
74     * @param authorityAttributeName the authority attribute name
75     * @param authorityDn the authority dn
76     * @param authorityMap the authority map
77     * @param authorityPrefix the authority prefix
78     */
79    public AbstractUserDetailsService(
80        String userBaseDn,
81        String userFindOneFilter,
82        SearchScope userFindOneSearchScope,
83        String userAccountControlAttributeName,
84        List<String> authorities,
85        String authorityAttributeName,
86        boolean authorityDn,
87        Map<String, String> authorityMap,
88        String authorityPrefix) {
89  
90      this.userBaseDn = userBaseDn;
91      this.userFindOneFilter = userFindOneFilter;
92      this.userFindOneSearchScope = userFindOneSearchScope != null ? userFindOneSearchScope : SearchScope.ONELEVEL;
93      this.userAccountControlAttributeName = userAccountControlAttributeName;
94      this.authorities = authorities;
95      this.authorityAttributeName = authorityAttributeName;
96      this.authorityDn = authorityDn;
97      this.authorityMap = authorityMap;
98      this.authorityPrefix = authorityPrefix;
99    }
100 
101   /**
102    * Return attributes.
103    *
104    * @return the attributes
105    */
106   protected Set<String> returnAttributes() {
107     Set<String> attributes = new HashSet<>();
108     if (StringUtils.hasText(userAccountControlAttributeName)) {
109       attributes.add(userAccountControlAttributeName);
110     }
111     if (StringUtils.hasText(authorityAttributeName)) {
112       attributes.add(authorityAttributeName);
113     }
114     return attributes;
115   }
116 
117   /**
118    * Gets user details mapper.
119    *
120    * @param userName the user name
121    * @return the user details mapper
122    */
123   protected LdaptiveEntryMapper<UserDetails> getUserDetailsLdapMapper(String userName) {
124     return new UserDetailsLdapMapper(
125         userName,
126         userAccountControlAttributeName,
127         authorities,
128         authorityAttributeName,
129         authorityDn,
130         authorityMap,
131         authorityPrefix);
132   }
133 
134 }