From 043c1beb30cfd8fbd4b892c19bca18737066a2e1 Mon Sep 17 00:00:00 2001 From: tanya-sinha_atko Date: Wed, 9 Oct 2024 09:21:39 +0530 Subject: [PATCH 1/3] added cyok implementation --- .../com/auth0/client/mgmt/KeysEntity.java | 17 ++++++++ .../com/auth0/net/EmptyBodyVoidRequest.java | 42 +++++++++++++++++++ .../com/auth0/client/mgmt/KeysEntityTest.java | 14 +++++++ 3 files changed, 73 insertions(+) create mode 100644 src/main/java/com/auth0/net/EmptyBodyVoidRequest.java diff --git a/src/main/java/com/auth0/client/mgmt/KeysEntity.java b/src/main/java/com/auth0/client/mgmt/KeysEntity.java index bbac2b13..e62fa780 100644 --- a/src/main/java/com/auth0/client/mgmt/KeysEntity.java +++ b/src/main/java/com/auth0/client/mgmt/KeysEntity.java @@ -3,6 +3,7 @@ import com.auth0.json.mgmt.keys.Key; import com.auth0.net.EmptyBodyRequest; import com.auth0.net.BaseRequest; +import com.auth0.net.EmptyBodyVoidRequest; import com.auth0.net.Request; import com.auth0.net.client.Auth0HttpClient; import com.auth0.net.client.HttpMethod; @@ -100,4 +101,20 @@ public Request revoke(String kid) { return new EmptyBodyRequest<>(this.client, tokenProvider, url, HttpMethod.PUT, new TypeReference() { }); } + + /** + * Perform rekeying operation on the key hierarchy. + * A token with scope create:encryption_keys and update:encryption_keys is needed + * See https://auth0.com/docs/api/management/v2#!/Keys/post-encryption-rekey + * @return a Request to execute. + */ + public Request rekey(){ + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/keys/encryption/rekey") + .build() + .toString(); + + return new EmptyBodyVoidRequest<>(this.client, tokenProvider, url, HttpMethod.POST, new TypeReference() {}); + } } diff --git a/src/main/java/com/auth0/net/EmptyBodyVoidRequest.java b/src/main/java/com/auth0/net/EmptyBodyVoidRequest.java new file mode 100644 index 00000000..f93f8825 --- /dev/null +++ b/src/main/java/com/auth0/net/EmptyBodyVoidRequest.java @@ -0,0 +1,42 @@ +package com.auth0.net; + +import com.auth0.client.mgmt.TokenProvider; +import com.auth0.exception.Auth0Exception; +import com.auth0.net.client.Auth0HttpClient; +import com.auth0.net.client.Auth0HttpResponse; +import com.auth0.net.client.HttpMethod; +import com.auth0.net.client.HttpRequestBody; +import com.fasterxml.jackson.core.type.TypeReference; + +/** + * Request class that does not accept parameters to be sent as part of its body and request doesn't return any value on its success. + * The content type of this request is "application/json". + * + * @param The type expected to be received as part of the response. + * @see BaseRequest + */ +public class EmptyBodyVoidRequest extends BaseRequest { + public EmptyBodyVoidRequest(Auth0HttpClient client, TokenProvider tokenProvider, String url, HttpMethod method, TypeReference tType) { + super(client, tokenProvider, url, method, tType); + } + + @Override + @SuppressWarnings("deprecation") + protected HttpRequestBody createRequestBody() { + return HttpRequestBody.create("application/json", new byte[0]); + } + + @Override + public EmptyBodyVoidRequest addParameter(String name, Object value) { + //do nothing + return this; + } + @Override + protected T parseResponseBody(Auth0HttpResponse response) throws Auth0Exception { + if (!response.isSuccessful()) { + throw super.createResponseException(response); + } + return null; + } + +} diff --git a/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java b/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java index 42243205..1d3aa0c4 100644 --- a/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java @@ -95,4 +95,18 @@ public void shouldRevokeKey() throws Exception { assertThat(response, is(notNullValue())); } + + @Test + public void shouldRekey() throws Exception { + Request request = api.keys().rekey(); + assertThat(request, is(notNullValue())); + + server.emptyResponse(204); + request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/keys/encryption/rekey")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + } } From a1fd41b9946703842edcc982a62b7b48e77d1f5b Mon Sep 17 00:00:00 2001 From: tanya-sinha_atko Date: Wed, 9 Oct 2024 09:27:21 +0530 Subject: [PATCH 2/3] updated method name to postEncryptionRekey --- src/main/java/com/auth0/client/mgmt/KeysEntity.java | 2 +- src/test/java/com/auth0/client/mgmt/KeysEntityTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/KeysEntity.java b/src/main/java/com/auth0/client/mgmt/KeysEntity.java index e62fa780..993ee8f7 100644 --- a/src/main/java/com/auth0/client/mgmt/KeysEntity.java +++ b/src/main/java/com/auth0/client/mgmt/KeysEntity.java @@ -108,7 +108,7 @@ public Request revoke(String kid) { * See https://auth0.com/docs/api/management/v2#!/Keys/post-encryption-rekey * @return a Request to execute. */ - public Request rekey(){ + public Request postEncryptionRekey(){ String url = baseUrl .newBuilder() .addPathSegments("api/v2/keys/encryption/rekey") diff --git a/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java b/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java index 1d3aa0c4..ef1e81c7 100644 --- a/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/KeysEntityTest.java @@ -98,7 +98,7 @@ public void shouldRevokeKey() throws Exception { @Test public void shouldRekey() throws Exception { - Request request = api.keys().rekey(); + Request request = api.keys().postEncryptionRekey(); assertThat(request, is(notNullValue())); server.emptyResponse(204); From f7277b08c366cfaa4d9d68c685923cce7befead8 Mon Sep 17 00:00:00 2001 From: tanya-sinha_atko Date: Wed, 9 Oct 2024 11:16:21 +0530 Subject: [PATCH 3/3] added test cases for EmptyBodyVoidRequest --- .../auth0/net/EmptyBodyVoidRequestTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/java/com/auth0/net/EmptyBodyVoidRequestTest.java diff --git a/src/test/java/com/auth0/net/EmptyBodyVoidRequestTest.java b/src/test/java/com/auth0/net/EmptyBodyVoidRequestTest.java new file mode 100644 index 00000000..ba356b36 --- /dev/null +++ b/src/test/java/com/auth0/net/EmptyBodyVoidRequestTest.java @@ -0,0 +1,53 @@ +package com.auth0.net; + +import com.auth0.client.MockServer; +import com.auth0.client.mgmt.TokenProvider; +import com.auth0.net.client.Auth0HttpClient; +import com.auth0.net.client.DefaultHttpClient; +import com.auth0.net.client.HttpMethod; +import com.fasterxml.jackson.core.type.TypeReference; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CompletableFuture; + +import static com.auth0.client.MockServer.AUTH_TOKENS; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class EmptyBodyVoidRequestTest { + + private Auth0HttpClient client; + private TokenProvider tokenProvider; + private MockServer server; + + @BeforeEach + public void setUp() throws Exception { + client = new DefaultHttpClient.Builder().build(); + server = new MockServer(); + tokenProvider = new TokenProvider() { + @Override + public String getToken() { + return "Bearer abc"; + } + + @Override + public CompletableFuture getTokenAsync() { + return CompletableFuture.completedFuture("Bearer abc"); + } + }; + } + + @Test + public void shouldCreatePOSTRequest() throws Exception { + EmptyBodyVoidRequest request = new EmptyBodyVoidRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.POST, new TypeReference() {}); + assertThat(request, is(notNullValue())); + + server.jsonResponse(AUTH_TOKENS, 200); + Void execute = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + assertThat(recordedRequest.getMethod(), is(HttpMethod.POST.toString())); + assertThat(execute, is(nullValue())); + } +}