Skip to content

Commit 3d4b5a1

Browse files
committed
Add support of HTTP basic and API key auth to Java codegen
1 parent 0c3f7a5 commit 3d4b5a1

File tree

7 files changed

+148
-9
lines changed

7 files changed

+148
-9
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ public JavaClientCodegen() {
5050
additionalProperties.put("artifactId", artifactId);
5151
additionalProperties.put("artifactVersion", artifactVersion);
5252

53+
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator);
54+
final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", java.io.File.separator);
55+
5356
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
54-
supportingFiles.add(new SupportingFile("apiInvoker.mustache",
55-
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.java"));
56-
supportingFiles.add(new SupportingFile("JsonUtil.mustache",
57-
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java"));
58-
supportingFiles.add(new SupportingFile("apiException.mustache",
59-
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.java"));
57+
supportingFiles.add(new SupportingFile("apiInvoker.mustache", invokerFolder, "ApiInvoker.java"));
58+
supportingFiles.add(new SupportingFile("JsonUtil.mustache", invokerFolder, "JsonUtil.java"));
59+
supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java"));
60+
supportingFiles.add(new SupportingFile("configuration.mustache", invokerFolder, "Configuration.java"));
61+
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
62+
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
63+
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
6064

6165
languageSpecificPrimitives = new HashSet<String>(
6266
Arrays.asList(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public class {{classname}} {
9292
}
9393

9494
try {
95-
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType);
95+
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
96+
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames);
9697
if(response != null){
9798
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
9899
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import java.io.UnsupportedEncodingException;
3131
import java.text.SimpleDateFormat;
3232
import java.text.ParseException;
3333

34+
import {{invokerPackage}}.auth.Authentication;
35+
3436
public class ApiInvoker {
3537
private static ApiInvoker INSTANCE = new ApiInvoker();
3638
private Map<String, Client> hostMap = new HashMap<String, Client>();
@@ -162,11 +164,12 @@ public class ApiInvoker {
162164
}
163165
}
164166

165-
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException {
167+
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException {
168+
processAuthParams(authNames, queryParams, headerParams);
169+
166170
Client client = getClient(host);
167171
168172
StringBuilder b = new StringBuilder();
169-
170173
for(String key : queryParams.keySet()) {
171174
String value = queryParams.get(key);
172175
if (value != null){
@@ -267,6 +270,14 @@ public class ApiInvoker {
267270
}
268271
}
269272

273+
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
274+
for(String authName : authNames) {
275+
Authentication auth = Configuration.getAuthentication(authName);
276+
if(auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
277+
auth.processParams(queryParams, headerParams);
278+
}
279+
}
280+
270281
private Client getClient(String host) {
271282
if(!hostMap.containsKey(host)) {
272283
Client client = Client.create();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package {{invokerPackage}}.auth;
2+
3+
import java.util.Map;
4+
5+
public class ApiKeyAuth implements Authentication {
6+
private final String location;
7+
private final String paramName;
8+
9+
private String apiKey;
10+
private String apiKeyPrefix;
11+
12+
public ApiKeyAuth(String location, String paramName) {
13+
this.location = location;
14+
this.paramName = paramName;
15+
}
16+
17+
public String getLocation() {
18+
return location;
19+
}
20+
21+
public String getParamName() {
22+
return paramName;
23+
}
24+
25+
public String getApiKey() {
26+
return apiKey;
27+
}
28+
29+
public void setApiKey(String apiKey) {
30+
this.apiKey = apiKey;
31+
}
32+
33+
public String getApiKeyPrefix() {
34+
return apiKeyPrefix;
35+
}
36+
37+
public void setApiKeyPrefix(String apiKeyPrefix) {
38+
this.apiKeyPrefix = apiKeyPrefix;
39+
}
40+
41+
@Override
42+
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
43+
String value;
44+
if (apiKeyPrefix != null) {
45+
value = apiKeyPrefix + " " + apiKey;
46+
} else {
47+
value = apiKey;
48+
}
49+
if (location == "query") {
50+
queryParams.put(paramName, value);
51+
} else if (location == "header") {
52+
headerParams.put(paramName, value);
53+
}
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package {{invokerPackage}}.auth;
2+
3+
import java.util.Map;
4+
5+
public interface Authentication {
6+
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package {{invokerPackage}}.auth;
2+
3+
import java.util.Map;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import javax.xml.bind.DatatypeConverter;
7+
8+
public class HttpBasicAuth implements Authentication {
9+
private String username;
10+
private String password;
11+
12+
public String getUsername() {
13+
return username;
14+
}
15+
16+
public void setUsername(String username) {
17+
this.username = username;
18+
}
19+
20+
public String getPassword() {
21+
return password;
22+
}
23+
24+
public void setPassword(String password) {
25+
this.password = password;
26+
}
27+
28+
@Override
29+
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
30+
try {
31+
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
32+
} catch (UnsupportedEncodingException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package {{invokerPackage}};
2+
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+
10+
public class Configuration {
11+
private static final Map<String, Authentication> AUTH;
12+
13+
static {
14+
AUTH = new HashMap<String, Authentication>();
15+
{{#authMethods}}
16+
{{#isBasic}}AUTH.put("{{name}}", new HttpBasicAuth());{{/isBasic}}
17+
{{#isApiKey}}AUTH.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}
18+
{{#isOAuth}}// TODO: support oauth{{/isOAuth}}
19+
{{/authMethods}}
20+
}
21+
22+
public static Authentication getAuthentication(String authName) {
23+
return AUTH.get(authName);
24+
}
25+
}

0 commit comments

Comments
 (0)