This commit is contained in:
Colin McDonald 2016-05-04 15:38:52 -04:00
parent 5d5a1d3468
commit 2d05637103
17 changed files with 86 additions and 31 deletions

17
pom.xml
View File

@ -50,18 +50,33 @@
<dependency> <dependency>
<groupId>com.sparkjava</groupId> <groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId> <artifactId>spark-core</artifactId>
<version>2.3</version> <version>2.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>19.0</version> <version>19.0</version>
</dependency> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.6.2</version> <version>2.6.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.librato.metrics</groupId>
<artifactId>metrics-librato</artifactId>
<version>4.1.2.5</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.mongodb</groupId> <groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId> <artifactId>mongo-java-driver</artifactId>

View File

@ -132,15 +132,16 @@ public final class APIv3 {
//put("/rank/:id", new PUTRank(), gson::toJson); //put("/rank/:id", new PUTRank(), gson::toJson);
delete("/rank/:id", new DELETERank(), gson::toJson); delete("/rank/:id", new DELETERank(), gson::toJson);
delete("/serverGroup/:id", new DELETEServerGroup(), gson::toJson);
get("/serverGroup/:id", new GETServerGroup(), gson::toJson); get("/serverGroup/:id", new GETServerGroup(), gson::toJson);
get("/serverGroups", new GETServerGroups(), gson::toJson); get("/serverGroups", new GETServerGroups(), gson::toJson);
post("/serverGroup", new POSTServerGroup(), gson::toJson); post("/serverGroup", new POSTServerGroup(), gson::toJson);
//put("/serverGroup/:id", new PUTServerGroup(), gson::toJson); //put("/serverGroup/:id", new PUTServerGroup(), gson::toJson);
delete("/serverGroup/:id", new DELETEServerGroup(), gson::toJson);
get("/server/:id", new GETServer(), gson::toJson); get("/server/:id", new GETServer(), gson::toJson);
get("/servers", new GETServers(), gson::toJson); get("/servers", new GETServers(), gson::toJson);
post("/server/heartbeat", new POSTServerHeartbeat(), gson::toJson); post("/server/heartbeat", new POSTServerHeartbeat(), gson::toJson);
post("/server/metrics", new POSTServerMetrics(), gson::toJson);
post("/server", new POSTServer(), gson::toJson); post("/server", new POSTServer(), gson::toJson);
//put("/server/:id", new PUTServer(), gson::toJson); //put("/server/:id", new PUTServer(), gson::toJson);
delete("/server/:id", new DELETEServer(), gson::toJson); delete("/server/:id", new DELETEServer(), gson::toJson);
@ -150,17 +151,17 @@ public final class APIv3 {
get("/user/:id/meta/:serverGroup", new GETUserMeta(), gson::toJson); get("/user/:id/meta/:serverGroup", new GETUserMeta(), gson::toJson);
get("/user/:id/grants", new GETUserGrants(), gson::toJson); get("/user/:id/grants", new GETUserGrants(), gson::toJson);
get("/user/:id/ipLog", new GETUserIPLog(), gson::toJson); get("/user/:id/ipLog", new GETUserIPLog(), gson::toJson);
get("/user/:id/verifyTOTP", new GETUserVerifyTOTP(), gson::toJson); post("/user/:id/verifyTOTP", new POSTUserVerifyTOTP(), gson::toJson);
get("/user/:id", new GETUser(), gson::toJson); get("/user/:id", new GETUser(), gson::toJson);
post("/user/:id:/grant", new POSTUserGrant(), gson::toJson); post("/user/:id:/grant", new POSTUserGrant(), gson::toJson);
post("/user/:id:/punish", new POSTUserPunish(), gson::toJson); post("/user/:id:/punish", new POSTUserPunish(), gson::toJson);
post("/user/:id/loginInfo", new POSTUserLoginInfo(), gson::toJson); post("/user/:id/login", new POSTUserLogin(), gson::toJson);
post("/user/:id/notify", new POSTUserNotify(), gson::toJson); post("/user/:id/notify", new POSTUserNotify(), gson::toJson);
post("/user/:id/register", new POSTUserRegister(), gson::toJson); post("/user/:id/register", new POSTUserRegister(), gson::toJson);
post("/user/:id/setupTOTP", new POSTUserSetupTOTP(), gson::toJson); post("/user/:id/setupTOTP", new POSTUserSetupTOTP(), gson::toJson);
post("/user/confirmRegister/:emailToken", new POSTUserConfirmRegister(), gson::toJson); post("/user/confirmRegister/:emailToken", new POSTUserConfirmRegister(), gson::toJson);
put("/user/:id/meta/:serverGroup", new PUTUserMeta(), gson::toJson); put("/user/:id/meta/:serverGroup", new PUTUserMeta(), gson::toJson);
delete("/user/:id/meta", new DELETEUserMeta(), gson::toJson); delete("/user/:id/meta/:serverGroup", new DELETEUserMeta(), gson::toJson);
// There's no way to do a JSON 404 page w/o doing this :( // There's no way to do a JSON 404 page w/o doing this :(
get("/*", new NotFound(), gson::toJson); get("/*", new NotFound(), gson::toJson);

View File

@ -4,12 +4,6 @@ public interface Actor {
boolean isAuthorized(); boolean isAuthorized();
String getName(); String getName();
Actor.Type getType(); ActorType getType();
enum Type {
WEBSITE, SERVER, USER, UNKNOWN
}
} }

View File

@ -0,0 +1,7 @@
package net.frozenorb.apiv3.actors;
public enum ActorType {
WEBSITE, SERVER, USER, UNKNOWN
}

View File

@ -18,8 +18,8 @@ public final class ServerActor implements Actor {
return server.getId(); return server.getId();
} }
public Actor.Type getType() { public ActorType getType() {
return Actor.Type.SERVER; return ActorType.SERVER;
} }
} }

