Merge pull request #1603 from xhh/java-ignore-empty-auth-values

[Java] Improvements on authentications
This commit is contained in:
wing328 2015-11-23 15:08:46 +08:00
commit 5183683692
17 changed files with 115 additions and 34 deletions

View File

@ -44,6 +44,9 @@ public class ApiKeyAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (apiKey == null) {
return;
}
String value; String value;
if (apiKeyPrefix != null) { if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey; value = apiKeyPrefix + " " + apiKey;

View File

@ -2,11 +2,12 @@ package {{invokerPackage}}.auth;
import {{invokerPackage}}.Pair; import {{invokerPackage}}.Pair;
import com.migcomponents.migbase64.Base64;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import javax.xml.bind.DatatypeConverter;
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class HttpBasicAuth implements Authentication { public class HttpBasicAuth implements Authentication {
@ -31,9 +32,12 @@ public class HttpBasicAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (username == null && password == null) {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
try { try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary(str.getBytes("UTF-8"))); headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -152,6 +152,13 @@
<version>${jodatime-version}</version> <version>${jodatime-version}</version>
</dependency> </dependency>
<!-- Base64 encoding that works in both JVM and Android -->
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -2,7 +2,7 @@ package {{invokerPackage}}.auth;
import {{invokerPackage}}.Pair; import {{invokerPackage}}.Pair;
import com.migcomponents.migbase64.Base64; import com.squareup.okhttp.Credentials;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@ -31,11 +31,11 @@ public class HttpBasicAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); if (username == null && password == null) {
try { return;
headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} }
headerParams.put("Authorization", Credentials.basic(
username == null ? "" : username,
password == null ? "" : password));
} }
} }

View File

@ -122,11 +122,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${gson-version}</version> <version>${gson-version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>

View File

@ -148,6 +148,13 @@
<version>${jodatime-version}</version> <version>${jodatime-version}</version>
</dependency> </dependency>
<!-- Base64 encoding that works in both JVM and Android -->
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -148,6 +148,13 @@
<version>${jodatime-version}</version> <version>${jodatime-version}</version>
</dependency> </dependency>
<!-- Base64 encoding that works in both JVM and Android -->
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -5,7 +5,7 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-30T16:36:47.681+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-20T17:28:23.285+08:00")
public class ApiKeyAuth implements Authentication { public class ApiKeyAuth implements Authentication {
private final String location; private final String location;
private final String paramName; private final String paramName;
@ -44,6 +44,9 @@ public class ApiKeyAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (apiKey == null) {
return;
}
String value; String value;
if (apiKeyPrefix != null) { if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey; value = apiKeyPrefix + " " + apiKey;

View File

@ -2,13 +2,14 @@ package io.swagger.client.auth;
import io.swagger.client.Pair; import io.swagger.client.Pair;
import com.migcomponents.migbase64.Base64;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import javax.xml.bind.DatatypeConverter;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-30T16:36:47.681+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-22T13:15:32.345+08:00")
public class HttpBasicAuth implements Authentication { public class HttpBasicAuth implements Authentication {
private String username; private String username;
private String password; private String password;
@ -31,9 +32,12 @@ public class HttpBasicAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (username == null && password == null) {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
try { try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary(str.getBytes("UTF-8"))); headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -152,6 +152,13 @@
<version>${jodatime-version}</version> <version>${jodatime-version}</version>
</dependency> </dependency>
<!-- Base64 encoding that works in both JVM and Android -->
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -5,7 +5,7 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-17T11:17:50.535-05:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-20T17:28:47.318+08:00")
public class ApiKeyAuth implements Authentication { public class ApiKeyAuth implements Authentication {
private final String location; private final String location;
private final String paramName; private final String paramName;
@ -44,6 +44,9 @@ public class ApiKeyAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (apiKey == null) {
return;
}
String value; String value;
if (apiKeyPrefix != null) { if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey; value = apiKeyPrefix + " " + apiKey;

View File

@ -2,13 +2,14 @@ package io.swagger.client.auth;
import io.swagger.client.Pair; import io.swagger.client.Pair;
import com.migcomponents.migbase64.Base64;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import javax.xml.bind.DatatypeConverter;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-17T11:17:50.535-05:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-22T13:15:27.225+08:00")
public class HttpBasicAuth implements Authentication { public class HttpBasicAuth implements Authentication {
private String username; private String username;
private String password; private String password;
@ -31,9 +32,12 @@ public class HttpBasicAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (username == null && password == null) {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
try { try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary(str.getBytes("UTF-8"))); headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -122,11 +122,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${gson-version}</version> <version>${gson-version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>

View File

@ -5,7 +5,7 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-20T11:42:25.339-07:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-20T17:28:54.086+08:00")
public class ApiKeyAuth implements Authentication { public class ApiKeyAuth implements Authentication {
private final String location; private final String location;
private final String paramName; private final String paramName;
@ -44,6 +44,9 @@ public class ApiKeyAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (apiKey == null) {
return;
}
String value; String value;
if (apiKeyPrefix != null) { if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey; value = apiKeyPrefix + " " + apiKey;

View File

@ -2,7 +2,7 @@ package io.swagger.client.auth;
import io.swagger.client.Pair; import io.swagger.client.Pair;
import com.migcomponents.migbase64.Base64; import com.squareup.okhttp.Credentials;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@ -31,11 +31,11 @@ public class HttpBasicAuth implements Authentication {
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); if (username == null && password == null) {
try { return;
headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} }
headerParams.put("Authorization", Credentials.basic(
username == null ? "" : username,
password == null ? "" : password));
} }
} }

View File

@ -29,9 +29,23 @@ public class ApiKeyAuthTest {
assertEquals(0, headerParams.size()); assertEquals(0, headerParams.size());
} }
@Test
public void testApplyToParamsInQueryWithNullValue() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey(null);
auth.applyToParams(queryParams, headerParams);
// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
}
@Test @Test
public void testApplyToParamsInHeaderWithPrefix() { public void testApplyToParamsInHeaderWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>(); List<Pair> queryParams = new ArrayList<Pair>();
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");
@ -44,4 +58,19 @@ public class ApiKeyAuthTest {
assertEquals(1, headerParams.size()); assertEquals(1, headerParams.size());
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN")); assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
} }
@Test
public void testApplyToParamsInHeaderWithNullValue() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey(null);
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
}
} }

View File

@ -48,5 +48,15 @@ public class HttpBasicAuthTest {
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix // the string below is base64-encoded result of "my-username:" with the "Basic " prefix
expected = "Basic bXktdXNlcm5hbWU6"; expected = "Basic bXktdXNlcm5hbWU6";
assertEquals(expected, headerParams.get("Authorization")); assertEquals(expected, headerParams.get("Authorization"));
// null username and password should be ignored
queryParams = new ArrayList<Pair>();
headerParams = new HashMap<String, String>();
auth.setUsername(null);
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
} }
} }