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 java.io.Serial;
20  import lombok.EqualsAndHashCode;
21  import org.bremersee.spring.security.ldaptive.userdetails.LdaptiveUserDetails;
22  import org.jspecify.annotations.NonNull;
23  import org.springframework.security.authentication.AbstractAuthenticationToken;
24  
25  /**
26   * The ldaptive authentication token.
27   *
28   * @author Christian Bremer
29   */
30  @EqualsAndHashCode(callSuper = true)
31  public class LdaptiveAuthenticationToken
32      extends AbstractAuthenticationToken
33      implements LdaptiveAuthentication {
34  
35    @Serial
36    private static final long serialVersionUID = 1L;
37  
38    /**
39     * The ldaptive user details.
40     */
41    private final LdaptiveUserDetails userDetails;
42  
43    /**
44     * Instantiates a new ldaptive authentication token.
45     *
46     * @param userDetails the user details
47     */
48    public LdaptiveAuthenticationToken(LdaptiveUserDetails userDetails) {
49      super(userDetails.getAuthorities());
50      this.userDetails = userDetails;
51    }
52  
53    @Override
54    public LdaptiveUserDetails getPrincipal() {
55      return userDetails;
56    }
57  
58    @Override
59    public @NonNull String getName() {
60      return userDetails.getUsername();
61    }
62  
63    @Override
64    public boolean isAuthenticated() {
65      return true;
66    }
67  
68    @Override
69    public Object getCredentials() {
70      return userDetails.getPassword();
71    }
72  
73  }