diff --git a/src/main/java/org/prebid/cache/handlers/cache/PostCacheHandler.java b/src/main/java/org/prebid/cache/handlers/cache/PostCacheHandler.java index 5f15430..18171c5 100644 --- a/src/main/java/org/prebid/cache/handlers/cache/PostCacheHandler.java +++ b/src/main/java/org/prebid/cache/handlers/cache/PostCacheHandler.java @@ -27,6 +27,7 @@ import org.prebid.cache.repository.CacheConfig; import org.prebid.cache.repository.ReactiveRepository; import org.prebid.cache.routers.ApiConfig; +import org.prebid.cache.routers.ApiRouter; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -106,7 +107,7 @@ public Mono save(final ServerRequest request) { final boolean isValidApiKey = validateApiKey(request); final boolean isValidUserKey = validateUserKey(request); - if (apiConfig.isCacheWriteSecured() && !(isValidApiKey || isValidUserKey)) { + if (apiConfig.isCacheWriteSecured() && !isValidApiKey && !isValidUserKey) { metricsRecorder.markMeterForTag(metricTagPrefix, MeasurementTag.ERROR_UNAUTHORIZED); return ServerResponse.status(HttpStatus.UNAUTHORIZED).build(); } @@ -185,7 +186,8 @@ private boolean validateApiKey(final ServerRequest request) { private boolean validateUserKey(ServerRequest request) { final String userKey = ObjectUtils.firstNonNull( request.headers().firstHeader(USER_KEY_PARAM), - request.queryParams().getFirst(USER_KEY_PARAM)); + request.queryParams().getFirst(USER_KEY_PARAM), + request.pathVariables().get(ApiRouter.USER_CACHE_PATH_PARAM)); final String user = StringUtils.isNotEmpty(userKey) ? userKeys.get(userKey) : null; if (user != null) { diff --git a/src/main/java/org/prebid/cache/routers/ApiRouter.java b/src/main/java/org/prebid/cache/routers/ApiRouter.java index 21388fb..b159551 100644 --- a/src/main/java/org/prebid/cache/routers/ApiRouter.java +++ b/src/main/java/org/prebid/cache/routers/ApiRouter.java @@ -1,6 +1,5 @@ package org.prebid.cache.routers; -import lombok.extern.slf4j.Slf4j; import org.prebid.cache.handlers.ErrorHandler; import org.prebid.cache.handlers.cache.GetCacheHandler; import org.prebid.cache.handlers.cache.PostCacheHandler; @@ -22,9 +21,10 @@ @Configuration @EnableWebFlux @ComponentScan(basePackages = "org.prebid.cache") -@Slf4j public class ApiRouter { + public static final String USER_CACHE_PATH_PARAM = "user"; + @Bean RouterFunction doRoute(final GetCacheHandler getCacheHandler, final PostCacheHandler postCacheHandler, @@ -35,17 +35,22 @@ RouterFunction doRoute(final GetCacheHandler getCacheHandler, return route( POST(apiConfig.getCachePath()) + .or(POST(apiConfig.getCachePath() + "/" + USER_CACHE_PATH_PARAM)) .and(accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_PLAIN)), postCacheHandler::save) .andRoute( GET(apiConfig.getCachePath()) + .or(GET(apiConfig.getCachePath() + "/" + USER_CACHE_PATH_PARAM)) .and(accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8)), getCacheHandler::fetch) .andRoute( - GET(apiConfig.getCachePath()).and(accept(MediaType.APPLICATION_XML)), + GET(apiConfig.getCachePath()) + .or(GET(apiConfig.getCachePath() + "/" + USER_CACHE_PATH_PARAM)) + .and(accept(MediaType.APPLICATION_XML)), getCacheHandler::fetch) .andRoute( - POST(apiConfig.getStoragePath()).and(accept(MediaType.APPLICATION_JSON)), + POST(apiConfig.getStoragePath()) + .and(accept(MediaType.APPLICATION_JSON)), postStorageHandler::save) .andRoute(GET(apiConfig.getStoragePath()), getStorageHandler::fetch) .andOther(route(RequestPredicates.all(), errorHandler::invalidRequest)); diff --git a/src/test/java/org/prebid/cache/handlers/PostCacheHandlerTests.java b/src/test/java/org/prebid/cache/handlers/PostCacheHandlerTests.java index 0cba73a..2988a36 100644 --- a/src/test/java/org/prebid/cache/handlers/PostCacheHandlerTests.java +++ b/src/test/java/org/prebid/cache/handlers/PostCacheHandlerTests.java @@ -392,6 +392,34 @@ void testWriteAuthorizationWithValidUserKeyQueryParameter() { .verify(); } + @Test + void testWriteAuthorizationWithValidUserKeyPathParameter() { + given(apiConfig.isCacheWriteSecured()).willReturn(true); + given(apiConfig.getUserKeys()).willReturn(Map.of("user-name", "user-key")); + given(repository.save(PAYLOAD_WRAPPER)).willReturn(Mono.just(PAYLOAD_WRAPPER)); + + final var handler = new PostCacheHandler( + repository, + cacheConfig, + metricsRecorder, + builder, + webClientCircuitBreaker, + samplingRate, + apiConfig); + + final var request = Mono.just(RequestObject.of(Collections.singletonList(PAYLOAD_TRANSFER))); + final var requestMono = MockServerRequest.builder() + .method(HttpMethod.POST) + .header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .pathVariable("user", "user-key") + .body(request); + + StepVerifier.create(handler.save(requestMono)) + .consumeNextWith(serverResponse -> assertEquals(200, serverResponse.statusCode().value())) + .expectComplete() + .verify(); + } + @Test void testWriteAuthorizationWithValidUserKeyShouldPrioritizeHeader() { given(apiConfig.isCacheWriteSecured()).willReturn(true); @@ -413,6 +441,7 @@ void testWriteAuthorizationWithValidUserKeyShouldPrioritizeHeader() { .header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .header("x-pbc-user-key", "user-key") .queryParam("x-pbc-user-key", "invalid") + .pathVariable("user", "invalid") .body(request); StepVerifier.create(handler.save(requestMono))