Skip to content

Commit 01058b3

Browse files
committed
fix: when Preemptive Authentication is enabled, basic auth creds were not being set correctly #1764
1 parent 111ae79 commit 01058b3

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/HalClient.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import au.com.dius.pact.core.support.jsonObject
1616
import au.com.dius.pact.core.support.unwrap
1717
import com.google.common.net.UrlEscapers
1818
import io.github.oshai.kotlinlogging.KLogging
19+
import org.apache.hc.client5.http.auth.AuthScope
1920
import org.apache.hc.client5.http.classic.methods.HttpGet
2021
import org.apache.hc.client5.http.classic.methods.HttpPost
2122
import org.apache.hc.client5.http.classic.methods.HttpPut
@@ -225,18 +226,19 @@ open class HalClient @JvmOverloads constructor(
225226
logger.warn { "Authentication options needs to be either an instance of Auth or a list of values, ignoring." }
226227
}
227228
val uri = URI(baseUrl)
228-
val result = HttpClient.newHttpClient(options["authentication"], uri, this.maxPublishRetries,
229+
val (client, credentialsProvider) = HttpClient.newHttpClient(options["authentication"], uri, this.maxPublishRetries,
229230
this.publishRetryInterval, config.insecureTLS)
230-
httpClient = result.first
231+
httpClient = client
231232

232233
if (System.getProperty(PREEMPTIVE_AUTHENTICATION) == "true") {
233234
val targetHost = HttpHost(uri.scheme, uri.host, uri.port)
234235
logger.warn { "Using preemptive basic authentication with the pact broker at $targetHost" }
235236
val authCache = BasicAuthCache()
236237
val basicAuth = BasicScheme()
237-
authCache.put(targetHost, basicAuth)
238238
httpContext = HttpClientContext.create()
239-
httpContext!!.credentialsProvider = result.second
239+
httpContext!!.credentialsProvider = credentialsProvider
240+
basicAuth.initPreemptive(credentialsProvider!!.getCredentials(AuthScope(uri.host, uri.port), httpContext))
241+
authCache.put(targetHost, basicAuth)
240242
httpContext!!.authCache = authCache
241243
}
242244
}

core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/HalClientSpec.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ class HalClientSpec extends Specification {
7373

7474
when:
7575
client.setupHttpClient()
76+
def authScheme = client.httpContext.authCache.get(host)
7677

7778
then:
7879
client.httpClient.credentialsProvider instanceof SystemDefaultCredentialsProvider
7980
client.httpContext != null
80-
client.httpContext.authCache.get(host) instanceof BasicScheme
81+
authScheme instanceof BasicScheme
82+
authScheme.username == '1'
83+
authScheme.password == ['2']
8184
}
8285

8386
def 'retry strategy is added to execution chain of client'() {

0 commit comments

Comments
 (0)