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  
17  package com.irurueta.authserver.authorization.api.web.totp;
18  
19  import java.io.Serial;
20  import java.util.List;
21  import lombok.EqualsAndHashCode;
22  import org.springframework.security.authentication.AbstractAuthenticationToken;
23  import org.springframework.security.core.Authentication;
24  
25  /**
26   * Request to verify a second factor: the authentication the user already holds (the primary factor) and the code the
27   * user typed, and what kind of code it is. It is never authenticated itself; {@link TotpAuthenticationProvider} answers
28   * with the primary authentication enriched with the TOTP authority and, when the code confirmed an enrolment, hands the
29   * new recovery codes back to the filter through {@link #issueRecoveryCodes(List)}.
30   */
31  @EqualsAndHashCode(callSuper = true)
32  public class TotpAuthenticationToken extends AbstractAuthenticationToken {
33  
34      /** Serialization version. */
35      @Serial
36      private static final long serialVersionUID = 1L;
37  
38      /** The authentication the user already holds. */
39      private final Authentication primary;
40  
41      /** The code as typed. */
42      private final String code;
43  
44      /** What kind of code it is. */
45      private final TotpCodeKind kind;
46  
47      /**
48       * Recovery codes generated while the request was verified (an enrolment), to be shown once; never serialized, as
49       * they only travel from the provider back to the filter of the same request.
50       */
51      @EqualsAndHashCode.Exclude
52      private transient List<String> issuedRecoveryCodes;
53  
54      /**
55       * Creates the request.
56       *
57       * @param primary the authentication the user already holds.
58       * @param code    the code as typed.
59       * @param kind    what kind of code it is.
60       */
61      public TotpAuthenticationToken(final Authentication primary, final String code, final TotpCodeKind kind) {
62          super(List.of());
63          this.primary = primary;
64          this.code = code;
65          this.kind = kind;
66      }
67  
68      /**
69       * Gets the code.
70       *
71       * @return the code as typed.
72       */
73      @Override
74      public String getCredentials() {
75          return code;
76      }
77  
78      /**
79       * Gets the principal.
80       *
81       * @return the principal of the primary authentication.
82       */
83      @Override
84      public Object getPrincipal() {
85          return primary.getPrincipal();
86      }
87  
88      /**
89       * Gets the authentication the user already holds.
90       *
91       * @return the primary authentication.
92       */
93      public Authentication primary() {
94          return primary;
95      }
96  
97      /**
98       * Tells what kind of code it is.
99       *
100      * @return the kind of code.
101      */
102     public TotpCodeKind kind() {
103         return kind;
104     }
105 
106     /**
107      * Gets the recovery codes generated while the request was verified.
108      *
109      * @return the codes; empty unless the code confirmed an enrolment.
110      */
111     public List<String> issuedRecoveryCodes() {
112         return issuedRecoveryCodes == null ? List.of() : issuedRecoveryCodes;
113     }
114 
115     /**
116      * Hands back the recovery codes generated while the request was verified.
117      *
118      * @param codes the codes in clear text.
119      */
120     public void issueRecoveryCodes(final List<String> codes) {
121         issuedRecoveryCodes = List.copyOf(codes);
122     }
123 }