View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import java.security.SecureRandom;
19  import java.util.Random;
20  
21  import org.apache.commons.lang3.RandomStringUtils;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.exception.InvalidAccessTokenException;
24  import org.codelibs.fess.util.ComponentUtil;
25  
26  import jakarta.servlet.http.HttpServletRequest;
27  
28  /**
29   * The helper for access token.
30   */
31  public class AccessTokenHelper {
32  
33      /**
34       * Default constructor.
35       */
36      public AccessTokenHelper() {
37          // nothing
38      }
39  
40      /**
41       * The bearer string.
42       */
43      protected static final String BEARER = "Bearer";
44  
45      /**
46       * The random instance.
47       */
48      protected Random random = new SecureRandom();
49  
50      /**
51       * Generate the access token.
52       * @return The access token.
53       */
54      public String generateAccessToken() {
55          return RandomStringUtils.random(ComponentUtil.getFessConfig().getApiAccessTokenLengthAsInteger(), 0, 0, true, true, null, random);
56      }
57  
58      /**
59       * Get the access token from the request.
60       * @param request The request.
61       * @return The access token.
62       */
63      public String getAccessTokenFromRequest(final HttpServletRequest request) {
64          final String token = request.getHeader("Authorization");
65          if (token != null) {
66              final String[] values = token.trim().split(" ");
67              if (values.length == 2 && BEARER.equals(values[0])) {
68                  return values[1];
69              }
70              if (values.length == 1 && !BEARER.equals(values[0])) {
71                  return values[0];
72              }
73              throw new InvalidAccessTokenException("invalid_request", "Invalid format: " + token);
74          }
75          final String name = ComponentUtil.getFessConfig().getApiAccessTokenRequestParameter();
76          if (StringUtil.isNotBlank(name)) {
77              return request.getParameter(name);
78          }
79          return null;
80      }
81  
82      /**
83       * Set the random instance.
84       * @param random The random instance.
85       */
86      public void setRandom(final Random random) {
87          this.random = random;
88      }
89  }