Move GET /user/:id/requiresTOTP logic into User to make future expansion easier
This commit is contained in:
parent
39f0b7d665
commit
a5acd2c9ff
@ -21,10 +21,7 @@ import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies;
|
|||||||
import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer;
|
import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer;
|
||||||
import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer;
|
import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer;
|
||||||
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
||||||
import net.frozenorb.apiv3.util.MojangUtils;
|
import net.frozenorb.apiv3.util.*;
|
||||||
import net.frozenorb.apiv3.util.PermissionUtils;
|
|
||||||
import net.frozenorb.apiv3.util.SyncUtils;
|
|
||||||
import net.frozenorb.apiv3.util.UuidUtils;
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@ -216,6 +213,36 @@ public final class User {
|
|||||||
return password != null && hashed.equals(password);
|
return password != null && hashed.equals(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void requiresTotpAuthorization(String ip, SingleResultCallback<RequiresTotpResult> callback) {
|
||||||
|
if (totpSecret == null) {
|
||||||
|
callback.onResult(RequiresTotpResult.NOT_REQUIRED_NOT_SET, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip == null || !IpUtils.isValidIp(ip)) {
|
||||||
|
callback.onResult(RequiresTotpResult.REQUIRED_NO_EXEMPTIONS, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TotpUtils.isPreAuthorized(this, ip, (ipPreAuth, error) -> {
|
||||||
|
if (error != null) {
|
||||||
|
callback.onResult(null, error);
|
||||||
|
} else if (ipPreAuth) {
|
||||||
|
callback.onResult(RequiresTotpResult.NOT_REQUIRED_IP_PRE_AUTHORIZED, null);
|
||||||
|
} else {
|
||||||
|
callback.onResult(RequiresTotpResult.REQUIRED_NO_EXEMPTIONS, null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum RequiresTotpResult {
|
||||||
|
|
||||||
|
NOT_REQUIRED_NOT_SET,
|
||||||
|
NOT_REQUIRED_IP_PRE_AUTHORIZED,
|
||||||
|
REQUIRED_NO_EXEMPTIONS
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Rank getHighestRankAnywhere() {
|
public Rank getHighestRankAnywhere() {
|
||||||
return getHighestRankScoped(null, Grant.findByUserSync(this));
|
return getHighestRankScoped(null, Grant.findByUserSync(this));
|
||||||
}
|
}
|
||||||
|
@ -13,42 +13,29 @@ import net.frozenorb.apiv3.util.TotpUtils;
|
|||||||
public final class GETUserRequiresTOTP implements Handler<RoutingContext> {
|
public final class GETUserRequiresTOTP implements Handler<RoutingContext> {
|
||||||
|
|
||||||
public void handle(RoutingContext ctx) {
|
public void handle(RoutingContext ctx) {
|
||||||
User user = User.findByIdSync(ctx.request().getParam("id"));
|
User.findById(ctx.request().getParam("id"), (user, error) -> {
|
||||||
|
if (error != null) {
|
||||||
|
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id"));
|
||||||
|
} else {
|
||||||
|
String userIp = ctx.request().getParam("userIp");
|
||||||
|
|
||||||
if (user == null) {
|
if (!IpUtils.isValidIp(userIp)) {
|
||||||
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id"));
|
ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.getTotpSecret() == null) {
|
user.requiresTotpAuthorization(userIp, (requiresTotpResult, error2) -> {
|
||||||
APIv3.respondJson(ctx, ImmutableMap.of(
|
if (error2 != null) {
|
||||||
"required", false,
|
ErrorUtils.respondInternalError(ctx, error2);
|
||||||
"message", "User does not have TOTP setup."
|
} else {
|
||||||
));
|
APIv3.respondJson(ctx, ImmutableMap.of(
|
||||||
return;
|
"required", (requiresTotpResult == User.RequiresTotpResult.REQUIRED_NO_EXEMPTIONS),
|
||||||
}
|
"message", requiresTotpResult.name()
|
||||||
|
));
|
||||||
String userIp = ctx.request().getParam("userIp");
|
}
|
||||||
|
});
|
||||||
if (!IpUtils.isValidIp(userIp)) {
|
}
|
||||||
ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid.");
|
});
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockingCallback<Boolean> preAuthorizedCallback = new BlockingCallback<>();
|
|
||||||
TotpUtils.isPreAuthorized(user, userIp, preAuthorizedCallback);
|
|
||||||
|
|
||||||
if (preAuthorizedCallback.get()) {
|
|
||||||
APIv3.respondJson(ctx, ImmutableMap.of(
|
|
||||||
"required", false,
|
|
||||||
"message", "User's ip has already been validated"
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
APIv3.respondJson(ctx, ImmutableMap.of(
|
|
||||||
"required", true,
|
|
||||||
"message", "User has no TOTP exemptions."
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user