Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Support wildcard for Basic auth domains #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -47,7 +47,14 @@ public HttpResponse clientToProxyRequest(HttpObject httpObject) {
String hostname = getHost(httpRequest);

// if there is an entry in the credentials map matching this hostname, add the credentials to the request
String base64CredentialsForHostname = credentialsByHostname.get(hostname);
String base64CredentialsForHostname = null;
for (String key : credentialsByHostname.keySet()) {
String regex = key.replace("*", ".*?");
if (hostname.matches(regex)) {
base64CredentialsForHostname = credentialsByHostname.get(key);
}
}

if (base64CredentialsForHostname != null) {
httpRequest.headers().add(HttpHeaderNames.AUTHORIZATION, "Basic " + base64CredentialsForHostname);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ class AutoAuthTest extends MockServerTest {
verify(1, getRequestedFor(urlMatching(stubUrl)))
}

@Test
void testBasicAuthWithWildcardAddedToHttpsRequest() {
// the base64-encoded rendering of "testUsername:testPassword" is dGVzdFVzZXJuYW1lOnRlc3RQYXNzd29yZA==
def stubUrl = "/basicAuthHttp"

stubFor(get(urlEqualTo(stubUrl))
.withHeader("Authorization", new EqualToPattern("Basic dGVzdFVzZXJuYW1lOnRlc3RQYXNzd29yZA=="))
.willReturn(ok().withBody("success")))

proxy = new BrowserUpProxyServer()
// * (wildcard) should match "localhost"
proxy.autoAuthorization("*", "testUsername", "testPassword", AuthType.BASIC)
proxy.setTrustAllServers(true)
proxy.start()

NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable {
String responseBody = NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet("https://localhost:${mockServerHttpsPort}/basicAuthHttp")).getEntity().getContent())
assertEquals("Did not receive expected response from mock server", "success", responseBody)
}

verify(1, getRequestedFor(urlMatching(stubUrl)))
}

@Test
void testCanStopBasicAuth() {
// the base64-encoded rendering of "testUsername:testPassword" is dGVzdFVzZXJuYW1lOnRlc3RQYXNzd29yZA==
Expand Down