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

Improve proxy authentication preventing NPE and allow Basic Authentication (v3) #2088

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -54,7 +54,12 @@ abstract class HttpClientAsyncBackend[F[_], S, P, BH, B](

val consumer = toJavaBiConsumer((t: HttpResponse[BH], u: Throwable) => {
if (t != null) {
try success(readResponse(t, Left(bodyHandlerBodyToBody(t.body())), request))
// sometimes body returned by HttpClient can be null, we handle this by returning empty body to prevent NPE
val body = Option(t.body())
.map(bodyHandlerBodyToBody)
.getOrElse(emptyBody())

try success(readResponse(t, Left(body), request))
catch {
case e: Exception => error(e)
}
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/scalajvm/sttp/client3/HttpClientBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import sttp.monad.MonadError
import sttp.monad.syntax._
import sttp.ws.WebSocket

import java.net.Authenticator.RequestorType
import java.net.http.{HttpClient, HttpRequest, HttpResponse}
import java.net.{Authenticator, PasswordAuthentication}
import java.time.{Duration => JDuration}
Expand Down Expand Up @@ -123,10 +124,12 @@ abstract class HttpClientBackend[F[_], S, P, B](
object HttpClientBackend {

type EncodingHandler[B] = PartialFunction[(B, String), B]
// TODO not sure if it works

private class ProxyAuthenticator(auth: SttpBackendOptions.ProxyAuth) extends Authenticator {
override def getPasswordAuthentication: PasswordAuthentication = {
new PasswordAuthentication(auth.username, auth.password.toCharArray)
if (getRequestorType == RequestorType.PROXY) {
new PasswordAuthentication(auth.username, auth.password.toCharArray)
} else null
}
}

Expand Down
10 changes: 10 additions & 0 deletions docs/conf/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ import sttp.client3._
SttpBackendOptions.httpProxy("some.host", 8080, "username", "password")
```

If your backend implements `HttpClientBackend` and your proxy server requires Basic authentication, you will need to enable it by
DybekK marked this conversation as resolved.
Show resolved Hide resolved
removing Basic from the `jdk.http.auth.tunneling.disabledSchemes` networking property, or by setting a system property of the same name to "".

```scala mdoc:compile-only
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "")
```

Please be aware that enabling Basic authentication for HTTP tunneling can expose your credentials to interception, so it
should only be done if you understand the risks and your network is secure. This behaviour is described in https://www.oracle.com/java/technologies/javase/8u111-relnotes.html.

## Ignoring and allowing specific hosts

There are two additional settings that can be provided to via `SttpBackendOptions`:
Expand Down
Loading