View Javadoc
1   /*
2    * Copyright (C) 2026 Alberto Irurueta Carro (alberto@irurueta.com)
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  package com.irurueta.authserver.authorization.api.web.ott;
17  
18  import java.io.Serial;
19  import java.util.List;
20  import lombok.EqualsAndHashCode;
21  import org.springframework.security.authentication.AbstractAuthenticationToken;
22  import org.springframework.security.core.Authentication;
23  
24  /**
25   * Request to verify a one-time code as a second factor: the authentication the user already holds (the primary
26   * factor), the code the user typed and the binding id of the session that requested it. It is never authenticated
27   * itself; {@link OttSecondFactorAuthenticationProvider} answers with the primary authentication enriched with the
28   * authority of the channel.
29   */
30  @EqualsAndHashCode(callSuper = true)
31  public class OttSecondFactorToken extends AbstractAuthenticationToken {
32  
33      /** Serialization version. */
34      @Serial
35      private static final long serialVersionUID = 1L;
36  
37      /** The authentication the user already holds. */
38      private final Authentication primary;
39  
40      /** The code as typed. */
41      private final String code;
42  
43      /** Binding id of the session that requested the code. */
44      private final String bindingId;
45  
46      /**
47       * Creates the request.
48       *
49       * @param primary   the authentication the user already holds.
50       * @param code      the code as typed.
51       * @param bindingId binding id of the session that requested the code, empty when it requested none.
52       */
53      public OttSecondFactorToken(final Authentication primary, final String code, final String bindingId) {
54          super(List.of());
55          this.primary = primary;
56          this.code = code;
57          this.bindingId = bindingId;
58      }
59  
60      /**
61       * Gets the code.
62       *
63       * @return the code as typed.
64       */
65      @Override
66      public String getCredentials() {
67          return code;
68      }
69  
70      /**
71       * Gets the principal.
72       *
73       * @return the principal of the primary authentication.
74       */
75      @Override
76      public Object getPrincipal() {
77          return primary.getPrincipal();
78      }
79  
80      /**
81       * Gets the authentication the user already holds.
82       *
83       * @return the primary authentication.
84       */
85      public Authentication primary() {
86          return primary;
87      }
88  
89      /**
90       * Gets the binding id of the session that requested the code.
91       *
92       * @return the binding id.
93       */
94      public String bindingId() {
95          return bindingId;
96      }
97  }