package okhttp3.repackaged; import com.google.common.net.HttpHeaders; import java.io.IOException; import java.net.Authenticator; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy; import java.util.List; /* loaded from: classes6.dex */ public final class JavaNetAuthenticator implements Authenticator { @Override // okhttp3.repackaged.Authenticator public final Request authenticate(Route route, Response response) throws IOException { PasswordAuthentication requestPasswordAuthentication; List challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.url(); boolean z = response.code() == 407; Proxy proxy = route.proxy(); int size = challenges.size(); for (int i = 0; i < size; i++) { Challenge challenge = challenges.get(i); if ("Basic".equalsIgnoreCase(challenge.scheme())) { if (z) { InetSocketAddress inetSocketAddress = (InetSocketAddress) proxy.address(); requestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(inetSocketAddress.getHostName(), a(proxy, url), inetSocketAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), Authenticator.RequestorType.PROXY); } else { requestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(url.host(), a(proxy, url), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), Authenticator.RequestorType.SERVER); } if (requestPasswordAuthentication != null) { return request.newBuilder().header(z ? HttpHeaders.PROXY_AUTHORIZATION : HttpHeaders.AUTHORIZATION, Credentials.basic(requestPasswordAuthentication.getUserName(), new String(requestPasswordAuthentication.getPassword()))).build(); } } } return null; } private InetAddress a(Proxy proxy, HttpUrl httpUrl) throws IOException { if (proxy != null && proxy.type() != Proxy.Type.DIRECT) { return ((InetSocketAddress) proxy.address()).getAddress(); } return InetAddress.getByName(httpUrl.host()); } }