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

     1  {{>licenseInfo}}
     2  
     3  package {{invokerPackage}}.auth;
     4  
     5  import {{invokerPackage}}.Pair;
     6  
     7  import java.util.Map;
     8  import java.util.List;
     9  
    10  {{>generatedAnnotation}}
    11  public class ApiKeyAuth implements Authentication {
    12    private final String location;
    13    private final String paramName;
    14  
    15    private String apiKey;
    16    private String apiKeyPrefix;
    17  
    18    public ApiKeyAuth(String location, String paramName) {
    19      this.location = location;
    20      this.paramName = paramName;
    21    }
    22  
    23    public String getLocation() {
    24      return location;
    25    }
    26  
    27    public String getParamName() {
    28      return paramName;
    29    }
    30  
    31    public String getApiKey() {
    32      return apiKey;
    33    }
    34  
    35    public void setApiKey(String apiKey) {
    36      this.apiKey = apiKey;
    37    }
    38  
    39    public String getApiKeyPrefix() {
    40      return apiKeyPrefix;
    41    }
    42  
    43    public void setApiKeyPrefix(String apiKeyPrefix) {
    44      this.apiKeyPrefix = apiKeyPrefix;
    45    }
    46  
    47    @Override
    48    public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
    49      if (apiKey == null) {
    50        return;
    51      }
    52      String value;
    53      if (apiKeyPrefix != null) {
    54        value = apiKeyPrefix + " " + apiKey;
    55      } else {
    56        value = apiKey;
    57      }
    58      if ("query".equals(location)) {
    59        queryParams.add(new Pair(paramName, value));
    60      } else if ("header".equals(location)) {
    61        headerParams.put(paramName, value);
    62      } else if ("cookie".equals(location)) {
    63        cookieParams.put(paramName, value);
    64      }
    65    }
    66  }