Skip to content

Commit

Permalink
Fix basic auth with encoded characters (spring-cloud#3815)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZIRAKrezovic committed Mar 26, 2024
1 parent 0bebfd1 commit 2dfa1b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
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
Expand Up @@ -18,13 +18,17 @@

import com.netflix.discovery.shared.resolver.DefaultEndpoint;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;

import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.WebClient;

/**
* @author Daniel Lavoie
* @author Armin Krezovic
*/
class WebClientTransportClientFactoryTest {

Expand All @@ -45,6 +49,24 @@ 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"));

WebClient.Builder builder = client.getWebClient().mutate();

StepVerifier.create(builder.filter(((request, next) -> {
Assertions.assertEquals(expectedAuthHeader, request.headers().getFirst(HttpHeaders.AUTHORIZATION));
Assertions.assertEquals(expectedUrl, request.url().toString());
return next.exchange(request);
})).build().head().retrieve().toBodilessEntity()).expectError().verify();
}

@Test
void testUserInfo() {
transportClientFatory.newClient(new DefaultEndpoint("http://test:test@localhost:8761"));
Expand Down

0 comments on commit 2dfa1b7

Please sign in to comment.