View File

@ -10,8 +10,8 @@ public final class UnknownActor implements Actor {
return "Unknown"; return "Unknown";
} }
public Actor.Type getType() { public ActorType getType() {
return Actor.Type.UNKNOWN; return ActorType.UNKNOWN;
} }
} }

View File

@ -32,8 +32,8 @@ public final class UserActor implements Actor {
return user.getLastUsername(); return user.getLastUsername();
} }
public Actor.Type getType() { public ActorType getType() {
return Actor.Type.USER; return ActorType.USER;
} }
} }

View File

@ -10,8 +10,8 @@ public final class WebsiteActor implements Actor {
return "Website"; return "Website";
} }
public Actor.Type getType() { public ActorType getType() {
return Actor.Type.WEBSITE; return ActorType.WEBSITE;
} }
} }

View File

@ -3,6 +3,7 @@ package net.frozenorb.apiv3.models;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import lombok.Getter; import lombok.Getter;
import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Entity;
@ -21,7 +22,7 @@ public final class AuditLogEntry {
@Getter private String performedByIp; @Getter private String performedByIp;
@Getter @Indexed private Date performedAt; @Getter @Indexed private Date performedAt;
@Getter private String actorName; @Getter private String actorName;
@Getter private Actor.Type actorType; @Getter private ActorType actorType;
@Getter private AuditLogActionType actionType; @Getter private AuditLogActionType actionType;
@Getter private Map<String, Object> actionData; @Getter private Map<String, Object> actionData;

View File

@ -197,7 +197,8 @@ public final class User {
"reason", accessDenialReason == null ? "Public server" : accessDenialReason "reason", accessDenialReason == null ? "Public server" : accessDenialReason
), ),
"rank", rank, "rank", rank,
"permissions", rankPermissions "permissions", rankPermissions,
"totpRequired", getTotpSecret() != null
); );
} }

View File

