Skip to content

Commit

Permalink
[Java] Support cookie-based security schemas in Java clients (#4155)
Browse files Browse the repository at this point in the history
* Adding cookie support and cookie-based AuthKeys to Java clients

* Fix indentation

* Revert accidental change

* Updating samples

* Fixing tests and regenerating samples
  • Loading branch information
atsharp authored and wing328 committed Oct 19, 2019
1 parent ab0b3d9 commit d75d089
Show file tree
Hide file tree
Showing 221 changed files with 2,555 additions and 799 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,57 @@ public class ApiKeyAuthTest {
public void testApplyToParamsInQuery() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey("my-api-key");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

assertEquals(1, queryParams.size());
for (Pair queryParam : queryParams) {
assertEquals("my-api-key", queryParam.getValue());
}

// no changes to header parameters
// no changes to header or cookie parameters
assertEquals(0, headerParams.size());
assertEquals(0, cookieParams.size());
}

@Test
public void testApplyToParamsInHeaderWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
// no changes to query or cookie parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());

assertEquals(1, headerParams.size());
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
}

@Test
public void testApplyToParamsInCookieWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query or header parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());

assertEquals(1, cookieParams.size());
assertEquals("Token my-api-token", cookieParams.get("X-API-TOKEN"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,31 @@ public void setup() {
public void testApplyToParams() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

auth.setUsername("my-username");
auth.setPassword("my-password");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
// no changes to query or cookie parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(1, headerParams.size());
// the string below is base64-encoded result of "my-username:my-password" with the "Basic " prefix
String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
assertEquals(expected, headerParams.get("Authorization"));

// null username should be treated as empty string
auth.setUsername(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of ":my-password" with the "Basic " prefix
expected = "Basic Om15LXBhc3N3b3Jk";
assertEquals(expected, headerParams.get("Authorization"));

// null password should be treated as empty string
auth.setUsername("my-username");
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix
expected = "Basic bXktdXNlcm5hbWU6";
assertEquals(expected, headerParams.get("Authorization"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,55 @@ public class ApiKeyAuthTest {
public void testApplyToParamsInQuery() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey("my-api-key");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

assertEquals(1, queryParams.size());
for (Pair queryParam : queryParams) {
assertEquals("my-api-key", queryParam.getValue());
}

// no changes to header parameters
// no changes to header or cookie parameters
assertEquals(0, headerParams.size());
assertEquals(0, cookieParams.size());
}

@Test
public void testApplyToParamsInHeaderWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(1, headerParams.size());
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
}

@Test
public void testApplyToParamsInCookieWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query or header parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
assertEquals(1, cookieParams.size());
assertEquals("Token my-api-token", cookieParams.get("X-API-TOKEN"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public void setup() {
public void testApplyToParams() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

auth.setUsername("my-username");
auth.setPassword("my-password");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
assertEquals(0, queryParams.size());
Expand All @@ -36,15 +37,15 @@ public void testApplyToParams() {

// null username should be treated as empty string
auth.setUsername(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of ":my-password" with the "Basic " prefix
expected = "Basic Om15LXBhc3N3b3Jk";
assertEquals(expected, headerParams.get("Authorization"));

// null password should be treated as empty string
auth.setUsername("my-username");
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix
expected = "Basic bXktdXNlcm5hbWU6";
assertEquals(expected, headerParams.get("Authorization"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,52 @@ public class ApiKeyAuthTest {
public void testApplyToParamsInQuery() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey("my-api-key");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

assertEquals(1, queryParams.size());
for (Pair queryParam : queryParams) {
assertEquals("my-api-key", queryParam.getValue());
}

// no changes to header parameters
// no changes to header or cookie parameters
assertEquals(0, headerParams.size());
assertEquals(0, cookieParams.size());
}

@Test
public void testApplyToParamsInQueryWithNullValue() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
assertEquals(0, cookieParams.size());
}

@Test
public void testApplyToParamsInHeaderWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
// no changes to query or cookie parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(1, headerParams.size());
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
}
Expand All @@ -63,14 +69,51 @@ public void testApplyToParamsInHeaderWithPrefix() {
public void testApplyToParamsInHeaderWithNullValue() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey(null);
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(0, headerParams.size());
}

@Test
public void testApplyToParamsInCookieWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query or header parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
assertEquals(1, cookieParams.size());
assertEquals("Token my-api-token", cookieParams.get("X-API-TOKEN"));
}

@Test
public void testApplyToParamsInCookieWithNullValue() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN");
auth.setApiKey(null);
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(0, headerParams.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,31 @@ public void setup() {
public void testApplyToParams() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();

auth.setUsername("my-username");
auth.setPassword("my-password");
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);

// no changes to query parameters
// no changes to query or cookie parameters
assertEquals(0, queryParams.size());
assertEquals(0, cookieParams.size());
assertEquals(1, headerParams.size());
// the string below is base64-encoded result of "my-username:my-password" with the "Basic " prefix
String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
assertEquals(expected, headerParams.get("Authorization"));

// null username should be treated as empty string
auth.setUsername(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of ":my-password" with the "Basic " prefix
expected = "Basic Om15LXBhc3N3b3Jk";
assertEquals(expected, headerParams.get("Authorization"));

// null password should be treated as empty string
auth.setUsername("my-username");
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix
expected = "Basic bXktdXNlcm5hbWU6";
assertEquals(expected, headerParams.get("Authorization"));
Expand All @@ -54,7 +56,7 @@ public void testApplyToParams() {
headerParams = new HashMap<String, String>();
auth.setUsername(null);
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams, cookieParams);
// no changes to parameters
assertEquals(0, queryParams.size());
assertEquals(0, headerParams.size());
Expand Down
Loading

0 comments on commit d75d089

Please sign in to comment.