Add access token CRUD routes

This commit is contained in:
Colin McDonald 2016-06-27 21:07:52 -04:00
parent 29a13c1647
commit e0af72caf5
7 changed files with 150 additions and 3 deletions

View File

@ -39,6 +39,10 @@ import net.frozenorb.apiv3.handler.MetricsHandler;
import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.model.*;
import net.frozenorb.apiv3.route.GETDumpsType; import net.frozenorb.apiv3.route.GETDumpsType;
import net.frozenorb.apiv3.route.GETWhoAmI; 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.DELETEAuditLogId;
import net.frozenorb.apiv3.route.auditLog.GETAuditLog; import net.frozenorb.apiv3.route.auditLog.GETAuditLog;
import net.frozenorb.apiv3.route.auditLog.POSTAuditLog; import net.frozenorb.apiv3.route.auditLog.POSTAuditLog;
@ -246,6 +250,12 @@ public final class APIv3 extends AbstractVerticle {
// TODO: The commented out routes // 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.get("/auditLog").handler(new GETAuditLog());
http.post("/auditLog").handler(new POSTAuditLog()); http.post("/auditLog").handler(new POSTAuditLog());
http.delete("/auditLog/:id").blockingHandler(new DELETEAuditLogId()); http.delete("/auditLog/:id").blockingHandler(new DELETEAuditLogId());

View File

@ -6,6 +6,9 @@ import net.frozenorb.apiv3.model.AuditLogEntry;
public enum AuditLogActionType { public enum AuditLogActionType {
ACCESS_TOKEN_CREATE(false),
ACCESS_TOKEN_UPDATE(false),
ACCESS_TOKEN_DELETE(false),
AUDIT_LOG_REVERT(false), AUDIT_LOG_REVERT(false),
BANNED_ASN_CREATE(false), BANNED_ASN_CREATE(false),
BANNED_ASN_UPDATE(false), BANNED_ASN_UPDATE(false),

View File

@ -50,11 +50,11 @@ public final class AccessToken {
public AccessToken(Server server) { 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. // 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) { public AccessToken(String actorName, ActorType actorType, List<String> lockedIps) {
this.id = id; this.id = UUID.randomUUID().toString().replace("-", "");
this.actorName = actorName; this.actorName = actorName;
this.actorType = actorType; this.actorType = actorType;
this.lockedIps = lockedIps; this.lockedIps = lockedIps;

View File

@ -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);
}
}
}

View File

@ -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);
}
});
}
}

View File

@ -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);
}
});
}
}

View File

@ -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);
}
}
}