Search in sources :

Example 1 with Response

use of com.djrapitops.plan.delivery.web.resolver.Response 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;
}
Also used : Response(com.djrapitops.plan.delivery.web.resolver.Response) Request(com.djrapitops.plan.delivery.web.resolver.request.Request) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException)

Example 2 with Response

use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.

the class QueryJSONResolver method getResponse.

private Response getResponse(Request request) {
    Optional<Response> cachedResult = checkForCachedResult(request);
    if (cachedResult.isPresent())
        return cachedResult.get();
    InputQueryDto inputQuery = parseInputQuery(request);
    List<InputFilterDto> queries = inputQuery.getFilters();
    Filter.Result result = filters.apply(queries);
    List<Filter.ResultPath> resultPath = result.getInverseResultPath();
    Collections.reverse(resultPath);
    return buildAndStoreResponse(inputQuery.getView(), result, resultPath);
}
Also used : Response(com.djrapitops.plan.delivery.web.resolver.Response) InputQueryDto(com.djrapitops.plan.delivery.domain.datatransfer.InputQueryDto) InputFilterDto(com.djrapitops.plan.delivery.domain.datatransfer.InputFilterDto) Filter(com.djrapitops.plan.storage.database.queries.filter.Filter)

Example 3 with Response

use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.

the class RequestHandler method handle.

@Override
public void handle(HttpExchange exchange) {
    try {
        Response response = getResponse(exchange);
        response.getHeaders().putIfAbsent("Access-Control-Allow-Origin", config.get(WebserverSettings.CORS_ALLOW_ORIGIN));
        response.getHeaders().putIfAbsent("Access-Control-Allow-Methods", "GET, OPTIONS");
        response.getHeaders().putIfAbsent("Access-Control-Allow-Credentials", "true");
        response.getHeaders().putIfAbsent("X-Robots-Tag", "noindex, nofollow");
        ResponseSender sender = new ResponseSender(addresses, exchange, response);
        sender.send();
    } catch (Exception e) {
        if (config.isTrue(PluginSettings.DEV_MODE)) {
            logger.warn("THIS ERROR IS ONLY LOGGED IN DEV MODE:");
            errorLogger.warn(e, ErrorContext.builder().whatToDo("THIS ERROR IS ONLY LOGGED IN DEV MODE").related(exchange.getRequestMethod(), exchange.getRemoteAddress(), exchange.getRequestHeaders(), exchange.getResponseHeaders(), exchange.getRequestURI()).build());
        }
    } finally {
        exchange.close();
    }
}
Also used : Response(com.djrapitops.plan.delivery.web.resolver.Response) IOException(java.io.IOException) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException)

Example 4 with Response

use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.

the class ResponseResolver method tryToGetResponse.

/**
 * @throws NotFoundException   In some cases when page was not found, not all.
 * @throws ForbiddenException  If the user is not allowed to see the page
 * @throws BadRequestException If the request did not have required things.
 */
private Response tryToGetResponse(Request request) {
    if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
        return Response.builder().setStatus(204).build();
    }
    Optional<WebUser> user = request.getUser();
    List<Resolver> foundResolvers = resolverService.getResolvers(request.getPath().asString());
    if (foundResolvers.isEmpty())
        return responseFactory.pageNotFound404();
    for (Resolver resolver : foundResolvers) {
        boolean isAuthRequired = webServer.get().isAuthRequired() && resolver.requiresAuth(request);
        if (isAuthRequired) {
            if (!user.isPresent()) {
                if (webServer.get().isUsingHTTPS()) {
                    throw new WebUserAuthException(FailReason.NO_USER_PRESENT);
                } else {
                    return responseFactory.forbidden403();
                }
            }
            if (resolver.canAccess(request)) {
                Optional<Response> resolved = resolver.resolve(request);
                if (resolved.isPresent())
                    return resolved.get();
            } else {
                return responseFactory.forbidden403();
            }
        } else {
            Optional<Response> resolved = resolver.resolve(request);
            if (resolved.isPresent())
                return resolved.get();
        }
    }
    return responseFactory.pageNotFound404();
}
Also used : Response(com.djrapitops.plan.delivery.web.resolver.Response) RootJSONResolver(com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver) Resolver(com.djrapitops.plan.delivery.web.resolver.Resolver) NoAuthResolver(com.djrapitops.plan.delivery.web.resolver.NoAuthResolver) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser)

Aggregations

Response (com.djrapitops.plan.delivery.web.resolver.Response)4 WebUserAuthException (com.djrapitops.plan.exceptions.WebUserAuthException)3 InputFilterDto (com.djrapitops.plan.delivery.domain.datatransfer.InputFilterDto)1 InputQueryDto (com.djrapitops.plan.delivery.domain.datatransfer.InputQueryDto)1 NoAuthResolver (com.djrapitops.plan.delivery.web.resolver.NoAuthResolver)1 Resolver (com.djrapitops.plan.delivery.web.resolver.Resolver)1 Request (com.djrapitops.plan.delivery.web.resolver.request.Request)1 WebUser (com.djrapitops.plan.delivery.web.resolver.request.WebUser)1 RootJSONResolver (com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver)1 Filter (com.djrapitops.plan.storage.database.queries.filter.Filter)1 IOException (java.io.IOException)1