From ab85960cdbfb0257addc8e9f772bd647a051ca8e Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Tue, 9 Apr 2019 22:56:52 -0700 Subject: [PATCH] Convert JavaNetAuthenticator to Kotlin (#4909) * Rename JavaNetAuthenticator.java to .kt * Convert JavaNetAuthenticator to Kotlin --- .../java/okhttp3/JavaNetAuthenticator.java | 75 ---------------- .../main/java/okhttp3/JavaNetAuthenticator.kt | 87 +++++++++++++++++++ .../okhttp3/KotlinSourceCompatibilityTest.kt | 3 +- 3 files changed, 89 insertions(+), 76 deletions(-) delete mode 100644 okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.java create mode 100644 okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt diff --git a/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.java b/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.java deleted file mode 100644 index f1892b719..000000000 --- a/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2013 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package okhttp3; - -import java.io.IOException; -import java.net.Authenticator.RequestorType; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.PasswordAuthentication; -import java.net.Proxy; -import java.util.List; -import okhttp3.internal.annotations.EverythingIsNonNull; - -/** - * Adapts {@link java.net.Authenticator} to {@link Authenticator}. Configure OkHttp to use {@link - * java.net.Authenticator} with {@link OkHttpClient.Builder#authenticator} or {@link - * OkHttpClient.Builder#proxyAuthenticator(Authenticator)}. - */ -@EverythingIsNonNull -public final class JavaNetAuthenticator implements Authenticator { - @Override public Request authenticate(Route route, Response response) throws IOException { - List challenges = response.challenges(); - Request request = response.request(); - HttpUrl url = request.url(); - boolean proxyAuthorization = response.code() == 407; - Proxy proxy = route.proxy(); - - for (int i = 0, size = challenges.size(); i < size; i++) { - Challenge challenge = challenges.get(i); - if (!"Basic".equalsIgnoreCase(challenge.scheme())) continue; - - PasswordAuthentication auth; - if (proxyAuthorization) { - InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); - auth = java.net.Authenticator.requestPasswordAuthentication( - proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), - url.scheme(), challenge.realm(), challenge.scheme(), url.url(), - RequestorType.PROXY); - } else { - auth = java.net.Authenticator.requestPasswordAuthentication( - url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), - challenge.realm(), challenge.scheme(), url.url(), RequestorType.SERVER); - } - - if (auth != null) { - String credential = Credentials.basic( - auth.getUserName(), new String(auth.getPassword()), challenge.charset()); - return request.newBuilder() - .header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential) - .build(); - } - } - - return null; // No challenges were satisfied! - } - - private InetAddress getConnectToInetAddress(Proxy proxy, HttpUrl url) throws IOException { - return proxy.type() != Proxy.Type.DIRECT - ? ((InetSocketAddress) proxy.address()).getAddress() - : InetAddress.getByName(url.host()); - } -} diff --git a/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt b/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt new file mode 100644 index 000000000..c0bc87105 --- /dev/null +++ b/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2013 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package okhttp3 + +import java.io.IOException +import java.net.Authenticator +import java.net.InetAddress +import java.net.InetSocketAddress +import java.net.Proxy + +/** + * Adapts [Authenticator] to [okhttp3.Authenticator]. Configure OkHttp to use [Authenticator] with + * [OkHttpClient.Builder.authenticator] or [OkHttpClient.Builder.proxyAuthenticator]. + */ +class JavaNetAuthenticator : okhttp3.Authenticator { + @Throws(IOException::class) + override fun authenticate(route: Route?, response: Response): Request? { + val challenges = response.challenges() + val request = response.request() + val url = request.url() + val proxyAuthorization = response.code() == 407 + val proxy = route?.proxy() ?: Proxy.NO_PROXY + + for (challenge in challenges) { + if (!"Basic".equals(challenge.scheme(), ignoreCase = true)) { + continue + } + + val auth = if (proxyAuthorization) { + val proxyAddress = proxy.address() as InetSocketAddress + Authenticator.requestPasswordAuthentication( + proxyAddress.hostName, + proxy.connectToInetAddress(url), + proxyAddress.port, + url.scheme(), + challenge.realm(), + challenge.scheme(), + url.url(), + Authenticator.RequestorType.PROXY + ) + } else { + Authenticator.requestPasswordAuthentication( + url.host(), + proxy.connectToInetAddress(url), + url.port(), + url.scheme(), + challenge.realm(), + challenge.scheme(), + url.url(), + Authenticator.RequestorType.SERVER + ) + } + + if (auth != null) { + val credentialHeader = if (proxyAuthorization) "Proxy-Authorization" else "Authorization" + val credential = Credentials.basic( + auth.userName, String(auth.password), challenge.charset()) + return request.newBuilder() + .header(credentialHeader, credential) + .build() + } + } + + return null // No challenges were satisfied! + } + + @Throws(IOException::class) + private fun Proxy.connectToInetAddress(url: HttpUrl): InetAddress { + return when { + type() == Proxy.Type.DIRECT -> InetAddress.getByName(url.host()) + else -> (address() as InetSocketAddress).address + } + } +} \ No newline at end of file diff --git a/okhttp/src/test/java/okhttp3/KotlinSourceCompatibilityTest.kt b/okhttp/src/test/java/okhttp3/KotlinSourceCompatibilityTest.kt index 2b4bfaaad..74bea0188 100644 --- a/okhttp/src/test/java/okhttp3/KotlinSourceCompatibilityTest.kt +++ b/okhttp/src/test/java/okhttp3/KotlinSourceCompatibilityTest.kt @@ -638,7 +638,8 @@ class KotlinSourceCompatibilityTest { fun javaNetAuthenticator() { val authenticator = JavaNetAuthenticator() val response = Response.Builder().build() - val request: Request? = authenticator.authenticate(newRoute(), response) + var request: Request? = authenticator.authenticate(newRoute(), response) + request = authenticator.authenticate(null, response) } @Test @Ignore