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.provider;
18  
19  import java.time.OffsetDateTime;
20  import java.util.Optional;
21  import org.bremersee.ldaptive.transcoder.UserAccountControl;
22  import org.bremersee.ldaptive.transcoder.ValueTranscoderFactory;
23  import org.bremersee.spring.security.ldaptive.authentication.AccountControlEvaluator;
24  import org.ldaptive.LdapAttribute;
25  import org.ldaptive.LdapEntry;
26  
27  /**
28   * Evaluator of the ldap attributes {@code userAccountControl} and {@code accountExpires} of an
29   * Active Directory.
30   *
31   * @author Christian Bremer
32   */
33  public class ActiveDirectoryAccountControlEvaluator implements AccountControlEvaluator {
34  
35    /**
36     * Instantiates a new active directory account control evaluator.
37     */
38    public ActiveDirectoryAccountControlEvaluator() {
39      super();
40    }
41  
42    @Override
43    public boolean isAccountNonExpired(LdapEntry ldapEntry) {
44      var valueTranscoder = ValueTranscoderFactory.getFileTimeToOffsetDateTimeValueTranscoder();
45      return Optional.ofNullable(ldapEntry)
46          .map(entry -> entry.getAttribute("accountExpires"))
47          .map(LdapAttribute::getStringValue)
48          .map(valueTranscoder::decodeStringValue)
49          .map(dateTime -> dateTime.isAfter(OffsetDateTime.now()))
50          .orElse(true);
51    }
52  
53    @Override
54    public boolean isAccountNonLocked(LdapEntry ldapEntry) {
55      return true;
56    }
57  
58    @Override
59    public boolean isCredentialsNonExpired(LdapEntry ldapEntry) {
60      return true;
61    }
62  
63    @Override
64    public boolean isEnabled(LdapEntry ldapEntry) {
65      var valueTranscoder = ValueTranscoderFactory.getUserAccountControlValueTranscoder();
66      return Optional.ofNullable(ldapEntry)
67          .map(entry -> entry.getAttribute("userAccountControl"))
68          .map(LdapAttribute::getStringValue)
69          .map(valueTranscoder::decodeStringValue)
70          .map(UserAccountControl::isEnabled)
71          .orElse(true);
72    }
73  }