Search in sources :

Example 1 with Request

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;
}
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 Request

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);
}
Also used : URIPath(com.djrapitops.plan.delivery.web.resolver.request.URIPath) Request(com.djrapitops.plan.delivery.web.resolver.request.Request) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser) URIQuery(com.djrapitops.plan.delivery.web.resolver.request.URIQuery)

Example 3 with Request

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;
}
Also used : MimeType(com.djrapitops.plan.delivery.web.resolver.MimeType) Request(com.djrapitops.plan.delivery.web.resolver.request.Request) ServerUUID(com.djrapitops.plan.identification.ServerUUID) URIQuery(com.djrapitops.plan.delivery.web.resolver.request.URIQuery) Singleton(javax.inject.Singleton) Response(com.djrapitops.plan.delivery.web.resolver.Response) Inject(javax.inject.Inject) AsyncJSONResolverService(com.djrapitops.plan.delivery.webserver.cache.AsyncJSONResolverService) Resolver(com.djrapitops.plan.delivery.web.resolver.Resolver) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser) Identifiers(com.djrapitops.plan.identification.Identifiers) BadRequestException(com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException) Optional(java.util.Optional) JSONStorage(com.djrapitops.plan.delivery.webserver.cache.JSONStorage) GraphJSONCreator(com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator) Collections(java.util.Collections) DataID(com.djrapitops.plan.delivery.webserver.cache.DataID) ServerUUID(com.djrapitops.plan.identification.ServerUUID) JSONStorage(com.djrapitops.plan.delivery.webserver.cache.JSONStorage)

Example 4 with Request

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());
    }
}
Also used : QueryFilters(com.djrapitops.plan.storage.database.queries.filter.QueryFilters) java.util(java.util) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) URLDecoder(java.net.URLDecoder) ServerUUID(com.djrapitops.plan.identification.ServerUUID) InputQueryDto(com.djrapitops.plan.delivery.domain.datatransfer.InputQueryDto) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) Resolver(com.djrapitops.plan.delivery.web.resolver.Resolver) TimeSettings(com.djrapitops.plan.settings.config.paths.TimeSettings) Gson(com.google.gson.Gson) BadRequestException(com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException) JSONStorage(com.djrapitops.plan.delivery.webserver.cache.JSONStorage) NetworkActivityIndexQueries(com.djrapitops.plan.storage.database.queries.analysis.NetworkActivityIndexQueries) Filter(com.djrapitops.plan.storage.database.queries.filter.Filter) ParseException(java.text.ParseException) Maps(com.djrapitops.plan.utilities.java.Maps) ExtensionQueryResultTableDataQuery(com.djrapitops.plan.extension.implementation.storage.queries.ExtensionQueryResultTableDataQuery) InputFilterDto(com.djrapitops.plan.delivery.domain.datatransfer.InputFilterDto) MimeType(com.djrapitops.plan.delivery.web.resolver.MimeType) ServerInfo(com.djrapitops.plan.identification.ServerInfo) Request(com.djrapitops.plan.delivery.web.resolver.request.Request) TimeAmount(net.playeranalytics.plugin.scheduling.TimeAmount) Formatters(com.djrapitops.plan.delivery.formatting.Formatters) IOException(java.io.IOException) Response(com.djrapitops.plan.delivery.web.resolver.Response) RequestBodyConverter(com.djrapitops.plan.delivery.webserver.RequestBodyConverter) DateMap(com.djrapitops.plan.delivery.domain.DateMap) PlayersTableJSONCreator(com.djrapitops.plan.delivery.rendering.json.PlayersTableJSONCreator) ViewDto(com.djrapitops.plan.delivery.domain.datatransfer.ViewDto) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser) Locale(com.djrapitops.plan.settings.locale.Locale) GeoInfoQueries(com.djrapitops.plan.storage.database.queries.objects.GeoInfoQueries) SessionQueries(com.djrapitops.plan.storage.database.queries.objects.SessionQueries) QueryTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.QueryTablePlayersQuery) GraphJSONCreator(com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator) DisplaySettings(com.djrapitops.plan.settings.config.paths.DisplaySettings) Formatter(com.djrapitops.plan.delivery.formatting.Formatter) ViewDto(com.djrapitops.plan.delivery.domain.datatransfer.ViewDto) InputQueryDto(com.djrapitops.plan.delivery.domain.datatransfer.InputQueryDto) InputFilterDto(com.djrapitops.plan.delivery.domain.datatransfer.InputFilterDto) BadRequestException(com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException) IOException(java.io.IOException)

Example 5 with Request

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;
}
Also used : URIPath(com.djrapitops.plan.delivery.web.resolver.request.URIPath) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) Request(com.djrapitops.plan.delivery.web.resolver.request.Request) Html(com.djrapitops.plan.delivery.rendering.html.Html) UUID(java.util.UUID) Singleton(javax.inject.Singleton) Response(com.djrapitops.plan.delivery.web.resolver.Response) StringUtils(org.apache.commons.lang3.StringUtils) ResponseFactory(com.djrapitops.plan.delivery.webserver.ResponseFactory) Inject(javax.inject.Inject) PluginSettings(com.djrapitops.plan.settings.config.paths.PluginSettings) Resolver(com.djrapitops.plan.delivery.web.resolver.Resolver) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser) Optional(java.util.Optional) UUIDUtility(com.djrapitops.plan.identification.UUIDUtility) URIPath(com.djrapitops.plan.delivery.web.resolver.request.URIPath) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser)

Aggregations

Request (com.djrapitops.plan.delivery.web.resolver.request.Request)5 Response (com.djrapitops.plan.delivery.web.resolver.Response)4 WebUser (com.djrapitops.plan.delivery.web.resolver.request.WebUser)4 Resolver (com.djrapitops.plan.delivery.web.resolver.Resolver)3 Inject (javax.inject.Inject)3 Singleton (javax.inject.Singleton)3 GraphJSONCreator (com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator)2 MimeType (com.djrapitops.plan.delivery.web.resolver.MimeType)2 BadRequestException (com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException)2 URIPath (com.djrapitops.plan.delivery.web.resolver.request.URIPath)2 URIQuery (com.djrapitops.plan.delivery.web.resolver.request.URIQuery)2 JSONStorage (com.djrapitops.plan.delivery.webserver.cache.JSONStorage)2 ServerUUID (com.djrapitops.plan.identification.ServerUUID)2 PlanConfig (com.djrapitops.plan.settings.config.PlanConfig)2 Optional (java.util.Optional)2 DateMap (com.djrapitops.plan.delivery.domain.DateMap)1 InputFilterDto (com.djrapitops.plan.delivery.domain.datatransfer.InputFilterDto)1 InputQueryDto (com.djrapitops.plan.delivery.domain.datatransfer.InputQueryDto)1 ViewDto (com.djrapitops.plan.delivery.domain.datatransfer.ViewDto)1 Formatter (com.djrapitops.plan.delivery.formatting.Formatter)1