Skip to content

Commit

Permalink
fix: when Preemptive Authentication is enabled, basic auth creds were…
Browse files Browse the repository at this point in the history
… not being set correctly #1764
  • Loading branch information
rholshausen committed Jan 30, 2024
1 parent 111ae79 commit 01058b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import au.com.dius.pact.core.support.jsonObject
import au.com.dius.pact.core.support.unwrap
import com.google.common.net.UrlEscapers
import io.github.oshai.kotlinlogging.KLogging
import org.apache.hc.client5.http.auth.AuthScope
import org.apache.hc.client5.http.classic.methods.HttpGet
import org.apache.hc.client5.http.classic.methods.HttpPost
import org.apache.hc.client5.http.classic.methods.HttpPut
Expand Down Expand Up @@ -225,18 +226,19 @@ open class HalClient @JvmOverloads constructor(
logger.warn { "Authentication options needs to be either an instance of Auth or a list of values, ignoring." }
}
val uri = URI(baseUrl)
val result = HttpClient.newHttpClient(options["authentication"], uri, this.maxPublishRetries,
val (client, credentialsProvider) = HttpClient.newHttpClient(options["authentication"], uri, this.maxPublishRetries,
this.publishRetryInterval, config.insecureTLS)
httpClient = result.first
httpClient = client

if (System.getProperty(PREEMPTIVE_AUTHENTICATION) == "true") {
val targetHost = HttpHost(uri.scheme, uri.host, uri.port)
logger.warn { "Using preemptive basic authentication with the pact broker at $targetHost" }
val authCache = BasicAuthCache()
val basicAuth = BasicScheme()
authCache.put(targetHost, basicAuth)
httpContext = HttpClientContext.create()
httpContext!!.credentialsProvider = result.second
httpContext!!.credentialsProvider = credentialsProvider
basicAuth.initPreemptive(credentialsProvider!!.getCredentials(AuthScope(uri.host, uri.port), httpContext))
authCache.put(targetHost, basicAuth)
httpContext!!.authCache = authCache
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ class HalClientSpec extends Specification {

when:
client.setupHttpClient()
def authScheme = client.httpContext.authCache.get(host)

then:
client.httpClient.credentialsProvider instanceof SystemDefaultCredentialsProvider
client.httpContext != null
client.httpContext.authCache.get(host) instanceof BasicScheme
authScheme instanceof BasicScheme
authScheme.username == '1'
authScheme.password == ['2']
}

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

0 comments on commit 01058b3

Please sign in to comment.