Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDKs support for Control Your Own Key #671

Merged
merged 4 commits into from
Oct 16, 2024
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
17 changes: 17 additions & 0 deletions src/main/java/com/auth0/client/mgmt/KeysEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,4 +101,20 @@ public Request<Key> revoke(String kid) {
return new EmptyBodyRequest<>(this.client, tokenProvider, url, HttpMethod.PUT, new TypeReference<Key>() {
});
}

/**
* 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<Void> postEncryptionRekey(){
String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/keys/encryption/rekey")
.build()
.toString();

return new EmptyBodyVoidRequest<>(this.client, tokenProvider, url, HttpMethod.POST, new TypeReference<Void>() {});
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/auth0/net/EmptyBodyVoidRequest.java
Original file line number Diff line number Diff line change
@@ -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 <T> The type expected to be received as part of the response.
* @see BaseRequest
*/
public class EmptyBodyVoidRequest<T> extends BaseRequest<T> {
public EmptyBodyVoidRequest(Auth0HttpClient client, TokenProvider tokenProvider, String url, HttpMethod method, TypeReference<T> tType) {
super(client, tokenProvider, url, method, tType);
}

@Override
@SuppressWarnings("deprecation")
protected HttpRequestBody createRequestBody() {
return HttpRequestBody.create("application/json", new byte[0]);
}

@Override
public EmptyBodyVoidRequest<T> 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;
}

}
14 changes: 14 additions & 0 deletions src/test/java/com/auth0/client/mgmt/KeysEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ public void shouldRevokeKey() throws Exception {

assertThat(response, is(notNullValue()));
}

@Test
public void shouldRekey() throws Exception {
Request<Void> 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"));
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/auth0/net/EmptyBodyVoidRequestTest.java
Original file line number Diff line number Diff line change
@@ -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<String> getTokenAsync() {
return CompletableFuture.completedFuture("Bearer abc");
}
};
}

@Test
public void shouldCreatePOSTRequest() throws Exception {
EmptyBodyVoidRequest<Void> request = new EmptyBodyVoidRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.POST, new TypeReference<Void>() {});
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()));
}
}
Loading