Skip to content

Commit 27925d4

Browse files
committed
made security method more generic for flexibility
1 parent 6eece3d commit 27925d4

1 file changed

Lines changed: 22 additions & 137 deletions

File tree

  • modules/openapi-generator/src/main/resources/Java/libraries/native

modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Lines changed: 22 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,8 @@ public class {{classname}} {
6464
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
6565
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
6666
67-
{{#hasHttpBearerMethods}}
68-
// Per-API Bearer authentication
69-
private String bearerToken;
70-
{{/hasHttpBearerMethods}}
71-
72-
{{#hasHttpBasicMethods}}
73-
// Per-API Basic authentication
74-
private String username;
75-
private String password;
76-
{{/hasHttpBasicMethods}}
77-
78-
{{#hasApiKeyMethods}}
79-
// Per-API API key authentication
80-
private String apiKey;
81-
private String apiKeyPrefix;
82-
{{/hasApiKeyMethods}}
83-
84-
{{#hasOAuthMethods}}
85-
// Per-API OAuth authentication
86-
private String accessToken;
87-
{{/hasOAuthMethods}}
67+
// Custom headers to be sent with every request from this API client
68+
private final Map<String, String> extraHeaders = new java.util.HashMap<>();
8869
8970
public {{classname}}() {
9071
this(Configuration.getDefaultApiClient());
@@ -100,126 +81,27 @@ public class {{classname}} {
10081
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
10182
}
10283

103-
{{#hasHttpBearerMethods}}
10484
/**
105-
* Helper method to set access token for Bearer authentication.
106-
* @param bearerToken Bearer token
107-
* @return {{classname}}
85+
* Add a custom header to be sent with every request from this API client.
86+
* @param name Header name
87+
* @param value Header value
88+
* @return this
10889
*/
109-
public {{classname}} setBearerToken(String bearerToken) {
110-
this.bearerToken = bearerToken;
90+
public {{classname}} addHeader(String name, String value) {
91+
this.extraHeaders.put(name, value);
11192
return this;
11293
}
113-
{{/hasHttpBearerMethods}}
11494

115-
{{#hasHttpBasicMethods}}
11695
/**
117-
* Helper method to set username for HTTP basic authentication.
118-
* @param username Username
119-
* @return {{classname}}
96+
* Remove a custom header.
97+
* @param name Header name
98+
* @return this
12099
*/
121-
public {{classname}} setUsername(String username) {
122-
this.username = username;
100+
public {{classname}} removeHeader(String name) {
101+
this.extraHeaders.remove(name);
123102
return this;
124103
}
125104

126-
/**
127-
* Helper method to set password for HTTP basic authentication.
128-
* @param password Password
129-
* @return {{classname}}
130-
*/
131-
public {{classname}} setPassword(String password) {
132-
this.password = password;
133-
return this;
134-
}
135-
{{/hasHttpBasicMethods}}
136-
137-
{{#hasApiKeyMethods}}
138-
/**
139-
* Helper method to set API key value for API key authentication.
140-
* @param apiKey API key
141-
* @return {{classname}}
142-
*/
143-
public {{classname}} setApiKey(String apiKey) {
144-
this.apiKey = apiKey;
145-
return this;
146-
}
147-
148-
/**
149-
* Helper method to set API key prefix for API key authentication.
150-
* @param apiKeyPrefix API key prefix
151-
* @return {{classname}}
152-
*/
153-
public {{classname}} setApiKeyPrefix(String apiKeyPrefix) {
154-
this.apiKeyPrefix = apiKeyPrefix;
155-
return this;
156-
}
157-
{{/hasApiKeyMethods}}
158-
159-
{{#hasOAuthMethods}}
160-
/**
161-
* Helper method to set access token for OAuth2 authentication.
162-
* @param accessToken Access token
163-
* @return {{classname}}
164-
*/
165-
public {{classname}} setAccessToken(String accessToken) {
166-
this.accessToken = accessToken;
167-
return this;
168-
}
169-
{{/hasOAuthMethods}}
170-
171-
/**
172-
* Apply authentication settings directly to request headers.
173-
* This avoids modifying the shared ApiClient's authentication state.
174-
*/
175-
private void applyAuthToHeaders(HttpRequest.Builder localVarRequestBuilder) {
176-
{{#hasHttpBearerMethods}}
177-
if (bearerToken != null) {
178-
localVarRequestBuilder.header("Authorization", "Bearer " + bearerToken);
179-
}
180-
{{/hasHttpBearerMethods}}
181-
{{#hasHttpBasicMethods}}
182-
if (username != null && password != null) {
183-
String credentials = java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
184-
localVarRequestBuilder.header("Authorization", "Basic " + credentials);
185-
}
186-
{{/hasHttpBasicMethods}}
187-
{{#hasApiKeyMethods}}
188-
if (apiKey != null) {
189-
{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}}
190-
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
191-
localVarRequestBuilder.header("{{keyParamName}}", keyValue);
192-
{{/isKeyInHeader}}{{/isApiKey}}{{/authMethods}}
193-
}
194-
{{/hasApiKeyMethods}}
195-
{{#hasOAuthMethods}}
196-
if (accessToken != null) {
197-
localVarRequestBuilder.header("Authorization", "Bearer " + accessToken);
198-
}
199-
{{/hasOAuthMethods}}
200-
}
201-
202-
/**
203-
* Apply authentication settings directly to query parameters.
204-
* This avoids modifying the shared ApiClient's authentication state.
205-
*/
206-
private String applyAuthToQueryParams(String queryString) {
207-
{{#hasApiKeyMethods}}
208-
if (apiKey != null) {
209-
{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
210-
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
211-
String authParam = "{{keyParamName}}=" + keyValue;
212-
if (queryString != null && !queryString.isEmpty()) {
213-
return queryString + "&" + authParam;
214-
} else {
215-
return authParam;
216-
}
217-
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}
218-
}
219-
{{/hasApiKeyMethods}}
220-
return queryString;
221-
}
222-
223105
{{#asyncNative}}
224106

225107
private ApiException getApiException(String operationId, HttpResponse<String> response) {
@@ -512,7 +394,8 @@ public class {{classname}} {
512394
{{#required}}
513395
// verify the required parameter '{{paramName}}' is set
514396
if ({{paramName}} == null) {
515-
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
397+
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"
398+
);
516399
}
517400
{{/required}}
518401
{{/allParams}}
@@ -582,10 +465,10 @@ public class {{classname}} {
582465
if (localVarQueryStringJoiner.length() != 0) {
583466
queryJoiner.add(localVarQueryStringJoiner.toString());
584467
}
585-
String finalQuery = applyAuthToQueryParams(queryJoiner.toString());
468+
String finalQuery = null; // No longer need to apply auth to query params
586469
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + finalQuery));
587470
} else {
588-
String authQuery = applyAuthToQueryParams(null);
471+
String authQuery = null; // No longer need to apply auth to query params
589472
if (authQuery != null && !authQuery.isEmpty()) {
590473
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + authQuery));
591474
} else {
@@ -594,7 +477,7 @@ public class {{classname}} {
594477
}
595478
{{/hasQueryParams}}
596479
{{^hasQueryParams}}
597-
String authQuery = applyAuthToQueryParams(null);
480+
String authQuery = null; // No longer need to apply auth to query params
598481
if (authQuery != null && !authQuery.isEmpty()) {
599482
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + authQuery));
600483
} else {
@@ -723,8 +606,10 @@ public class {{classname}} {
723606
if (memberVarReadTimeout != null) {
724607
localVarRequestBuilder.timeout(memberVarReadTimeout);
725608
}
726-
// Apply per-API authentication directly to the request
727-
applyAuthToHeaders(localVarRequestBuilder);
609+
// Add custom headers
610+
for (Map.Entry<String, String> entry : extraHeaders.entrySet()) {
611+
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
612+
}
728613
if (memberVarInterceptor != null) {
729614
memberVarInterceptor.accept(localVarRequestBuilder);
730615
}

0 commit comments

Comments
 (0)