@ -1,6 +1,7 @@
package net.frozenorb.apiv3.routes.announcements; package net.frozenorb.apiv3.routes.announcements;
import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.ServerGroup;
import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.ErrorUtils;
@ -13,7 +14,7 @@ public final class GETAnnouncements implements Route {
public Object handle(Request req, Response res) { public Object handle(Request req, Response res) {
Actor actor = req.attribute("actor"); Actor actor = req.attribute("actor");
if (actor.getType() != Actor.Type.SERVER) { if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly(); return ErrorUtils.serverOnly();
} }

View File

@ -1,6 +1,7 @@
package net.frozenorb.apiv3.routes.chatFilterList; package net.frozenorb.apiv3.routes.chatFilterList;
import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.ServerGroup;
import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.ErrorUtils;
@ -13,7 +14,7 @@ public final class GETChatFilterList implements Route {
public Object handle(Request req, Response res) { public Object handle(Request req, Response res) {
Actor actor = req.attribute("actor"); Actor actor = req.attribute("actor");
if (actor.getType() != Actor.Type.SERVER) { if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly(); return ErrorUtils.serverOnly();
} }

View File

@ -3,6 +3,7 @@ package net.frozenorb.apiv3.routes.servers;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.models.User;
import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.ErrorUtils;
@ -19,7 +20,7 @@ public final class POSTServerHeartbeat implements Route {
public Object handle(Request req, Response res) { public Object handle(Request req, Response res) {
Actor actor = req.attribute("actor"); Actor actor = req.attribute("actor");
if (actor.getType() != Actor.Type.SERVER) { if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly(); return ErrorUtils.serverOnly();
} }

View File

@ -0,0 +1,34 @@
package net.frozenorb.apiv3.routes.servers;
import com.google.common.collect.ImmutableMap;
import com.librato.metrics.LibratoBatch;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.User;
import net.frozenorb.apiv3.utils.ErrorUtils;
import org.bson.Document;
import spark.Request;
import spark.Response;
import spark.Route;
import java.util.*;
public final class POSTServerMetrics implements Route {
public Object handle(Request req, Response res) {
Actor actor = req.attribute("actor");
if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly();
}
Server actorServer = Server.byId(actor.getName());
//LibratoBatch batch = new LibratoBatch();
return ImmutableMap.of();
}
}

View File

@ -2,6 +2,7 @@ package net.frozenorb.apiv3.routes.users;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.models.User;
import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.ErrorUtils;
@ -11,7 +12,7 @@ import spark.Route;
import java.util.UUID; import java.util.UUID;
public final class POSTUserLoginInfo implements Route { public final class POSTUserLogin implements Route {
public Object handle(Request req, Response res) { public Object handle(Request req, Response res) {
User user = User.byId(req.params("id")); User user = User.byId(req.params("id"));
@ -19,7 +20,7 @@ public final class POSTUserLoginInfo implements Route {
String userIp = req.queryParams("userIp"); String userIp = req.queryParams("userIp");
Actor actor = req.attribute("actor"); Actor actor = req.attribute("actor");
if (actor.getType() != Actor.Type.SERVER) { if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly(); return ErrorUtils.serverOnly();
} }

View File

@ -8,7 +8,7 @@ import spark.Request;
import spark.Response; import spark.Response;
import spark.Route; import spark.Route;
public final class GETUserVerifyTOTP implements Route { public final class POSTUserVerifyTOTP implements Route {
public Object handle(Request req, Response res) { public Object handle(Request req, Response res) {
User user = User.byId(req.params("id")); User user = User.byId(req.params("id"));

View File

@ -14,9 +14,7 @@ public final class LoggingExceptionHandler implements ExceptionHandler {
public void handle(Exception ex, Request req, Response res) { public void handle(Exception ex, Request req, Response res) {
String code = new ObjectId().toHexString(); String code = new ObjectId().toHexString();
log.error(code + ":"); log.error(code + ":", ex);
ex.printStackTrace();
res.body(APIv3.getGson().toJson(ErrorUtils.error("An unknown error has occurred. Please contact a developer with the code \"" + code + "\"."))); res.body(APIv3.getGson().toJson(ErrorUtils.error("An unknown error has occurred. Please contact a developer with the code \"" + code + "\".")));
} }