Add access token CRUD routes
This commit is contained in:
parent
29a13c1647
commit
e0af72caf5
@ -39,6 +39,10 @@ import net.frozenorb.apiv3.handler.MetricsHandler;
|
||||
import net.frozenorb.apiv3.model.*;
|
||||
import net.frozenorb.apiv3.route.GETDumpsType;
|
||||
import net.frozenorb.apiv3.route.GETWhoAmI;
|
||||
import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId;
|
||||
import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens;
|
||||
import net.frozenorb.apiv3.route.accessTokens.GETAccessTokensId;
|
||||
import net.frozenorb.apiv3.route.accessTokens.POSTAccessTokens;
|
||||
import net.frozenorb.apiv3.route.auditLog.DELETEAuditLogId;
|
||||
import net.frozenorb.apiv3.route.auditLog.GETAuditLog;
|
||||
import net.frozenorb.apiv3.route.auditLog.POSTAuditLog;
|
||||
@ -246,6 +250,12 @@ public final class APIv3 extends AbstractVerticle {
|
||||
|
||||
// TODO: The commented out routes
|
||||
|
||||
http.get("/accessTokens/:id").handler(new GETAccessTokensId());
|
||||
http.get("/accessTokens").handler(new GETAccessTokens());
|
||||
http.post("/accessTokens").blockingHandler(new POSTAccessTokens(), false);
|
||||
//http.put("/accessTokens/:id").blockingHandler(new PUTAccessTokensId(), false);
|
||||
http.delete("/accessTokens/:id").blockingHandler(new DELETEAccessTokensId(), false);
|
||||
|
||||
http.get("/auditLog").handler(new GETAuditLog());
|
||||
http.post("/auditLog").handler(new POSTAuditLog());
|
||||
http.delete("/auditLog/:id").blockingHandler(new DELETEAuditLogId());
|
||||
|
@ -6,6 +6,9 @@ import net.frozenorb.apiv3.model.AuditLogEntry;
|
||||
|
||||
public enum AuditLogActionType {
|
||||
|
||||
ACCESS_TOKEN_CREATE(false),
|
||||
ACCESS_TOKEN_UPDATE(false),
|
||||
ACCESS_TOKEN_DELETE(false),
|
||||
AUDIT_LOG_REVERT(false),
|
||||
BANNED_ASN_CREATE(false),
|
||||
BANNED_ASN_UPDATE(false),
|
||||
|
@ -50,11 +50,11 @@ public final class AccessToken {
|
||||
|
||||
public AccessToken(Server server) {
|
||||
// Can't extract server host code to another line because the call to another constructor must be on the first line.
|
||||
this(UUID.randomUUID().toString().replace("-", ""), server.getId(), ActorType.SERVER, ImmutableList.of(server.getServerIp().split(":")[0]));
|
||||
this(server.getId(), ActorType.SERVER, ImmutableList.of(server.getServerIp().split(":")[0]));
|
||||
}
|
||||
|
||||
public AccessToken(String id, String actorName, ActorType actorType, List<String> lockedIps) {
|
||||
this.id = id;
|
||||
public AccessToken(String actorName, ActorType actorType, List<String> lockedIps) {
|
||||
this.id = UUID.randomUUID().toString().replace("-", "");
|
||||
this.actorName = actorName;
|
||||
this.actorType = actorType;
|
||||
this.lockedIps = lockedIps;
|
||||
|
@ -0,0 +1,48 @@
|
||||
package net.frozenorb.apiv3.route.accessTokens;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
import net.frozenorb.apiv3.auditLog.AuditLog;
|
||||
import net.frozenorb.apiv3.auditLog.AuditLogActionType;
|
||||
import net.frozenorb.apiv3.model.AccessToken;
|
||||
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
||||
import net.frozenorb.apiv3.util.ErrorUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public final class DELETEAccessTokensId implements Handler<RoutingContext> {
|
||||
|
||||
public void handle(RoutingContext ctx) {
|
||||
BlockingCallback<AccessToken> accessTokenCallback = new BlockingCallback<>();
|
||||
AccessToken.findById(ctx.request().getParam("id"), accessTokenCallback);
|
||||
AccessToken accessToken = accessTokenCallback.get();
|
||||
|
||||
if (accessToken == null) {
|
||||
ErrorUtils.respondNotFound(ctx, "Access token", ctx.request().getParam("id"));
|
||||
return;
|
||||
}
|
||||
|
||||
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
|
||||
accessToken.delete(callback);
|
||||
callback.get();
|
||||
|
||||
JsonObject requestBody = ctx.getBodyAsJson();
|
||||
|
||||
if (requestBody.containsKey("addedBy")) {
|
||||
AuditLog.log(UUID.fromString(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.ACCESS_TOKEN_DELETE, ImmutableMap.of("accessTokenId", accessToken.getId()), (ignored, error) -> {
|
||||
if (error != null) {
|
||||
ErrorUtils.respondInternalError(ctx, error);
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessToken);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package net.frozenorb.apiv3.route.accessTokens;
|
||||
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
import net.frozenorb.apiv3.model.AccessToken;
|
||||
import net.frozenorb.apiv3.util.ErrorUtils;
|
||||
|
||||
public final class GETAccessTokens implements Handler<RoutingContext> {
|
||||
|
||||
public void handle(RoutingContext ctx) {
|
||||
AccessToken.findAll((accessTokens, error) -> {
|
||||
if (error != null) {
|
||||
ErrorUtils.respondInternalError(ctx, error);
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessTokens);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package net.frozenorb.apiv3.route.accessTokens;
|
||||
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
import net.frozenorb.apiv3.model.AccessToken;
|
||||
import net.frozenorb.apiv3.util.ErrorUtils;
|
||||
|
||||
public final class GETAccessTokensId implements Handler<RoutingContext> {
|
||||
|
||||
public void handle(RoutingContext ctx) {
|
||||
AccessToken.findById(ctx.request().getParam("id"), (accessToken, error) -> {
|
||||
if (error != null) {
|
||||
ErrorUtils.respondInternalError(ctx, error);
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessToken);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package net.frozenorb.apiv3.route.accessTokens;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
import net.frozenorb.apiv3.actor.ActorType;
|
||||
import net.frozenorb.apiv3.auditLog.AuditLog;
|
||||
import net.frozenorb.apiv3.auditLog.AuditLogActionType;
|
||||
import net.frozenorb.apiv3.model.AccessToken;
|
||||
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
||||
import net.frozenorb.apiv3.util.ErrorUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class POSTAccessTokens implements Handler<RoutingContext> {
|
||||
|
||||
public void handle(RoutingContext ctx) {
|
||||
JsonObject requestBody = ctx.getBodyAsJson();
|
||||
String actorName = requestBody.getString("actorName");
|
||||
ActorType actorType = ActorType.valueOf(requestBody.getString("actorType").toUpperCase());
|
||||
List<String> lockedIps = (List<String>) requestBody.getJsonArray("lockedIps").getList();
|
||||
|
||||
AccessToken accessToken = new AccessToken(actorName, actorType, lockedIps);
|
||||
BlockingCallback<Void> callback = new BlockingCallback<>();
|
||||
accessToken.insert(callback);
|
||||
callback.get();
|
||||
|
||||
if (requestBody.containsKey("addedBy")) {
|
||||
AuditLog.log(UUID.fromString(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.ACCESS_TOKEN_CREATE, ImmutableMap.of("accessTokenActorName", actorName), (ignored, error) -> {
|
||||
if (error != null) {
|
||||
ErrorUtils.respondInternalError(ctx, error);
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessToken);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
APIv3.respondJson(ctx, accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user