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 java.util.Optional;
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.bremersee.spring.security.ldaptive.authentication.AccountControlEvaluator;
23  import org.ldaptive.LdapAttribute;
24  import org.ldaptive.LdapEntry;
25  import org.springframework.util.ObjectUtils;
26  
27  /**
28   * The ldaptive pwdLastSet remember-me token provider.
29   *
30   * @author Christian Bremer
31   */
32  public class LdaptivePwdLastSetRememberMeTokenProvider
33      extends LdaptiveEvaluatedRememberMeTokenProvider {
34  
35    /**
36     * The pwdLastSet attribute name.
37     */
38    @Getter(AccessLevel.PROTECTED)
39    private String pwdLastSetAttributeName = "pwdLastSet";
40  
41    /**
42     * Instantiates a ldaptive pwd last set remember-me token provider.
43     */
44    public LdaptivePwdLastSetRememberMeTokenProvider() {
45      this(null);
46    }
47  
48    /**
49     * Instantiates a ldaptive pwd last set remember-me token provider.
50     *
51     * @param accountControlEvaluator the account control evaluator
52     */
53    public LdaptivePwdLastSetRememberMeTokenProvider(
54        AccountControlEvaluator accountControlEvaluator) {
55      this(accountControlEvaluator, null);
56    }
57  
58    /**
59     * Instantiates a ldaptive pwd last set remember-me token provider.
60     *
61     * @param accountControlEvaluator the account control evaluator
62     * @param pwdLastSetAttributeName the pwd last set attribute name
63     */
64    public LdaptivePwdLastSetRememberMeTokenProvider(
65        AccountControlEvaluator accountControlEvaluator,
66        String pwdLastSetAttributeName) {
67      super(accountControlEvaluator);
68      setPwdLastSetAttributeName(pwdLastSetAttributeName);
69    }
70  
71    /**
72     * Sets pwdLastSet attribute name.
73     *
74     * @param pwdLastSetAttributeName the pwdLastSet attribute name
75     */
76    public void setPwdLastSetAttributeName(String pwdLastSetAttributeName) {
77      if (!ObjectUtils.isEmpty(pwdLastSetAttributeName)) {
78        this.pwdLastSetAttributeName = pwdLastSetAttributeName;
79      }
80    }
81  
82    @Override
83    public String getRememberMeToken(LdapEntry ldapEntry) {
84      return Optional
85          .ofNullable(ldapEntry.getAttribute(getPwdLastSetAttributeName()))
86          .map(LdapAttribute::getStringValue)
87          .map(value -> getAccessControlValue(ldapEntry) + value)
88          .orElseGet(() -> LdaptiveRememberMeTokenProvider.invalid().getRememberMeToken(ldapEntry));
89    }
90  
91  }