Skip to content

Commit fffc5d7

Browse files
committed
Move authentications into ApiClient
1 parent 947935f commit fffc5d7

File tree

9 files changed

+255
-214
lines changed

9 files changed

+255
-214
lines changed

modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family;
1717
import javax.ws.rs.core.MediaType;
1818

1919
import java.util.Collection;
20+
import java.util.Collections;
2021
import java.util.Map;
2122
import java.util.Map.Entry;
2223
import java.util.HashMap;
@@ -34,13 +35,18 @@ import java.text.SimpleDateFormat;
3435
import java.text.ParseException;
3536

3637
import {{invokerPackage}}.auth.Authentication;
38+
import {{invokerPackage}}.auth.HttpBasicAuth;
39+
import {{invokerPackage}}.auth.ApiKeyAuth;
40+
import {{invokerPackage}}.auth.OAuth;
3741

3842
public class ApiClient {
3943
private Map<String, Client> hostMap = new HashMap<String, Client>();
4044
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
4145
private boolean debugging = false;
4246
private String basePath = "{{basePath}}";
4347
48+
private Map<String, Authentication> authentications;
49+
4450
private DateFormat dateFormat;
4551
4652
public ApiClient() {
@@ -53,6 +59,14 @@ public class ApiClient {
5359
5460
// Set default User-Agent.
5561
setUserAgent("Java-Swagger");
62+
63+
// Setup authentications (key: authentication name, value: authentication).
64+
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
65+
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
66+
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
67+
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
68+
// Prevent the authentications from being modified.
69+
authentications = Collections.unmodifiableMap(authentications);
5670
}
5771

5872
public String getBasePath() {
@@ -64,6 +78,75 @@ public class ApiClient {
6478
return this;
6579
}
6680

81+
/**
82+
* Get authentications (key: authentication name, value: authentication).
83+
*/
84+
public Map<String, Authentication> getAuthentications() {
85+
return authentications;
86+
}
87+
88+
/**
89+
* Get authentication for the given name.
90+
*
91+
* @param authName The authentication name
92+
* @return The authentication, null if not found
93+
*/
94+
public Authentication getAuthentication(String authName) {
95+
return authentications.get(authName);
96+
}
97+
98+
/**
99+
* Helper method to set username for the first HTTP basic authentication.
100+
*/
101+
public void setUsername(String username) {
102+
for (Authentication auth : authentications.values()) {
103+
if (auth instanceof HttpBasicAuth) {
104+
((HttpBasicAuth) auth).setUsername(username);
105+
return;
106+
}
107+
}
108+
throw new RuntimeException("No HTTP basic authentication configured!");
109+
}
110+
111+
/**
112+
* Helper method to set password for the first HTTP basic authentication.
113+
*/
114+
public void setPassword(String password) {
115+
for (Authentication auth : authentications.values()) {
116+
if (auth instanceof HttpBasicAuth) {
117+
((HttpBasicAuth) auth).setPassword(password);
118+
return;
119+
}
120+
}
121+
throw new RuntimeException("No HTTP basic authentication configured!");
122+
}
123+
124+
/**
125+
* Helper method to set API key value for the first API key authentication.
126+
*/
127+
public void setApiKey(String apiKey) {
128+
for (Authentication auth : authentications.values()) {
129+
if (auth instanceof ApiKeyAuth) {
130+
((ApiKeyAuth) auth).setApiKey(apiKey);
131+
return;
132+
}
133+
}
134+
throw new RuntimeException("No API key authentication configured!");
135+
}
136+
137+
/**
138+
* Helper method to set API key prefix for the first API key authentication.
139+
*/
140+
public void setApiKeyPrefix(String apiKeyPrefix) {
141+
for (Authentication auth : authentications.values()) {
142+
if (auth instanceof ApiKeyAuth) {
143+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
144+
return;
145+
}
146+
}
147+
throw new RuntimeException("No API key authentication configured!");
148+
}
149+
67150
/**
68151
* Set the User-Agent header's value (by adding to the default header map).
69152
*/
@@ -340,8 +423,8 @@ public class ApiClient {
340423
*/
341424
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
342425
for (String authName : authNames) {
343-
Authentication auth = Configuration.getAuthentication(authName);
344-
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
426+
Authentication auth = authentications.get(authName);
427+
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
345428
auth.applyToParams(queryParams, headerParams);
346429
}
347430
}
Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package {{invokerPackage}};
22

3-
import java.util.Map;
4-
import java.util.HashMap;
5-
6-
import {{invokerPackage}}.auth.Authentication;
7-
import {{invokerPackage}}.auth.HttpBasicAuth;
8-
import {{invokerPackage}}.auth.ApiKeyAuth;
9-
import {{invokerPackage}}.auth.OAuth;
10-
113
public class Configuration {
124
private static ApiClient defaultApiClient = new ApiClient();
135
@@ -26,64 +18,4 @@ public class Configuration {
2618
public static void setDefaultApiClient(ApiClient apiClient) {
2719
defaultApiClient = apiClient;
2820
}
29-
30-
private static final Map<String, Authentication> AUTH;
31-
32-
static {
33-
// setup authentications
34-
AUTH = new HashMap<String, Authentication>();
35-
{{#authMethods}}
36-
{{#isBasic}}AUTH.put("{{name}}", new HttpBasicAuth());{{/isBasic}}
37-
{{#isApiKey}}AUTH.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}
38-
{{#isOAuth}}AUTH.put("{{name}}", new OAuth());{{/isOAuth}}
39-
{{/authMethods}}
40-
}
41-
42-
public static Authentication getAuthentication(String authName) {
43-
return AUTH.get(authName);
44-
}
45-
46-
/** Set username for the first HTTP basic authentication. */
47-
public static void setUsername(String username) {
48-
for (Authentication auth : AUTH.values()) {
49-
if (auth instanceof HttpBasicAuth) {
50-
((HttpBasicAuth) auth).setUsername(username);
51-
return;
52-
}
53-
}
54-
throw new RuntimeException("No HTTP basic authentication configured!");
55-
}
56-
57-
/** Set password for the first HTTP basic authentication. */
58-
public static void setPassword(String password) {
59-
for (Authentication auth : AUTH.values()) {
60-
if (auth instanceof HttpBasicAuth) {
61-
((HttpBasicAuth) auth).setPassword(password);
62-
return;
63-
}
64-
}
65-
throw new RuntimeException("No HTTP basic authentication configured!");
66-
}
67-
68-
/** Set API key value for the first API key authentication. */
69-
public static void setApiKey(String apiKey) {
70-
for (Authentication auth : AUTH.values()) {
71-
if (auth instanceof ApiKeyAuth) {
72-
((ApiKeyAuth) auth).setApiKey(apiKey);
73-
return;
74-
}
75-
}
76-
throw new RuntimeException("No API key authentication configured!");
77-
}
78-
79-
/** Set API key prefix for the first API key authentication. */
80-
public static void setApiKeyPrefix(String apiKeyPrefix) {
81-
for (Authentication auth : AUTH.values()) {
82-
if (auth instanceof ApiKeyAuth) {
83-
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
84-
return;
85-
}
86-
}
87-
throw new RuntimeException("No API key authentication configured!");
88-
}
8921
}

samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import javax.ws.rs.core.MediaType;
1818

1919
import java.util.Collection;
20+
import java.util.Collections;
2021
import java.util.Map;
2122
import java.util.Map.Entry;
2223
import java.util.HashMap;
@@ -34,13 +35,18 @@
3435
import java.text.ParseException;
3536

3637
import io.swagger.client.auth.Authentication;
38+
import io.swagger.client.auth.HttpBasicAuth;
39+
import io.swagger.client.auth.ApiKeyAuth;
40+
import io.swagger.client.auth.OAuth;
3741

3842
public class ApiClient {
3943
private Map<String, Client> hostMap = new HashMap<String, Client>();
4044
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
4145
private boolean debugging = false;
4246
private String basePath = "http://petstore.swagger.io/v2";
4347

48+
private Map<String, Authentication> authentications;
49+
4450
private DateFormat dateFormat;
4551

4652
public ApiClient() {
@@ -53,6 +59,13 @@ public ApiClient() {
5359

5460
// Set default User-Agent.
5561
setUserAgent("Java-Swagger");
62+
63+
// Setup authentications (key: authentication name, value: authentication).
64+
authentications = new HashMap<String, Authentication>();
65+
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
66+
authentications.put("petstore_auth", new OAuth());
67+
// Prevent the authentications from being modified.
68+
authentications = Collections.unmodifiableMap(authentications);
5669
}
5770

5871
public String getBasePath() {
@@ -64,6 +77,75 @@ public ApiClient setBasePath(String basePath) {
6477
return this;
6578
}
6679

80+
/**
81+
* Get authentications (key: authentication name, value: authentication).
82+
*/
83+
public Map<String, Authentication> getAuthentications() {
84+
return authentications;
85+
}
86+
87+
/**
88+
* Get authentication for the given name.
89+
*
90+
* @param authName The authentication name
91+
* @return The authentication, null if not found
92+
*/
93+
public Authentication getAuthentication(String authName) {
94+
return authentications.get(authName);
95+
}
96+
97+
/**
98+
* Helper method to set username for the first HTTP basic authentication.
99+
*/
100+
public void setUsername(String username) {
101+
for (Authentication auth : authentications.values()) {
102+
if (auth instanceof HttpBasicAuth) {
103+
((HttpBasicAuth) auth).setUsername(username);
104+
return;
105+
}
106+
}
107+
throw new RuntimeException("No HTTP basic authentication configured!");
108+
}
109+
110+
/**
111+
* Helper method to set password for the first HTTP basic authentication.
112+
*/
113+
public void setPassword(String password) {
114+
for (Authentication auth : authentications.values()) {
115+
if (auth instanceof HttpBasicAuth) {
116+
((HttpBasicAuth) auth).setPassword(password);
117+
return;
118+
}
119+
}
120+
throw new RuntimeException("No HTTP basic authentication configured!");
121+
}
122+
123+
/**
124+
* Helper method to set API key value for the first API key authentication.
125+
*/
126+
public void setApiKey(String apiKey) {
127+
for (Authentication auth : authentications.values()) {
128+
if (auth instanceof ApiKeyAuth) {
129+
((ApiKeyAuth) auth).setApiKey(apiKey);
130+
return;
131+
}
132+
}
133+
throw new RuntimeException("No API key authentication configured!");
134+
}
135+
136+
/**
137+
* Helper method to set API key prefix for the first API key authentication.
138+
*/
139+
public void setApiKeyPrefix(String apiKeyPrefix) {
140+
for (Authentication auth : authentications.values()) {
141+
if (auth instanceof ApiKeyAuth) {
142+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
143+
return;
144+
}
145+
}
146+
throw new RuntimeException("No API key authentication configured!");
147+
}
148+
67149
/**
68150
* Set the User-Agent header's value (by adding to the default header map).
69151
*/
@@ -340,8 +422,8 @@ else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
340422
*/
341423
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
342424
for (String authName : authNames) {
343-
Authentication auth = Configuration.getAuthentication(authName);
344-
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
425+
Authentication auth = authentications.get(authName);
426+
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
345427
auth.applyToParams(queryParams, headerParams);
346428
}
347429
}

0 commit comments

Comments
 (0)