Skip to content

Commit 6a904d4

Browse files
committed
Use proxy authentication if set
I also set the proxy selector although it seems like it already uses the proxy somehow, it is just not authenticating.
1 parent b5faea8 commit 6a904d4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import com.intellij.openapi.components.Service
2222
import com.intellij.openapi.diagnostic.Logger
2323
import com.intellij.openapi.extensions.PluginId
2424
import com.intellij.openapi.util.SystemInfo
25+
import com.intellij.util.net.HttpConfigurable
26+
import okhttp3.Credentials
2527
import okhttp3.OkHttpClient
2628
import okhttp3.internal.tls.OkHostnameVerifier
2729
import okhttp3.logging.HttpLoggingInterceptor
@@ -95,9 +97,22 @@ class CoderRestClient(
9597
pluginVersion = PluginManagerCore.getPlugin(PluginId.getId("com.coder.gateway"))!!.version // this is the id from the plugin.xml
9698
}
9799

100+
val proxy = HttpConfigurable.getInstance()
101+
98102
val socketFactory = coderSocketFactory(settings)
99103
val trustManagers = coderTrustManagers(settings.tlsCAPath)
100104
httpClient = OkHttpClient.Builder()
105+
.proxySelector(proxy.onlyBySettingsSelector)
106+
.proxyAuthenticator { _, response ->
107+
val login = proxy.proxyLogin
108+
val pass = proxy.plainProxyPassword
109+
if (proxy.PROXY_AUTHENTICATION && login != null && pass != null) {
110+
val credentials = Credentials.basic(login, pass)
111+
response.request.newBuilder()
112+
.header("Proxy-Authorization", credentials)
113+
.build()
114+
} else null
115+
}
101116
.sslSocketFactory(socketFactory, trustManagers[0] as X509TrustManager)
102117
.hostnameVerifier(CoderHostnameVerifier(settings.tlsAlternateHostname))
103118
.addInterceptor { it.proceed(it.request().newBuilder().addHeader("Coder-Session-Token", token).build()) }
@@ -451,4 +466,4 @@ class MergedSystemTrustManger(private val otherTrustManager: X509TrustManager) :
451466
override fun getAcceptedIssuers(): Array<X509Certificate> {
452467
return otherTrustManager.acceptedIssuers + systemTrustManager.acceptedIssuers
453468
}
454-
}
469+
}

0 commit comments

Comments
 (0)