Skip to content

Commit 6a6473e

Browse files
committed
Allow omitting username/password for HTTP basic auth
1 parent e10e1f5 commit 6a6473e

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public class HttpBasicAuth implements Authentication {
2727

2828
@Override
2929
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
30+
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
3031
try {
31-
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
32+
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary(str.getBytes("UTF-8")));
3233
} catch (UnsupportedEncodingException e) {
3334
throw new RuntimeException(e);
3435
}

samples/client/petstore/java/src/main/java/io/swagger/client/auth/HttpBasicAuth.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public void setPassword(String password) {
2727

2828
@Override
2929
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
30+
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
3031
try {
31-
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
32+
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary(str.getBytes("UTF-8")));
3233
} catch (UnsupportedEncodingException e) {
3334
throw new RuntimeException(e);
3435
}

samples/client/petstore/java/src/test/java/io/swagger/client/auth/HttpBasicAuthTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,22 @@ public void testApplyToParams() {
2727
assertEquals(0, queryParams.size());
2828
assertEquals(1, headerParams.size());
2929
// the string below is base64-encoded result of "my-username:my-password" with the "Basic " prefix
30-
final String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
30+
String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
31+
assertEquals(expected, headerParams.get("Authorization"));
32+
33+
// null username should be treated as empty string
34+
auth.setUsername(null);
35+
auth.applyToParams(queryParams, headerParams);
36+
// the string below is base64-encoded result of ":my-password" with the "Basic " prefix
37+
expected = "Basic Om15LXBhc3N3b3Jk";
38+
assertEquals(expected, headerParams.get("Authorization"));
39+
40+
// null password should be treated as empty string
41+
auth.setUsername("my-username");
42+
auth.setPassword(null);
43+
auth.applyToParams(queryParams, headerParams);
44+
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix
45+
expected = "Basic bXktdXNlcm5hbWU6";
3146
assertEquals(expected, headerParams.get("Authorization"));
3247
}
3348
}

0 commit comments

Comments
 (0)