github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/jersey2-experimental/auth/HttpBearerAuth.mustache (about)

     1  {{>licenseInfo}}
     2  
     3  package {{invokerPackage}}.auth;
     4  
     5  import {{invokerPackage}}.Pair;
     6  import {{invokerPackage}}.ApiException;
     7  
     8  import java.net.URI;
     9  import java.util.Map;
    10  import java.util.List;
    11  
    12  {{>generatedAnnotation}}
    13  public class HttpBearerAuth implements Authentication {
    14    private final String scheme;
    15    private String bearerToken;
    16  
    17    public HttpBearerAuth(String scheme) {
    18      this.scheme = scheme;
    19    }
    20  
    21    /**
    22     * Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
    23     *
    24     * @return The bearer token
    25     */
    26    public String getBearerToken() {
    27      return bearerToken;
    28    }
    29  
    30    /**
    31     * Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
    32     *
    33     * @param bearerToken The bearer token to send in the Authorization header
    34     */
    35    public void setBearerToken(String bearerToken) {
    36      this.bearerToken = bearerToken;
    37    }
    38  
    39    @Override
    40    public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException {
    41      if(bearerToken == null) {
    42        return;
    43      }
    44  
    45      headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
    46    }
    47  
    48    private static String upperCaseBearer(String scheme) {
    49      return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
    50    }
    51  }