Skip to content

Commit

Permalink
Merge pull request #431 from auth0/support-keys-mgmt-api
Browse files Browse the repository at this point in the history
Added support for Key management API
  • Loading branch information
poovamraj committed May 20, 2022
2 parents 2e8ed01 + b81fc93 commit 33f2b65
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/main/java/com/auth0/client/mgmt/KeysEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.auth0.client.mgmt;

import com.auth0.json.mgmt.Key;
import com.auth0.net.CustomRequest;
import com.auth0.net.EmptyBodyRequest;
import com.auth0.net.Request;
import com.auth0.utils.Asserts;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

import java.util.List;

/**
* Class that provides an implementation of the Keys methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Keys
* <p>
* This class is not thread-safe.
*
* @see ManagementAPI
*/
public class KeysEntity extends BaseManagementEntity {

KeysEntity(OkHttpClient client, HttpUrl baseUrl,
String apiToken) {
super(client, baseUrl, apiToken);
}

/**
* Request all Application Signing Keys.
* A token with read:signing_keys is needed
* See https://auth0.com/docs/api/management/v2#!/Keys/get_signing_keys
*
* @return a Request to execute
*/
public Request<List<Key>> list() {
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addEncodedPathSegments("api/v2/keys/signing");
String url = builder.build().toString();
CustomRequest<List<Key>> request = new CustomRequest<>(this.client, url, "GET", new TypeReference<List<Key>>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}


/**
* Request an Application Signing Key. A token with scope read:signing_keys is needed.
* See https://auth0.com/docs/api/management/v2#!/Keys/get_signing_key
*
* @param kid the id of the Application Signing Key to retrieve.
* @return a Request to execute.
*/
public Request<Key> get(String kid) {
Asserts.assertNotNull(kid, "kid");

HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/keys/signing")
.addPathSegment(kid);
String url = builder.build().toString();
CustomRequest<Key> request = new CustomRequest<>(client, url, "GET", new TypeReference<Key>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}

/**
* Rotate the Application Signing Key.
* A token with scope create:signing_keys and update:signing_keys is needed.
* See https://auth0.com/docs/api/management/v2#!/Keys/post_signing_keys
*
* @return a Request to execute.
*/
public Request<Key> rotate() {
String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/keys/signing/rotate")
.build()
.toString();
CustomRequest<Key> request = new EmptyBodyRequest<>(this.client, url, "POST", new TypeReference<Key>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}

/**
* Revoke an Application Signing Key.
* A token with scope update:signing_keys is needed.
* See https://auth0.com/docs/api/management/v2#!/Keys/put_signing_keys
*
* @param kid the id of the Application Signing Key to revoke.
* @return a Request to execute.
*/
public Request<Key> revoke(String kid) {
Asserts.assertNotNull(kid, "kid");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/keys/signing/")
.addPathSegment(kid)
.addPathSegment("revoke")
.build()
.toString();
CustomRequest<Key> request = new EmptyBodyRequest<>(this.client, url, "PUT", new TypeReference<Key>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,13 @@ public ActionsEntity actions() {
public AttackProtectionEntity attackProtection() {
return new AttackProtectionEntity(client, baseUrl, apiToken);
}

/**
* Getter for the Keys Entity
*
* @return the Keys Entity
*/
public KeysEntity keys() {
return new KeysEntity(client, baseUrl, apiToken);
}
}
175 changes: 175 additions & 0 deletions src/main/java/com/auth0/json/mgmt/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.auth0.json.mgmt;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;

/**
* Class that represents an Auth0 Key object. Related to the {@link com.auth0.client.mgmt.KeysEntity} entity.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Key {

@JsonProperty("kid")
private String kid;

@JsonProperty("cert")
private String cert;

@JsonProperty("pkcs7")
private String pkcs7;

@JsonProperty("current")
private Boolean current;

@JsonProperty("next")
private Boolean next;

@JsonProperty("previous")
private Boolean previous;

@JsonProperty("fingerprint")
private String fingerprint;

@JsonProperty("thumbprint")
private String thumbprint;

@JsonProperty("revoked")
private Boolean revoked;

@JsonProperty("current_since")
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Date currentSince;

@JsonProperty("current_until")
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Date currentUntil;

@JsonProperty("revoked_at")
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Date revokedAt;

@JsonProperty("kid")
public String getKid() {
return kid;
}

@JsonProperty("kid")
public void setKid(String kid) {
this.kid = kid;
}

@JsonProperty("cert")
public String getCert() {
return cert;
}

@JsonProperty("cert")
public void setCert(String cert) {
this.cert = cert;
}

@JsonProperty("pkcs7")
public String getPkcs7() {
return pkcs7;
}

@JsonProperty("pkcs7")
public void setPkcs7(String pkcs7) {
this.pkcs7 = pkcs7;
}

@JsonProperty("current")
public Boolean getCurrent() {
return current;
}

@JsonProperty("current")
public void setCurrent(Boolean current) {
this.current = current;
}

@JsonProperty("next")
public Boolean getNext() {
return next;
}

@JsonProperty("next")
public void setNext(Boolean next) {
this.next = next;
}

@JsonProperty("previous")
public Boolean getPrevious() {
return previous;
}

@JsonProperty("previous")
public void setPrevious(Boolean previous) {
this.previous = previous;
}

@JsonProperty("fingerprint")
public String getFingerprint() {
return fingerprint;
}

@JsonProperty("fingerprint")
public void setFingerprint(String fingerprint) {
this.fingerprint = fingerprint;
}

@JsonProperty("thumbprint")
public String getThumbprint() {
return thumbprint;
}

@JsonProperty("thumbprint")
public void setThumbprint(String thumbprint) {
this.thumbprint = thumbprint;
}

@JsonProperty("revoked")
public Boolean getRevoked() {
return revoked;
}

@JsonProperty("revoked")
public void setRevoked(Boolean revoked) {
this.revoked = revoked;
}

@JsonProperty("current_since")
public Date getCurrentSince() {
return currentSince;
}

@JsonProperty("current_since")
public void setCurrentSince(Date currentSince) {
this.currentSince = currentSince;
}

@JsonProperty("current_until")
public Date getCurrentUntil() {
return currentUntil;
}

@JsonProperty("current_until")
public void setCurrentUntil(Date currentUntil) {
this.currentUntil = currentUntil;
}

@JsonProperty("revoked_at")
public Date getRevokedAt() {
return revokedAt;
}

@JsonProperty("revoked_at")
public void setRevokedAt(Date revokedAt) {
this.revokedAt = revokedAt;
}
}
4 changes: 4 additions & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public class MockServer {
public static final String BREACHED_PASSWORD_SETTINGS = "src/test/resources/mgmt/breached_password_settings.json";
public static final String BRUTE_FORCE_CONFIGURATION = "src/test/resources/mgmt/brute_force_configuration.json";
public static final String SUSPICIOUS_IP_THROTTLING_CONFIGURATION = "src/test/resources/mgmt/suspicious_ip_throttling_configuration.json";
public static final String KEY = "src/test/resources/mgmt/key.json";
public static final String KEY_LIST = "src/test/resources/mgmt/key_list.json";
public static final String KEY_REVOKE = "src/test/resources/mgmt/key_revoke.json";
public static final String KEY_ROTATE = "src/test/resources/mgmt/key_rotate.json";

private final MockWebServer server;

Expand Down
Loading

0 comments on commit 33f2b65

Please sign in to comment.