forked from loafle/openapi-generator-original
Refactor auth method naming, add comments
This commit is contained in:
parent
6eb1a89205
commit
e10e1f5249
@ -166,7 +166,7 @@ public class ApiInvoker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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 {
|
||||||
processAuthParams(authNames, queryParams, headerParams);
|
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||||
|
|
||||||
Client client = getClient(host);
|
Client client = getClient(host);
|
||||||
|
|
||||||
@ -266,11 +266,12 @@ public class ApiInvoker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
|
/* Update hearder and query params based on authentication settings. */
|
||||||
|
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
for (String authName : authNames) {
|
for (String authName : authNames) {
|
||||||
Authentication auth = Configuration.getAuthentication(authName);
|
Authentication auth = Configuration.getAuthentication(authName);
|
||||||
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
|
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
|
||||||
auth.processParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ public class ApiKeyAuth implements Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
String value;
|
String value;
|
||||||
if (apiKeyPrefix != null) {
|
if (apiKeyPrefix != null) {
|
||||||
value = apiKeyPrefix + " " + apiKey;
|
value = apiKeyPrefix + " " + apiKey;
|
||||||
|
@ -3,5 +3,6 @@ package {{invokerPackage}}.auth;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface Authentication {
|
public interface Authentication {
|
||||||
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
|
/** Apply authentication settings to header and query params. */
|
||||||
|
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class HttpBasicAuth implements Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
try {
|
try {
|
||||||
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
|
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
@ -4,7 +4,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class OAuth implements Authentication {
|
public class OAuth implements Authentication {
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
// TODO: support oauth
|
// TODO: support oauth
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ public class ApiInvoker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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 {
|
||||||
processAuthParams(authNames, queryParams, headerParams);
|
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||||
|
|
||||||
Client client = getClient(host);
|
Client client = getClient(host);
|
||||||
|
|
||||||
@ -266,11 +266,12 @@ public class ApiInvoker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
|
/* Update hearder and query params based on authentication settings. */
|
||||||
|
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
for (String authName : authNames) {
|
for (String authName : authNames) {
|
||||||
Authentication auth = Configuration.getAuthentication(authName);
|
Authentication auth = Configuration.getAuthentication(authName);
|
||||||
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
|
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
|
||||||
auth.processParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ public class ApiKeyAuth implements Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
String value;
|
String value;
|
||||||
if (apiKeyPrefix != null) {
|
if (apiKeyPrefix != null) {
|
||||||
value = apiKeyPrefix + " " + apiKey;
|
value = apiKeyPrefix + " " + apiKey;
|
||||||
|
@ -3,5 +3,6 @@ package io.swagger.client.auth;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface Authentication {
|
public interface Authentication {
|
||||||
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
|
/** Apply authentication settings to header and query params. */
|
||||||
|
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class HttpBasicAuth implements Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
try {
|
try {
|
||||||
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
|
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
@ -4,7 +4,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class OAuth implements Authentication {
|
public class OAuth implements Authentication {
|
||||||
@Override
|
@Override
|
||||||
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||||
// TODO: support oauth
|
// TODO: support oauth
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,13 @@ import org.junit.*;
|
|||||||
|
|
||||||
public class ApiKeyAuthTest {
|
public class ApiKeyAuthTest {
|
||||||
@Test
|
@Test
|
||||||
public void testProcessParamsInQuery() {
|
public void testApplyToParamsInQuery() {
|
||||||
Map<String, String> queryParams = new HashMap<String, String>();
|
Map<String, String> queryParams = new HashMap<String, String>();
|
||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
|
|
||||||
ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
|
ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
|
||||||
auth.setApiKey("my-api-key");
|
auth.setApiKey("my-api-key");
|
||||||
auth.processParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
|
|
||||||
assertEquals(1, queryParams.size());
|
assertEquals(1, queryParams.size());
|
||||||
assertEquals("my-api-key", queryParams.get("api_key"));
|
assertEquals("my-api-key", queryParams.get("api_key"));
|
||||||
@ -23,14 +23,14 @@ public class ApiKeyAuthTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProcessParamsInHeaderWithPrefix() {
|
public void testApplyToParamsInHeaderWithPrefix() {
|
||||||
Map<String, String> queryParams = new HashMap<String, String>();
|
Map<String, String> queryParams = new HashMap<String, String>();
|
||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
|
|
||||||
ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
|
ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
|
||||||
auth.setApiKey("my-api-token");
|
auth.setApiKey("my-api-token");
|
||||||
auth.setApiKeyPrefix("Token");
|
auth.setApiKeyPrefix("Token");
|
||||||
auth.processParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
|
|
||||||
// no changes to query parameters
|
// no changes to query parameters
|
||||||
assertEquals(0, queryParams.size());
|
assertEquals(0, queryParams.size());
|
||||||
|
@ -15,13 +15,13 @@ public class HttpBasicAuthTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProcessParams() {
|
public void testApplyToParams() {
|
||||||
Map<String, String> queryParams = new HashMap<String, String>();
|
Map<String, String> queryParams = new HashMap<String, String>();
|
||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
|
|
||||||
auth.setUsername("my-username");
|
auth.setUsername("my-username");
|
||||||
auth.setPassword("my-password");
|
auth.setPassword("my-password");
|
||||||
auth.processParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
|
|
||||||
// no changes to query parameters
|
// no changes to query parameters
|
||||||
assertEquals(0, queryParams.size());
|
assertEquals(0, queryParams.size());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user