diff --git a/src/main/java/com/auth0/client/mgmt/KeysEntity.java b/src/main/java/com/auth0/client/mgmt/KeysEntity.java index bbac2b13..993ee8f7 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 postEncryptionRekey(){ + 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..ef1e81c7 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().postEncryptionRekey(); + 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")); + } } 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())); + } +}