Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix basic auth with encoded characters (#3815) #4260

Merged
merged 1 commit into from
Apr 3, 2024
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
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;

/**
* Provides the custom {@link WebClient.Builder} required by the
Expand All @@ -58,6 +59,7 @@
*
* @author Daniel Lavoie
* @author Haytham Mohamed
* @author Armin Krezovic
*/
public class WebClientTransportClientFactory implements TransportClientFactory {

Expand All @@ -78,14 +80,14 @@ public EurekaHttpClient newClient(EurekaEndpoint endpoint) {
}

private WebClient.Builder setUrl(WebClient.Builder builder, String serviceUrl) {
String url = serviceUrl;
String url = UriComponentsBuilder.fromUriString(serviceUrl).userInfo(null).toUriString();

try {
URI serviceURI = new URI(serviceUrl);
if (serviceURI.getUserInfo() != null) {
String[] credentials = serviceURI.getUserInfo().split(":");
if (credentials.length == 2) {
builder.filter(ExchangeFilterFunctions.basicAuthentication(credentials[0], credentials[1]));
url = serviceUrl.replace(credentials[0] + ":" + credentials[1] + "@", "");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,23 +16,57 @@

package org.springframework.cloud.netflix.eureka.http;

import java.time.Duration;

import com.netflix.discovery.shared.resolver.DefaultEndpoint;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
ZIRAKrezovic marked this conversation as resolved.
Show resolved Hide resolved
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import reactor.core.publisher.Mono;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.WebClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

/**
* @author Daniel Lavoie
* @author Armin Krezovic
*/
@MockitoSettings(strictness = Strictness.LENIENT)
class WebClientTransportClientFactoryTest {

@Mock
private ExchangeFunction exchangeFunction;

@Captor
private ArgumentCaptor<ClientRequest> captor;

private WebClientTransportClientFactory transportClientFatory;

@BeforeEach
void setup() {
transportClientFatory = new WebClientTransportClientFactory(WebClient::builder);
ClientResponse mockResponse = mock();
when(mockResponse.statusCode()).thenReturn(HttpStatus.OK);
when(mockResponse.bodyToMono(Void.class)).thenReturn(Mono.empty());
given(exchangeFunction.exchange(captor.capture())).willReturn(Mono.just(mockResponse));
ZIRAKrezovic marked this conversation as resolved.
Show resolved Hide resolved

transportClientFatory = new WebClientTransportClientFactory(
() -> WebClient.builder().exchangeFunction(exchangeFunction));
}

@Test
Expand All @@ -45,6 +79,23 @@ void testInvalidUserInfo() {
transportClientFatory.newClient(new DefaultEndpoint("http://test@localhost:8761"));
}

@Test
void testUserInfoWithEncodedCharacters() {
String encodedBasicAuth = HttpHeaders.encodeBasicAuth("test", "MyPassword@", null);
String expectedAuthHeader = "Basic " + encodedBasicAuth;
String expectedUrl = "http://localhost:8761";

WebClientEurekaHttpClient client = (WebClientEurekaHttpClient) transportClientFatory
.newClient(new DefaultEndpoint("http://test:MyPassword%40@localhost:8761"));

client.getWebClient().get().retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10));

ClientRequest request = verifyAndGetRequest();

assertThat(request.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo(expectedAuthHeader);
assertThat(request.url().toString()).isEqualTo(expectedUrl);
}

@Test
void testUserInfo() {
transportClientFatory.newClient(new DefaultEndpoint("http://test:test@localhost:8761"));
Expand All @@ -55,4 +106,11 @@ void shutdown() {
transportClientFatory.shutdown();
}

private ClientRequest verifyAndGetRequest() {
ClientRequest request = captor.getValue();
verify(exchangeFunction).exchange(request);
verifyNoMoreInteractions(exchangeFunction);
return request;
}

}