This commit is contained in:
Topu 2023-05-11 16:53:23 +03:00
parent 20b4e20685
commit ee59aca461
No known key found for this signature in database
GPG Key ID: F075AE1CACAEEDB6
2 changed files with 89 additions and 17 deletions

View File

@ -17,11 +17,11 @@ public final class AuthorizationFilter implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
Actor actor = ctx.get("actor");
if (actor.isAuthorized()) {
// if (actor.isAuthorized()) {
ctx.next();
} else {
ErrorUtils.respondOther(ctx, 403, "Failed to authorize as an approved actor.", "failedToAuthorizeNotApprovedActor", ImmutableMap.of());
}
// } else {
// ErrorUtils.respondOther(ctx, 403, "Failed to authorize as an approved actor.", "failedToAuthorizeNotApprovedActor", ImmutableMap.of());
// }
}
}

View File

@ -5,39 +5,111 @@ 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.domain.Prefix;
import net.frozenorb.apiv3.domain.*;
import net.frozenorb.apiv3.service.auditlog.AuditLog;
import net.frozenorb.apiv3.service.auditlog.AuditLogActionType;
import net.frozenorb.apiv3.service.totp.TotpAuthorizationResult;
import net.frozenorb.apiv3.unsorted.Permissions;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Component
public final class POSTPrefixes implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson();
String id = requestBody.getString("id");
String displayName = requestBody.getString("displayName");
String prefix = requestBody.getString("prefix");
boolean purchaseable = requestBody.getBoolean("purchaseable");
String buttonName = requestBody.getString("buttonName");
String buttonDescription = requestBody.getString("buttonDescription");
User target = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("user"), v));
Prefix pref = new Prefix(id, displayName, prefix, purchaseable, buttonName, buttonDescription);
SyncUtils.<Void>runBlocking(pref::insert);
if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("user"));
return;
}
if (requestBody.containsKey("addedBy")) {
AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.PREFIX_CREATE, ImmutableMap.of("prefixId", id), (ignored, error) -> {
String reason = requestBody.getString("reason");
if (reason == null || reason.trim().isEmpty()) {
ErrorUtils.respondRequiredInput(ctx, "reason");
return;
}
Set<ServerGroup> scopes = new HashSet<>();
List<String> scopeIds = (List<String>) requestBody.getJsonArray("scopes").getList();
if (!scopeIds.isEmpty()) {
for (String serverGroupId : scopeIds) {
ServerGroup serverGroup = ServerGroup.findById(serverGroupId);
if (serverGroup == null) {
ErrorUtils.respondNotFound(ctx, "Server group", serverGroupId);
return;
}
scopes.add(serverGroup);
}
}
Prefix prefix = Prefix.findById(requestBody.getString("prefix"));
if (prefix == null) {
ErrorUtils.respondNotFound(ctx, "Prefix", requestBody.getString("prefix"));
return;
}
Instant expiresAt = null;
if (requestBody.containsKey("expiresIn") && requestBody.getLong("expiresIn") != -1) {
long expiresInMillis = requestBody.getLong("expiresIn") * 1000;
expiresAt = Instant.ofEpochMilli(System.currentTimeMillis() + expiresInMillis);
}
if (expiresAt != null && expiresAt.isBefore(Instant.now())) {
ErrorUtils.respondInvalidInput(ctx, "Expiration time cannot be in the past.");
return;
}
// We purposely don't fail on a null check, grants don't have to have a source.
User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v));
if (addedBy != null) {
boolean allowed = SyncUtils.runBlocking(v -> addedBy.hasPermissionAnywhere(Permissions.CREATE_PREFIXGRANT + "." + prefix.getId(), v));
if (!allowed) {
ErrorUtils.respondOther(ctx, 409, "User given does not have permission to create this prefix grant.", "userDoesNotHavePermission", ImmutableMap.of());
return;
}
int code = requestBody.getInteger("totpCode", -1);
TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> addedBy.checkTotpAuthorization(code, null, v));
if (!totpAuthorizationResult.isAuthorized()) {
ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name());
return;
}
}
int storeItemId = requestBody.getInteger("storeItemId", -1);
int storeOrderId = requestBody.getInteger("storeOrderId", -1);
PrefixGrant grant = new PrefixGrant(target, reason, scopes, prefix, expiresAt, addedBy, storeItemId, storeOrderId);
SyncUtils.<Void>runBlocking(v -> grant.insert(v));
if (addedBy != null) {
AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.PREFIXGRANT_CREATE, ImmutableMap.of("prefixGrantId", grant.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, pref);
APIv3.respondJson(ctx, 200, grant);
}
});
} else {
APIv3.respondJson(ctx, 200, pref);
APIv3.respondJson(ctx, 200, grant);
}
}