Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -370,4 +371,57 @@ public Request<Void> 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 <a href="https://auth0.com/docs/api/management/v2#!/connections/get-connection-clients">https://auth0.com/docs/api/management/v2#!/connections/get-connection-clients</a>
* @param filter the filter to use. Can be null.
* @return a Request to execute.
*/
public Request<EnabledClientResponse> 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<String, Object> 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<EnabledClientResponse>() {
});
}

/**
* Update the enabled clients for a connection.
* A token with scope update:connections is needed.
* @see <a href="https://auth0.com/docs/api/management/v2#!/connections/patch-clients">https://auth0.com/docs/api/management/v2#!/connections/patch-clients</a>
*
* @param connectionId the connection id.
* @param enabledClientRequests the enabled client request to set.
* @return a Request to execute.
*/
public Request<Void> updateEnabledClients(String connectionId, List<EnabledClientRequest> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/auth0/json/mgmt/connections/Clients.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public String getStrategy() {

/**
* Getter for the list of applications this connection is enabled for.
* <p><b>Deprecated:</b> 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.
*/
Expand All @@ -130,6 +132,8 @@ public List<String> getEnabledClients() {

/**
* Setter for the list of applications this connection is enabled for.
* <p><b>Deprecated:</b> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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> clients;
@JsonProperty("next")
private String next;

/**
* Getter for the list of clients.
*
* @return the list of clients.
*/
public List<Clients> getClients() {
return clients;
}

/**
* Setter for the list of clients.
*
* @param clients the list of clients to set.
*/
public void setClients(List<Clients> 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;
}
}
1 change: 1 addition & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<EnabledClientResponse> 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<EnabledClientResponse> 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<EnabledClientResponse> 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<EnabledClientRequest> 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<EnabledClientRequest> enabledClientRequests = new ArrayList<>();
// enabledClientRequests.add(clientRequest);
// Request<Void> 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<String, Object> 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<String> scopes = new ArrayList<>();
Expand Down
Loading
Loading