Skip to content

Commit df04f2e

Browse files
authored
Added support for GET/PATCH Connection Endpoints (#718)
1 parent ed4e616 commit df04f2e

File tree

12 files changed

+404
-4
lines changed

12 files changed

+404
-4
lines changed

src/main/java/com/auth0/client/mgmt/ConnectionsEntity.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.client.mgmt.filter.ConnectionFilter;
4+
import com.auth0.client.mgmt.filter.EnabledClientsFilter;
45
import com.auth0.json.mgmt.connections.*;
56
import com.auth0.net.BaseRequest;
67
import com.auth0.net.Request;
@@ -370,4 +371,57 @@ public Request<Void> checkConnectionStatus(String connectionId){
370371

371372
return new VoidRequest(client, tokenProvider, url, HttpMethod.GET);
372373
}
374+
375+
/**
376+
* Get the enabled clients for a connection.
377+
* A token with scope read:connections is needed.
378+
* @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>
379+
* @param filter the filter to use. Can be null.
380+
* @return a Request to execute.
381+
*/
382+
public Request<EnabledClientResponse> getEnabledClients(String connectionId, EnabledClientsFilter filter) {
383+
Asserts.assertNotNull(connectionId, "connection id");
384+
385+
HttpUrl.Builder builder = baseUrl
386+
.newBuilder()
387+
.addPathSegments("api/v2/connections")
388+
.addPathSegment(connectionId)
389+
.addPathSegment("clients");
390+
391+
if (filter != null) {
392+
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
393+
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
394+
}
395+
}
396+
String url = builder.build().toString();
397+
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<EnabledClientResponse>() {
398+
});
399+
}
400+
401+
/**
402+
* Update the enabled clients for a connection.
403+
* A token with scope update:connections is needed.
404+
* @see <a href="https://auth0.com/docs/api/management/v2#!/connections/patch-clients">https://auth0.com/docs/api/management/v2#!/connections/patch-clients</a>
405+
*
406+
* @param connectionId the connection id.
407+
* @param enabledClientRequests the enabled client request to set.
408+
* @return a Request to execute.
409+
*/
410+
public Request<Void> updateEnabledClients(String connectionId, List<EnabledClientRequest> enabledClientRequests){
411+
Asserts.assertNotNull(connectionId, "connection id");
412+
Asserts.assertNotEmpty(enabledClientRequests, "enabled client Request");
413+
414+
String url = baseUrl
415+
.newBuilder()
416+
.addPathSegments("api/v2/connections")
417+
.addPathSegment(connectionId)
418+
.addPathSegments("clients")
419+
.build()
420+
.toString();
421+
422+
VoidRequest request = new VoidRequest(client, tokenProvider, url, HttpMethod.PATCH);
423+
request.setBody(enabledClientRequests);
424+
return request;
425+
}
426+
373427
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.auth0.client.mgmt.filter;
2+
3+
public class EnabledClientsFilter extends BaseFilter {
4+
/**
5+
* Include the {@code from} parameter to specify where to start the page selection. Only applicable for endpoints that
6+
* support checkpoint pagination.
7+
*
8+
* @param from the ID from which to start selection. This can be obtained from the {@code next} field returned from
9+
* a checkpoint-paginated result.
10+
* @return this filter instance.
11+
*/
12+
public EnabledClientsFilter withFrom(String from) {
13+
parameters.put("from", from);
14+
return this;
15+
}
16+
17+
/**
18+
* Include the {@code take} parameter to specify the amount of results to return per page. Only applicable for endpoints that
19+
* support checkpoint pagination.
20+
*
21+
* @param take the amount of entries to retrieve per page.
22+
* @return this filter instance.
23+
*/
24+
public EnabledClientsFilter withTake(int take) {
25+
parameters.put("take", take);
26+
return this;
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.auth0.json.mgmt.connections;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
@JsonInclude(JsonInclude.Include.NON_NULL)
8+
@JsonIgnoreProperties(ignoreUnknown = true)
9+
public class Clients {
10+
@JsonProperty("client_id")
11+
private String clientId;
12+
13+
/**
14+
* Default constructor for the Clients class.
15+
*/
16+
public Clients() {
17+
}
18+
19+
/**
20+
* Constructor for the Clients class.
21+
*
22+
* @param clientId the client ID.
23+
*/
24+
public Clients(String clientId) {
25+
this.clientId = clientId;
26+
}
27+
28+
/**
29+
* Getter for the client ID.
30+
*
31+
* @return the client ID.
32+
*/
33+
public String getClientId() {
34+
return clientId;
35+
}
36+
}

src/main/java/com/auth0/json/mgmt/connections/Connection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public String getStrategy() {
120120

121121
/**
122122
* Getter for the list of applications this connection is enabled for.
123+
* <p><b>Deprecated:</b> This field is deprecated and will be removed in future versions.
124+
* Use `updateEnabledClients` and `getEnabledClients` methods instead for managing enabled clients
123125
*
124126
* @return the list of enabled applications.
125127
*/
@@ -130,6 +132,8 @@ public List<String> getEnabledClients() {
130132

131133
/**
132134
* Setter for the list of applications this connection is enabled for.
135+
* <p><b>Deprecated:</b> This field is deprecated and will be removed in future versions.
136+
* Use `updateEnabledClients` and `getEnabledClients` methods instead for managing enabled clients
133137
*
134138
* @param enabledClients the list of enabled applications to set.
135139
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.auth0.json.mgmt.connections;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
@JsonInclude(JsonInclude.Include.NON_NULL)
8+
@JsonIgnoreProperties(ignoreUnknown = true)
9+
public class EnabledClientRequest {
10+
@JsonProperty("client_id")
11+
private String clientId;
12+
13+
@JsonProperty("status")
14+
private boolean status;
15+
16+
/**
17+
* Constructor for the EnabledClientRequest.
18+
* @param clientId
19+
* @param status
20+
*/
21+
public EnabledClientRequest(String clientId, boolean status) {
22+
this.clientId = clientId;
23+
this.status = status;
24+
}
25+
26+
/**
27+
* Getter for the client ID.
28+
*
29+
* @return the client ID.
30+
*/
31+
public String getClientId() {
32+
return clientId;
33+
}
34+
35+
/**
36+
* Getter for the status.
37+
*
38+
* @return the status.
39+
*/
40+
@JsonProperty("status")
41+
public boolean isStatus() {
42+
return status;
43+
}
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.auth0.json.mgmt.connections;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import java.util.List;
8+
9+
@JsonInclude(JsonInclude.Include.NON_NULL)
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
public class EnabledClientResponse {
12+
@JsonProperty("clients")
13+
private List<Clients> clients;
14+
@JsonProperty("next")
15+
private String next;
16+
17+
/**
18+
* Getter for the list of clients.
19+
*
20+
* @return the list of clients.
21+
*/
22+
public List<Clients> getClients() {
23+
return clients;
24+
}
25+
26+
/**
27+
* Setter for the list of clients.
28+
*
29+
* @param clients the list of clients to set.
30+
*/
31+
public void setClients(List<Clients> clients) {
32+
this.clients = clients;
33+
}
34+
35+
/**
36+
* Getter for the next page URL.
37+
*
38+
* @return the next page URL.
39+
*/
40+
public String getNext() {
41+
return next;
42+
}
43+
44+
/**
45+
* Setter for the next page URL.
46+
*
47+
* @param next the next page URL to set.
48+
*/
49+
public void setNext(String next) {
50+
this.next = next;
51+
}
52+
}

src/test/java/com/auth0/client/MockServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class MockServer {
4848
public static final String MGMT_CONNECTION_DEFAULT_SCIM_CONFIGURATION = "src/test/resources/mgmt/default_connection_scim_configuration.json";
4949
public static final String MGMT_CONNECTION_SCIM_TOKENS = "src/test/resources/mgmt/connection_scim_tokens.json";
5050
public static final String MGMT_CONNECTION_SCIM_TOKEN = "src/test/resources/mgmt/connection_scim_token.json";
51+
public static final String MGMT_ENABLED_CLIENTS_FOR_CONNECTION = "src/test/resources/mgmt/enabled_clients_for_connection.json";
5152
public static final String MGMT_DEVICE_CREDENTIALS_LIST = "src/test/resources/mgmt/device_credentials_list.json";
5253
public static final String MGMT_DEVICE_CREDENTIALS = "src/test/resources/mgmt/device_credentials.json";
5354
public static final String MGMT_GRANTS_LIST = "src/test/resources/mgmt/grants_list.json";

src/test/java/com/auth0/client/mgmt/ConnectionsEntityTest.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.client.mgmt.filter.ConnectionFilter;
4+
import com.auth0.client.mgmt.filter.EnabledClientsFilter;
45
import com.auth0.json.mgmt.connections.*;
56
import com.auth0.net.Request;
67
import com.auth0.net.client.HttpMethod;
@@ -545,6 +546,108 @@ public void shouldCheckConnectionStatus() throws Exception {
545546
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
546547
}
547548

549+
@Test
550+
public void shouldThrowOnGetEnabledClientsWithNullId() {
551+
verifyThrows(IllegalArgumentException.class,
552+
() -> api.connections().getEnabledClients(null, new EnabledClientsFilter()),
553+
"'connection id' cannot be null!");
554+
}
555+
556+
@Test
557+
public void shouldGetEnabledClientsWithoutFilter() throws Exception {
558+
Request<EnabledClientResponse> request = api.connections().getEnabledClients("1", null);
559+
assertThat(request, is(notNullValue()));
560+
561+
server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200);
562+
EnabledClientResponse response = request.execute().getBody();
563+
RecordedRequest recordedRequest = server.takeRequest();
564+
565+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients"));
566+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
567+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
568+
569+
assertThat(response, is(notNullValue()));
570+
assertThat(response.getClients(), hasSize(2));
571+
}
572+
573+
@Test
574+
public void shouldGetEnabledClientsWithFromFilter() throws Exception {
575+
EnabledClientsFilter filter = new EnabledClientsFilter().withFrom("1");
576+
Request<EnabledClientResponse> request = api.connections().getEnabledClients("1", filter);
577+
assertThat(request, is(notNullValue()));
578+
579+
server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200);
580+
EnabledClientResponse response = request.execute().getBody();
581+
RecordedRequest recordedRequest = server.takeRequest();
582+
583+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients"));
584+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
585+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
586+
assertThat(recordedRequest, hasQueryParameter("from", "1"));
587+
588+
assertThat(response, is(notNullValue()));
589+
assertThat(response.getClients(), hasSize(2));
590+
}
591+
592+
@Test
593+
public void shouldGetEnabledClientsWithTakeFilter() throws Exception {
594+
EnabledClientsFilter filter = new EnabledClientsFilter().withTake(2);
595+
Request<EnabledClientResponse> request = api.connections().getEnabledClients("1", filter);
596+
assertThat(request, is(notNullValue()));
597+
598+
server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200);
599+
EnabledClientResponse response = request.execute().getBody();
600+
RecordedRequest recordedRequest = server.takeRequest();
601+
602+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/connections/1/clients"));
603+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
604+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
605+
assertThat(recordedRequest, hasQueryParameter("take", "2"));
606+
607+
assertThat(response, is(notNullValue()));
608+
assertThat(response.getClients(), hasSize(2));
609+
}
610+
611+
@Test
612+
public void shouldThrowOnUpdateEnabledClientsWithNullId() {
613+
EnabledClientRequest clientRequest = new EnabledClientRequest("clientId", true);
614+
List<EnabledClientRequest> enabledClientRequests = new ArrayList<>();
615+
enabledClientRequests.add(clientRequest);
616+
verifyThrows(IllegalArgumentException.class,
617+
() -> api.connections().updateEnabledClients(null, enabledClientRequests),
618+
"'connection id' cannot be null!");
619+
}
620+
621+
// @Test
622+
// public void shouldThrowOnUpdateEnabledClientsWithNullRequest() {
623+
//; verifyThrows(IllegalArgumentException.class,
624+
// () -> api.connections().updateEnabledClients("1", null),
625+
// "'client id' cannot be null!");
626+
// }
627+
628+
629+
// @Test
630+
// public void shouldUpdateEnabledClients() throws Exception {
631+
// EnabledClientRequest clientRequest = new EnabledClientRequest("clientId", true);
632+
// List<EnabledClientRequest> enabledClientRequests = new ArrayList<>();
633+
// enabledClientRequests.add(clientRequest);
634+
// Request<Void> request = api.connections().updateEnabledClients("1", enabledClientRequests);
635+
// assertThat(request, is(notNullValue()));
636+
//
637+
// server.jsonResponse(MGMT_ENABLED_CLIENTS_FOR_CONNECTION, 200);
638+
// request.execute().getBody();
639+
// RecordedRequest recordedRequest = server.takeRequest();
640+
//
641+
// assertThat(recordedRequest, hasMethodAndPath(HttpMethod.PATCH, "/api/v2/connections/1/clients"));
642+
// assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
643+
// assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
644+
//
645+
// Map<String, Object> body = bodyFromRequest(recordedRequest);
646+
// assertThat(body.size(), is(2));
647+
// assertThat(body, hasEntry("client_id", "clientId"));
648+
// assertThat(body, hasEntry("status", true));
649+
// }
650+
548651
private ScimTokenRequest getScimToken() {
549652
ScimTokenRequest request = new ScimTokenRequest();
550653
List<String> scopes = new ArrayList<>();

0 commit comments

Comments
 (0)