Skip to content

Commit 6eaa43c

Browse files
committed
add bearer capability
1 parent a5f638f commit 6eaa43c

File tree

2 files changed

+208
-6
lines changed

2 files changed

+208
-6
lines changed

modules/openapi-generator/src/main/resources/Java/ApiClient.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
846846
}
847847
}
848848

849+
/**
850+
* Get authentications (key: authentication name, value: authentication).
851+
*
852+
* @return Map of authentication objects
853+
*/
854+
public Map<String, Authentication> getAuthentications() {
855+
return authentications;
856+
}
857+
849858
/**
850859
* Update query and header parameters based on authentication settings.
851860
*

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

Lines changed: 199 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import {{invokerPackage}}.ApiClient;
88
import {{invokerPackage}}.Configuration;
99
import {{modelPackage}}.*;
1010
import {{invokerPackage}}.Pair;
11+
import {{invokerPackage}}.auth.Authentication;
12+
{{#hasHttpBearerMethods}}
13+
import {{invokerPackage}}.auth.HttpBearerAuth;
14+
{{/hasHttpBearerMethods}}
15+
{{#hasHttpBasicMethods}}
16+
import {{invokerPackage}}.auth.HttpBasicAuth;
17+
{{/hasHttpBasicMethods}}
18+
{{#hasApiKeyMethods}}
19+
import {{invokerPackage}}.auth.ApiKeyAuth;
20+
{{/hasApiKeyMethods}}
21+
{{#hasOAuthMethods}}
22+
import {{invokerPackage}}.auth.OAuth;
23+
{{/hasOAuthMethods}}
1124

1225
{{#imports}}import {{import}};
1326
{{/imports}}
@@ -22,6 +35,20 @@ import java.util.Map;
2235
{{#operations}}
2336
public class {{classname}} {
2437
private ApiClient apiClient;
38+
{{#hasHttpBearerMethods}}
39+
private String bearerToken;
40+
{{/hasHttpBearerMethods}}
41+
{{#hasHttpBasicMethods}}
42+
private String username;
43+
private String password;
44+
{{/hasHttpBasicMethods}}
45+
{{#hasApiKeyMethods}}
46+
private String apiKey;
47+
private String apiKeyPrefix;
48+
{{/hasApiKeyMethods}}
49+
{{#hasOAuthMethods}}
50+
private String accessToken;
51+
{{/hasOAuthMethods}}
2552

2653
public {{classname}}() {
2754
this(Configuration.getDefaultApiClient());
@@ -39,6 +66,164 @@ public class {{classname}} {
3966
this.apiClient = apiClient;
4067
}
4168

69+
{{#hasHttpBearerMethods}}
70+
/**
71+
* Helper method to set access token for Bearer authentication.
72+
* @param bearerToken Bearer token
73+
* @return {{classname}}
74+
*/
75+
public {{classname}} setBearerToken(String bearerToken) {
76+
this.bearerToken = bearerToken;
77+
return this;
78+
}
79+
{{/hasHttpBearerMethods}}
80+
81+
{{#hasHttpBasicMethods}}
82+
/**
83+
* Helper method to set username for HTTP basic authentication.
84+
* @param username Username
85+
* @return {{classname}}
86+
*/
87+
public {{classname}} setUsername(String username) {
88+
this.username = username;
89+
return this;
90+
}
91+
92+
/**
93+
* Helper method to set password for HTTP basic authentication.
94+
* @param password Password
95+
* @return {{classname}}
96+
*/
97+
public {{classname}} setPassword(String password) {
98+
this.password = password;
99+
return this;
100+
}
101+
{{/hasHttpBasicMethods}}
102+
103+
{{#hasApiKeyMethods}}
104+
/**
105+
* Helper method to set API key value for API key authentication.
106+
* @param apiKey API key
107+
* @return {{classname}}
108+
*/
109+
public {{classname}} setApiKey(String apiKey) {
110+
this.apiKey = apiKey;
111+
return this;
112+
}
113+
114+
/**
115+
* Helper method to set API key prefix for API key authentication.
116+
* @param apiKeyPrefix API key prefix
117+
* @return {{classname}}
118+
*/
119+
public {{classname}} setApiKeyPrefix(String apiKeyPrefix) {
120+
this.apiKeyPrefix = apiKeyPrefix;
121+
return this;
122+
}
123+
{{/hasApiKeyMethods}}
124+
125+
{{#hasOAuthMethods}}
126+
/**
127+
* Helper method to set access token for OAuth2 authentication.
128+
* @param accessToken Access token
129+
* @return {{classname}}
130+
*/
131+
public {{classname}} setAccessToken(String accessToken) {
132+
this.accessToken = accessToken;
133+
return this;
134+
}
135+
{{/hasOAuthMethods}}
136+
137+
/**
138+
* Apply authentication settings to the API client for this request only.
139+
* This does not modify the shared ApiClient's authentication state.
140+
*/
141+
private void applyAuthToApiClient() {
142+
{{#hasHttpBearerMethods}}
143+
if (bearerToken != null) {
144+
for (Authentication auth : apiClient.getAuthentications().values()) {
145+
if (auth instanceof HttpBearerAuth) {
146+
((HttpBearerAuth) auth).setBearerToken(bearerToken);
147+
break;
148+
}
149+
}
150+
}
151+
{{/hasHttpBearerMethods}}
152+
{{#hasHttpBasicMethods}}
153+
if (username != null || password != null) {
154+
for (Authentication auth : apiClient.getAuthentications().values()) {
155+
if (auth instanceof HttpBasicAuth) {
156+
if (username != null) ((HttpBasicAuth) auth).setUsername(username);
157+
if (password != null) ((HttpBasicAuth) auth).setPassword(password);
158+
break;
159+
}
160+
}
161+
}
162+
{{/hasHttpBasicMethods}}
163+
{{#hasApiKeyMethods}}
164+
if (apiKey != null || apiKeyPrefix != null) {
165+
for (Authentication auth : apiClient.getAuthentications().values()) {
166+
if (auth instanceof ApiKeyAuth) {
167+
if (apiKey != null) ((ApiKeyAuth) auth).setApiKey(apiKey);
168+
if (apiKeyPrefix != null) ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
169+
break;
170+
}
171+
}
172+
}
173+
{{/hasApiKeyMethods}}
174+
{{#hasOAuthMethods}}
175+
if (accessToken != null) {
176+
for (Authentication auth : apiClient.getAuthentications().values()) {
177+
if (auth instanceof OAuth) {
178+
((OAuth) auth).setAccessToken(accessToken);
179+
break;
180+
}
181+
}
182+
}
183+
{{/hasOAuthMethods}}
184+
}
185+
186+
/**
187+
* Clear authentication settings from the API client after the request.
188+
* This ensures no authentication state persists in the shared ApiClient.
189+
*/
190+
private void clearAuthFromApiClient() {
191+
{{#hasHttpBearerMethods}}
192+
for (Authentication auth : apiClient.getAuthentications().values()) {
193+
if (auth instanceof HttpBearerAuth) {
194+
((HttpBearerAuth) auth).setBearerToken((String) null);
195+
break;
196+
}
197+
}
198+
{{/hasHttpBearerMethods}}
199+
{{#hasHttpBasicMethods}}
200+
for (Authentication auth : apiClient.getAuthentications().values()) {
201+
if (auth instanceof HttpBasicAuth) {
202+
((HttpBasicAuth) auth).setUsername(null);
203+
((HttpBasicAuth) auth).setPassword(null);
204+
break;
205+
}
206+
}
207+
{{/hasHttpBasicMethods}}
208+
{{#hasApiKeyMethods}}
209+
for (Authentication auth : apiClient.getAuthentications().values()) {
210+
if (auth instanceof ApiKeyAuth) {
211+
((ApiKeyAuth) auth).setApiKey(null);
212+
((ApiKeyAuth) auth).setApiKeyPrefix(null);
213+
break;
214+
}
215+
}
216+
{{/hasApiKeyMethods}}
217+
{{#hasOAuthMethods}}
218+
for (Authentication auth : apiClient.getAuthentications().values()) {
219+
if (auth instanceof OAuth) {
220+
((OAuth) auth).setAccessToken(null);
221+
break;
222+
}
223+
}
224+
{{/hasOAuthMethods}}
225+
}
226+
42227
{{#operation}}
43228
/**
44229
* {{summary}}
@@ -108,12 +293,20 @@ public class {{classname}} {
108293

109294
String[] localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };
110295

111-
{{#returnType}}
112-
GenericType<{{{returnType}}}> localVarReturnType = new GenericType<{{{returnType}}}>() {};
113-
return apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
114-
{{/returnType}}{{^returnType}}
115-
apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, null);
116-
{{/returnType}}
296+
// Apply authentication for this request only
297+
applyAuthToApiClient();
298+
299+
try {
300+
{{#returnType}}
301+
GenericType<{{{returnType}}}> localVarReturnType = new GenericType<{{{returnType}}}>() {};
302+
return apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
303+
{{/returnType}}{{^returnType}}
304+
apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, null);
305+
{{/returnType}}
306+
} finally {
307+
// Clear authentication to prevent it from persisting in the shared ApiClient
308+
clearAuthFromApiClient();
309+
}
117310
}
118311
{{/operation}}
119312
}

0 commit comments

Comments
 (0)