use of com.djrapitops.plan.delivery.web.resolver.request.Request in project Plan by plan-player-analytics.
the class RequestHandler method getResponse.
public Response getResponse(HttpExchange exchange) {
if (ipWhitelist == null) {
ipWhitelist = config.isTrue(WebserverSettings.IP_WHITELIST) ? config.get(WebserverSettings.WHITELIST) : Collections.emptyList();
}
String accessor = getAccessorAddress(exchange);
Request request = null;
Response response;
try {
request = buildRequest(exchange);
if (bruteForceGuard.shouldPreventRequest(accessor)) {
response = responseFactory.failedLoginAttempts403();
} else if (!ipWhitelist.isEmpty() && !ipWhitelist.contains(accessor)) {
response = responseFactory.ipWhitelist403(accessor);
logger.info(locale.getString(PluginLang.WEB_SERVER_NOTIFY_IP_WHITELIST_BLOCK, accessor, exchange.getRequestURI().toString()));
} else {
response = responseResolver.getResponse(request);
}
} catch (WebUserAuthException thrownByAuthentication) {
FailReason failReason = thrownByAuthentication.getFailReason();
if (failReason == FailReason.USER_PASS_MISMATCH) {
bruteForceGuard.increaseAttemptCountOnFailedLogin(accessor);
response = responseFactory.badRequest(failReason.getReason(), "/auth/login");
} else {
String from = exchange.getRequestURI().toASCIIString();
String directTo = StringUtils.startsWithAny(from, "/auth/", "/login") ? "/login" : "/login?from=." + from;
response = Response.builder().redirectTo(directTo).setHeader("Set-Cookie", "auth=expired; Path=/; Max-Age=0; SameSite=Lax; Secure;").build();
}
}
if (bruteForceGuard.shouldPreventRequest(accessor)) {
response = responseFactory.failedLoginAttempts403();
}
if (// Not failed
response.getCode() != 401 && // Not blocked
response.getCode() != 403 && request != null && // Logged in
request.getUser().isPresent()) {
bruteForceGuard.resetAttemptCount(accessor);
}
return response;
}
use of com.djrapitops.plan.delivery.web.resolver.request.Request in project Plan by plan-player-analytics.
the class RequestHandler method buildRequest.
private Request buildRequest(HttpExchange exchange) {
String requestMethod = exchange.getRequestMethod();
URIPath path = new URIPath(exchange.getRequestURI().getPath());
URIQuery query = new URIQuery(exchange.getRequestURI().getRawQuery());
byte[] requestBody = readRequestBody(exchange);
WebUser user = getWebUser(exchange);
Map<String, String> headers = getRequestHeaders(exchange);
return new Request(requestMethod, path, query, user, headers, requestBody);
}
use of com.djrapitops.plan.delivery.web.resolver.request.Request in project Plan by plan-player-analytics.
the class GraphsJSONResolver method getGraphJSON.
private JSONStorage.StoredJSON getGraphJSON(Request request, DataID dataID) {
Optional<Long> timestamp = Identifiers.getTimestamp(request);
JSONStorage.StoredJSON storedJSON;
if (request.getQuery().get("server").isPresent()) {
// Can throw BadRequestException
ServerUUID serverUUID = identifiers.getServerUUID(request);
storedJSON = jsonResolverService.resolve(timestamp, dataID, serverUUID, theServerUUID -> generateGraphDataJSONOfType(dataID, theServerUUID, request.getQuery()));
} else {
// Assume network
storedJSON = jsonResolverService.resolve(timestamp, dataID, () -> generateGraphDataJSONOfType(dataID));
}
return storedJSON;
}
use of com.djrapitops.plan.delivery.web.resolver.request.Request in project Plan by plan-player-analytics.
the class QueryJSONResolver method parseInputQueryFromQueryParams.
private InputQueryDto parseInputQueryFromQueryParams(Request request) {
String q = request.getQuery().get("q").orElseThrow(() -> new BadRequestException("'q' parameter not set (expecting json array)"));
try {
String query = URLDecoder.decode(q, "UTF-8");
List<InputFilterDto> queryFilters = InputFilterDto.parse(query, gson);
ViewDto view = request.getQuery().get("view").map(viewJson -> gson.fromJson(viewJson, ViewDto.class)).orElseThrow(() -> new BadRequestException("'view' parameter not set (expecting json object {afterDate, afterTime, beforeDate, beforeTime})"));
return new InputQueryDto(view, queryFilters);
} catch (IOException e) {
throw new BadRequestException("Failed to decode json: '" + q + "', " + e.getMessage());
}
}
use of com.djrapitops.plan.delivery.web.resolver.request.Request in project Plan by plan-player-analytics.
the class PlayerPageResolver method canAccess.
@Override
public boolean canAccess(Request request) {
URIPath path = request.getPath();
WebUser user = request.getUser().orElse(new WebUser(""));
boolean isOwnPage = path.getPart(1).map(nameOrUUID -> {
// name matches user
if (user.getName().equalsIgnoreCase(nameOrUUID))
return true;
return // uuid matches user
uuidUtility.getNameOf(nameOrUUID).map(user.getName()::equalsIgnoreCase).orElse(// uuid or name don't match
false);
}).orElse(// No name or UUID given
true);
return user.hasPermission("page.player.other") || user.hasPermission("page.player.self") && isOwnPage;
}
Aggregations