View Javadoc
1   /*
2    * Copyright 2024-2026 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.userdetails;
18  
19  import static java.util.Objects.nonNull;
20  
21  import lombok.AccessLevel;
22  import lombok.Getter;
23  import org.bremersee.spring.security.ldaptive.authentication.AccountControlEvaluator;
24  import org.bremersee.spring.security.ldaptive.authentication.provider.NoAccountControlEvaluator;
25  import org.ldaptive.LdapEntry;
26  
27  /**
28   * The ldaptive evaluated remember-me token provider.
29   *
30   * @author Christian Bremer
31   */
32  public class LdaptiveEvaluatedRememberMeTokenProvider implements LdaptiveRememberMeTokenProvider {
33  
34    /**
35     * The account control evaluator.
36     */
37    @Getter(AccessLevel.PROTECTED)
38    private AccountControlEvaluator accountControlEvaluator = new NoAccountControlEvaluator();
39  
40    /**
41     * Instantiates a new ldaptive evaluated remember-me token provider.
42     */
43    public LdaptiveEvaluatedRememberMeTokenProvider() {
44      this(null);
45    }
46  
47    /**
48     * Instantiates a new ldaptive evaluated remember-me token provider.
49     *
50     * @param accountControlEvaluator the account control evaluator
51     */
52    public LdaptiveEvaluatedRememberMeTokenProvider(
53        AccountControlEvaluator accountControlEvaluator) {
54      setAccountControlEvaluator(accountControlEvaluator);
55    }
56  
57    /**
58     * Sets account control evaluator.
59     *
60     * @param accountControlEvaluator the account control evaluator
61     */
62    public void setAccountControlEvaluator(AccountControlEvaluator accountControlEvaluator) {
63      if (nonNull(accountControlEvaluator)) {
64        this.accountControlEvaluator = accountControlEvaluator;
65      }
66    }
67  
68    @Override
69    public String getRememberMeToken(LdapEntry ldapEntry) {
70      return getAccessControlValue(ldapEntry) + ldapEntry.getDn();
71    }
72  
73    /**
74     * Gets access control value.
75     *
76     * @param ldapEntry the ldap entry
77     * @return the access control value
78     */
79    protected String getAccessControlValue(LdapEntry ldapEntry) {
80      return String.format("%s:%s:%s:%s-",
81          accountControlEvaluator.isAccountNonExpired(ldapEntry),
82          accountControlEvaluator.isAccountNonLocked(ldapEntry),
83          accountControlEvaluator.isCredentialsNonExpired(ldapEntry),
84          accountControlEvaluator.isEnabled(ldapEntry));
85    }
86  
87  }