diff --git a/src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java b/src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java index 43c07cfaa..2add53c93 100644 --- a/src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java @@ -1,6 +1,7 @@ package com.auth0.client.mgmt; import com.auth0.client.mgmt.filter.ConnectionFilter; +import com.auth0.client.mgmt.filter.EnabledClientsFilter; import com.auth0.json.mgmt.connections.*; import com.auth0.net.BaseRequest; import com.auth0.net.Request; @@ -370,4 +371,57 @@ public Request checkConnectionStatus(String connectionId){ return new VoidRequest(client, tokenProvider, url, HttpMethod.GET); } + + /** + * Get the enabled clients for a connection. + * A token with scope read:connections is needed. + * @see https://auth0.com/docs/api/management/v2#!/connections/get-connection-clients + * @param filter the filter to use. Can be null. + * @return a Request to execute. + */ + public Request getEnabledClients(String connectionId, EnabledClientsFilter filter) { + Asserts.assertNotNull(connectionId, "connection id"); + + HttpUrl.Builder builder = baseUrl + .newBuilder() + .addPathSegments("api/v2/connections") + .addPathSegment(connectionId) + .addPathSegment("clients"); + + if (filter != null) { + for (Map.Entry e : filter.getAsMap().entrySet()) { + builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue())); + } + } + String url = builder.build().toString(); + return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference() { + }); + } + + /** + * Update the enabled clients for a connection. + * A token with scope update:connections is needed. + * @see https://auth0.com/docs/api/management/v2#!/connections/patch-clients + * + * @param connectionId the connection id. + * @param enabledClientRequests the enabled client request to set. + * @return a Request to execute. + */ + public Request updateEnabledClients(String connectionId, List enabledClientRequests){ + Asserts.assertNotNull(connectionId, "connection id"); + Asserts.assertNotEmpty(enabledClientRequests, "enabled client Request"); + + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/connections") + .addPathSegment(connectionId) + .addPathSegments("clients") + .build() + .toString(); + + VoidRequest request = new VoidRequest(client, tokenProvider, url, HttpMethod.PATCH); + request.setBody(enabledClientRequests); + return request; + } + } diff --git a/src/main/java/com/auth0/client/mgmt/filter/EnabledClientsFilter.java b/src/main/java/com/auth0/client/mgmt/filter/EnabledClientsFilter.java new file mode 100644 index 000000000..65cea8a04 --- /dev/null +++ b/src/main/java/com/auth0/client/mgmt/filter/EnabledClientsFilter.java @@ -0,0 +1,28 @@ +package com.auth0.client.mgmt.filter; + +public class EnabledClientsFilter extends BaseFilter { + /** + * Include the {@code from} parameter to specify where to start the page selection. Only applicable for endpoints that + * support checkpoint pagination. + * + * @param from the ID from which to start selection. This can be obtained from the {@code next} field returned from + * a checkpoint-paginated result. + * @return this filter instance. + */ + public EnabledClientsFilter withFrom(String from) { + parameters.put("from", from); + return this; + } + + /** + * Include the {@code take} parameter to specify the amount of results to return per page. Only applicable for endpoints that + * support checkpoint pagination. + * + * @param take the amount of entries to retrieve per page. + * @return this filter instance. + */ + public EnabledClientsFilter withTake(int take) { + parameters.put("take", take); + return this; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/connections/Clients.java b/src/main/java/com/auth0/json/mgmt/connections/Clients.java new file mode 100644 index 000000000..f0d5f0d02 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/connections/Clients.java @@ -0,0 +1,36 @@ +package com.auth0.json.mgmt.connections; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Clients { + @JsonProperty("client_id") + private String clientId; + + /** + * Default constructor for the Clients class. + */ + public Clients() { + } + + /** + * Constructor for the Clients class. + * + * @param clientId the client ID. + */ + public Clients(String clientId) { + this.clientId = clientId; + } + + /** + * Getter for the client ID. + * + * @return the client ID. + */ + public String getClientId() { + return clientId; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/connections/Connection.java b/src/main/java/com/auth0/json/mgmt/connections/Connection.java index a26a9c1af..b40a3dc56 100644 --- a/src/main/java/com/auth0/json/mgmt/connections/Connection.java +++ b/src/main/java/com/auth0/json/mgmt/connections/Connection.java @@ -120,6 +120,8 @@ public String getStrategy() { /** * Getter for the list of applications this connection is enabled for. + *

Deprecated: This field is deprecated and will be removed in future versions. + * Use `updateEnabledClients` and `getEnabledClients` methods instead for managing enabled clients * * @return the list of enabled applications. */ @@ -130,6 +132,8 @@ public List getEnabledClients() { /** * Setter for the list of applications this connection is enabled for. + *

Deprecated: This field is deprecated and will be removed in future versions. + * Use `updateEnabledClients` and `getEnabledClients` methods instead for managing enabled clients * * @param enabledClients the list of enabled applications to set. */ diff --git a/src/main/java/com/auth0/json/mgmt/connections/EnabledClientRequest.java b/src/main/java/com/auth0/json/mgmt/connections/EnabledClientRequest.java new file mode 100644 index 000000000..5845b652e --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/connections/EnabledClientRequest.java @@ -0,0 +1,44 @@ +package com.auth0.json.mgmt.connections; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class EnabledClientRequest { + @JsonProperty("client_id") + private String clientId; + + @JsonProperty("status") + private boolean status; + + /** + * Constructor for the EnabledClientRequest. + * @param clientId + * @param status + */ + public EnabledClientRequest(String clientId, boolean status) { + this.clientId = clientId; + this.status = status; + } + + /** + * Getter for the client ID. + * + * @return the client ID. + */ + public String getClientId() { + return clientId; + } + + /** + * Getter for the status. + * + * @return the status. + */ + @JsonProperty("status") + public boolean isStatus() { + return status; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/connections/EnabledClientResponse.java b/src/main/java/com/auth0/json/mgmt/connections/EnabledClientResponse.java new file mode 100644 index 000000000..c139d95ab --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/connections/EnabledClientResponse.java @@ -0,0 +1,52 @@ +package com.auth0.json.mgmt.connections; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class EnabledClientResponse { + @JsonProperty("clients") + private List clients; + @JsonProperty("next") + private String next; + + /** + * Getter for the list of clients. + * + * @return the list of clients. + */ + public List getClients() { + return clients; + } + + /** + * Setter for the list of clients. + * + * @param clients the list of clients to set. + */ + public void setClients(List clients) { + this.clients = clients; + } + + /** + * Getter for the next page URL. + * + * @return the next page URL. + */ + public String getNext() { + return next; + } + + /** + * Setter for the next page URL. + * + * @param next the next page URL to set. + */ + public void setNext(String next) { + this.next = next; + } +} diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index 93193de93..87c63ba5d 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -48,6 +48,7 @@ public class MockServer { public static final String MGMT_CONNECTION_DEFAULT_SCIM_CONFIGURATION = "src/test/resources/mgmt/default_connection_scim_configuration.json"; public static final String MGMT_CONNECTION_SCIM_TOKENS = "src/test/resources/mgmt/connection_scim_tokens.json"; public static final String MGMT_CONNECTION_SCIM_TOKEN = "src/test/resources/mgmt/connection_scim_token.json"; + public static final String MGMT_ENABLED_CLIENTS_FOR_CONNECTION = "src/test/resources/mgmt/enabled_clients_for_connection.json"; public static final String MGMT_DEVICE_CREDENTIALS_LIST = "src/test/resources/mgmt/device_credentials_list.json"; public static final String MGMT_DEVICE_CREDENTIALS = "src/test/resources/mgmt/device_credentials.json"; public static final String MGMT_GRANTS_LIST = "src/test/resources/mgmt/grants_list.json"; diff --git a/src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java b/src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java index e918723f2..d6ded9060 100644 --- a/src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java @@ -1,6 +1,7 @@ package com.auth0.client.mgmt; import com.auth0.client.mgmt.filter.ConnectionFilter; +import com.auth0.client.mgmt.filter.EnabledClientsFilter; import com.auth0.json.mgmt.connections.*; import com.auth0.net.Request; import com.auth0.net.client.HttpMethod; @@ -545,6 +546,108 @@ public void shouldCheckConnectionStatus() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); } + @Test + public void shouldThrowOnGetEnabledClientsWithNullId() { + verifyThrows(IllegalArgumentException.class, + () -> api.connections().getEnabledClients(null, new EnabledClientsFilter()), + "'connection id' cannot be null!"); + } + + @Test + public void shouldGetEnabledClientsWithoutFilter() throws Exception { + Request request = api.connections().getEnabledClients("1", null); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200); + EnabledClientResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + assertThat(response.getClients(), hasSize(2)); + } + + @Test + public void shouldGetEnabledClientsWithFromFilter() throws Exception { + EnabledClientsFilter filter = new EnabledClientsFilter().withFrom("1"); + Request request = api.connections().getEnabledClients("1", filter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200); + EnabledClientResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("from", "1")); + + assertThat(response, is(notNullValue())); + assertThat(response.getClients(), hasSize(2)); + } + + @Test + public void shouldGetEnabledClientsWithTakeFilter() throws Exception { + EnabledClientsFilter filter = new EnabledClientsFilter().withTake(2); + Request request = api.connections().getEnabledClients("1", filter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200); + EnabledClientResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("take", "2")); + + assertThat(response, is(notNullValue())); + assertThat(response.getClients(), hasSize(2)); + } + + @Test + public void shouldThrowOnUpdateEnabledClientsWithNullId() { + EnabledClientRequest clientRequest = new EnabledClientRequest("clientId", true); + List enabledClientRequests = new ArrayList<>(); + enabledClientRequests.add(clientRequest); + verifyThrows(IllegalArgumentException.class, + () -> api.connections().updateEnabledClients(null, enabledClientRequests), + "'connection id' cannot be null!"); + } + +// @Test +// public void shouldThrowOnUpdateEnabledClientsWithNullRequest() { +//; verifyThrows(IllegalArgumentException.class, +// () -> api.connections().updateEnabledClients("1", null), +// "'client id' cannot be null!"); +// } + + +// @Test +// public void shouldUpdateEnabledClients() throws Exception { +// EnabledClientRequest clientRequest = new EnabledClientRequest("clientId", true); +// List enabledClientRequests = new ArrayList<>(); +// enabledClientRequests.add(clientRequest); +// Request request = api.connections().updateEnabledClients("1", enabledClientRequests); +// assertThat(request, is(notNullValue())); +// +// server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200); +// request.execute().getBody(); +// RecordedRequest recordedRequest = server.takeRequest(); +// +// assertThat(recordedRequest, hasMethodAndPath(HttpMethod.PATCH, "/api/v2/connections/1/clients")); +// assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); +// assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); +// +// Map body = bodyFromRequest(recordedRequest); +// assertThat(body.size(), is(2)); +// assertThat(body, hasEntry("client_id", "clientId")); +// assertThat(body, hasEntry("status", true)); +// } + private ScimTokenRequest getScimToken() { ScimTokenRequest request = new ScimTokenRequest(); List scopes = new ArrayList<>(); diff --git a/src/test/java/com/auth0/json/mgmt/connections/ConnectionTest.java b/src/test/java/com/auth0/json/mgmt/connections/ConnectionTest.java index 2d994bcd0..529f3a86e 100644 --- a/src/test/java/com/auth0/json/mgmt/connections/ConnectionTest.java +++ b/src/test/java/com/auth0/json/mgmt/connections/ConnectionTest.java @@ -22,7 +22,8 @@ public void shouldSerialize() throws Exception { Connection connection = new Connection("my-connection", "auth0"); connection.setDisplayName("COOL!"); connection.setOptions(new HashMap()); - connection.setEnabledClients(Arrays.asList("client1", "client2")); + // enabled_clients is deprecated as part of the new connections API GET/PATCH Enabled Clients +// connection.setEnabledClients(Arrays.asList("client1", "client2")); connection.setMetadata(new HashMap()); connection.setRealms(Arrays.asList("realm1", "realm2")); @@ -32,7 +33,8 @@ public void shouldSerialize() throws Exception { assertThat(serialized, JsonMatcher.hasEntry("display_name", "COOL!")); assertThat(serialized, JsonMatcher.hasEntry("strategy", "auth0")); assertThat(serialized, JsonMatcher.hasEntry("options", notNullValue())); - assertThat(serialized, JsonMatcher.hasEntry("enabled_clients", Arrays.asList("client1", "client2"))); + // enabled_clients is deprecated as part of the new connections API GET/PATCH Enabled Clients +// assertThat(serialized, JsonMatcher.hasEntry("enabled_clients", Arrays.asList("client1", "client2"))); assertThat(serialized, JsonMatcher.hasEntry("provisioning_ticket_url", null)); assertThat(serialized, JsonMatcher.hasEntry("metadata", notNullValue())); assertThat(serialized, JsonMatcher.hasEntry("realms", Arrays.asList("realm1", "realm2"))); @@ -47,7 +49,8 @@ public void shouldDeserialize() throws Exception { assertThat(connection.getDisplayName(), is("My cool connection!")); assertThat(connection.getOptions(), is(notNullValue())); assertThat(connection.getStrategy(), is("auth0")); - assertThat(connection.getEnabledClients(), contains("client1", "client2")); + // enabled_clients is deprecated as part of the new connections API GET/PATCH Enabled Clients +// assertThat(connection.getEnabledClients(), contains("client1", "client2")); assertThat(connection.getProvisioningTicketUrl(), is(nullValue())); assertThat(connection.getMetadata(), is(notNullValue())); assertThat(connection.getRealms(), contains("realm1", "realm2")); @@ -61,7 +64,8 @@ public void shouldDeserializeAd() throws Exception { assertThat(connection.getName(), is("my-ad-connection")); assertThat(connection.getOptions(), is(notNullValue())); assertThat(connection.getStrategy(), is("ad")); - assertThat(connection.getEnabledClients(), contains("client1", "client2")); + // enabled_clients is deprecated as part of the new connections API GET/PATCH Enabled Clients + //assertThat(connection.getEnabledClients(), contains("client1", "client2")); assertThat(connection.getProvisioningTicketUrl(), is("https://demo.auth0.com/p/ad/ddQTRlVt")); assertThat(connection.getMetadata(), is(nullValue())); } diff --git a/src/test/java/com/auth0/json/mgmt/connections/EnabledClientRequestTest.java b/src/test/java/com/auth0/json/mgmt/connections/EnabledClientRequestTest.java new file mode 100644 index 000000000..46164f764 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/connections/EnabledClientRequestTest.java @@ -0,0 +1,23 @@ +package com.auth0.json.mgmt.connections; + +import com.auth0.json.JsonMatcher; +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class EnabledClientRequestTest extends JsonTest { + private static final String json = "{\"client_id\":\"1\",\"status\":true}"; + + @Test + public void shouldSerialize() throws Exception { + EnabledClientRequest enabledClientRequest = new EnabledClientRequest("1", true); + + String serialized = toJSON(enabledClientRequest); + assertThat(serialized, is(notNullValue())); + assertThat(serialized, JsonMatcher.hasEntry("client_id", "1")); + assertThat(serialized, JsonMatcher.hasEntry("status", true)); + } + +} diff --git a/src/test/java/com/auth0/json/mgmt/connections/EnabledClientResponseTest.java b/src/test/java/com/auth0/json/mgmt/connections/EnabledClientResponseTest.java new file mode 100644 index 000000000..e304f7140 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/connections/EnabledClientResponseTest.java @@ -0,0 +1,40 @@ +package com.auth0.json.mgmt.connections; + +import com.auth0.json.JsonMatcher; +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +public class EnabledClientResponseTest extends JsonTest { + private static final String json = "src/test/resources/mgmt/enabled_clients_for_connection.json"; + + @Test + public void shouldSerialize() throws Exception { + EnabledClientResponse enabledClientResponse = new EnabledClientResponse(); + List clientsList = new ArrayList<>(); + clientsList.add(new Clients("client-1")); + clientsList.add(new Clients("client-2")); + enabledClientResponse.setClients(clientsList); + enabledClientResponse.setNext("next"); + + String serialized = toJSON(enabledClientResponse); + assertThat(serialized, is(notNullValue())); + assertThat(serialized, JsonMatcher.hasEntry("next", "next")); + } + + @Test + public void shouldDeserialize() throws Exception { + EnabledClientResponse deserialized = fromJSON(readTextFile(json), EnabledClientResponse.class); + + assertThat(deserialized, is(notNullValue())); + assertThat(deserialized.getClients().get(0).getClientId(), is("client-1")); + assertThat(deserialized.getClients().get(1).getClientId(), is("client-2")); + assertThat(deserialized.getNext(), is("next")); + } +} diff --git a/src/test/resources/mgmt/enabled_clients_for_connection.json b/src/test/resources/mgmt/enabled_clients_for_connection.json new file mode 100644 index 000000000..d45d04a29 --- /dev/null +++ b/src/test/resources/mgmt/enabled_clients_for_connection.json @@ -0,0 +1,11 @@ +{ + "clients": [ + { + "client_id": "client-1" + }, + { + "client_id": "client-2" + } + ], + "next": "next" +}