Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,7 +107,7 @@ public Mono<ServerResponse> 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();
}
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/prebid/cache/routers/ApiRouter.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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));
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/prebid/cache/handlers/PostCacheHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
Expand Down
Loading